I have the following code (which is in fact just 1 part of 4 needed to run all the project I am working on..):
#python classify.py --model models/svm.cpickle --image images/image.png
from __future__ import print_function
from sklearn.externals import joblib
from hog import HOG
import dataset
import argparse
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())
model = joblib.load(args["model"])
hog = HOG(orientations = 18, pixelsPerCell = (10, 10),
cellsPerBlock = (1, 1), transform = True)
image = cv2.imread(args["image"])
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
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)
cnts = sorted([(c, cv2.boundingRect(c)[0]) for c in cnts], key =
lambda x: x[1])
for (c, _) in cnts:
(x, y, w, h) = cv2.boundingRect(c)
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)
thresh = dataset.deskew(thresh, 20)
thresh = dataset.center_extent(thresh, (20, 20))
cv2.imshow("thresh", thresh)
hist = hog.describe(thresh)
digit = model.predict([hist])[0]
print("I think that number is: {}".format(digit))
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)
This code is detecting and recognizing handwriten digits from images. Here is an example:
Let's say I don't care about the accuracy recognition.
My problem is the following: as you can see, the program take all the numbers he can see and print them in console. From console I can save them in a text file if I want BUT I can't tell the program that there is a space between the numbers.
What I want is that, if I print the numbers in a text file, they should be separated as in the image (sorry but it's a bit hard to explain..). The numbers should not be (even in console) printed all together but, where there is blank space, printed a blank area also.
Take a look at the firs image. After the first 10 digits, there is a blank space in image which there isn't in console.
Anyway, here is a link to full code. There are 4 .py
files and 3 folders. To execute, open a CMD in the folder and paste the command python classify.py --model models/svm.cpickle --image images/image.png
where image.png
is the name of one file in images folder.
Thanks in advance. In my opinion all this work would have to be done using neural networks but I want to try it first this way. I'm pretty new to this.
Used this code to do the job. It detects region of text/digits in images.
The code is based on this other question/answer: Extracting text OpenCV