File size: 1,474 Bytes
5f61664
46e65a1
 
 
 
 
81d6d36
46e65a1
 
 
 
 
81d6d36
 
46e65a1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
87947a4
46e65a1
87947a4
5f61664
8c08500
 
 
7861bab
8c08500
 
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
import gradio as gr
import torch
from transformers import DetrForObjectDetection, DetrImageProcessor, AutoModel
import supervision as sv
from supervision.detection.annotate import BoxAnnotator
from supervision.utils.notebook import plot_image
import cv2

og_model = 'facebook/detr-resnet-50'
image_processor =  DetrImageProcessor.from_pretrained(og_model)
model = AutoModel.from_pretrained("taroii/notfinetuned-detr-50")

def predict(image_path):
    image = cv2.imread(image_path)
    with torch.no_grad():
        # load image and predict
        inputs = image_processor(images=image, return_tensors='pt')
        outputs = model(**inputs)
    
        # post-process
        target_sizes = torch.tensor([image.shape[:2]])
        results = image_processor.post_process_object_detection(
            outputs=outputs, 
            threshold=CONFIDENCE_TRESHOLD, 
            target_sizes=target_sizes
        )[0]
    
    # annotate
    detections = sv.Detections.from_transformers(transformers_results=results).with_nms(threshold=0.5)
    labels = [f"{id2label[class_id]} {confidence:.2f}" for _, confidence, class_id, _ in detections]
    frame = box_annotator.annotate(scene=image.copy(), detections=detections, labels=labels)
    
    #return plot_image(frame, (16, 16))
    
    return frame

gr.Interface(
    predict,
    inputs=gr.inputs.Image(label="Upload hot dog candidate", type="filepath"),
    outputs=gr.Image(),
    title="Non-Fine-Tuned Model"
).launch()