Gabolozano commited on
Commit
ceb95cf
1 Parent(s): 246f207

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -18
app.py CHANGED
@@ -1,34 +1,57 @@
 
1
  import os
2
  import gradio as gr
3
  from transformers import pipeline, DetrForObjectDetection, DetrConfig, DetrImageProcessor
 
 
 
4
 
5
- # Initialize the configuration for DetrForObjectDetection
6
- config = DetrConfig.from_pretrained("facebook/detr-resnet-50")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7
 
8
- # Create the model for object detection using the specified configuration
 
9
  model = DetrForObjectDetection.from_pretrained("facebook/detr-resnet-50", config=config)
10
-
11
- # Initialize the image processor for DETR
12
  image_processor = DetrImageProcessor.from_pretrained("facebook/detr-resnet-50")
13
-
14
- # Initialize the object detection pipeline with the model and image processor
15
  od_pipe = pipeline(task='object-detection', model=model, image_processor=image_processor)
16
 
17
  def get_pipeline_prediction(pil_image):
18
- # Run the object detection pipeline on the input image
19
  pipeline_output = od_pipe(pil_image)
20
 
21
- # You might need to implement or adjust the rendering function based on the `pipeline_output`
22
- # The `render_results_in_image` function is assumed here to draw bounding boxes and labels on the input image,
23
- # but you'll need to define it according to your specific needs.
24
- # For now, the output is directly returned since the question doesn't define `render_results_in_image`.
25
- return pipeline_output
26
 
27
  demo = gr.Interface(
28
- fn=get_pipeline_prediction,
29
- inputs=gr.Image(label="Input image",
30
- type="pil"),
31
- outputs=gr.JSON(label="Detected objects") # Adjusted to show JSON output if rendering function is not defined
 
 
32
  )
33
 
34
- demo.launch()
 
1
+ python
2
  import os
3
  import gradio as gr
4
  from transformers import pipeline, DetrForObjectDetection, DetrConfig, DetrImageProcessor
5
+ import numpy as np
6
+ import cv2
7
+ from PIL import Image
8
 
9
+ def draw_detections(image, detections):
10
+ # Convert PIL image to a numpy array
11
+ np_image = np.array(image)
12
+
13
+ # Convert RGB to BGR for OpenCV
14
+ np_image = cv2.cvtColor(np_image, cv2.COLOR_RGB2BGR)
15
+
16
+ for detection in detections:
17
+ # Each detection includes ['score', 'label', 'box']
18
+ score = detection['score']
19
+ label = detection['label']
20
+ box = detection['box']
21
+ x_min, y_min, x_max, y_max = map(int, box)
22
+ cv2.rectangle(np_image, (x_min, y_min), (x_max, y_max), (0, 255, 0), 2)
23
+ cv2.putText(np_image, f'{label} {score:.2f}', (x_min, max(y_min - 10, 0)),
24
+ cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 1)
25
+
26
+ # Convert BGR to RGB for displaying
27
+ final_image = cv2.cvtColor(np_image, cv2.COLOR_BGR2RGB)
28
+ # Convert the numpy array to PIL Image
29
+ final_pil_image = Image.fromarray(final_image)
30
+ return final_pil_image
31
 
32
+ # Initialize objects from transformers
33
+ config = DetrConfig.from_pretrained("facebook/detr-resnet-50")
34
  model = DetrForObjectDetection.from_pretrained("facebook/detr-resnet-50", config=config)
 
 
35
  image_processor = DetrImageProcessor.from_pretrained("facebook/detr-resnet-50")
 
 
36
  od_pipe = pipeline(task='object-detection', model=model, image_processor=image_processor)
37
 
38
  def get_pipeline_prediction(pil_image):
39
+ # Run the object detection pipeline
40
  pipeline_output = od_pipe(pil_image)
41
 
42
+ # Draw the detection results on the image
43
+ processed_image = draw_detections(pil_image, pipeline_output)
44
+
45
+ # Provide both the image and the JSON detection results
46
+ return processed_image, pipeline_output
47
 
48
  demo = gr.Interface(
49
+ fn=get_pipeline_prediction,
50
+ inputs=gr.Image(label="Input image", type="pil"),
51
+ outputs=[
52
+ gr.Image(label="Annotated Image"),
53
+ gr.JSON(label="Detected Objects")
54
+ ]
55
  )
56
 
57
+ demo.launch()