-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGetData.py
49 lines (39 loc) · 1.25 KB
/
GetData.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
from imutils import paths
import argparse
import requests
import cv2
import os
# construct the argument parse and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-u", "--urls", required=True,
help="path to file containing image URLs")
ap.add_argument("-o", "--output", required=True,
help="path to output directory of 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
for url in rows:
try:
r = requests.get(url, timeout=60)
p = os.path.sep.join([args["output"], "{}.jpg".format(str(total).zfill(8))])
f = open(p, "wb")
f.write(r.content)
f.close()
print("[INFO] downloaded: {}".format(p))
total += 1
except:
print("[INFO] error downloading {}...skipping".format(p))
for imagePath in paths.list_images(args["output"]):
delete = False
try:
image = cv2.imread(imagePath)
if image is None:
delete = True
except:
print("Except")
delete = True
if delete:
print("[INFO] deleting {}".format(imagePath))
os.remove(imagePath)