ranggaaldosas commited on
Commit
dd57c98
1 Parent(s): 2b9911c

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +79 -0
app.py ADDED
@@ -0,0 +1,79 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import cv2
3
+ import requests
4
+ import os
5
+
6
+ from ultralytics import YOLO
7
+
8
+ model = YOLO('best_model.pt')
9
+ example_list = [["examples/" + example] for example in os.listdir("examples")]
10
+
11
+ def show_preds_image(image_path):
12
+ image = cv2.imread(image_path)
13
+ outputs = model.predict(source=image_path)
14
+ results = outputs[0].cpu().numpy()
15
+ for i, det in enumerate(results.boxes.xyxy):
16
+ cv2.rectangle(
17
+ image,
18
+ (int(det[0]), int(det[1])),
19
+ (int(det[2]), int(det[3])),
20
+ color=(0, 0, 255),
21
+ thickness=2,
22
+ lineType=cv2.LINE_AA
23
+ )
24
+ return cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
25
+
26
+ inputs_image = [
27
+ gr.components.Image(type="filepath", label="Input Image"),
28
+ ]
29
+ outputs_image = [
30
+ gr.components.Image(type="numpy", label="Output Image"),
31
+ ]
32
+ interface_image = gr.Interface(
33
+ fn=show_preds_image,
34
+ inputs=inputs_image,
35
+ outputs=outputs_image,
36
+ title="Pothole detector",
37
+ examples=path,
38
+ cache_examples=False,
39
+ )
40
+
41
+ def show_preds_video(video_path):
42
+ cap = cv2.VideoCapture(video_path)
43
+ while(cap.isOpened()):
44
+ ret, frame = cap.read()
45
+ if ret:
46
+ frame_copy = frame.copy()
47
+ outputs = model.predict(source=frame)
48
+ results = outputs[0].cpu().numpy()
49
+ for i, det in enumerate(results.boxes.xyxy):
50
+ cv2.rectangle(
51
+ frame_copy,
52
+ (int(det[0]), int(det[1])),
53
+ (int(det[2]), int(det[3])),
54
+ color=(0, 0, 255),
55
+ thickness=2,
56
+ lineType=cv2.LINE_AA
57
+ )
58
+ yield cv2.cvtColor(frame_copy, cv2.COLOR_BGR2RGB)
59
+
60
+ inputs_video = [
61
+ gr.components.Video(type="filepath", label="Input Video"),
62
+
63
+ ]
64
+ outputs_video = [
65
+ gr.components.Image(type="numpy", label="Output Image"),
66
+ ]
67
+ interface_video = gr.Interface(
68
+ fn=show_preds_video,
69
+ inputs=inputs_video,
70
+ outputs=outputs_video,
71
+ title="Pothole detector",
72
+ examples=video_path,
73
+ cache_examples=False,
74
+ )
75
+
76
+ gr.TabbedInterface(
77
+ [interface_image, interface_video],
78
+ tab_names=['Image inference', 'Video inference']
79
+ ).queue().launch()