from matplotlib import pyplot as plt
from paddleocr import PaddleOCR, draw_ocr
from PIL import Image, ImageDraw, ImageFont
ocr = PaddleOCR(use_angle_cls=True, use_gpu=False)
result = ocr.ocr(img_path, cls=True)
def infor_write(img, rect, result):
text = result[0][0][1][0]
cv2img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
pilimg = Image.fromarray(cv2img)
draw = ImageDraw.Draw(pilimg)
font = ImageFont.truetype("simhei.ttf", 20, encoding="utf-8")
draw.text((rect[2], rect[1]), str(text), (0, 255, 0), font=font)
cv2charimg = cv2.cvtColor(np.array(pilimg), cv2.COLOR_RGB2BGR)
plt.imshow(img,camp='gray')
img = cv2.GaussianBlur(img, (1, 1), 0)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
a = 400 * img.shape[0] / img.shape[1]
img = cv2.resize(img, (400, a))
Sobel_x = cv2.Sobel(img, cv2.CV_16S, 1, 0)
absX = cv2.convertScaleAbs(Sobel_x)
def find_retangle(contour):
return [min(y), min(x), max(y), max(x)]
contours, hierarchy = cv2.findContours(img, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
x, y, w, h = cv2.boundingRect(c)
a = (r[2] - r[0]) * (r[3] - r[1])
s = (r[2] - r[0]) / (r[3] - r[1])
if (w > (h * 3)) and (w < (h * 5)):
blocks = sorted(blocks, key=lambda b: b[1])[-3:]
maxweight, maxindex = 0, -1
for i in range(len(blocks)):
b = oriimg[blocks[i][0][1]:blocks[i][0][3], blocks[i][0][0]:blocks[i][0][2]]
hsv = cv2.cvtColor(b, cv2.COLOR_BGR2HSV)
lower = np.array([70, 150, 50])
upper = np.array([120, 255, 255])
mask = cv2.inRange(hsv, lower, upper)
print('blocks是', blocks[maxindex])
print('blocks0是',blocks[maxindex][0])
return blocks[maxindex][0]
def fine_lisecenpts(img):
sobel = Sobel_detec(guss)
ret, threshold = cv2.threshold(sobel, 0, 255, cv2.THRESH_OTSU)
kernelX = cv2.getStructuringElement(cv2.MORPH_RECT, (30, 10))
closing = cv2.morphologyEx(threshold, cv2.MORPH_CLOSE, kernelX, iterations=1)
kernelX = cv2.getStructuringElement(cv2.MORPH_RECT, (50, 1))
kernelY = cv2.getStructuringElement(cv2.MORPH_RECT, (1, 20))
img = cv2.dilate(closing, kernelX)
img = cv2.erode(img, kernelX)
img = cv2.erode(img, kernelY)
img = cv2.dilate(img, kernelY)
Blur = cv2.medianBlur(img, 15)
rect = locate_license(Blur)
def seg_char(rect_list, img):
img = oriimg[rect_list[1]:rect_list[3], rect_list[0]:rect_list[2]]
k1 = np.ones((1, 1), np.uint8)
close = cv2.morphologyEx(gray, cv2.MORPH_CLOSE, k1)
cv2.imshow('close', close)
cv2.imwrite(r"E:\ultralytics-20240216\21\img2\6.jpg", close)
res = text_scan(r"E:\ultralytics-20240216\21\img2\6.jpg")
def put_chinese_text(img, text, left_top):
img_PIL = Image.fromarray(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
draw = ImageDraw.Draw(img_PIL)
font = ImageFont.truetype('simhei.ttf', 30, encoding="utf-8")
draw.text(position, text, font=font, fill=fillColor)
img_out = cv2.cvtColor(np.asarray(img_PIL),cv2.COLOR_RGB2BGR)
if __name__ == '__main__':
img = cv2.imread(r"E:\ultralytics-20240216\21\img2\5.jpg")
rect, img = fine_lisecenpts(img)
result = seg_char(rect, oriimg)
text = result[0][0][1][0]
left_top = tuple(rect[0:2])
right_bottom = tuple(rect[2:4])
cv2.rectangle(oriimg, left_top, right_bottom, (0, 255, 255), 2)
text_position = (right_bottom[0] + 1, right_bottom[1])
oriimg = put_chinese_text(oriimg, text, text_position)
cv2.imshow("Image with text", oriimg)