Spaces:
Sleeping
Sleeping
File size: 2,304 Bytes
d4c3acc 246f207 ceb95cf c21a752 648d371 ceb95cf d5aac08 aca9f11 b3747be ceb95cf b6fa5d6 b3747be ceb95cf 6564a3a cfd90f9 b3747be e14364d aca9f11 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
import os
import gradio as gr
from transformers import pipeline, DetrForObjectDetection, DetrConfig, DetrImageProcessor
import numpy as np
import cv2
from PIL import Image
# Initialize the model
config = DetrConfig.from_pretrained("facebook/detr-resnet-50")
model = DetrForObjectDetection.from_pretrained("facebook/detr-resnet-50", config=config)
image_processor = DetrImageProcessor.from_pretrained("facebook/detr-resnet-50")
# Initialize the pipeline
od_pipe = pipeline(task='object-detection', model=model, image_processor=image_processor)
def draw_detections(image, detections):
# Convert PIL image to a numpy array
np_image = np.array(image)
# Convert RGB to BGR for OpenCV
np_image = cv2.cvtColor(np_image, cv2.COLOR_RGB2BGR)
for detection in detections:
score = detection['score']
label = detection['label']
box = detection['box']
x_min = box['xmin']
y_min = box['ymin']
x_max = box['xmax']
y_max = box['ymax']
# Draw rectangles and text with a larger font
cv2.rectangle(np_image, (x_min, y_min), (x_max, y_max), (0, 255, 0), 2)
label_text = f'{label} {score:.2f}'
cv2.putText(np_image, label_text, (x_min, y_min - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (255, 255, 255), 3)
# Convert BGR to RGB for displaying
final_image = cv2.cvtColor(np_image, cv2.COLOR_BGR2RGB)
final_pil_image = Image.fromarray(final_image)
return final_pil_image
def get_pipeline_prediction(pil_image):
try:
pipeline_output = od_pipe(pil_image)
processed_image = draw_detections(pil_image, pipeline_output)
return processed_image, pipeline_output
except Exception as e:
print(f"An error occurred: {str(e)}")
return pil_image, {"error": str(e)}
# Define the Gradio blocks interface
with gr.Blocks() as demo:
gr.Markdown("## Object Detection")
with gr.Row():
inp_image = gr.Image(label="Input image", type="pil", tool=None)
btn_run = gr.Button('Run Detection')
with gr.Tab("Annotated Image"):
out_image = gr.Image()
with gr.Tab("Detection Results"):
out_json = gr.JSON()
btn_run.click(get_pipeline_prediction, inputs=inp_image, outputs=[out_image, out_json])
demo.launch() |