Python Forum
Help with CV2 pytesseract detcet numbers
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help with CV2 pytesseract detcet numbers
#1
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
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  12345

2 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,
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  help with pytesseract.image_to_string(savedImage, config='--psm 11')iamge to string korenron 0 2,694 Apr-29-2021, 10:08 AM
Last Post: korenron
  PYTesseract Problem gw1500se 2 3,139 Mar-26-2020, 02:11 PM
Last Post: gw1500se
  Error when executing pytesseract to get the text from image bvdinesh 3 17,896 Oct-19-2019, 05:01 PM
Last Post: cwco00
  Print Numbers starting at 1 vertically with separator for output numbers Pleiades 3 3,720 May-09-2019, 12:19 PM
Last Post: Pleiades
  pytesseract - enhance image before OCR kerzol81 0 3,707 Nov-19-2018, 06:21 PM
Last Post: kerzol81
  Getting Tesseract to work for pytesseract bigmit37 0 7,475 May-03-2017, 05:20 PM
Last Post: bigmit37

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020