Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -30,19 +30,52 @@ def detect_objects(image):
|
|
30 |
|
31 |
return image
|
32 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
33 |
|
34 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
35 |
image = Image.open(file.name)
|
36 |
image_with_boxes = detect_objects(image)
|
37 |
return image_with_boxes
|
38 |
|
39 |
-
|
40 |
-
|
|
|
|
|
|
|
|
|
|
|
41 |
inputs="file",
|
42 |
outputs="image",
|
43 |
-
title="Object Detection",
|
44 |
-
description="Upload an image and detect objects using DETR model.",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
45 |
allow_flagging=False
|
46 |
)
|
47 |
|
48 |
-
|
|
|
|
30 |
|
31 |
return image
|
32 |
|
33 |
+
def detect_labels(image):
|
34 |
+
# Load the pre-trained DETR model
|
35 |
+
processor = DetrImageProcessor.from_pretrained("facebook/detr-resnet-50")
|
36 |
+
model = DetrForObjectDetection.from_pretrained("facebook/detr-resnet-50")
|
37 |
+
|
38 |
+
inputs = processor(images=image, return_tensors="pt")
|
39 |
+
outputs = model(**inputs)
|
40 |
|
41 |
+
# convert outputs (bounding boxes and class logits) to COCO API
|
42 |
+
# let's only keep detections with score > 0.9
|
43 |
+
target_sizes = torch.tensor([image.size[::-1]])
|
44 |
+
results = processor.post_process_object_detection(outputs, target_sizes=target_sizes, threshold=0.9)[0]
|
45 |
+
|
46 |
+
labels = []
|
47 |
+
for label_id in results["labels"]:
|
48 |
+
labels.append(model.config.id2label[label_id.item()])
|
49 |
+
|
50 |
+
return labels
|
51 |
+
|
52 |
+
def upload_image_with_boxes(file):
|
53 |
image = Image.open(file.name)
|
54 |
image_with_boxes = detect_objects(image)
|
55 |
return image_with_boxes
|
56 |
|
57 |
+
def upload_image_with_labels(file):
|
58 |
+
image = Image.open(file.name)
|
59 |
+
labels = detect_labels(image)
|
60 |
+
return ", ".join(labels)
|
61 |
+
|
62 |
+
iface_boxes = gr.Interface(
|
63 |
+
fn=upload_image_with_boxes,
|
64 |
inputs="file",
|
65 |
outputs="image",
|
66 |
+
title="Object Detection with Boxes",
|
67 |
+
description="Upload an image and detect objects using DETR model. Boxes will be drawn around the detected objects.",
|
68 |
+
allow_flagging=False
|
69 |
+
)
|
70 |
+
|
71 |
+
iface_labels = gr.Interface(
|
72 |
+
fn=upload_image_with_labels,
|
73 |
+
inputs="file",
|
74 |
+
outputs="text",
|
75 |
+
title="Detected Object Labels",
|
76 |
+
description="Upload an image and get the detected object labels using DETR model.",
|
77 |
allow_flagging=False
|
78 |
)
|
79 |
|
80 |
+
iface_boxes.launch()
|
81 |
+
iface_labels.launch()
|