Spaces:
Sleeping
Sleeping
Fixing bbox
Browse files
app.py
CHANGED
|
@@ -26,28 +26,36 @@ def extract_text_from_image(image):
|
|
| 26 |
for line in result[0]:
|
| 27 |
if not line or len(line) < 2:
|
| 28 |
continue
|
| 29 |
-
|
| 30 |
-
bbox = line[0]
|
| 31 |
-
text_info = line[1]
|
| 32 |
-
|
| 33 |
-
# Handle different formats
|
| 34 |
-
if isinstance(text_info, tuple) or isinstance(text_info, list):
|
| 35 |
-
text = text_info[0]
|
| 36 |
-
confidence = text_info[1] if len(text_info) > 1 else 0.0
|
| 37 |
-
else:
|
| 38 |
-
text = str(text_info)
|
| 39 |
-
confidence = 0.0
|
| 40 |
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
|
| 52 |
return text_blocks
|
| 53 |
|
|
|
|
| 26 |
for line in result[0]:
|
| 27 |
if not line or len(line) < 2:
|
| 28 |
continue
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
+
try:
|
| 31 |
+
bbox = line[0]
|
| 32 |
+
text_info = line[1]
|
| 33 |
+
|
| 34 |
+
# Handle different formats
|
| 35 |
+
if isinstance(text_info, (tuple, list)):
|
| 36 |
+
text = text_info[0]
|
| 37 |
+
confidence = text_info[1] if len(text_info) > 1 else 0.0
|
| 38 |
+
else:
|
| 39 |
+
text = str(text_info)
|
| 40 |
+
confidence = 0.0
|
| 41 |
+
|
| 42 |
+
# bbox should be a list of 4 points [[x1,y1], [x2,y2], [x3,y3], [x4,y4]]
|
| 43 |
+
if not isinstance(bbox, (list, tuple)) or len(bbox) < 4:
|
| 44 |
+
continue
|
| 45 |
+
|
| 46 |
+
# Calculate center point for positioning
|
| 47 |
+
y_center = (bbox[0][1] + bbox[2][1]) / 2
|
| 48 |
+
x_center = (bbox[0][0] + bbox[2][0]) / 2
|
| 49 |
+
|
| 50 |
+
text_blocks.append({
|
| 51 |
+
'text': text,
|
| 52 |
+
'y': y_center,
|
| 53 |
+
'x': x_center,
|
| 54 |
+
'confidence': confidence
|
| 55 |
+
})
|
| 56 |
+
except (IndexError, TypeError, KeyError) as e:
|
| 57 |
+
# Skip problematic entries
|
| 58 |
+
continue
|
| 59 |
|
| 60 |
return text_blocks
|
| 61 |
|