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 optimize_unwarp(): target = "威凯检测技术有限公司" img_path = "seal_cropped.png" img = cv2.imread(img_path) if img is None: return h_orig, w_orig = img.shape[:2] cx_base, cy_base = w_orig // 2, h_orig // 2 radius = min(cx_base, cy_base) ocr = PaddleOCR(use_angle_cls=True, lang='ch') # Preprocess: remove red? # For now just use original to be safe. # Test a small grid around the center best_overall_sim = 0.0 best_params = {} for dx in [-10, -5, 0, 5, 10]: for dy in [-10, -5, 0, 5, 10]: cx, cy = cx_base + dx, cy_base + dy # We use a larger factor to be sure out_theta = 1500 out_r = radius unwarped = cv2.warpPolar(img, (out_r, out_theta), (cx, cy), radius, cv2.WARP_POLAR_LINEAR) final = cv2.transpose(unwarped) # Try a few different annuli for r_start, r_end in [(0.7, 0.9), (0.75, 0.95), (0.8, 1.0)]: strip = final[int(out_r * r_start):int(out_r * r_end), :] # Padding is essential for small strips padded = cv2.copyMakeBorder(strip, 40, 40, 20, 20, cv2.BORDER_CONSTANT, value=[255, 255, 255]) res = ocr.ocr(padded) current_best_text = "" current_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 > current_best_sim: current_best_sim = s current_best_text = ct if current_best_sim > best_overall_sim: best_overall_sim = current_best_sim best_params = { "dx": dx, "dy": dy, "r_start": r_start, "r_end": r_end, "text": current_best_text, "sim": current_best_sim } print(f"NEW BEST: {best_overall_sim:.4f} | {best_params}") print(f"\nFINAL BEST: {best_overall_sim:.4f}") print(best_params) if __name__ == "__main__": optimize_unwarp()