Nassira commited on
Commit
e5fbd65
·
verified ·
1 Parent(s): 73055c5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -28
app.py CHANGED
@@ -138,27 +138,32 @@ doctr_model, easyocr_reader, paddleocr_reader = load_models(language)
138
  # Streamlit application
139
  st.title("OCR Text Extraction and Evaluation")
140
 
141
- # Upload image files
142
- uploaded_files = st.file_uploader("Choose image files", type=["png", "jpg", "jpeg"], accept_multiple_files=True)
143
-
144
- # Upload ground truth text files
145
- uploaded_truth_files = st.file_uploader("Choose ground truth text files", type=["txt"], accept_multiple_files=True)
146
-
147
- if uploaded_files and uploaded_truth_files:
148
- total_precision = 0.0
149
- total_recall = 0.0
150
- total_f1 = 0.0
151
- num_files = 0
152
-
153
- for image_file, truth_file in zip(uploaded_files, uploaded_truth_files):
 
 
 
 
154
  start_time = time.time()
155
 
156
  # Read the image
157
- image = Image.open(image_file)
 
158
 
159
  # Extract text from the image using different OCR methods
160
  results = {
161
- 'doctr': extract_text_doctr(image_file, doctr_model),
162
  'easyocr': extract_text_easyocr(image, easyocr_reader),
163
  'paddleocr': extract_text_paddleocr(image, paddleocr_reader)
164
  }
@@ -166,8 +171,9 @@ if uploaded_files and uploaded_truth_files:
166
  # Combine results from different OCR methods
167
  combined_text = combine_ocr_results(results)
168
 
169
- # Read text from the pre-existing text file
170
- pre_existing_text = truth_file.getvalue().decode('utf-8')
 
171
 
172
  # Calculate precision
173
  precision = calculate_precision(combined_text, pre_existing_text)
@@ -175,29 +181,28 @@ if uploaded_files and uploaded_truth_files:
175
  recall = calculate_recall(combined_text, pre_existing_text)
176
  # Calculate F1 score
177
  f1_score = calculate_f1(precision, recall)
178
-
179
- # Add to total precision, recall, F1 score and increment file count
180
  total_precision += precision
181
  total_recall += recall
182
  total_f1 += f1_score
183
  num_files += 1
184
 
185
  # Display results for each file
186
- st.subheader(f"Results for {image_file.name}")
187
- st.write(f"Extracted Text: {combined_text}")
188
- st.write(f"Ground Truth Text: {pre_existing_text}")
189
  st.write(f"Precision: {precision:.2f}")
190
  st.write(f"Recall: {recall:.2f}")
191
  st.write(f"F1 Score: {f1_score:.2f}")
192
-
193
- # Calculate and output the average precision, recall, F1 score
194
  if num_files > 0:
195
  average_precision = total_precision / num_files
196
  average_recall = total_recall / num_files
197
  average_f1 = total_f1 / num_files
198
- st.subheader("Average Results")
199
- st.write(f"Average Precision: {average_precision:.2f}")
200
  st.write(f"Average Recall: {average_recall:.2f}")
201
  st.write(f"Average F1 Score: {average_f1:.2f}")
202
- else:
203
- st.write("No matching text files found for any images.")
 
 
138
  # Streamlit application
139
  st.title("OCR Text Extraction and Evaluation")
140
 
141
+ # Directories containing the images and text files
142
+ image_dir = 'Images CV'
143
+ text_dir = 'ground_truth'
144
+
145
+ # List files in the directories
146
+ image_files = [f for f in os.listdir(image_dir) if f.endswith(('.png', '.jpg', '.jpeg'))]
147
+ text_files = [f for f in os.listdir(text_dir) if f.endswith('.txt')]
148
+
149
+ # Initialize variables for averages
150
+ total_precision = 0.0
151
+ total_recall = 0.0
152
+ total_f1 = 0.0
153
+ num_files = 0
154
+
155
+ # Ensure uploaded files are matched
156
+ if len(image_files) == len(text_files):
157
+ for image_filename, text_filename in zip(image_files, text_files):
158
  start_time = time.time()
159
 
160
  # Read the image
161
+ image_path = os.path.join(image_dir, image_filename)
162
+ image = Image.open(image_path)
163
 
164
  # Extract text from the image using different OCR methods
165
  results = {
166
+ 'doctr': extract_text_doctr(image_path, doctr_model),
167
  'easyocr': extract_text_easyocr(image, easyocr_reader),
168
  'paddleocr': extract_text_paddleocr(image, paddleocr_reader)
169
  }
 
171
  # Combine results from different OCR methods
172
  combined_text = combine_ocr_results(results)
173
 
174
+ # Read text from the corresponding text file
175
+ text_file_path = os.path.join(text_dir, text_filename)
176
+ pre_existing_text = read_text_file(text_file_path)
177
 
178
  # Calculate precision
179
  precision = calculate_precision(combined_text, pre_existing_text)
 
181
  recall = calculate_recall(combined_text, pre_existing_text)
182
  # Calculate F1 score
183
  f1_score = calculate_f1(precision, recall)
184
+
185
+ # Update totals and count
186
  total_precision += precision
187
  total_recall += recall
188
  total_f1 += f1_score
189
  num_files += 1
190
 
191
  # Display results for each file
192
+ st.subheader(f"Results for {image_filename}")
 
 
193
  st.write(f"Precision: {precision:.2f}")
194
  st.write(f"Recall: {recall:.2f}")
195
  st.write(f"F1 Score: {f1_score:.2f}")
196
+
197
+ # Calculate and display average precision, recall, and F1 score
198
  if num_files > 0:
199
  average_precision = total_precision / num_files
200
  average_recall = total_recall / num_files
201
  average_f1 = total_f1 / num_files
202
+
203
+ st.write(f"\nAverage Precision: {average_precision:.2f}")
204
  st.write(f"Average Recall: {average_recall:.2f}")
205
  st.write(f"Average F1 Score: {average_f1:.2f}")
206
+
207
+ else:
208
+ st.write("The number of images and text files do not match.")