Edvin Behdadijd
commited on
Update app.py
Browse files
app.py
CHANGED
|
@@ -72,7 +72,6 @@ def process_file(input_file):
|
|
| 72 |
img.save(os.path.join(input_folder, f'image_{i}.jpg'))
|
| 73 |
|
| 74 |
return images
|
| 75 |
-
|
| 76 |
def run_detection_and_ocr():
|
| 77 |
# Load models
|
| 78 |
ocr_model = Model.load('hezarai/crnn-fa-printed-96-long')
|
|
@@ -81,29 +80,33 @@ def run_detection_and_ocr():
|
|
| 81 |
input_folder = 'output_images'
|
| 82 |
yolo_model.predict(input_folder, save=True, imgsz=320, conf=0.5, save_crop=True)
|
| 83 |
|
| 84 |
-
output_folder = '
|
|
|
|
|
|
|
| 85 |
results = []
|
| 86 |
|
| 87 |
for filename in os.listdir(input_folder):
|
| 88 |
if filename.endswith('.JPEG') or filename.endswith('.jpg'):
|
| 89 |
image_path = os.path.join(input_folder, filename)
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
|
|
|
|
|
|
| 107 |
|
| 108 |
output_json_path = 'output.json'
|
| 109 |
with open(output_json_path, 'w', encoding='utf-8') as f:
|
|
|
|
| 72 |
img.save(os.path.join(input_folder, f'image_{i}.jpg'))
|
| 73 |
|
| 74 |
return images
|
|
|
|
| 75 |
def run_detection_and_ocr():
|
| 76 |
# Load models
|
| 77 |
ocr_model = Model.load('hezarai/crnn-fa-printed-96-long')
|
|
|
|
| 80 |
input_folder = 'output_images'
|
| 81 |
yolo_model.predict(input_folder, save=True, imgsz=320, conf=0.5, save_crop=True)
|
| 82 |
|
| 83 |
+
output_folder = 'runs/detect/predict' # Remove leading slash if needed
|
| 84 |
+
crop_folder = os.path.join(output_folder, 'crops')
|
| 85 |
+
|
| 86 |
results = []
|
| 87 |
|
| 88 |
for filename in os.listdir(input_folder):
|
| 89 |
if filename.endswith('.JPEG') or filename.endswith('.jpg'):
|
| 90 |
image_path = os.path.join(input_folder, filename)
|
| 91 |
+
|
| 92 |
+
# Check if crop_folder exists
|
| 93 |
+
if os.path.exists(crop_folder):
|
| 94 |
+
crops = []
|
| 95 |
+
for crop_label in os.listdir(crop_folder):
|
| 96 |
+
crop_label_folder = os.path.join(crop_folder, crop_label)
|
| 97 |
+
if os.path.isdir(crop_label_folder):
|
| 98 |
+
for crop_filename in os.listdir(crop_label_folder):
|
| 99 |
+
crop_image_path = os.path.join(crop_label_folder, crop_filename)
|
| 100 |
+
text_prediction = predict_text(ocr_model, crop_image_path)
|
| 101 |
+
crops.append({
|
| 102 |
+
'crop_image_path': crop_image_path,
|
| 103 |
+
'text_prediction': text_prediction,
|
| 104 |
+
'class_label': crop_label
|
| 105 |
+
})
|
| 106 |
+
results.append({
|
| 107 |
+
'image': filename,
|
| 108 |
+
'crops': crops
|
| 109 |
+
})
|
| 110 |
|
| 111 |
output_json_path = 'output.json'
|
| 112 |
with open(output_json_path, 'w', encoding='utf-8') as f:
|