使用opencv和pytesseract检测车牌号码失败



我刚刚从以下链接学习车牌号码检测:

教程链接"我的代码:

import cv2
import imutils
import pytesseract
pytesseract.pytesseract.tesseract_cmd = '/usr/bin/tesseract'
image = cv2.imread('test.jpg')
image = imutils.resize(image, width=300)
cv2.imshow("original image", image)
cv2.waitKey(0)
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
cv2.imshow("greyed image", gray_image)
cv2.waitKey(0)
gray_image = cv2.bilateralFilter(gray_image, 11, 17, 17)
cv2.imshow("smoothened image", gray_image)
cv2.waitKey(0)
edged = cv2.Canny(gray_image, 30, 200)
cv2.imshow("edged image", edged)
cv2.waitKey(0)
cnts, new = cv2.findContours(edged.copy(), cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
image1 = image.copy()
cv2.drawContours(image1, cnts, -1, (0, 255, 0), 3)
cv2.imshow("contours", image1)
cv2.waitKey(0)
cnts = sorted(cnts, key=cv2.contourArea, reverse=True)[:30]
screenCnt = None
image2 = image.copy()
cv2.drawContours(image2, cnts, -1, (0, 255, 0), 3)
cv2.imshow("Top 30 contours", image2)
cv2.waitKey(0)
i = 7
for c in cnts:
perimeter = cv2.arcLength(c, True)
approx = cv2.approxPolyDP(c, 0.018 * perimeter, True)
if len(approx) == 4:
screenCnt = approx
x, y, w, h = cv2.boundingRect(c)
new_img = image[y:y+h, x:x+w]
cv2.imwrite('./'+str(i)+'.png', new_img)
i += 1
break
cv2.drawContours(image, [screenCnt], -1, (0, 255, 0), 3)
cv2.imshow("image with detected license plate", image)
cv2.waitKey(0)
Cropped_loc = './7.png'
cv2.imshow("cropped", cv2.imread(Cropped_loc))
plate = pytesseract.image_to_string(Cropped_loc, lang='eng')
print("Number plate is:", plate)
cv2.waitKey(0)
cv2.destroyAllWindows()

但是我不能得到预期的输出。我甚至不能排除故障,因为没有错误。执行代码后的输出"我以为这很简单,网站要求更多的细节,这就是为什么我在报价中说垃圾话。">

这部分代码需要在if语句中。

x, y, w, h = cv2.boundingRect(c)
new_img = image[y:y+h, x:x+w]
cv2.imwrite('./'+str(i)+'.png', new_img)
i += 1
break

这部分代码需要跳出循环。

cv2.drawContours(image, [screenCnt], -1, (0, 255, 0), 3)
cv2.imshow("image with detected license plate", image)
cv2.waitKey(0)

所以你的循环需要像这样:

for c in cnts:
perimeter = cv2.arcLength(c, True)
approx = cv2.approxPolyDP(c, 0.018 * perimeter, True)
if len(approx) == 4:
screenCnt = approx
x, y, w, h = cv2.boundingRect(c)
new_img = image[y:y+h, x:x+w]
cv2.imwrite('./'+str(i)+'.png', new_img)
i += 1
break
cv2.drawContours(image, [screenCnt], -1, (0, 255, 0), 3)
cv2.imshow("image with detected license plate", image)
cv2.waitKey(0)

最新更新