-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgather_examples.py.bak.bak
executable file
·93 lines (75 loc) · 2.8 KB
/
gather_examples.py.bak.bak
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# USAGE
# python gather_examples.py --images ../full_lp_dataset --examples output/examples
# import the necessary packages
from .include.license_plate import LicensePlateDetector
from imutils import paths
import traceback
import argparse
import imutils
import numpy as np
import random
import cv2
import os
# construct the argument parser and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-i", "--images", required=True, help="path to the images to be classified")
ap.add_argument("-e", "--examples", required=True, help="path to the output examples directory")
args = vars(ap.parse_args())
# randomly select a portion of the images and initialize the dictionary of character
# counts
imagePaths = list(paths.list_images(args["images"]))
random.shuffle(imagePaths)
imagePaths = imagePaths[:int(len(imagePaths) * 0.5)]
counts = {}
# loop over the images
for imagePath in imagePaths:
# show the image path
print("[EXAMINING] {}".format(imagePath))
try:
# load the image
image = cv2.imread(imagePath)
# if the width is greater than 640 pixels, then resize the image
if image.shape[1] > 640:
image = imutils.resize(image, width=640)
# initialize the license plate detector and detect characters on the license plate
lpd = LicensePlateDetector(image, numChars=7)
plates = lpd.detect()
# loop over the license plates
for (lpBox, chars) in plates:
# restructure lpBox
lpBox = np.array(lpBox).reshape((-1, 1, 2)).astype(np.int32)
# draw the bounding box surrounding the license plate and display it for
# reference purposes
plate = image.copy()
cv2.drawContours(plate, [lpBox], -1, (0, 255, 0), 2)
cv2.imshow("License Plate", plate)
# loop over the characters
for char in chars:
# display the character and wait for a keypress
cv2.imshow("Char", char)
key = cv2.waitKey(0)
# if the '`' key was pressed, then ignore the character
if key == ord("`"):
print("[IGNORING] {}".format(imagePath))
continue
# grab the key that was pressed and construct the path to the output
# directory
key = chr(key).upper()
dirPath = "{}/{}".format(args["examples"], key)
# if the output directory does not exist, create it
if not os.path.exists(dirPath):
os.makedirs(dirPath)
# write the labeled character to file
count = counts.get(key, 1)
path = "{}/{}.png".format(dirPath, str(count).zfill(5))
cv2.imwrite(path, char)
# increment the count for the current key
counts[key] = count + 1
# we are trying to control-c out of the script, so break from the loop
except KeyboardInterrupt:
break
# an unknwon error occured for this particular image, so do not process it and display
# a traceback for debugging purposes
except:
print(traceback.format_exc())
print("[ERROR] {}".format(imagePath))