Spaces:
Running
Running
Update ocr_engine.py
Browse files- ocr_engine.py +1 -26
ocr_engine.py
CHANGED
@@ -1,27 +1,3 @@
|
|
1 |
-
You've indicated that the previous changes didn't resolve the issue, and the application is still failing to detect the weight from the image. To help debug and improve the accuracy, I'll provide the full `ocr_engine.py` code again, but this time with a few more refinements and **added visual debugging steps**. These debugging steps will save intermediate images, which can be very helpful in understanding where the detection process might be going wrong.
|
2 |
-
|
3 |
-
**Important Steps to Use This Debugging Version:**
|
4 |
-
|
5 |
-
1. **Replace `ocr_engine.py`:** Completely replace the content of your `ocr_engine.py` file with the code provided below.
|
6 |
-
2. **Create a Debug Folder:** Before running your application, create a folder named `debug_images` in the same directory as your `app.py` and `ocr_engine.py` files. This is where the intermediate images will be saved.
|
7 |
-
3. **Run Your App:** Execute your `app.py` as usual.
|
8 |
-
4. **Upload Image and Check Debug Folder:** Upload the `Screenshot 2025-06-16 154525.png` image (or any other problematic image). After processing, check the `debug_images` folder. You should find several images showing:
|
9 |
-
* The original image.
|
10 |
-
* The grayscale version.
|
11 |
-
* The thresholded image used for ROI detection.
|
12 |
-
* The dilated image used for ROI detection.
|
13 |
-
* The detected ROI (cropped image).
|
14 |
-
* Thresholded image of the ROI used for digit detection.
|
15 |
-
* Individual digit images detected by EasyOCR.
|
16 |
-
* Sharpened and adaptively thresholded images used for general EasyOCR fallback.
|
17 |
-
|
18 |
-
By examining these images, we can pinpoint at which stage the OCR process is failing (e.g., if the ROI isn't detected correctly, if digits aren't isolated well, or if segments aren't properly recognized).
|
19 |
-
|
20 |
-
---
|
21 |
-
|
22 |
-
Here is the **full updated code for `ocr_engine.py`** with enhanced logic and visual debugging:
|
23 |
-
|
24 |
-
```python
|
25 |
import easyocr
|
26 |
import numpy as np
|
27 |
import cv2
|
@@ -487,5 +463,4 @@ def extract_weight_from_image(pil_img):
|
|
487 |
|
488 |
except Exception as e:
|
489 |
logging.error(f"Weight extraction failed unexpectedly: {str(e)}")
|
490 |
-
return "Not detected", 0.0
|
491 |
-
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import easyocr
|
2 |
import numpy as np
|
3 |
import cv2
|
|
|
463 |
|
464 |
except Exception as e:
|
465 |
logging.error(f"Weight extraction failed unexpectedly: {str(e)}")
|
466 |
+
return "Not detected", 0.0
|
|