E:\song2\paddler_ocr\paddleocr_learn00\le00.py
| |
| from paddleocr import PaddleOCR |
| |
| |
| |
| ocr = PaddleOCR() |
| img_path = './12.jpg' |
| |
| |
| result = ocr.ocr(img_path, rec=True) |
| print(f"The predicted text box of {img_path} are follows.") |
| print(result) |
| |
E:\song2\paddler_ocr\paddleocr_learn00\le01.py
| import numpy as np |
| import cv2 |
| import matplotlib.pyplot as plt |
| |
| from paddleocr import PaddleOCR |
| |
| |
| img_path = './13.jpg' |
| |
| |
| ocr = PaddleOCR() |
| |
| |
| result = ocr.ocr(img_path, rec=True) |
| |
| |
| image = cv2.imread(img_path) |
| |
| |
| |
| |
| for res1 in result: |
| for res2 in res1: |
| pos = res2[0] |
| text= res2[1] |
| print('------------') |
| print(pos) |
| print(text) |
| |
| |
| box_temp = np.reshape(np.array(pos), [-1, 1, 2]).astype(np.int64) |
| image = cv2.polylines(np.array(image), [box_temp], True, (255, 0, 0), 2) |
| |
| |
| plt.figure(figsize=(10, 10)) |
| plt.imshow(image) |
| plt.show() |
| |
| |
| |
| |
| |
| |
E:\song2\paddler_ocr\paddleocr_learn00\le02.py
| |
| import numpy as np |
| res = [ |
| [ |
| [[[42.0, 414.0], [483.0, 391.0], [485.0, 428.0], [44.0, 451.0]], ('上海斯格威铂尔受大酒店', 0.8757175803184509)], |
| [[[187.0, 457.0], [399.0, 448.0], [401.0, 480.0], [188.0, 489.0]], ('打浦路15号', 0.8970528244972229) ], |
| [[[23.0, 508.0], [514.0, 488.0], [516.0, 529.0], [25.0, 549.0]], ('绿洲仕格维花园公寓', 0.9934195280075073)], |
| [[[74.0, 554.0], [428.0, 542.0], [429.0, 571.0], [75.0, 582.0]], ('打浦路252935号', 0.9223073720932007)] |
| ] |
| ] |
| |
| |
| |
| boxes = [line[0] for line in res] |
| |
| |
| for box in boxes: |
| |
| box = np.reshape(np.array(box[0]), [-1, 1, 2]).astype(np.int64) |
| print(box) |
| |
| |
E:\song2\paddler_ocr\paddleocr_learn00\le03.py
| |
| import cv2 |
| import numpy as np |
| |
| img = cv2.imread("12.jpg", 1) |
| pts = np.array([[10, 10], [400, 10], [400, 400], [10, 400]], np.int64) |
| print(pts) |
| |
| pts = pts.reshape((-1, 1, 2)) |
| print('-------------') |
| print(pts) |
| |
| |
| cv2.polylines(img, [pts], isClosed=True, color=(0,0,255), thickness=1) |
| |
| |
| |
| cv2.imshow('img', img) |
| cv2.waitKey() |
E:\song2\paddler_ocr\paddleocr_learn00\le04.py
| |
| |
| |
| |
| import cv2 |
| import numpy as np |
| |
| |
| car_original = cv2.imread('./car.jpg') |
| |
| rows,cols,td = car_original.shape |
| print(rows,cols,td) |
| cv2.imshow('car_original',car_original) |
| |
| |
| ''' |
| # 基于颜色信息进行二值化 |
| 正常光照下蓝色车牌的BGR大约是(138,63,23), |
| 由于存在一定的偏差,在二值化的时候设置一个范围,在范围之内的颜色(车牌的蓝色背景)设置成白色,否则设置成黑色。 |
| ''' |
| blue_goal = 138 |
| green_goal = 63 |
| red_goal = 23 |
| |
| threshold = 50 |
| |
| |
| for i in range(0,rows): |
| for j in range(0,cols): |
| B = car_original[i,j,0] |
| G = car_original[i,j,1] |
| R = car_original[i,j,2] |
| if abs(B-blue_goal)<threshold and abs(G-green_goal)<threshold and abs(R-red_goal)<threshold: |
| car_original[i,j,0] = 255 |
| car_original[i,j,1] = 255 |
| car_original[i,j,2] = 255 |
| else: |
| car_original[i,j,0] = 0 |
| car_original[i,j,1] = 0 |
| car_original[i,j,2] = 0 |
| |
| cv2.imshow('car_binarization',car_original) |
| |
| |
| |
| kernel = np.ones((3,3),np.int8) |
| dilation = cv2.dilate(car_original,kernel,iterations=3) |
| cv2.imshow('car_dilation',dilation) |
| |
| erosion = cv2.erode(dilation,kernel,iterations=3) |
| cv2.imshow('car_erosion',erosion) |
| |
| |
| |
| car = cv2.cvtColor(erosion,cv2.COLOR_BGR2GRAY) |
| |
| |
| |
| contours,hierarchy = cv2.findContours(car,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE) |
| for c in contours: |
| x,y,w,h = cv2.boundingRect(c) |
| |
| |
| if w>60 and h>20: |
| |
| x_goal = x |
| y_goal = y |
| w_goal = w |
| h_goal = h |
| goal = c |
| |
| car_goal = cv2.imread('./car.jpg') |
| cv2.rectangle(car_goal,(x_goal,y_goal),(x_goal+w_goal,y_goal+h_goal),(0,255,0),2) |
| cv2.imshow('goal',car_goal) |
| cv2.waitKey(0) |
| |
| |
| |
| |
E:\song2\paddler_ocr\paddleocr_learn00\le05.py
| |
| import cv2 |
| import numpy as np |
| |
| from imutils.perspective import four_point_transform |
| |
| |
| car_original = cv2.imread('car_edge.jpg') |
| car_gray = cv2.cvtColor(car_original,cv2.COLOR_BGR2GRAY) |
| |
| |
| car_blur = cv2.GaussianBlur(car_gray,(3,3),0) |
| car_edge = cv2.Canny(car_blur,250,300) |
| |
| |
| |
| kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (9,1)) |
| car_dilation_2 = cv2.dilate(car_edge,kernel,iterations=2) |
| |
| car_erosion_4 = cv2.erode(car_dilation_2,kernel,iterations=4) |
| |
| car_dilation_4 = cv2.dilate(car_erosion_4,kernel,iterations=2) |
| |
| |
| |
| kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (1,5)) |
| car_erosion_6 = cv2.erode(car_dilation_4,kernel,iterations=2) |
| |
| car_dilation_6 = cv2.dilate(car_erosion_6,kernel,iterations=2) |
| |
| |
| |
| contours,hierarchy = cv2.findContours(car_dilation_6,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE) |
| for c in contours: |
| x,y,w,h = cv2.boundingRect(c) |
| if w>60 and h>20: |
| x_goal = x |
| y_goal = y |
| w_goal = w |
| h_goal = h |
| goal = c |
| car_goal = cv2.imread('car_edge.jpg') |
| cv2.rectangle(car_goal,(x_goal,y_goal),(x_goal+w_goal,y_goal+h_goal),(0,255,0),2) |
| |
| |
| license_plate = car_original[y_goal:y_goal+h_goal,x_goal:x_goal+w_goal,:] |
| |
| |
| |
| |
| ''' |
| 传统的字符分割方法主要有:直接分割法、基于识别基础上的分割法、自适应分割线聚类法 |
| 直接分割法简单,但是其局限是分割点的确定需要比较高的准确性 |
| 基于识别基础上的分割法,把识别和分割结合起来,但是需要识别的高准确性,根据识别和分类的耦合程度又有不同的划分 |
| 自适应分割线聚类法,建立一个分类器,用来判断图像的每一列是否是分割线,是根据训练样本来进行自适应学习的神经网络分类器,但是对于粘连字符训练困难 |
| 还有直接把字符组成的单词当作一个整体来识别的,例如运用马尔科夫数学模型等方法进行处理,主要用于印刷体文本识别 |
| ''' |
| |
| license_plate_large = cv2.resize(license_plate,None,fx=3,fy=3,interpolation=cv2.INTER_CUBIC) |
| |
| license_plate_gray = cv2.cvtColor(license_plate_large,cv2.COLOR_BGR2GRAY) |
| img_blur = cv2.GaussianBlur(license_plate_gray,(5,5),0) |
| canny_edge = cv2.Canny(img_blur,80,200) |
| |
| |
| |
| ''' |
| #使用Otsu算法进行二值化 |
| ret,threshold = cv2.threshold(img_blur,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU) |
| cv2.imshow('threshold',threshold) #仅显示车牌区域 |
| ''' |
| |
| (x,y)=canny_edge.shape |
| |
| canny_edge[:,0] = 0 |
| canny_edge[:,y-1] = 0 |
| pointCount=np.zeros(y,dtype=np.uint8) |
| |
| for i in range(0,y): |
| for j in range(0,x): |
| if(canny_edge[j,i]==255): |
| pointCount[i]=pointCount[i]+1 |
| |
| |
| |
| start = [] |
| end = [] |
| |
| for index in range(1, y): |
| |
| if ((pointCount[index-1] == 0) & (pointCount[index] != 0)): |
| start.append(index) |
| |
| elif ((pointCount[index-1] != 0) & (pointCount[index] == 0)): |
| end.append(index) |
| |
| |
| |
| imgArr=np.zeros(x) |
| imgArr=imgArr.reshape((-1,1)) |
| for idx in range(0,len(start)): |
| if idx in [0,1,3,4,5,6,7]: |
| temp_img=canny_edge[:,start[idx]:end[idx]] |
| imgArr=np.hstack((imgArr,temp_img)) |
| (xx,yy)=imgArr.shape |
| imgArr=imgArr[:,1:yy] |
| |
| cv2.imshow('vertical_cut',imgArr) |
| |
| |
| |
| cv2.waitKey(0) |
| |
E:\song2\paddler_ocr\paddleocr_learn00\le06.py
| import cv2 |
| import matplotlib.pyplot as plt |
| |
| import numpy as np |
| |
| from paddleocr import PaddleOCR |
| |
| |
| |
| |
| img_original = cv2.imread('./13.jpg') |
| rows,cols,td = img_original.shape |
| |
| cv2.imshow('car_original',img_original) |
| |
| |
| |
| blue_goal =254 |
| green_goal = 61 |
| red_goal = 24 |
| |
| threshold = 50 |
| |
| |
| for i in range(0,rows): |
| for j in range(0,cols): |
| B = img_original[i,j,0] |
| G = img_original[i,j,1] |
| R = img_original[i,j,2] |
| if abs(B-blue_goal)<threshold and abs(G-green_goal)<threshold and abs(R-red_goal)<threshold: |
| img_original[i,j,0] = 255 |
| img_original[i,j,1] = 255 |
| img_original[i,j,2] = 255 |
| else: |
| img_original[i,j,0] = 0 |
| img_original[i,j,1] = 0 |
| img_original[i,j,2] = 0 |
| |
| |
| cv2.imshow('img_binarization',img_original) |
| |
| |
| |
| |
| |
| |
| kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (10,10)) |
| dilation_1 = cv2.dilate(img_original,kernel,iterations=3) |
| |
| kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (1,10)) |
| dilation_2 = cv2.dilate(dilation_1,kernel,iterations=3) |
| |
| cv2.imshow('dilation1',dilation_2) |
| |
| |
| kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (10,10)) |
| erosion_1 = cv2.erode(dilation_2,kernel,iterations=5) |
| |
| cv2.imshow('car_dilation',erosion_1) |
| |
| |
| |
| dst_img = cv2.cvtColor(erosion_1,cv2.COLOR_BGR2GRAY) |
| |
| |
| |
| contours,hierarchy = cv2.findContours(dst_img,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE) |
| |
| for c in contours: |
| x,y,w,h = cv2.boundingRect(c) |
| |
| if w>320 and h>210: |
| |
| x_goal = x |
| y_goal = y |
| w_goal = w |
| h_goal = h |
| goal = c |
| |
| car_goal = cv2.imread('./13.jpg') |
| cv2.rectangle(car_goal,(x_goal,y_goal),(x_goal+w_goal,y_goal+h_goal),(0,0,255),4) |
| cv2.imshow('goal',car_goal) |
| |
| |
| |
| img_humi = img_original[y_goal:y_goal+h_goal,x_goal:x_goal+w_goal] |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| plt.figure(figsize=(10, 10)) |
| plt.imshow(img_humi) |
| plt.show() |
| |
| |
| |
| |
| |
| |
| |
E:\song2\paddler_ocr\paddleocr_learn00\le07.py
| import cv2 |
| import matplotlib.pyplot as plt |
| |
| import numpy as np |
| |
| from paddleocr import PaddleOCR |
| |
| |
| |
| |
| img_original = cv2.imread('./13.jpg') |
| rows,cols,td = img_original.shape |
| |
| |
| |
| |
| |
| blue_goal =254 |
| green_goal = 61 |
| red_goal = 24 |
| |
| threshold = 50 |
| |
| |
| for i in range(0,rows): |
| for j in range(0,cols): |
| B = img_original[i,j,0] |
| G = img_original[i,j,1] |
| R = img_original[i,j,2] |
| if abs(B-blue_goal)<threshold and abs(G-green_goal)<threshold and abs(R-red_goal)<threshold: |
| img_original[i,j,0] = 255 |
| img_original[i,j,1] = 255 |
| img_original[i,j,2] = 255 |
| else: |
| img_original[i,j,0] = 0 |
| img_original[i,j,1] = 0 |
| img_original[i,j,2] = 0 |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (10,10)) |
| dilation_1 = cv2.dilate(img_original,kernel,iterations=3) |
| |
| kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (1,10)) |
| dilation_2 = cv2.dilate(dilation_1,kernel,iterations=3) |
| |
| |
| |
| |
| kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (10,10)) |
| erosion_1 = cv2.erode(dilation_2,kernel,iterations=5) |
| |
| |
| |
| |
| |
| dst_img = cv2.cvtColor(erosion_1,cv2.COLOR_BGR2GRAY) |
| |
| |
| |
| contours,hierarchy = cv2.findContours(dst_img,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE) |
| |
| for c in contours: |
| x,y,w,h = cv2.boundingRect(c) |
| |
| if w>320 and h>210: |
| |
| x_goal = x |
| y_goal = y |
| w_goal = w |
| h_goal = h |
| goal = c |
| |
| |
| car_goal = cv2.imread('./13.jpg') |
| cv2.rectangle(car_goal,(x_goal,y_goal),(x_goal+w_goal,y_goal+h_goal),(0,0,255),4) |
| |
| |
| |
| |
| img_humi = car_goal[y_goal:y_goal+h_goal,x_goal:x_goal+w_goal] |
| |
| |
| |
| |
| |
| |
| |
| ocr = PaddleOCR() |
| |
| |
| result = ocr.ocr(img_humi, rec=True) |
| |
| |
| |
| |
| |
| |
| |
| for res1 in result: |
| for res2 in res1: |
| pos = res2[0] |
| text= res2[1] |
| print('------------') |
| print(pos[0]) |
| print(pos[0][0]) |
| print(pos[0][1]) |
| print(type( pos[0][1])) |
| print(text) |
| |
| |
| box_temp = np.reshape(np.array(pos), [-1, 1, 2]).astype(np.int64) |
| img_humi = cv2.polylines(np.array(img_humi), [box_temp], True, (0, 255, 0), 2) |
| |
| cv2.putText(img_humi,text[0],(int(pos[0][0]), int(pos[0][1])), cv2.FONT_HERSHEY_SIMPLEX, 0.5,( 0,255,255),1,cv2.LINE_AA) |
| |
| |
| |
| |
| |
| plt.figure(figsize=(10, 10)) |
| plt.imshow(img_humi) |
| plt.show() |
| |
| |
| cv2.waitKey(0) |
| |
E:\song2\paddler_ocr\paddleocr_learn00\le08.py
| import cv2 |
| import matplotlib.pyplot as plt |
| |
| import numpy as np |
| |
| from paddleocr import PaddleOCR |
| |
| |
| |
| |
| img_original = cv2.imread('./13.jpg') |
| rows,cols,td = img_original.shape |
| |
| |
| |
| |
| |
| blue_goal =49 |
| green_goal = 63 |
| red_goal = 200 |
| |
| threshold = 50 |
| |
| |
| for i in range(0,rows): |
| for j in range(0,cols): |
| B = img_original[i,j,0] |
| G = img_original[i,j,1] |
| R = img_original[i,j,2] |
| if abs(B-blue_goal)<threshold and abs(G-green_goal)<threshold and abs(R-red_goal)<threshold: |
| img_original[i,j,0] = 255 |
| img_original[i,j,1] = 255 |
| img_original[i,j,2] = 255 |
| else: |
| img_original[i,j,0] = 0 |
| img_original[i,j,1] = 0 |
| img_original[i,j,2] = 0 |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (10,10)) |
| dilation_1 = cv2.dilate(img_original,kernel,iterations=3) |
| |
| kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (1,10)) |
| dilation_2 = cv2.dilate(dilation_1,kernel,iterations=3) |
| |
| |
| |
| |
| kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (10,10)) |
| erosion_1 = cv2.erode(dilation_2,kernel,iterations=5) |
| |
| |
| |
| |
| |
| dst_img = cv2.cvtColor(erosion_1,cv2.COLOR_BGR2GRAY) |
| |
| |
| |
| contours,hierarchy = cv2.findContours(dst_img,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE) |
| |
| for c in contours: |
| x,y,w,h = cv2.boundingRect(c) |
| |
| if w>320 and h>210: |
| |
| x_goal = x |
| y_goal = y |
| w_goal = w |
| h_goal = h |
| goal = c |
| |
| |
| car_goal = cv2.imread('./13.jpg') |
| cv2.rectangle(car_goal,(x_goal,y_goal),(x_goal+w_goal,y_goal+h_goal),(0,0,255),4) |
| |
| |
| |
| |
| img_humi = car_goal[y_goal:y_goal+h_goal,x_goal:x_goal+w_goal] |
| |
| |
| |
| |
| |
| |
| |
| ocr = PaddleOCR() |
| |
| |
| result = ocr.ocr(img_humi, rec=True) |
| |
| |
| |
| |
| |
| |
| |
| for res1 in result: |
| for res2 in res1: |
| pos = res2[0] |
| text= res2[1] |
| print('------------') |
| print(pos[0]) |
| print(pos[0][0]) |
| print(pos[0][1]) |
| print(type( pos[0][1])) |
| print(text) |
| |
| |
| box_temp = np.reshape(np.array(pos), [-1, 1, 2]).astype(np.int64) |
| img_humi = cv2.polylines(np.array(img_humi), [box_temp], True, (0, 255, 0), 2) |
| |
| cv2.putText(img_humi,text[0],(int(pos[0][0]), int(pos[0][1])), cv2.FONT_HERSHEY_SIMPLEX, 0.5,( 0,255,255),1,cv2.LINE_AA) |
| |
| |
| |
| |
| |
| plt.figure(figsize=(10, 10)) |
| plt.imshow(img_humi) |
| plt.show() |
| |
| |
| cv2.waitKey(0) |
| |
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· 没有源码,如何修改代码逻辑?
· PowerShell开发游戏 · 打蜜蜂
· 在鹅厂做java开发是什么体验
· WPF到Web的无缝过渡:英雄联盟客户端的OpenSilver迁移实战