Raghvender commited on
Commit
d7401b7
·
1 Parent(s): 3e8be1f
Files changed (1) hide show
  1. app.py +53 -0
app.py ADDED
@@ -0,0 +1,53 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from ultralytics import YOLO
3
+ import cv2
4
+
5
+ model = YOLO('head_yolov8s.pt')
6
+
7
+ def head_detect(path):
8
+ img = cv2.imread(path)
9
+ output = model(source=img)
10
+ res = output[0].cpu().numpy()
11
+
12
+ # Extract bbox, cls id and conf
13
+ bboxes = res.boxes.xyxy
14
+ class_ids = res.boxes.cls
15
+ conf_scores = res.boxes.conf
16
+
17
+ for i in range(len(bboxes)):
18
+ xmin, ymin, xmax, ymax = int(bboxes[i][0]), int(bboxes[i][1]), int(bboxes[i][2]), int(bboxes[i][3])
19
+ conf = conf_scores[i]
20
+ cls_id = int(class_ids[i])
21
+ label = model.names[cls_id] # Get the label name
22
+
23
+ # Draw rectangle for bounding box
24
+ cv2.rectangle(img, (xmin, ymin), (xmax, ymax), color=(0, 0, 255), thickness=2, lineType=cv2.LINE_AA)
25
+
26
+ # Prepare label text with confidence score
27
+ label_text = f'{label} {conf:.2f}'
28
+
29
+ # Put text (label) on the image
30
+ cv2.putText(img, label_text, (xmin, ymin - 10), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 1, lineType=cv2.LINE_AA)
31
+
32
+ return cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
33
+
34
+ input_image = [
35
+ gr.components.Image(type='filepath', label='Input Image'),
36
+ ]
37
+
38
+ output_image = [
39
+ gr.components.Image(type='numpy', label='Prediction'),
40
+ ]
41
+
42
+ interface = gr.Interface(
43
+ fn=head_detect,
44
+ inputs=input_image,
45
+ outputs=output_image,
46
+ title='Top Head Detection',
47
+ cache_examples=False,
48
+ )
49
+
50
+ gr.TabbedInterface(
51
+ [interface],
52
+ tab_names=['Image Inference']
53
+ ).queue().launch()