61 lines
1.9 KiB
Python
61 lines
1.9 KiB
Python
|
|
|
||
|
|
import cv2
|
||
|
|
import os
|
||
|
|
import difflib
|
||
|
|
import numpy as np
|
||
|
|
from paddleocr import PaddleOCR
|
||
|
|
|
||
|
|
def similarity(s1, s2):
|
||
|
|
return difflib.SequenceMatcher(None, s1, s2).ratio()
|
||
|
|
|
||
|
|
def rapid_test():
|
||
|
|
target = "威凯检测技术有限公司"
|
||
|
|
img_path = "seal_cropped.png"
|
||
|
|
img = cv2.imread(img_path)
|
||
|
|
if img is None: return
|
||
|
|
|
||
|
|
h_orig, w_orig = img.shape[:2]
|
||
|
|
cx, cy = w_orig // 2, h_orig // 2
|
||
|
|
radius = min(cx, cy)
|
||
|
|
|
||
|
|
ocr = PaddleOCR(use_angle_cls=True, lang='ch')
|
||
|
|
|
||
|
|
# We'll test a few angles and two factors
|
||
|
|
angles = [0, 90, 180, 270]
|
||
|
|
factors = [1.0, 1.2]
|
||
|
|
|
||
|
|
for angle in angles:
|
||
|
|
M_rot = cv2.getRotationMatrix2D((cx, cy), angle, 1.0)
|
||
|
|
rotated = cv2.warpAffine(img, M_rot, (w_orig, h_orig), borderValue=(255, 255, 255))
|
||
|
|
|
||
|
|
for factor in factors:
|
||
|
|
out_w = int(radius * 2 * np.pi * factor)
|
||
|
|
out_h = radius
|
||
|
|
unwarped = cv2.warpPolar(rotated, (out_w, out_h), (cx, cy), radius, cv2.WARP_POLAR_LINEAR)
|
||
|
|
|
||
|
|
# Annulus for outer text
|
||
|
|
strip = unwarped[int(out_h * 0.75):int(out_h * 0.95), :]
|
||
|
|
|
||
|
|
# Padding
|
||
|
|
padded = cv2.copyMakeBorder(strip, 30, 30, 0, 0, cv2.BORDER_CONSTANT, value=[255, 255, 255])
|
||
|
|
|
||
|
|
# OCR
|
||
|
|
res = ocr.ocr(padded)
|
||
|
|
|
||
|
|
best_text = ""
|
||
|
|
best_sim = 0.0
|
||
|
|
if res:
|
||
|
|
for page in res:
|
||
|
|
if 'rec_texts' in page:
|
||
|
|
for t in page['rec_texts']:
|
||
|
|
ct = t.replace(" ", "")
|
||
|
|
s = similarity(target, ct)
|
||
|
|
if s > best_sim:
|
||
|
|
best_sim = s
|
||
|
|
best_text = ct
|
||
|
|
|
||
|
|
print(f"Angle: {angle} | Factor: {factor} | Sim: {best_sim:.4f} | Text: {best_text}")
|
||
|
|
|
||
|
|
if __name__ == "__main__":
|
||
|
|
rapid_test()
|