Reputation: 31
I'm trying to write a program for scraping images to create datasets to use for neural networks, however I'm getting a few problems
here's the code:
from imutils import paths
import argparse
import requests
import cv2
import os
ap = argparse.ArgumentParser()
ap.add_argument("-u", "--C:/Users/artus/datasets/urls.txt",
required=False, help="path containing URLs")
ap.add_argument("-o", "--C:/Users/artus/datasets/ShoesImage",
required=False, help="folder for downloaded images")
args = vars(ap.parse_args())
# grab the list of URLs from the input file, then initialize the
# total number of images downloaded thus far
rows = open(args["urls"]).read().strip().split("\n")
total = 0
when executed it should download all the images from the urls specified in the urls.txt file, however I'm getting this error:
Traceback (most recent call last):
File "C:/Users/artus/untitled5/imagescraping.py", line 16, in <module>
rows = open(args["urls"]).read().strip().split("\n")
KeyError: 'urls'
Upvotes: 0
Views: 1042
Reputation: 231615
When I copy-n-paste your argparse code to a script:
import argparse
ap = argparse.ArgumentParser()
ap.add_argument("-u", "--C:/Users/artus/datasets/urls.txt",
required=False, help="path containing URLs")
ap.add_argument("-o", "--C:/Users/artus/datasets/ShoesImage",
required=False, help="folder for downloaded images")
args = ap.parse_args()
print(args)
and call it without arguments:
0923:~/mypy$ python3 stack56745387.py
Namespace(**{'C:/Users/artus/datasets/ShoesImage': None, 'C:/Users/artus/datasets/urls.txt': None})
and asking for help:
1743:~/mypy$ python3 stack56745387.py -h
usage: stack56745387.py [-h] [-u C:/USERS/ARTUS/DATASETS/URLS.TXT]
[-o C:/USERS/ARTUS/DATASETS/SHOESIMAGE]
optional arguments:
-h, --help show this help message and exit
-u C:/USERS/ARTUS/DATASETS/URLS.TXT, --C:/Users/artus/datasets/urls.txt C:/USERS/ARTUS/DATASETS/URLS.TXT
path containing URLs
-o C:/USERS/ARTUS/DATASETS/SHOESIMAGE, --C:/Users/artus/datasets/ShoesImage C:/USERS/ARTUS/DATASETS/SHOESIMAGE
folder for downloaded images
You probably intended the "--C:/Users/artus/datasets/urls.txt" to be something like the default value, but you defined it as the long flag and dest
for the argument. (Nothing in your setup specified urls
as the desired dest
or key.)
which you'd have to use as:
1750:~/mypy$ python3 stack56745387.py --C:/Users/artus/datasets/urls.txt foobar
Namespace(**{'C:/Users/artus/datasets/ShoesImage': None, 'C:/Users/artus/datasets/urls.txt': 'foobar'})
Changing the code to:
import argparse
ap = argparse.ArgumentParser()
ap.add_argument("-u", "--urls", default="C:/Users/artus/datasets/urls.txt",
required=False, help="path containing URLs (default: %(default)s)")
ap.add_argument("-o", "--images",default="C:/Users/artus/datasets/ShoesImage",
required=False, help="folder for downloaded images (default: %(default)s)")
args = ap.parse_args()
print(args)
1802:~/mypy$ python3 stack56745387.py -h
usage: stack56745387.py [-h] [-u URLS] [-o IMAGES]
optional arguments:
-h, --help show this help message and exit
-u URLS, --urls URLS path containing URLs (default:
C:/Users/artus/datasets/urls.txt)
-o IMAGES, --images IMAGES
folder for downloaded images (default:
C:/Users/artus/datasets/ShoesImage)
1803:~/mypy$ python3 stack56745387.py --urls foobar
Namespace(images='C:/Users/artus/datasets/ShoesImage', urls='foobar')
Now you could use args.urls
or vars(args)['urls']
.
Upvotes: 0
Reputation: 8422
The second parameter of add_argument is the "long name" for the argument. For the first argument, you'll be passing --urls
, and then argparse will make the value the user passes available as args["urls"]
:
# ...
ap.add_argument("-u", "--urls", type=str,
required=False, help="path containing URLs")
Then, at the command line, pass in the argument:
python imagescraping.py --urls C:/Users/artus/datasets/urls.txt
Also, I don't think you need to wrap it in vars
.
Upvotes: 2