[英]How to solve this type of KeyError?
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: 执行时,应从urls.txt文件中指定的url下载所有图像,但是出现此错误:
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'
The second parameter of add_argument is the "long name" for the argument. add_argument的第二个参数是该参数的“长名称”。 For the first argument, you'll be passing
--urls
, and then argparse will make the value the user passes available as args["urls"]
: 对于第一个参数,您将传递
--urls
,然后argparse将使用户传递的值作为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
. 另外,我认为您不需要将其包装在
vars
。
When I copy-n-paste your argparse code to a script: 当我将您的argparse代码复制-n-粘贴到脚本中时:
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. 您可能希望“ --C:/Users/artus/datasets/urls.txt”类似于默认值,但您将其定义为该参数的long标志和
dest
。 (Nothing in your setup specified urls
as the desired dest
or key.) (您的设置中没有将
urls
指定为所需的dest
或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']
. 现在您可以使用
args.urls
或vars(args)['urls']
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.