Gabolozano commited on
Commit
3c72bd0
·
verified ·
1 Parent(s): 425f707

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +69 -0
app.py ADDED
@@ -0,0 +1,69 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import gradio as gr
3
+ from transformers import pipeline, DetrForObjectDetection, DetrConfig, DetrImageProcessor
4
+ import numpy as np
5
+ import cv2
6
+ from PIL import Image
7
+ def model_is_panoptic(model_name):
8
+ return "panoptic" in model_name
9
+ def load_model(model_name, threshold):
10
+ config = DetrConfig.from_pretrained(model_name, threshold=threshold)
11
+ model = DetrForObjectDetection.from_pretrained(model_name, config=config)
12
+ image_processor = DetrImageProcessor.from_pretrained(model_name)
13
+ return pipeline(task='object-detection', model=model, image_processor=image_processor)
14
+ # Initial model with default threshold
15
+ od_pipe = load_model("facebook/detr-resnet-101", 0.25)
16
+ def draw_detections(image, detections, model_name):
17
+ np_image = np.array(image)
18
+ np_image = cv2.cvtColor(np_image, cv2.COLOR_RGB2BGR)
19
+ for detection in detections:
20
+ if model_is_panoptic(model_name):
21
+ # Handle segmentations for panoptic models
22
+ mask = detection['mask']
23
+ color = np.random.randint(0, 255, size=3)
24
+ mask = np.round(mask * 255).astype(np.uint8)
25
+ mask = cv2.resize(mask, (image.width, image.height))
26
+ mask_image = np.stack([mask]*3, axis=-1)
27
+ np_image[mask == 255] = np_image[mask == 255] * 0.5 + color * 0.5
28
+ else:
29
+ # Handle bounding boxes for standard models
30
+ score = detection['score']
31
+ label = detection['label']
32
+ box = detection['box']
33
+ x_min, y_min = box['xmin'], box['ymin']
34
+ x_max, y_max = box['xmax'], box['ymax']
35
+ cv2.rectangle(np_image, (x_min, y_min), (x_max, y_max), (0, 255, 0), 2)
36
+ label_text = f'{label} {score:.2f}'
37
+ cv2.putText(np_image, label_text, (x_min, y_min - 10), cv2.FONT_HERSHEY_SIMPLEX, 1.5, (255, 255, 255), 4)
38
+ final_image = cv2.cvtColor(np_image, cv2.COLOR_BGR2RGB)
39
+ final_pil_image = Image.fromarray(final_image)
40
+ return final_pil_image
41
+ def get_pipeline_prediction(model_name, threshold, pil_image):
42
+ global od_pipe
43
+ od_pipe = load_model(model_name, threshold)
44
+ try:
45
+ if not isinstance(pil_image, Image.Image):
46
+ pil_image = Image.fromarray(np.array(pil_image).astype('uint8'), 'RGB')
47
+ result = od_pipe(pil_image)
48
+ processed_image = draw_detections(pil_image, result, model_name)
49
+ description = f'Model used: {model_name}, Detection Threshold: {threshold}'
50
+ return processed_image, result, description
51
+ except Exception as e:
52
+ return pil_image, {"error": str(e)}, "Failed to process image"
53
+ with gr.Blocks() as demo:
54
+ with gr.Row():
55
+ with gr.Column():
56
+ gr.Markdown("## Object Detection")
57
+ inp_image = gr.Image(label="Upload your image here")
58
+ model_dropdown = gr.Dropdown(choices=["facebook/detr-resnet-50", "facebook/detr-resnet-50-panoptic", "facebook/detr-resnet-101", "facebook/detr-resnet-101-panoptic"], value="facebook/detr-resnet-101", label="Select Model")
59
+ threshold_slider = gr.Slider(minimum=0.0, maximum=1.0, step=0.01, value=0.25, label="Detection Threshold")
60
+ run_button = gr.Button("Detect Objects")
61
+ with gr.Column():
62
+ with gr.Tab("Annotated Image"):
63
+ output_image = gr.Image()
64
+ with gr.Tab("Detection Results"):
65
+ output_data = gr.JSON()
66
+ with gr.Tab("Description"):
67
+ description_output = gr.Textbox()
68
+ run_button.click(get_pipeline_prediction, inputs=[model_dropdown, threshold_slider, inp_image], outputs=[output_image, output_data, description_output])
69
+ demo.launch()