Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -36,29 +36,31 @@ def predict(image):
|
|
| 36 |
# Unpack the normalized bounding box (x_min, y_min, x_max, y_max)
|
| 37 |
x_min, y_min, x_max, y_max = box
|
| 38 |
|
| 39 |
-
#
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
#
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
|
|
|
|
|
|
| 62 |
|
| 63 |
# Return OCR results
|
| 64 |
return {"ocr_results": ocr_results}
|
|
|
|
| 36 |
# Unpack the normalized bounding box (x_min, y_min, x_max, y_max)
|
| 37 |
x_min, y_min, x_max, y_max = box
|
| 38 |
|
| 39 |
+
# Ensure the coordinates are valid (x_max > x_min, y_max > y_min)
|
| 40 |
+
if x_min >= x_max or y_min >= y_max:
|
| 41 |
+
continue
|
| 42 |
+
|
| 43 |
+
# Convert normalized coordinates to pixel values
|
| 44 |
+
left = max(int(x_min * image_width), 0)
|
| 45 |
+
top = max(int(y_min * image_height), 0)
|
| 46 |
+
right = min(int(x_max * image_width), image_width)
|
| 47 |
+
bottom = min(int(y_max * image_height), image_height)
|
| 48 |
+
|
| 49 |
+
# Double-check that the coordinates are valid after conversion
|
| 50 |
+
if right <= left or bottom <= top:
|
| 51 |
+
continue
|
| 52 |
+
|
| 53 |
+
# Crop the image to the bounding box area
|
| 54 |
+
cropped_image = image.crop((left, top, right, bottom))
|
| 55 |
+
|
| 56 |
+
# Perform OCR on the cropped image
|
| 57 |
+
ocr_text = pytesseract.image_to_string(cropped_image)
|
| 58 |
+
|
| 59 |
+
# Append OCR result for this box
|
| 60 |
+
ocr_results.append({
|
| 61 |
+
"box": [left, top, right, bottom],
|
| 62 |
+
"text": ocr_text
|
| 63 |
+
})
|
| 64 |
|
| 65 |
# Return OCR results
|
| 66 |
return {"ocr_results": ocr_results}
|