Exemple #1
0
cnts = sorted([(c, cv2.boundingRect(c)[0]) for c in cnts], key = lambda x: x[1])

# loop over the contours
for (c, _) in cnts:
	(x,y,w,h) = cv2.boundingRect(c)

	# if the width is atleast 7 pixels and the height is atleast 20 pixels, the ocntour is likely a digit
	if w >= 7 and h >= 20:
		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)

		# deskwe 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 reshape the array
		hist = hog.describe(thresh)
		hist = hist.reshape(1, -1)
		# classify
		digit = model.predict(hist)[0]
		print("I think that number is: {}".format(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)
Exemple #2
0
                "--model",
                required=True,
                help="path to where the model will be stored")
args = vars(ap.parse_args())

# load the dataset and initialize the data matrix
(digits, target) = dataset.load_digits(args["dataset"])
data = []

# initialize the HOG descriptor
hog = HOG(orientations=18,
          pixelsPerCell=(10, 10),
          cellsPerBlock=(1, 1),
          normalize=True)

# loop over the images
for image in digits:
    # deskew the image, center it
    image = dataset.deskew(image, 20)
    image = dataset.center_extent(image, (20, 20))

    # describe the image and update the data matrix
    hist = hog.describe(image)
    data.append(hist)

# train the model
model = LinearSVC(random_state=42)
model.fit(data, target)

# dump the model to file
joblib.dump(model, args["model"])