HilmiZr commited on
Commit
ebd11ab
·
1 Parent(s): 522c012

initial commit

Browse files
Files changed (4) hide show
  1. .gitignore +6 -0
  2. YOLOv8-SafetyProtocol-best.pt +3 -0
  3. app.py +110 -0
  4. requirements.txt +47 -0
.gitignore ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ flagged/
2
+ *.png
3
+ *.jpg
4
+ *.mp4
5
+ *.mkv
6
+ gradio_cached_examples/
YOLOv8-SafetyProtocol-best.pt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:faef599117d868a2a51d176079890e63e179ddecff2fd8c484e2c267ae948a6a
3
+ size 6280416
app.py ADDED
@@ -0,0 +1,110 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import cv2
3
+ import requests
4
+ import os
5
+
6
+ from ultralytics import YOLO
7
+
8
+ file_urls = [
9
+ 'https://www.dropbox.com/s/b5g97xo901zb3ds/pothole_example.jpg?dl=1',
10
+ 'https://www.dropbox.com/s/86uxlxxlm1iaexa/pothole_screenshot.png?dl=1',
11
+ 'https://www.dropbox.com/s/7sjfwncffg8xej2/video_7.mp4?dl=1'
12
+ ]
13
+
14
+
15
+ def download_file(url, save_name):
16
+ url = url
17
+ if not os.path.exists(save_name):
18
+ file = requests.get(url)
19
+ open(save_name, 'wb').write(file.content)
20
+
21
+
22
+ for i, url in enumerate(file_urls):
23
+ if 'mp4' in file_urls[i]:
24
+ download_file(
25
+ file_urls[i],
26
+ f"video.mp4"
27
+ )
28
+ else:
29
+ download_file(
30
+ file_urls[i],
31
+ f"image_{i}.jpg"
32
+ )
33
+
34
+ model = YOLO('YOLOv8-SafetyProtocol-best.pt')
35
+ path = [['image_0.jpg'], ['image_1.jpg']]
36
+ video_path = [['video.mp4']]
37
+
38
+
39
+ def show_preds_image(image_path):
40
+ image = cv2.imread(image_path)
41
+ outputs = model.predict(source=image_path)
42
+ results = outputs[0].cpu().numpy()
43
+ for i, det in enumerate(results.boxes.xyxy):
44
+ cv2.rectangle(
45
+ image,
46
+ (int(det[0]), int(det[1])),
47
+ (int(det[2]), int(det[3])),
48
+ color=(0, 0, 255),
49
+ thickness=2,
50
+ lineType=cv2.LINE_AA
51
+ )
52
+ return cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
53
+
54
+
55
+ inputs_image = [
56
+ gr.components.Image(type="filepath", label="Input Image"),
57
+ ]
58
+ outputs_image = [
59
+ gr.components.Image(type="numpy", label="Output Image"),
60
+ ]
61
+ interface_image = gr.Interface(
62
+ fn=show_preds_image,
63
+ inputs=inputs_image,
64
+ outputs=outputs_image,
65
+ title="Safety Gear Detector",
66
+ examples=path,
67
+ cache_examples=False,
68
+ )
69
+
70
+
71
+ def show_preds_video(video_path):
72
+ cap = cv2.VideoCapture(video_path)
73
+ while (cap.isOpened()):
74
+ ret, frame = cap.read()
75
+ if ret:
76
+ frame_copy = frame.copy()
77
+ outputs = model.predict(source=frame)
78
+ results = outputs[0].cpu().numpy()
79
+ for i, det in enumerate(results.boxes.xyxy):
80
+ cv2.rectangle(
81
+ frame_copy,
82
+ (int(det[0]), int(det[1])),
83
+ (int(det[2]), int(det[3])),
84
+ color=(0, 0, 255),
85
+ thickness=2,
86
+ lineType=cv2.LINE_AA
87
+ )
88
+ yield cv2.cvtColor(frame_copy, cv2.COLOR_BGR2RGB)
89
+
90
+
91
+ inputs_video = [
92
+ gr.components.Video(type="filepath", label="Input Video"),
93
+
94
+ ]
95
+ outputs_video = [
96
+ gr.components.Image(type="numpy", label="Output Image"),
97
+ ]
98
+ interface_video = gr.Interface(
99
+ fn=show_preds_video,
100
+ inputs=inputs_video,
101
+ outputs=outputs_video,
102
+ title="Safety Gear Detector",
103
+ examples=video_path,
104
+ cache_examples=False,
105
+ )
106
+
107
+ gr.TabbedInterface(
108
+ [interface_image, interface_video],
109
+ tab_names=['Image inference', 'Video inference']
110
+ ).queue().launch()
requirements.txt ADDED
@@ -0,0 +1,47 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Ultralytics requirements
2
+ # Usage: pip install -r requirements.txt
3
+
4
+ # Base ----------------------------------------
5
+ hydra-core>=1.2.0
6
+ matplotlib>=3.2.2
7
+ numpy>=1.18.5
8
+ opencv-python>=4.1.1
9
+ Pillow>=7.1.2
10
+ PyYAML>=5.3.1
11
+ requests>=2.23.0
12
+ scipy>=1.4.1
13
+ torch>=1.7.0
14
+ torchvision>=0.8.1
15
+ tqdm>=4.64.0
16
+ ultralytics
17
+
18
+ # Logging -------------------------------------
19
+ tensorboard>=2.4.1
20
+ # clearml
21
+ # comet
22
+
23
+ # Plotting ------------------------------------
24
+ pandas>=1.1.4
25
+ seaborn>=0.11.0
26
+
27
+ # Export --------------------------------------
28
+ # coremltools>=6.0 # CoreML export
29
+ # onnx>=1.12.0 # ONNX export
30
+ # onnx-simplifier>=0.4.1 # ONNX simplifier
31
+ # nvidia-pyindex # TensorRT export
32
+ # nvidia-tensorrt # TensorRT export
33
+ # scikit-learn==0.19.2 # CoreML quantization
34
+ # tensorflow>=2.4.1 # TF exports (-cpu, -aarch64, -macos)
35
+ # tensorflowjs>=3.9.0 # TF.js export
36
+ # openvino-dev # OpenVINO export
37
+
38
+ # Extras --------------------------------------
39
+ ipython # interactive notebook
40
+ psutil # system utilization
41
+ thop>=0.1.1 # FLOPs computation
42
+ # albumentations>=1.0.3
43
+ # pycocotools>=2.0.6 # COCO mAP
44
+ # roboflow
45
+
46
+ # HUB -----------------------------------------
47
+ GitPython>=3.1.24