Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,3 +1,31 @@
|
|
1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
2 |
|
3 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
+
from transformers import DetrImageProcessor, DetrForObjectDetection
|
3 |
+
import torch
|
4 |
+
import supervision as sv
|
5 |
+
import json
|
6 |
|
7 |
+
image_processor = DetrImageProcessor.from_pretrained("facebook/detr-resnet-50")
|
8 |
+
model = DetrForObjectDetection.from_pretrained("Guy2/AirportSec-100epoch")
|
9 |
+
id2label = {0: 'dangerous-items', 1: 'Gun', 2: 'Knife', 3: 'Pliers', 4: 'Scissors', 5: 'Wrench'}
|
10 |
+
def anylize(img):
|
11 |
+
image = img
|
12 |
+
|
13 |
+
with torch.no_grad():
|
14 |
+
|
15 |
+
inputs = image_processor(images=image, return_tensors='pt')
|
16 |
+
outputs = model(**inputs)
|
17 |
+
|
18 |
+
target_sizes = torch.tensor([image.shape[:2]])
|
19 |
+
results = image_processor.post_process_object_detection(
|
20 |
+
outputs=outputs,
|
21 |
+
threshold=0.8,
|
22 |
+
target_sizes=target_sizes
|
23 |
+
)[0]
|
24 |
+
|
25 |
+
# annotate
|
26 |
+
detections = sv.Detections.from_transformers(transformers_results=results).with_nms(threshold=0.5)
|
27 |
+
labels = [str([list(xyxy), confidence, id2label[class_id]]) for xyxy, _, confidence, class_id, _ in detections]
|
28 |
+
json_list = json.dumps(labels)
|
29 |
+
return json_list
|
30 |
+
|
31 |
+
gr.Interface(fn = anylize, inputs="image", outputs=gr.JSON()).launch()
|