Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,32 +1,22 @@
|
|
1 |
import gradio as gr
|
2 |
-
from transformers import
|
3 |
-
from PIL import Image
|
4 |
-
import requests
|
5 |
-
from io import BytesIO
|
6 |
-
import numpy as np
|
7 |
|
8 |
-
# Load the
|
9 |
-
|
10 |
-
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
11 |
-
model = AutoModelForSequenceClassification.from_pretrained(model_name)
|
12 |
|
13 |
-
# Function to
|
14 |
-
def
|
15 |
-
|
16 |
-
|
17 |
-
|
|
|
|
|
|
|
|
|
18 |
|
19 |
-
#
|
20 |
-
|
21 |
-
|
22 |
-
inputs = tokenizer(image, return_tensors="pt", padding=True, truncation=True)
|
23 |
-
outputs = model(**inputs)
|
24 |
-
logits = outputs.logits.detach().numpy()[0]
|
25 |
-
probabilities = np.exp(logits) / np.exp(logits).sum(-1)
|
26 |
-
predicted_class = np.argmax(probabilities)
|
27 |
-
return {str(i): float(prob) for i, prob in enumerate(probabilities)}
|
28 |
|
29 |
-
# Create
|
30 |
-
|
31 |
-
output_label = gr.outputs.Label(num_top_classes=3)
|
32 |
-
gr.Interface(classify_image, inputs=input_image, outputs=output_label).launch()
|
|
|
1 |
import gradio as gr
|
2 |
+
from transformers import pipeline
|
|
|
|
|
|
|
|
|
3 |
|
4 |
+
# Load the DETR object detection pipeline
|
5 |
+
object_detection_pipeline = pipeline("object-detection", model="facebook/detr-resnet-50")
|
|
|
|
|
6 |
|
7 |
+
# Function to perform object detection on an image
|
8 |
+
def detect_objects(image):
|
9 |
+
# Perform object detection
|
10 |
+
results = object_detection_pipeline(image)
|
11 |
+
# Extract bounding boxes and object labels
|
12 |
+
bounding_boxes = [obj["bbox"] for obj in results]
|
13 |
+
labels = [obj["label"] for obj in results]
|
14 |
+
# Return bounding boxes and labels
|
15 |
+
return bounding_boxes, labels
|
16 |
|
17 |
+
# Define Gradio interface
|
18 |
+
inputs = gr.inputs.Image()
|
19 |
+
outputs = gr.outputs.ObjectDetection(labels=["person", "car", "truck", "bicycle", "motorcycle"]) # Customize labels as needed
|
|
|
|
|
|
|
|
|
|
|
|
|
20 |
|
21 |
+
# Create Gradio interface
|
22 |
+
gr.Interface(fn=detect_objects, inputs=inputs, outputs=outputs, title="Object Detection", description="Detect objects in images.").launch()
|
|
|
|