Download app.py from Nassira/comp: direct link, hf CLI and curl.
- Browser
- Download file 3.22 kB
-
https://huggingface.co/spaces/Nassira/comp/resolve/main/app.py
- Command line
-
hf download hf://spaces/Nassira/comp/app.py
-
curl -L -o app.py https://huggingface.co/spaces/Nassira/comp/resolve/main/app.py
3.22 kB
| import os | |
| import time | |
| import easyocr | |
| import json | |
| reader = easyocr.Reader(['en', 'fr']) | |
| def extract_text_from_image(image_path): | |
| result = reader.readtext(image_path) | |
| return ' '.join([item[1] for item in result]) | |
| def read_text_file(file_path): | |
| with open(file_path, 'r', encoding='utf-8') as file: | |
| return file.read() | |
| def calculate_metrics(extracted_text, reference_text): | |
| extracted_words = set(extracted_text.lower().split()) | |
| reference_words = set(reference_text.lower().split()) | |
| true_positives = len(extracted_words.intersection(reference_words)) | |
| false_positives = len(extracted_words - reference_words) | |
| false_negatives = len(reference_words - extracted_words) | |
| precision = true_positives / (true_positives + false_positives) if (true_positives + false_positives) > 0 else 0 | |
| recall = true_positives / (true_positives + false_negatives) if (true_positives + false_negatives) > 0 else 0 | |
| f1_score = 2 * (precision * recall) / (precision + recall) if (precision + recall) > 0 else 0 | |
| return precision, recall, f1_score | |
| image_dir = 'Images CV' | |
| text_dir = 'ground_truth' | |
| results = [] | |
| batch_size = 10 | |
| processed_count = 0 | |
| for image_filename in os.listdir(image_dir): | |
| if image_filename.endswith('.png'): | |
| image_path = os.path.join(image_dir, image_filename) | |
| text_file_path = os.path.join(text_dir, image_filename.replace('.png', '.txt')) | |
| if os.path.exists(text_file_path): | |
| print(f"Processing {image_filename}...") | |
| start_time = time.time() | |
| extracted_text = extract_text_from_image(image_path) | |
| pre_existing_text = read_text_file(text_file_path) | |
| precision, recall, f1_score = calculate_metrics(extracted_text, pre_existing_text) | |
| execution_time = time.time() - start_time | |
| results.append({ | |
| 'filename': image_filename, | |
| 'precision': precision, | |
| 'recall': recall, | |
| 'f1_score': f1_score, | |
| 'execution_time': execution_time | |
| }) | |
| processed_count += 1 | |
| if processed_count % batch_size == 0: | |
| print(f"Processed {processed_count} files. Saving interim results...") | |
| with open('interim_results.json', 'w') as f: | |
| json.dump(results, f) | |
| else: | |
| print(f"Text file not found for image: {image_filename}") | |
| # Save final results | |
| with open('final_results.json', 'w') as f: | |
| json.dump(results, f) | |
| # Calculate and output averages | |
| if results: | |
| avg_precision = sum(r['precision'] for r in results) / len(results) | |
| avg_recall = sum(r['recall'] for r in results) / len(results) | |
| avg_f1 = sum(r['f1_score'] for r in results) / len(results) | |
| avg_time = sum(r['execution_time'] for r in results) / len(results) | |
| print(f"\nAverage Precision: {avg_precision:.2f}") | |
| print(f"Average Recall: {avg_recall:.2f}") | |
| print(f"Average F1 Score: {avg_f1:.2f}") | |
| print(f"Average Execution Time: {avg_time:.2f} seconds per file") | |
| else: | |
| print("No matching text files found for any images.") |