taroii commited on
Commit
46e65a1
·
1 Parent(s): 5f61664

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -0
app.py CHANGED
@@ -1,3 +1,37 @@
1
  import gradio as gr
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2
 
3
  gr.Interface.load("models/taroii/notfinetuned-detr-50").launch()
 
1
  import gradio as gr
2
+ import torch
3
+ from transformers import DetrForObjectDetection, DetrImageProcessor, AutoModel
4
+ import supervision as sv
5
+ from supervision.detection.annotate import BoxAnnotator
6
+ from supervision.utils.notebook import plot_image
7
+
8
+ og_model = 'facebook/detr-resnet-50'
9
+ image_processor = DetrImageProcessor.from_pretrained(og_model)
10
+ model = AutoModel.from_pretrained("taroii/notfinetuned-detr-50")
11
+
12
+ def query(image):
13
+ with torch.no_grad():
14
+ # load image and predict
15
+ inputs = image_processor(images=image, return_tensors='pt')
16
+ outputs = model(**inputs)
17
+
18
+ # post-process
19
+ target_sizes = torch.tensor([image.shape[:2]])
20
+ results = image_processor.post_process_object_detection(
21
+ outputs=outputs,
22
+ threshold=CONFIDENCE_TRESHOLD,
23
+ target_sizes=target_sizes
24
+ )[0]
25
+
26
+ # annotate
27
+ detections = sv.Detections.from_transformers(transformers_results=results).with_nms(threshold=0.5)
28
+ labels = [f"{id2label[class_id]} {confidence:.2f}" for _, confidence, class_id, _ in detections]
29
+ frame = box_annotator.annotate(scene=image.copy(), detections=detections, labels=labels)
30
+
31
+ print('detections')
32
+ %matplotlib inline
33
+ plot_image(frame, (16, 16))
34
+
35
+ return labels, frame
36
 
37
  gr.Interface.load("models/taroii/notfinetuned-detr-50").launch()