#python #opencv #python-tesseract
#python #opencv #python-tesseract
Вопрос:
Мой код OpenCV работает просто отлично. Он находит номерной знак, извлекает его черно-белую версию, используя контуры, а затем, когда я передаю его в pytesseract, он не прочитает ни одной буквы. Я отслеживал программу по каждой строке кода, и OpenCV работает нормально, но pytesseract не извлекает текст из изображения. Ошибок нет, он просто не читает текст. Номерной знак мой.
import cv2
# pip install imutils
import imutils
import pytesseract
pytesseract.pytesseract.tesseract_cmd = r'C:Program Files (x86)Tesseract-OCRtesseract.exe'
# Read the image file
image = cv2.imread('LP.jpg')
# image = imutils.resize(image, width=500)
# Convert to Grayscale Image
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# Removes Noise
gray_image = cv2.bilateralFilter(gray_image, 11, 17, 17)
# Canny Edge Detection
canny_edge = cv2.Canny(gray_image, 100, 200)
# Find contours based on Edges
# The code below needs an - or else you'll get a ValueError: too many values to unpack (expected 2) or a numpy error
_, contours, new = cv2.findContours(canny_edge.copy(), cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
contours = sorted(contours, key=cv2.contourArea, reverse=True)[:30]
# # Initialize license Plate contour and x,y coordinates
contour_with_license_plate = None
license_plate = None
x = None
y = None
w = None
h = None
# Find the contour with 4 potential corners and create a Region of Interest around it
for contour in contours:
# Find Perimeter of contour and it should be a closed contour
perimeter = cv2.arcLength(contour, True)
approx = cv2.approxPolyDP(contour, 0.02 * perimeter, True)
# This checks if it's a rectangle
if len(approx) == 4:
contour_with_license_plate = approx
x, y, w, h = cv2.boundingRect(contour)
license_plate = gray_image[y:y h, x:x w]
break
# # approximate_contours = cv2.drawContours(image, [contour_with_license_plate], -1, (0, 255, 0), 3)
# Text Recognition
text = pytesseract.image_to_string(license_plate, lang='eng')
print(text)
# Draw License Plate and write the Text
image = cv2.rectangle(image, (x, y), (x w, y h), (0, 255, 0), 3)
image = cv2.putText(image, text, (x-100, y-50), cv2.FONT_HERSHEY_SIMPLEX, 3, (0, 255, 0), 6, cv2.LINE_AA)
print("License Plate: ", text)
cv2.imshow("License Plate Detection", image)
cv2.waitKey(0)
Комментарии:
1. Не могли бы вы указать, как выглядит изображение, прежде чем отправлять его на распознавание?
2. Используйте cv2.inRange() для порогового значения черного цвета, чтобы буквы были белыми, а остальные черными. Затем получите контуры и их ограничивающие рамки
3. @yudhiesh, я обновил файл изображением, на которое смотрит OCR. Пожалуйста, дайте мне знать, если это поможет определить мою проблему.
Ответ №1:
Вот мой частичный ответ, может быть, вы сможете его усовершенствовать.
Примените adaptive-threshold
bitwise-not
операции к license_plate
переменной.
Результатом будет:
Теперь, если вы прочитаете это:
txt = pytesseract.image_to_string(bnt, config="--psm 6")
print(txt)
Результат:
277 BOY
К сожалению Q
, распознается как O
.
Код: (Просто замените часть с комментарием text recogniiton на приведенную ниже)
thr = cv2.adaptiveThreshold(license_plate, 252, cv2.ADAPTIVE_THRESH_MEAN_C,
cv2.THRESH_BINARY_INV, 91, 93)
bnt = cv2.bitwise_not(thr)
txt = pytesseract.image_to_string(bnt, config="--psm 6")
print(txt)