10.31OpenCV_图像预处理习题

实现在图片里截取车牌并识别车牌内容

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()
上一篇:网管平台(三):如何高效管理无线网络


下一篇:加强科技平台企业赋能 加快发展新质生产力