-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclassify.py
59 lines (52 loc) · 2.35 KB
/
classify.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
50
51
52
53
54
55
56
57
58
59
# USAGE
# python3 classify.py --model models/svm.cpickle --image <image-path>
from pyimagesearch.hog import HOG
from pyimagesearch import dataset
import argparse
import _pickle as pickle
import mahotas
import cv2
ap = argparse.ArgumentParser()
ap.add_argument("-m", "--model", required = True, help = "path to where the model will be stored")
ap.add_argument("-i", "--image", required = True, help = "path to the image file")
args = vars(ap.parse_args())
# load the model
model = open(args["model"], 'rb')
model = pickle.load(model, encoding='latin1')
# initialize the HOG descriptor
hog = HOG(orientations = 18, pixelsPerCell = (10, 10), cellsPerBlock = (1, 1), normalize = True)
# load the image and convert it to grayscale
image = cv2.imread(args["image"])
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# blur the image, find edges, and then find contours along the edged regions
blurred = cv2.GaussianBlur(gray, (5, 5), 0)
edged = cv2.Canny(blurred, 30, 150)
(_, cnts, _) = cv2.findContours(edged.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
# sort the contours by their x-axis position, ensuring that we read the numbers from left to right
cnts = sorted([(c, cv2.boundingRect(c)[0]) for c in cnts], key = lambda x: x[1])
# loop over the contours
for (c, _) in cnts:
# compute the bounding box for the rectangle
(x, y, w, h) = cv2.boundingRect(c)
# if the width is at least 7 pixels and the height is at least 20 pixels, the contour is likely a digit
if (w >= 7 and h >= 20):
# crop the ROI and then threshold the grayscale ROI to reveal the digit
roi = gray[y:y + h, x:x + w]
thresh = roi.copy()
T = mahotas.thresholding.otsu(roi)
thresh[thresh > T] = 255
thresh = cv2.bitwise_not(thresh)
# deskew the image center its extent
thresh = dataset.deskew(thresh, 20)
thresh = dataset.center_extent(thresh, (20, 20))
cv2.imshow("thresh", thresh)
# extract features from the image and classify it
hist = hog.describe(thresh)
hist = [hist]
digit = model.predict(hist)[0]
print("I think that number is: %d" % (digit))
# draw a rectangle around the digit, the show what the digit was classified as
cv2.rectangle(image, (x, y), (x + w, y + h), (0, 255, 0), 1)
cv2.putText(image, str(digit), (x - 10, y – 10), cv2.FONT_HERSHEY_SIMPLEX, 1.2, (0, 255, 0), 2)
cv2.imshow("image", image)
cv2.waitKey(0)