Spaces:
Sleeping
Sleeping
Saanvi12011
commited on
Commit
•
3712cce
1
Parent(s):
173b89f
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,28 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from transformers import pipeline
|
3 |
+
|
4 |
+
# Initialize the zero-shot-object-detection pipeline
|
5 |
+
pipe = pipeline("zero-shot-object-detection", model="google/owlvit-base-patch16")
|
6 |
+
|
7 |
+
# Define the function to use the pipeline
|
8 |
+
def detect_objects(image, labels):
|
9 |
+
# Split the labels into a list
|
10 |
+
candidate_labels = [label.strip() for label in labels.split(",")]
|
11 |
+
result = pipe(image, candidate_labels=candidate_labels)
|
12 |
+
# Process the result to return the detected objects and their confidence scores
|
13 |
+
return result
|
14 |
+
|
15 |
+
# Create the Gradio interface
|
16 |
+
iface = gr.Interface(
|
17 |
+
fn=detect_objects, # function to process input
|
18 |
+
inputs=[
|
19 |
+
gr.inputs.Image(type="filepath", label="Upload Image"), # input for image
|
20 |
+
gr.inputs.Textbox(lines=2, label="Candidate Labels (comma separated)"), # input for candidate labels
|
21 |
+
],
|
22 |
+
outputs="json", # output as JSON for multiple object detection results
|
23 |
+
title="Zero-Shot Object Detection", # Title of the interface
|
24 |
+
description="Upload an image and provide a list of labels (comma separated) for object detection.", # Description
|
25 |
+
)
|
26 |
+
|
27 |
+
# Launch the interface
|
28 |
+
iface.launch()
|