My code is not well done for me. I can't read the license plate fair well. How can I improve my code. Can you help me?
There is my License Plate Recognition code:
lpr.py import cv2
import imutils
import numpy as np
import pytesseract
def rec(img):
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
gray = cv2.bilateralFilter(gray, 11, 17, 17)
edged = cv2.Canny(gray, 30, 200)
try:
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]
screenCnt = None
for c in cnts:
peri = cv2.arcLength(c, True)
approx = cv2.approxPolyDP(c, 0.018 * peri, True)
if len(approx) == 4:
screenCnt = approx
break
if screenCnt is None:
detected = 0
else:
detected = 1
if detected == 1:
cv2.drawContours(img, [screenCnt], -1, (0, 255, 0), 3)
mask = np.zeros(gray.shape, np.uint8)
new_image = cv2.drawContours(mask, [screenCnt], 0, 255, -1,)
new_image = cv2.bitwise_and(img, img, mask=mask)
(x, y) = np.where(mask == 255)
(topx, topy) = (np.min(x), np.min(y))
(bottomx, bottomy) = (np.max(x), np.max(y))
Cropped = gray[topx:bottomx + 1, topy:bottomy + 1]
text = pytesseract.image_to_string(
Cropped, config="-c tessedit_char_whitelist='ABCDEFGHIJKLMNOPRSTUVYZ0123456789 '")
text = text.rstrip("\n")
if len(text) > 7:
i = 0
temp = text
while not temp.isdigit() or i == len(text) - 1:
i += 1
temp = text
text = text[i:-2]
except Exception:
pass
return img
main.py
import numpy as np
import cv2
import LPR as lpr
cap = cv2.VideoCapture(0)
while True:
ret, frame = cap.read()
img = lpr.rec(frame)
cv2.imshow('frame', img)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
![Image]()
There is my License Plate Recognition code:
lpr.py
Code:
import imutils
import numpy as np
import pytesseract
def rec(img):
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
gray = cv2.bilateralFilter(gray, 11, 17, 17)
edged = cv2.Canny(gray, 30, 200)
try:
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]
screenCnt = None
for c in cnts:
peri = cv2.arcLength(c, True)
approx = cv2.approxPolyDP(c, 0.018 * peri, True)
if len(approx) == 4:
screenCnt = approx
break
if screenCnt is None:
detected = 0
else:
detected = 1
if detected == 1:
cv2.drawContours(img, [screenCnt], -1, (0, 255, 0), 3)
mask = np.zeros(gray.shape, np.uint8)
new_image = cv2.drawContours(mask, [screenCnt], 0, 255, -1,)
new_image = cv2.bitwise_and(img, img, mask=mask)
(x, y) = np.where(mask == 255)
(topx, topy) = (np.min(x), np.min(y))
(bottomx, bottomy) = (np.max(x), np.max(y))
Cropped = gray[topx:bottomx + 1, topy:bottomy + 1]
text = pytesseract.image_to_string(
Cropped, config="-c tessedit_char_whitelist='ABCDEFGHIJKLMNOPRSTUVYZ0123456789 '")
text = text.rstrip("\n")
if len(text) > 7:
i = 0
temp = text
while not temp.isdigit() or i == len(text) - 1:
i += 1
temp = text
text = text[i:-2]
except Exception:
pass
return img
Code:
import numpy as np
import cv2
import LPR as lpr
cap = cv2.VideoCapture(0)
while True:
ret, frame = cap.read()
img = lpr.rec(frame)
cv2.imshow('frame', img)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
Statistics: Posted by ahmet4635 — Sat Jun 07, 2025 6:26 pm