Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Surya Recognizer #4

Open
titipata opened this issue Oct 31, 2024 · 1 comment
Open

Surya Recognizer #4

titipata opened this issue Oct 31, 2024 · 1 comment
Labels
documentation Improvements or additions to documentation

Comments

@titipata
Copy link
Contributor

titipata commented Oct 31, 2024

ตัวอย่างการใช้ผลจาก YOLO dectector ดึงส่วนของภาพออกมาและใช้ surya-ocr ในการอ่านข้อความ

from pathlib import Path
from ultralytics import YOLO

checkpoint = Path("best_detection_yolo.pt").resolve()
# Load a model from a checkpoint
model = YOLO(checkpoint)
results = model.predict(images, imgsz=640, conf=0.25, half=True)
from surya.recognition import batch_recognition
from surya.model.recognition.model import load_model as load_recognizer
from surya.model.recognition.processor import load_processor as load_recognizer_processor

recognizer = load_recognizer()
recognizer_processor = load_recognizer_processor()

# Process each YOLO result
padding = 0
images_for_ocr = []
languages_for_ocr = []
for idx, result in enumerate(results):
    orig_img = result.orig_img
    if result.boxes is not None:
        boxes = result.boxes.xyxy.cpu().numpy()
        confidences = result.boxes.conf.cpu().numpy()
        for box, conf in zip(boxes, confidences):
            # Extract coordinates with padding
            x1, y1, x2, y2 = map(int, box)
            x1 = max(0, x1 - padding)
            y1 = max(0, y1 - padding)
            x2 = min(orig_img.shape[1], x2 + padding)
            y2 = min(orig_img.shape[0], y2 + padding)
            region = orig_img[y1:y2, x1:x2]
            pil_region = Image.fromarray(region)
            images_for_ocr.append(pil_region)
languages_for_ocr = [["th", "en"]] * len(images_for_ocr)

text_results, confidence_scores = batch_recognition(
    images=images_for_ocr,
    languages=languages_for_ocr,
    model=recognizer,
    processor=recognizer_processor
)
@titipata titipata added the documentation Improvements or additions to documentation label Oct 31, 2024
@titipata
Copy link
Contributor Author

titipata commented Nov 2, 2024

Matching (alternative)

from fuzzywuzzy import process
process.extractOne(
    "ดีเสล", ["ดีเซล", "เบนซิน"], score_cutoff = 60
)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation
Projects
None yet
Development

No branches or pull requests

1 participant