LoF*_*F10 6 python opencv computer-vision python-tesseract
我正在尝试通过 opencv 和 pytesseract 运行一个简单的车牌图像来获取文本,但我无法从中获取任何内容。按照此处的教程进行操作:
我在 macbook 上运行,所有东西都安装在 anaconda 中,据我所知,没有错误,但是当我运行代码时,我得到了裁剪后的图像,但没有检测到数字:
(computer_vision) mac@x86_64-apple-darwin13 lpr % python explore.py
Detected Number is:
Run Code Online (Sandbox Code Playgroud)
代码如下:
import cv2
import numpy as np
import imutils
import pytesseract
img = cv2.imread('plate1.jpg')
img = cv2.resize(img, (620,480))
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) #convert to grey scale
gray = cv2.bilateralFilter(gray, 11, 17, 17)
edged = cv2.Canny(gray, 30, 200) #Perform Edge detection
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
# loop over our contours
for c in cnts:
# approximate the contour
peri = cv2.arcLength(c, True)
approx = cv2.approxPolyDP(c, 0.018 * peri, True)
# if our approximated contour has four points, then
# we can assume that we have found our screen
if len(approx) == 4:
screenCnt = approx
break
# Masking the part other than the number plate
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)
# Now crop
(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]
#Read the number plate
text = pytesseract.image_to_string(Cropped, config='--psm 11')
print("Detected Number is:",text)
cv2.imshow('image',Cropped)
cv2.waitKey(0)
cv2.destroyAllWindows()
Run Code Online (Sandbox Code Playgroud)
Rot*_*tem 11
您可以尝试不同的psm配置:
text = pytesseract.image_to_string(Cropped, config='--psm 3')
Run Code Online (Sandbox Code Playgroud)
输出是:Detected Number is: PHR. 26.BR 9044;.
text = pytesseract.image_to_string(Cropped, config='--psm 3')
Run Code Online (Sandbox Code Playgroud)
看起来 psm 11 尚未发布(或者太新):
0 = Orientation and script detection (OSD) only.
1 = Automatic page segmentation with OSD.
2 = Automatic page segmentation, but no OSD, or OCR. (not implemented)
3 = Fully automatic page segmentation, but no OSD. (Default)
4 = Assume a single column of text of variable sizes.
5 = Assume a single uniform block of vertically aligned text.
6 = Assume a single uniform block of text.
7 = Treat the image as a single text line.
8 = Treat the image as a single word.
9 = Treat the image as a single word in a circle.
10 = Treat the image as a single character.
11 = Sparse text. Find as much text as possible in no particular order.
12 = Sparse text with OSD.
13 = Raw line. Treat the image as a single text line,
bypassing hacks that are Tesseract-specific.
I don't know why `psm` `11` is not giving any output...
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8893 次 |
| 最近记录: |