import cv2 import os import json import difflib import numpy as np from paddleocr import PaddleOCR def similarity(s1, s2): return difflib.SequenceMatcher(None, s1, s2).ratio() def test_rotation_ocr(): target = "威凯检测技术有限公司" img_path = "seal_cropped.png" if not os.path.exists(img_path): print(f"Error: {img_path} not found") return img = cv2.imread(img_path) h, w = img.shape[:2] cx, cy = w // 2, h // 2 ocr = PaddleOCR(use_angle_cls=True, lang='ch') results = [] # Test angles from -90 to 90 with step 15 for angle in range(-120, 121, 15): M = cv2.getRotationMatrix2D((cx, cy), angle, 1.0) rotated = cv2.warpAffine(img, M, (w, h), borderValue=(255, 255, 255)) # We can also try different scales or enhancements res = ocr.ocr(rotated) if res: # Handle list of dicts (PaddleX format) for page in res: if 'rec_texts' in page: for text in page['rec_texts']: clean_text = text.replace(" ", "") sim = similarity(target, clean_text) if sim > 0.1: print(f"Angle: {angle} | Sim: {sim:.4f} | Text: {clean_text}") results.append({ "angle": angle, "text": clean_text, "sim": sim }) # Handle standard PaddleOCR format just in case elif isinstance(page, list): for line in page: if isinstance(line, list) and len(line) > 1: clean_text = line[1][0].replace(" ", "") sim = similarity(target, clean_text) if sim > 0.1: print(f"Angle: {angle} | Sim: {sim:.4f} | Text: {clean_text}") results.append({ "angle": angle, "text": clean_text, "sim": sim }) results.sort(key=lambda x: x['sim'], reverse=True) with open("rotation_ocr_results.json", "w", encoding="utf-8") as f: json.dump(results, f, ensure_ascii=False, indent=2) if __name__ == "__main__": test_rotation_ocr()