Spaces:
Runtime error
Runtime error
Add application file
Browse files
app.py
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#Falah with Gradio
|
| 2 |
+
import gradio as gr
|
| 3 |
+
from transformers import pipeline
|
| 4 |
+
from PIL import Image, ImageDraw
|
| 5 |
+
|
| 6 |
+
checkpoint = "google/owlvit-base-patch32"
|
| 7 |
+
detector = pipeline(model=checkpoint, task="zero-shot-object-detection")
|
| 8 |
+
|
| 9 |
+
def detect_and_visualize_objects(image):
|
| 10 |
+
# Convert the image to RGB format
|
| 11 |
+
image = image.convert("RGB")
|
| 12 |
+
|
| 13 |
+
# Process the image using the object detection model
|
| 14 |
+
predictions = detector(
|
| 15 |
+
image,
|
| 16 |
+
candidate_labels=["human face", "rocket", "nasa badge", "star-spangled banner"],
|
| 17 |
+
)
|
| 18 |
+
|
| 19 |
+
# Draw bounding boxes and labels on the image
|
| 20 |
+
draw = ImageDraw.Draw(image)
|
| 21 |
+
for prediction in predictions:
|
| 22 |
+
box = prediction["box"]
|
| 23 |
+
label = prediction["label"]
|
| 24 |
+
score = prediction["score"]
|
| 25 |
+
|
| 26 |
+
xmin, ymin, xmax, ymax = box.values()
|
| 27 |
+
draw.rectangle((xmin, ymin, xmax, ymax), outline="red", width=1)
|
| 28 |
+
draw.text((xmin, ymin), f"{label}: {round(score, 2)}", fill="white")
|
| 29 |
+
|
| 30 |
+
# Return the annotated image
|
| 31 |
+
return image
|
| 32 |
+
|
| 33 |
+
# Define the Gradio interface
|
| 34 |
+
image_input = gr.inputs.Image(type="pil")
|
| 35 |
+
image_output = gr.outputs.Image(type="pil")
|
| 36 |
+
|
| 37 |
+
iface = gr.Interface(
|
| 38 |
+
fn=detect_and_visualize_objects,
|
| 39 |
+
inputs=image_input,
|
| 40 |
+
outputs=image_output,
|
| 41 |
+
title="Object Detection",
|
| 42 |
+
description="Detect objects in an image using a pre-trained model and visualize the results.",
|
| 43 |
+
)
|
| 44 |
+
|
| 45 |
+
# Launch the Gradio interface
|
| 46 |
+
iface.launch(debug=True)
|