Gabolozano commited on
Commit
b6fa5d6
1 Parent(s): 648d371

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -16
app.py CHANGED
@@ -21,7 +21,6 @@ def draw_detections(image, detections):
21
  np_image = cv2.cvtColor(np_image, cv2.COLOR_RGB2BGR)
22
 
23
  for detection in detections:
24
- # Extract scores, labels, and bounding boxes correctly
25
  score = detection['score']
26
  label = detection['label']
27
  box = detection['box']
@@ -30,40 +29,38 @@ def draw_detections(image, detections):
30
  x_max = box['xmax']
31
  y_max = box['ymax']
32
 
33
- # Draw rectangles and text on the image
34
  cv2.rectangle(np_image, (x_min, y_min), (x_max, y_max), (0, 255, 0), 2)
35
- cv2.putText(np_image, f'{label} {score:.2f}', (x_min, max(y_min - 10, 0)),
36
- cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 2)
 
 
 
37
 
38
  # Convert BGR to RGB for displaying
39
  final_image = cv2.cvtColor(np_image, cv2.COLOR_BGR2RGB)
40
- # Convert the numpy array to PIL Image
41
  final_pil_image = Image.fromarray(final_image)
42
  return final_pil_image
43
 
44
  def get_pipeline_prediction(pil_image):
45
  try:
46
- # Run the object detection pipeline
47
  pipeline_output = od_pipe(pil_image)
48
-
49
- # Draw the detection results on the image
50
  processed_image = draw_detections(pil_image, pipeline_output)
51
-
52
- # Provide both the image and the JSON detection results
53
  return processed_image, pipeline_output
54
  except Exception as e:
55
- # Log the error
56
  print(f"An error occurred: {str(e)}")
57
- # Return a message and an empty JSON
58
  return pil_image, {"error": str(e)}
59
 
 
60
  demo = gr.Interface(
61
  fn=get_pipeline_prediction,
62
- inputs=gr.Image(label="Input image", type="pil"),
63
  outputs=[
64
- gr.Image(label="Annotated Image"),
65
- gr.JSON(label="Detected Objects")
66
- ]
 
 
67
  )
68
 
69
  demo.launch()
 
21
  np_image = cv2.cvtColor(np_image, cv2.COLOR_RGB2BGR)
22
 
23
  for detection in detections:
 
24
  score = detection['score']
25
  label = detection['label']
26
  box = detection['box']
 
29
  x_max = box['xmax']
30
  y_max = box['ymax']
31
 
32
+ # Draw rectangles and label with a larger font size
33
  cv2.rectangle(np_image, (x_min, y_min), (x_max, y_max), (0, 255, 0), 2)
34
+ label_text = f'{label} {score:.2f}'
35
+ label_size = cv2.getTextSize(label_text, cv2.FONT_HERSHEY_SIMPLEX, 0.8, 2)[0]
36
+ label_x = x_min
37
+ label_y = y_min - label_size[1] if y_min - label_size[1] > 10 else y_min + label_size[1]
38
+ cv2.putText(np_image, label_text, (label_x, label_y), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (255, 255, 255), 2)
39
 
40
  # Convert BGR to RGB for displaying
41
  final_image = cv2.cvtColor(np_image, cv2.COLOR_BGR2RGB)
 
42
  final_pil_image = Image.fromarray(final_image)
43
  return final_pil_image
44
 
45
  def get_pipeline_prediction(pil_image):
46
  try:
 
47
  pipeline_output = od_pipe(pil_image)
 
 
48
  processed_image = draw_detections(pil_image, pipeline_output)
 
 
49
  return processed_image, pipeline_output
50
  except Exception as e:
 
51
  print(f"An error occurred: {str(e)}")
 
52
  return pil_image, {"error": str(e)}
53
 
54
+ # Setting up Gradio interface with tabs for the outputs
55
  demo = gr.Interface(
56
  fn=get_pipeline_prediction,
57
+ inputs=gr.inputs.Image(label="Input image", type="pil"),
58
  outputs=[
59
+ gr.outputs.Image(type="pil", label="Annotated Image"),
60
+ gr.outputs.JSON(label="Detected Objects")
61
+ ],
62
+ outputs_per_tab =[['image'], ['json']],
63
+ tabs=["Annotated Image", "Detection Results"]
64
  )
65
 
66
  demo.launch()