实现在图片里截取车牌并识别车牌内容
import cv2
import numpy as np
import paddlehub as hub
def get_text():
img = cv2.imread("images/car.png")
ocr = hub.Module(name="chinese_ocr_db_crnn_server")
results = ocr.recognize_text(images=[img])
for result in results:
data = result['data']
for x in data:
print('文本: ', x['text'])
if __name__ =="__main__":
get_text()
img = cv2.imread("images/car.png")
hsv_img = cv2.cvtColor(img,cv2.COLOR_BGR2HSV)
lower = np.array([100,100,50])
height = np.array([140,255,255])
mask = cv2.inRange(hsv_img,lower,height)
ret,t_img = cv2.threshold(mask,20,255,cv2.THRESH_BINARY)
myList,c = cv2.findContours(t_img,cv2.RETR_LIST,cv2.CHAIN_APPROX_SIMPLE)
bgr_img = cv2.cvtColor(t_img,cv2.COLOR_GRAY2BGR)
out_img = cv2.drawContours(bgr_img,myList,-1,(0,255,0),1);
for c in myList:
x,y,w,h = cv2.boundingRect(c)
if w > 100 and h >50:
cv2.rectangle(img, (x, y), (x + w, y + h), (0, 255, 0), 1)
cai_img = img[y:y + h, x:w + x]
cv2.imshow("a", cai_img)
cv2.waitKey(0)
cv2.destroyAllWindows()