|
|
|
|
|
""" |
|
|
Example usage of the Khmer OCR Recognition Model |
|
|
Demonstrates how to use the model for Khmer text recognition |
|
|
""" |
|
|
|
|
|
from paddleocr import PaddleOCR |
|
|
import cv2 |
|
|
import os |
|
|
import json |
|
|
|
|
|
def khmer_ocr_example(image_path, model_dir="."): |
|
|
""" |
|
|
Example function showing how to use the Khmer OCR model |
|
|
|
|
|
Args: |
|
|
image_path (str): Path to the image containing Khmer text |
|
|
model_dir (str): Directory containing the model files |
|
|
|
|
|
Returns: |
|
|
list: OCR results with text, confidence, and bounding boxes |
|
|
""" |
|
|
|
|
|
print(f"π Processing: {image_path}") |
|
|
print("=" * 50) |
|
|
|
|
|
|
|
|
try: |
|
|
ocr = PaddleOCR( |
|
|
use_angle_cls=True, |
|
|
lang='ch', |
|
|
rec_model_dir=model_dir, |
|
|
rec_char_dict_path=os.path.join(model_dir, 'khmer_char_dict.txt'), |
|
|
show_log=False |
|
|
) |
|
|
print("β
Model loaded successfully") |
|
|
except Exception as e: |
|
|
print(f"β Error loading model: {e}") |
|
|
return None |
|
|
|
|
|
|
|
|
if not os.path.exists(image_path): |
|
|
print(f"β Image file not found: {image_path}") |
|
|
return None |
|
|
|
|
|
|
|
|
try: |
|
|
result = ocr.ocr(image_path, cls=True) |
|
|
print("β
OCR processing completed") |
|
|
except Exception as e: |
|
|
print(f"β Error processing image: {e}") |
|
|
return None |
|
|
|
|
|
|
|
|
if result[0] is None: |
|
|
print("β οΈ No text detected in the image.") |
|
|
return [] |
|
|
|
|
|
all_results = [] |
|
|
total_confidence = 0 |
|
|
|
|
|
print(f"\nπ Detected Text Regions: {len(result[0])}") |
|
|
print("-" * 50) |
|
|
|
|
|
for idx, line in enumerate(result[0]): |
|
|
box = line[0] |
|
|
text = line[1][0] |
|
|
confidence = line[1][1] |
|
|
|
|
|
|
|
|
result_item = { |
|
|
'region_id': idx + 1, |
|
|
'text': text, |
|
|
'confidence': confidence, |
|
|
'bounding_box': box |
|
|
} |
|
|
all_results.append(result_item) |
|
|
total_confidence += confidence |
|
|
|
|
|
|
|
|
print(f"Region {idx + 1}:") |
|
|
print(f" π Text: {text}") |
|
|
print(f" π― Confidence: {confidence:.3f}") |
|
|
print(f" π Box: [{box[0][0]:.0f},{box[0][1]:.0f}] β [{box[2][0]:.0f},{box[2][1]:.0f}]") |
|
|
print() |
|
|
|
|
|
|
|
|
avg_confidence = total_confidence / len(result[0]) if result[0] else 0 |
|
|
print("π Summary:") |
|
|
print(f" Total regions: {len(result[0])}") |
|
|
print(f" Average confidence: {avg_confidence:.3f}") |
|
|
|
|
|
|
|
|
full_text = " ".join([item['text'] for item in all_results]) |
|
|
print(f" π Full text: {full_text}") |
|
|
|
|
|
return all_results |
|
|
|
|
|
def batch_process_images(image_dir, model_dir=".", output_file="ocr_results.json"): |
|
|
""" |
|
|
Process multiple images in a directory |
|
|
|
|
|
Args: |
|
|
image_dir (str): Directory containing images |
|
|
model_dir (str): Directory containing model files |
|
|
output_file (str): Output JSON file for results |
|
|
""" |
|
|
|
|
|
print(f"π Batch processing images from: {image_dir}") |
|
|
|
|
|
|
|
|
image_extensions = ['.jpg', '.jpeg', '.png', '.bmp', '.tiff'] |
|
|
image_files = [] |
|
|
|
|
|
if os.path.isdir(image_dir): |
|
|
for file in os.listdir(image_dir): |
|
|
if any(file.lower().endswith(ext) for ext in image_extensions): |
|
|
image_files.append(os.path.join(image_dir, file)) |
|
|
|
|
|
if not image_files: |
|
|
print(f"β No image files found in {image_dir}") |
|
|
return |
|
|
|
|
|
print(f"π Found {len(image_files)} images") |
|
|
|
|
|
all_results = {} |
|
|
|
|
|
for image_path in image_files: |
|
|
print(f"\nπΌοΈ Processing: {os.path.basename(image_path)}") |
|
|
results = khmer_ocr_example(image_path, model_dir) |
|
|
if results: |
|
|
all_results[image_path] = results |
|
|
|
|
|
|
|
|
try: |
|
|
with open(output_file, 'w', encoding='utf-8') as f: |
|
|
json.dump(all_results, f, ensure_ascii=False, indent=2) |
|
|
print(f"\nπΎ Results saved to: {output_file}") |
|
|
except Exception as e: |
|
|
print(f"β Error saving results: {e}") |
|
|
|
|
|
def main(): |
|
|
"""Main function with example usage""" |
|
|
|
|
|
print("π°π Khmer OCR Recognition Model - Example Usage") |
|
|
print("=" * 60) |
|
|
|
|
|
|
|
|
print("\nπ Example 1: Single Image Processing") |
|
|
print("-" * 40) |
|
|
|
|
|
|
|
|
example_image = "sample_khmer_image.jpg" |
|
|
|
|
|
if os.path.exists(example_image): |
|
|
results = khmer_ocr_example(example_image) |
|
|
if results: |
|
|
print("β
Single image processing completed successfully!") |
|
|
else: |
|
|
print(f"βΉοΈ Example image '{example_image}' not found.") |
|
|
print(" Please provide your own Khmer text image.") |
|
|
|
|
|
|
|
|
print("\nπ Example 2: Batch Processing") |
|
|
print("-" * 40) |
|
|
|
|
|
sample_dir = "sample_images" |
|
|
if os.path.exists(sample_dir): |
|
|
batch_process_images(sample_dir) |
|
|
else: |
|
|
print(f"βΉοΈ Sample directory '{sample_dir}' not found.") |
|
|
print(" Create a directory with Khmer images to test batch processing.") |
|
|
|
|
|
|
|
|
print("\nπ Example 3: Model Information") |
|
|
print("-" * 40) |
|
|
|
|
|
model_files = [ |
|
|
'inference.pdiparams', |
|
|
'inference.yml', |
|
|
'inference.json', |
|
|
'khmer_char_dict.txt' |
|
|
] |
|
|
|
|
|
print("π Required model files:") |
|
|
for file in model_files: |
|
|
if os.path.exists(file): |
|
|
size = os.path.getsize(file) / (1024*1024) |
|
|
print(f" β
{file} ({size:.1f}MB)") |
|
|
else: |
|
|
print(f" β {file} - Missing!") |
|
|
|
|
|
|
|
|
char_dict_path = 'khmer_char_dict.txt' |
|
|
if os.path.exists(char_dict_path): |
|
|
try: |
|
|
with open(char_dict_path, 'r', encoding='utf-8') as f: |
|
|
chars = f.read().strip().split('\n') |
|
|
print(f"\nπ Character Dictionary: {len(chars)} characters supported") |
|
|
print(f" Sample characters: {' '.join(chars[:20])}...") |
|
|
except Exception as e: |
|
|
print(f"β Error reading character dictionary: {e}") |
|
|
|
|
|
print("\nπ― Usage Tips:") |
|
|
print(" β’ Best for 3-5 word text segments") |
|
|
print(" β’ Use high-contrast, clear images") |
|
|
print(" β’ Combine with text detection for full documents") |
|
|
print(" β’ Model supports 188 Khmer and Latin characters") |
|
|
|
|
|
print("\n⨠Happy OCR-ing with Khmer text!") |
|
|
|
|
|
if __name__ == "__main__": |
|
|
main() |
|
|
|