wjbmattingly commited on
Commit
87e06fb
1 Parent(s): fdd3057

fixing the issue with bboxes being returned

Browse files
Files changed (1) hide show
  1. app/main.py +30 -24
app/main.py CHANGED
@@ -24,13 +24,36 @@ class RawResponse(BaseModel):
24
  result: Any
25
 
26
  def serialize_line(line):
27
- return {
28
- "id": line.id,
29
- "bbox": line.bbox,
30
- "baseline": line.baseline if hasattr(line, 'baseline') else None,
31
- "polygon": line.polygon if hasattr(line, 'polygon') else None,
32
- "tags": line.tags,
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
33
  }
 
 
 
 
 
 
 
34
 
35
  @app.post("/detect_lines", response_model=RawResponse)
36
  async def detect_lines(file: UploadFile = File(...)):
@@ -40,26 +63,9 @@ async def detect_lines(file: UploadFile = File(...)):
40
  # Perform baseline and layout analysis (BLLA) segmentation with default model
41
  baseline_seg = blla.segment(image)
42
 
43
- def serialize_line(line):
44
- # Create a dictionary with all available attributes
45
- line_dict = vars(line)
46
-
47
- # If 'bbox' is not available but 'polygon' is, calculate bbox from polygon
48
- if 'bbox' not in line_dict and 'polygon' in line_dict and line_dict['polygon'] is not None:
49
- x_coords, y_coords = zip(*line_dict['polygon'])
50
- bbox = [min(x_coords), min(y_coords), max(x_coords), max(y_coords)]
51
- line_dict['bbox'] = bbox
52
-
53
- # Convert numpy arrays to lists for JSON serialization
54
- for key, value in line_dict.items():
55
- if isinstance(value, np.ndarray):
56
- line_dict[key] = value.tolist()
57
-
58
- return line_dict
59
-
60
  serialized_seg = {
61
  "lines": [serialize_line(line) for line in baseline_seg.lines],
62
- "regions": [vars(region) for region in baseline_seg.regions],
63
  "type": baseline_seg.type,
64
  "text_direction": baseline_seg.text_direction,
65
  "script_detection": baseline_seg.script_detection,
 
24
  result: Any
25
 
26
  def serialize_line(line):
27
+ # Create a dictionary with all available attributes
28
+ line_dict = vars(line)
29
+
30
+ # If 'bbox' is not available but 'polygon' is, calculate bbox from polygon
31
+ if 'bbox' not in line_dict and 'polygon' in line_dict and line_dict['polygon'] is not None:
32
+ x_coords, y_coords = zip(*line_dict['polygon'])
33
+ bbox = [min(x_coords), min(y_coords), max(x_coords), max(y_coords)]
34
+ line_dict['bbox'] = bbox
35
+
36
+ # Convert numpy arrays to lists for JSON serialization
37
+ for key, value in line_dict.items():
38
+ if isinstance(value, np.ndarray):
39
+ line_dict[key] = value.tolist()
40
+
41
+ return line_dict
42
+
43
+ def serialize_region(region):
44
+ # Create a dictionary with known attributes
45
+ region_dict = {
46
+ "id": getattr(region, 'id', None),
47
+ "boundary": getattr(region, 'boundary', None),
48
+ "tags": getattr(region, 'tags', None),
49
  }
50
+
51
+ # Convert numpy arrays to lists for JSON serialization
52
+ for key, value in region_dict.items():
53
+ if isinstance(value, np.ndarray):
54
+ region_dict[key] = value.tolist()
55
+
56
+ return region_dict
57
 
58
  @app.post("/detect_lines", response_model=RawResponse)
59
  async def detect_lines(file: UploadFile = File(...)):
 
63
  # Perform baseline and layout analysis (BLLA) segmentation with default model
64
  baseline_seg = blla.segment(image)
65
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
66
  serialized_seg = {
67
  "lines": [serialize_line(line) for line in baseline_seg.lines],
68
+ "regions": [serialize_region(region) for region in baseline_seg.regions],
69
  "type": baseline_seg.type,
70
  "text_direction": baseline_seg.text_direction,
71
  "script_detection": baseline_seg.script_detection,