Hi fellas, welcome to the last episode of our TSLSR adventure. When you reach the end of this article, you are free to go but I will continue to develop TSLSR
. So you can follow the repo from github.
This time, we will successfully detect digits from the image that we got. To make this possible, I used template matching methodology. And of course, you can find the templates in repository in folder /tslsr/digits
. Let’s look at the code.
###
# /main.py
###
digits = tslsr.extractDigits(roi)
plt.subplot(222)
plt.imshow(mroi)
croi = roi.copy()
for (x, y, w, h) in utils.eliminate_child_rects(rects):
cv2.rectangle(croi, (x, y), (x+w, y+h), (0, 255, 0), 1)
plt.subplot(223)
plt.imshow(croi)
plt.figure(3)
recognizedDigits = []
for digit in digits:
res = tslsr.recognizeDigit(digit)
print("Recoginition res:", res)
recognizedDigits.append(res[0])
As you all know, we fetched digits from roi in last episode. And now with the help of recognizeDigit function of tslsr
module, we can get the digit as integer from the image. You will find details of recognizeDigit function in below.
###
# /tslsr/tslsr.py
###
def recognizeDigit(digit, method = REC_METHOD_TEMPLATE_MATCHING, threshold= 55):
"""
Finds the best match for the given digit(RGB or gray color scheme). And returns the result and percentage as an integer.
@threshold percentage of similarity
"""
__readDigitTemplates()
digit = digit.copy()
if digit.shape[2] == 3:
digit = cv2.cvtColor(digit, cv2.COLOR_RGB2GRAY)
ret, digit = cv2.threshold(digit, 90, 255, cv2.THRESH_BINARY_INV)
bestDigit = -1
if method == REC_METHOD_TEMPLATE_MATCHING:
bestMatch = None
for i in range(len(__DIGIT_TEMPLATES)):
template = __DIGIT_TEMPLATES[i].copy()
if digit.shape[1] < template.shape[1]:
template = cv2.resize(template, (digit.shape[1], digit.shape[0]))
else:
digit = cv2.resize(digit, (template.shape[1], template.shape[0]))
result = cv2.matchTemplate(digit, template, cv2.TM_CCORR_NORMED)#cv2.TM_CCOEFF_NORMED)
(_, max_val, _, max_loc) = cv2.minMaxLoc(result)
if bestMatch is None or max_val > bestMatch:
bestMatch = max_val
bestDigit = i
print("New Best Match:", bestMatch, bestDigit)
if (bestMatch * 100) >= threshold:
return (bestDigit, bestMatch * 100)
return (-1, 0)
To get more reasonable results, we need to resize our digit image or template image. While we decide to choose which image needs to be resized, we need to find which one is smaller. Then, we can resize the bigger image. After resizing, we are using the matchTemplate function of OpenCV
to get the result.
You can find the resuls of the program below:
If you want to see the source code of the part 3 please go to GitHub Repo.