Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,3 +1,33 @@
|
|
1 |
import gradio as gr
|
|
|
|
|
2 |
|
3 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
+
from transformers import DetrImageProcessor, DetrForObjectDetection
|
3 |
+
import torch
|
4 |
|
5 |
+
|
6 |
+
def anylize(img):
|
7 |
+
# input_image_path = os.path.join(os.getcwd(), img.get_data()[0].name)
|
8 |
+
# return input_image_path
|
9 |
+
image = img
|
10 |
+
|
11 |
+
processor = DetrImageProcessor.from_pretrained("facebook/detr-resnet-50")
|
12 |
+
model = DetrForObjectDetection.from_pretrained("taroii/finetuned-detr-50")
|
13 |
+
|
14 |
+
inputs = processor(images=image, return_tensors="pt")
|
15 |
+
outputs = model(**inputs)
|
16 |
+
|
17 |
+
#target_sizes = torch.tensor([image.size])
|
18 |
+
#target_sizes = torch.tensor([image.size[::-1]])
|
19 |
+
target_sizes = torch.tensor([image.shape[:2]])
|
20 |
+
results = processor.post_process_object_detection(outputs, target_sizes=target_sizes, threshold=0.9)[0]
|
21 |
+
|
22 |
+
for score, label, box in zip(results["scores"], results["labels"], results["boxes"]):
|
23 |
+
box = [round(i, 2) for i in box.tolist()]
|
24 |
+
return(
|
25 |
+
f"Detected {model.config.id2label[label.item()]} with confidence "
|
26 |
+
f"{round(score.item(), 3)} at location {box}"
|
27 |
+
)
|
28 |
+
|
29 |
+
# return "Hello " + img + "!"
|
30 |
+
|
31 |
+
app = gr.Interface(fn=anylize, inputs="image", outputs="text")
|
32 |
+
|
33 |
+
app.launch()
|