|
|
| import pytesseract
|
| from PIL import Image
|
| import os
|
| import cv2
|
|
|
|
|
| pytesseract.pytesseract.tesseract_cmd = r"C:\Program Files\Tesseract-OCR\tesseract.exe"
|
|
|
|
|
| input_folder = "input_images"
|
| output_folder = "ocr_output"
|
|
|
|
|
| os.makedirs(output_folder, exist_ok=True)
|
|
|
|
|
| lang = 'ben'
|
|
|
|
|
| for filename in os.listdir(input_folder):
|
| if filename.lower().endswith(('.png', '.jpg', '.jpeg', '.tif')):
|
|
|
| img_path = os.path.join(input_folder, filename)
|
| img = cv2.imread(img_path)
|
|
|
|
|
| gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
|
| _, thresh = cv2.threshold(gray, 150, 255, cv2.THRESH_BINARY)
|
|
|
|
|
| text = pytesseract.image_to_string(thresh, lang=lang, config='--psm 6')
|
|
|
|
|
| output_file = os.path.splitext(filename)[0] + ".txt"
|
| output_path = os.path.join(output_folder, output_file)
|
|
|
|
|
| with open(output_path, "w", encoding="utf-8") as f:
|
| f.write(text)
|
|
|
| print(f"Processed: {filename} → {output_file}")
|
|
|
| print("Batch OCR completed.") |