''' Set up the argument parser which will get the CSV file and location where model is to be stored''' argparser = argparse.ArgumentParser() argparser.add_argument("-d", "--dataset", required = True, help = "path to the dataset file") argparser.add_argument("-m", "--model", required = True, help = "path to where the model will be stored") args = vars(argparser.parse_args()) (digits, labels) = dataset.load_data(args["dataset"]) hog = HOG(orientations = 18, pixelsPerCell = (10, 10), cellsPerBlock = (1, 1), normalise = True) data = [] # Add histogram for each digit in a list for digit in digits: digit = dataset.deskew(digit) hist = hog.describe(digit.reshape((28,28))) data.append(hist) # Set up and train the model SVC_model = LinearSVC() SVC_model.fit(data, labels) # Save the model to file joblib.dump(SVC_model, args["model"], compress = 3)
# Find all the bounding rectangles of the contours rects = [cv2.boundingRect(contour) for contour in contours] # Loop over all the rectangles for rect in rects: (x, y, w, h) = rect cv2.rectangle(image, (x, y), (x + w, y + h), (0, 255, 0), 2) # Find the region of interest, and resize it roi = gray[y: y + h, x: x + w] roi = cv2.resize(roi, (28, 28), interpolation = cv2.INTER_AREA) # Threshold the region of interest thresh = roi.copy() _, thresh = cv2.threshold(thresh, 90, 255, cv2.THRESH_BINARY_INV) thresh = dataset.deskew(thresh) # Get the histogram and predict the number hist = hog.describe(thresh) predict = SVC_model.predict(np.array([hist], 'float64')) cv2.putText(image, str(predict), (x-10, y-10), cv2.FONT_HERSHEY_SIMPLEX, 1.2, (0, 255, 0) , 2) # Display the result cv2.imshow("Image", image) cv2.waitKey(0)