| import cv2 |
| import numpy as np |
| import os |
| from PIL import Image |
| from google import genai |
| from google.genai import types |
|
|
| |
| client = genai.Client(api_key="AIzaSyA0odvjKCTS-vZsEjMOlsVsJ4iGaYj5jf0") |
|
|
| def process_and_ocr(image_path): |
| print(f"\n--- Processing: {image_path} ---") |
| |
| |
| img = cv2.imread(image_path) |
| gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) |
| blur = cv2.GaussianBlur(gray, (7,7), 0) |
| thresh = cv2.adaptiveThreshold(blur, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY_INV, 11, 2) |
| |
| |
| kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (50, 5)) |
| dilate = cv2.dilate(thresh, kernel, iterations=1) |
| contours, _ = cv2.findContours(dilate, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) |
| |
| |
| contours = sorted(contours, key=lambda c: cv2.boundingRect(c)[1]) |
| |
| crops = [] |
| max_w = 0 |
| for c in contours: |
| x, y, w, h = cv2.boundingRect(c) |
| if w > 60 and h > 20: |
| crop = img[y:y+h, x:x+w] |
| crops.append(crop) |
| if w > max_w: max_w = w |
|
|
| |
| |
| final_crops = [] |
| for c in crops: |
| h, w, _ = c.shape |
| padded = cv2.copyMakeBorder(c, 5, 5, 0, max_w - w, cv2.BORDER_CONSTANT, value=[255, 255, 255]) |
| final_crops.append(padded) |
| |
| if not final_crops: |
| print("Error: No text blocks found!") |
| return |
|
|
| |
| stitched_strip = cv2.vconcat(final_crops) |
| strip_name = f"strip_{image_path}" |
| cv2.imwrite(strip_name, stitched_strip) |
| print(f"Stitched strip saved as {strip_name}") |
|
|
| |
| pil_img = Image.open(strip_name) |
| |
| prompt = """ |
| 讛转诪讜谞讛 讛诪爪讜专驻转 讛讬讗 "住讟专讬驻" 讛诪讜专讻讘 诪讞讬转讜讻讬诐 砖诇 砖讗诇转 讘讙专讜转 讘诪转诪讟讬拽讛. |
| 讛诪砖讬诪讛 砖诇讱: 讞诇抓 讗转 讻诇 讛讟拽住讟 讜讛诪砖讜讜讗讜转 讘讚讬讜拽 诪讜砖诇诐 砖诇 100%. |
| 讞讜拽讬诐: |
| 1. 拽专讗 讗转 讛注讘专讬转 讜讛诪转诪讟讬拽讛 讘专爪祝 诪诇诪注诇讛 诇诪讟讛. |
| 2. 砖讬诐 诇讘 诇驻专讟讬诐 讛拽讟谞讬诐 讘讬讜转专: 砖讘专讬诐, 讞讝拽讜转, e, ln, 讜住讬诪谞讬 诪讬谞讜住. |
| 3. 讛讞讝专 LaTeX 转拽谞讬 诇讻诇 讛诪转诪讟讬拽讛 讜注讘专讬转 谞拽讬讬讛. |
| 4. 讛讞讝专 专拽 讗转 讛讟拽住讟 讛诪讞讜诇抓. |
| """ |
|
|
| response = client.models.generate_content( |
| model='gemini-2.0-flash', |
| contents=[pil_img, prompt], |
| config=types.GenerateContentConfig(temperature=0.0, max_output_tokens=4096) |
| ) |
| |
| print("\n--- OCR RESULT ---") |
| print(response.text) |
| print("------------------") |
|
|
| |
| test_files = ["hardest_1.jpg", "ocr_only.jpeg", "simple.jpg"] |
| for file in test_files: |
| if os.path.exists(file): |
| process_and_ocr(file) |
| else: |
| print(f"File {file} not found, skipping...") |