Blessing commited on
Commit
42c5d54
1 Parent(s): 86c8ca3

Added some files

Browse files
Files changed (4) hide show
  1. .gitignore +8 -0
  2. app.py +175 -0
  3. best.pt +3 -0
  4. requirements.txt +45 -0
.gitignore ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
 
1
+ flagged/
2
+ *.pt
3
+ *.png
4
+ *.jpg
5
+ *.JPG
6
+ *.mp4
7
+ *.mkv
8
+ gradio_cached_examples/
app.py ADDED
@@ -0,0 +1,175 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import cv2
3
+ import requests
4
+ import os
5
+ import random
6
+
7
+ from ultralytics import YOLO
8
+
9
+ file_urls = [
10
+ 'https://www.dropbox.com/scl/fi/34yt1vrl4mc4n9ujdf9gm/all_76.jpg?rlkey=f7b6nq478r2m9yahcalzjzif5&dl=1',
11
+ 'https://www.dropbox.com/scl/fi/lns6cewinp7rgf3v2g1n8/all_5.jpg?rlkey=20zvut81b829k9lg5yk8ve99z&dl=1',
12
+ 'https://www.dropbox.com/scl/fi/13jr2f1znuzulmsyabl2f/long3.jpg?rlkey=jeyriw5a8c0t42e7y2986y53m&dl=1',
13
+ 'https://www.dropbox.com/scl/fi/nglwcza7msjo1vu4kw27r/pot4.jpg?rlkey=1ynm35b4j100ta0p5g3fx7hx4&dl=1',
14
+ 'https://www.dropbox.com/s/7sjfwncffg8xej2/video_7.mp4?dl=1'
15
+ ]
16
+
17
+ # def download_file(url, save_name):
18
+ # url = url
19
+ # if not os.path.exists(save_name):
20
+ # file = requests.get(url)
21
+ # open(save_name, 'wb').write(file.content)
22
+
23
+ # for i, url in enumerate(file_urls):
24
+ # if 'mp4' in file_urls[i]:
25
+ # download_file(
26
+ # file_urls[i],
27
+ # f"video.mp4"
28
+ # )
29
+ # else:
30
+ # download_file(
31
+ # file_urls[i],
32
+ # f"image_{i}.jpg"
33
+ # )
34
+
35
+
36
+ model = YOLO('best.pt')
37
+ # path = [['image_0.jpg'], ['image_1.jpg'], ['image_2.jpg'], ['image_3.jpg']]
38
+
39
+ path = [['IMG_7612.JPG'], ['IMG_7678.JPG'], ['all_33.jpg'], ['all_80.jpg'],
40
+ ['DSC02813.JPG'], ['DSC02373.JPG']]
41
+
42
+
43
+ # path = [['sc_1_0 (1) (1).JPG'], ['sc_1_0 (16) (1).JPG'],
44
+ # ['sc_1_0 (18) (1).JPG'], ['sc_1_0 (18).JPG']]
45
+
46
+ video_path = [['VID-20230809-WA0021.mp4'], ['VID-20230809-WA0022.mp4'],
47
+ ['VID-20230809-WA0024.mp4'], ['VID-20230809-WA0032.mp4']]
48
+
49
+ classes = ['alligator_cracking', 'longitudinal_cracking', 'potholes', 'ravelling']
50
+
51
+ def show_preds_image(image_path):
52
+ image = cv2.imread(image_path)
53
+ outputs = model.predict(source=image_path, agnostic_nms=True, conf=0.25, iou=0.4, imgsz=1024)
54
+ results = outputs[0].cpu().numpy()
55
+
56
+ re_boxes = results.boxes.data.tolist()
57
+
58
+ class_colors = {1 : (95, 255, 54), 2: (242, 210, 100), 3: (96, 7, 70), 4:(221, 59, 41)}
59
+ random.seed(42)
60
+ # class_colors = [(random.randint(0, 255), random.randint(0, 255), random.randint(0, 255)) for _ in range(4)]
61
+
62
+ for i, det in enumerate(results.boxes.xyxy):
63
+ x1, y1, x2, y2 = int(det[0]), int(det[1]), int(det[2]), int(det[3])
64
+
65
+ class_label = int(re_boxes[i][-1])
66
+ rectangle_color = class_colors.get(class_label)
67
+ # rectangle_color = class_colors[class_label]
68
+ text_color = rectangle_color
69
+ cv2.rectangle(
70
+ image,
71
+ (int(det[0]), int(det[1])),
72
+ (int(det[2]), int(det[3])),
73
+ color=rectangle_color,
74
+ thickness=3,
75
+ lineType=cv2.LINE_AA
76
+ )
77
+
78
+ text_position = (x1, y1+100)
79
+ conf = re_boxes[i][-2]
80
+ class_name = classes[class_label]
81
+ # class_label = class_name.split('_')[0] + '\n' + class_name.split('_')[1] if '_' in class_name else class_name
82
+ cv2.putText(image, classes[class_label] + f' = {round(conf, 2)}',
83
+ text_position, cv2.FONT_HERSHEY_SIMPLEX, 1.5, text_color, 3)
84
+
85
+
86
+ # print(class_ids)
87
+ return cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
88
+
89
+ inputs_image = [
90
+ gr.components.Image(type="filepath", label="Input Image"),
91
+ ]
92
+ outputs_image = [
93
+ gr.components.Image(type="numpy", label="Output Image"),
94
+ ]
95
+ interface_image = gr.Interface(
96
+ fn=show_preds_image,
97
+ inputs=inputs_image,
98
+ outputs=outputs_image,
99
+ title="Asphalt Road Pavement Distresses Detector",
100
+ examples=path,
101
+ cache_examples=False,
102
+ description= 'This is a demo app that takes in images or videos of Asphalt pavement surfaces and \
103
+ \n detects the following pavement distresses: \
104
+ \n \
105
+ \n Alligator cracking \
106
+ \n Longitudinal cracking \
107
+ \n Potholes \
108
+ \n Ravelling \
109
+ \n \
110
+ \n This is specifically for Inference and educational purpose.\
111
+ \n \
112
+ \n The model might ocassionaly give false outputs'
113
+ )
114
+
115
+ def show_preds_video(video_path):
116
+ cap = cv2.VideoCapture(video_path)
117
+ while(cap.isOpened()):
118
+ ret, frame = cap.read()
119
+ if ret:
120
+ frame_copy = frame.copy()
121
+ outputs = model.predict(source=frame, agnostic_nms=True, conf=0.25, iou=0.4, imgsz=1024)
122
+ results = outputs[0].cpu().numpy()
123
+ re_boxes = results.boxes.data.tolist()
124
+
125
+ class_colors = {1 : (95, 255, 54), 2: (242, 210, 100), 3: (96, 7, 70), 4:(221, 59, 41)}
126
+ random.seed(42)
127
+ # class_colors = [(random.randint(0, 255), random.randint(0, 255), random.randint(0, 255)) for _ in range(4)]
128
+
129
+ for i, det in enumerate(results.boxes.xyxy):
130
+ x1, y1, x2, y2 = int(det[0]), int(det[1]), int(det[2]), int(det[3])
131
+
132
+ class_label = int(re_boxes[i][-1])
133
+ rectangle_color = class_colors.get(class_label)
134
+ # rectangle_color = class_colors[class_label]
135
+ text_color = rectangle_color
136
+
137
+ cv2.rectangle(
138
+ frame_copy,
139
+ (int(det[0]), int(det[1])),
140
+ (int(det[2]), int(det[3])),
141
+ color=rectangle_color,
142
+ thickness=2,
143
+ lineType=cv2.LINE_AA
144
+ )
145
+
146
+
147
+ text_position = (x1, y1+100)
148
+ conf = re_boxes[i][-2]
149
+ class_name = classes[class_label]
150
+ # class_label = class_name.split('_')[0] + '\n' + class_name.split('_')[1] if '_' in class_name else class_name
151
+ cv2.putText(frame_copy, classes[class_label] + f' = {round(conf, 2)}',
152
+ text_position, cv2.FONT_HERSHEY_SIMPLEX, 1.5, text_color, 3)
153
+
154
+ yield cv2.cvtColor(frame_copy, cv2.COLOR_BGR2RGB)
155
+
156
+ inputs_video = [
157
+ gr.components.Video(type="filepath", label="Input Video"),
158
+
159
+ ]
160
+ outputs_video = [
161
+ gr.components.Image(type="numpy", label="Output Video"),
162
+ ]
163
+ interface_video = gr.Interface(
164
+ fn=show_preds_video,
165
+ inputs=inputs_video,
166
+ outputs=outputs_video,
167
+ title="Asphalt Road Pavement Distresses Detector",
168
+ examples=video_path,
169
+ cache_examples=False,
170
+ # live=True
171
+ )
172
+ gr.TabbedInterface(
173
+ [interface_image, interface_video],
174
+ tab_names=['Image inference', 'Video inference'],
175
+ ).queue().launch()
best.pt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:d9e59dae4ba88de0caa60558da6873bc12460e12757e356e25200035c1071dc4
3
+ size 22527918
requirements.txt ADDED
@@ -0,0 +1,45 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Ultralytics requirements
2
+ # Usage: pip install -r requirements.txt
3
+
4
+ # Base ----------------------------------------
5
+ matplotlib>=3.2.2
6
+ numpy>=1.22.2 # pinned by Snyk to avoid a vulnerability
7
+ opencv-python>=4.6.0
8
+ pillow>=7.1.2
9
+ pyyaml>=5.3.1
10
+ requests>=2.23.0
11
+ scipy>=1.4.1
12
+ torch>=1.7.0
13
+ torchvision>=0.8.1
14
+ tqdm>=4.64.0
15
+
16
+ # Logging -------------------------------------
17
+ # tensorboard>=2.13.0
18
+ # dvclive>=2.12.0
19
+ # clearml
20
+ # comet
21
+
22
+ # Plotting ------------------------------------
23
+ pandas>=1.1.4
24
+ seaborn>=0.11.0
25
+
26
+ # Export --------------------------------------
27
+ # coremltools>=7.0.b1 # CoreML export
28
+ # onnx>=1.12.0 # ONNX export
29
+ # onnxsim>=0.4.1 # ONNX simplifier
30
+ # nvidia-pyindex # TensorRT export
31
+ # nvidia-tensorrt # TensorRT export
32
+ # scikit-learn==0.19.2 # CoreML quantization
33
+ # tensorflow>=2.4.1 # TF exports (-cpu, -aarch64, -macos)
34
+ # tflite-support
35
+ # tensorflowjs>=3.9.0 # TF.js export
36
+ # openvino-dev>=2023.0 # OpenVINO export
37
+
38
+ # Extras --------------------------------------
39
+ psutil # system utilization
40
+ py-cpuinfo # display CPU info
41
+ # thop>=0.1.1 # FLOPs computation
42
+ # ipython # interactive notebook
43
+ # albumentations>=1.0.3 # training augmentations
44
+ # pycocotools>=2.0.6 # COCO mAP
45
+ # roboflow