hello ,
I want to run a code that detect numbers
I took very simple test case ,
use windows paint to write "12 345 67" in black , font 100 and save it as a L1.jpg file
this is the code I wrote
this is my output :
1. why I don't get the same resualt in both cases ?
2. why I don't get the all numbers?
Thanks,
I want to run a code that detect numbers
I took very simple test case ,
use windows paint to write "12 345 67" in black , font 100 and save it as a L1.jpg file
this is the code I wrote
import cv2
# import time
# import sys
import imutils
import numpy
import numpy as np
import pytesseract
# import tesseract
path = r'C:\L1.jpg'
path1 = r'C:\L1.jpg'
savedImage = r'C:\test.jpg'
pytesseract.pytesseract.tesseract_cmd = 'C:\\Program Files\\Tesseract-OCR\\tesseract.exe' # your path may be different
src = cv2.imread(path)
src1 = cv2.imread(path1)
cv2.imshow('original', src)
gray = cv2.cvtColor(src, cv2.COLOR_BGR2GRAY)
cv2.imshow('gray', gray)
gray1 = cv2.bilateralFilter(gray, 11, 17, 17)
cv2.imshow('gray1', gray1)
edged = cv2.Canny(gray1, 30, 200)
cv2.imshow('edged', edged)
cnts = cv2.findContours(edged.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
cnts = imutils.grab_contours(cnts)
cnts = sorted(cnts, key=cv2.contourArea, reverse=True)[:10]
#print(cnts)
screenCnt = None
detectLicenseRectangle = False
for c in cnts:
peri = cv2.arcLength(c, True)
approx = cv2.approxPolyDP(c, 0.018 * peri, True) # need to understand the math
if len(approx) > 3:
print('I found %d corners \r\n ' % (len(approx)))
print(approx) # will print the coordinate of the license plate
screenCnt = approx
print('Found license plate location!')
detectLicenseRectangle = True
break
else:
print("Can't find license plate")
print('----', detectLicenseRectangle)
if detectLicenseRectangle:
print('this is all the corners I have found ', str(screenCnt), '\n\r', str(type(screenCnt)))
cv2.drawContours(src, [screenCnt], -1, (255, 0, 0), 3)
cv2.imshow('Original with blue', src)
# will black the image except the license plate
mask = np.zeros(gray.shape, np.uint8)
new_image = cv2.drawContours(mask, [screenCnt], 0, 255, -1, )
new_image = cv2.bitwise_and(edged, edged, mask=mask)
cv2.imshow('New Mask Image', new_image)
(x, y) = np.where(mask == 255)
(topx, topy) = (np.min(x), np.min(y))
(bottomx, bottomy) = (np.max(x), np.max(y))
print('top left ', (topy, topx))
print('button right ', (bottomy, bottomx))
Cropped = new_image[topx:bottomx+1, topy:bottomy+1]
cv2.imshow('Cropped image ', Cropped)
cv2.imwrite(savedImage, Cropped)
LicensePlateNumber = pytesseract.image_to_string(Cropped, config='--psm 11 -c tessedit_char_whitelist=0123456789-')
print('you license plate is ', LicensePlateNumber)
topLeft = (np.min(y), np.min(x))
buttonLeft = (np.min(y), np.max(x))
topRight = (np.max(y), np.min(x))
buttonRight = (np.max(y), np.max(x))
testArray = topLeft, topRight, buttonRight, buttonLeft
testArray = numpy.asarray(testArray)
cv2.drawContours(src1, [testArray], -1, (255, 0, 0), 3)
cv2.imshow('My OWN TESTING!!!!!!!!', src1)
mask = np.zeros(gray.shape, np.uint8)
new_image1 = cv2.drawContours(mask, [testArray], 0, 255, -1, )
new_image1 = cv2.bitwise_and(edged, edged, mask=mask)
cv2.imshow('New Mask Image11111', new_image1)
Cropped1 = new_image1[np.min(x):np.max(x)+1, np.min(y):np.max(y)+1]
cv2.imshow('Cropped image1 ', Cropped1)
licensePlateNumber1 = pytesseract.image_to_string(Cropped1, config='--psm 11 -c tessedit_char_whitelist=-0123456789')
print('you license plate 1 is ', licensePlateNumber1)
cv2.waitKey(0)
cv2.destroyAllWindows() this is my output :
you license plate is 123456 you license plate 1 is 123452 things I need hep with
1. why I don't get the same resualt in both cases ?
2. why I don't get the all numbers?
Thanks,
