File size: 1,297 Bytes
d9f0b09
6503948
 
 
77c3c13
6503948
3f5c0c0
6503948
3f5c0c0
6503948
 
 
 
 
 
 
 
 
 
 
 
 
 
d9f0b09
3f5c0c0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import gradio as gr
from transformers import DetrImageProcessor, DetrForObjectDetection
import torch
import supervision as sv
import json

id2label = {0: 'dangerous-items', 1: 'Gun', 2: 'Knife', 3: 'Pliers', 4: 'Scissors', 5: 'Wrench'}

def anylize(image):
    with torch.no_grad():

        inputs = image_processor(images=image, return_tensors='pt')
        outputs = model(**inputs)
    
        target_sizes = torch.tensor([image.shape[:2]])
        results = image_processor.post_process_object_detection(
            outputs=outputs, 
            threshold=0.8, 
            target_sizes=target_sizes
        )[0]

    # annotate
    detections = sv.Detections.from_transformers(transformers_results=results).with_nms(threshold=0.5)

    out = {}
    
    for idx, detection in enumerate(detections):
        cls = id2label[detection.class_id]
        confidence = detection.confidence
        box = detection.xyxy

        out[str(idx)] = {
            "box":list(box),
            "cls":cls,
            "conf":confidence
        }
        
    #labels = [str([list(xyxy), confidence, id2label[class_id]]) for xyxy, _, confidence, class_id, _ in detections]
    #json_list = json.dumps(str(labels[0]))

    return out

gr.Interface(fn = anylize, inputs="image", outputs=gr.JSON()).launch()