Spaces:
Runtime error
Runtime error
initial
Browse files- .gitattributes +3 -0
- app.py +8 -0
- app_helper.py +47 -0
- display.py +154 -0
- main.py +42 -0
- model.py +16 -0
- models/model.weights.h5 +3 -0
- models/model_e15.weights.h5 +3 -0
- requirements.txt +0 -0
- smoker ban.mp4 +3 -0
- smokers.mp4 +3 -0
- thomos.mp4 +3 -0
.gitattributes
CHANGED
@@ -33,3 +33,6 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
|
|
33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
36 |
+
smoker[[:space:]]ban.mp4 filter=lfs diff=lfs merge=lfs -text
|
37 |
+
smokers.mp4 filter=lfs diff=lfs merge=lfs -text
|
38 |
+
thomos.mp4 filter=lfs diff=lfs merge=lfs -text
|
app.py
ADDED
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from app_helper import detect_objects
|
3 |
+
|
4 |
+
|
5 |
+
# Create interface
|
6 |
+
input_video = gr.Video(sources='upload', label="Upload a video file")
|
7 |
+
output_video = gr.Video(label="Detected Objects")
|
8 |
+
gr.Interface(detect_objects, inputs=input_video, outputs=output_video).launch()
|
app_helper.py
ADDED
@@ -0,0 +1,47 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import cv2 as cv
|
2 |
+
from model import model
|
3 |
+
import tensorflow as tf
|
4 |
+
import numpy as np
|
5 |
+
from display import draw_bouding_box
|
6 |
+
import tempfile
|
7 |
+
|
8 |
+
|
9 |
+
def detect_objects(video_file):
|
10 |
+
|
11 |
+
# Output video setup
|
12 |
+
output_file = tempfile.NamedTemporaryFile(suffix=".mp4").name
|
13 |
+
|
14 |
+
# Define the codec and create VideoWriter object
|
15 |
+
fourcc = cv.VideoWriter_fourcc(*'mp4v')
|
16 |
+
fps = 3.0 # Frames per second
|
17 |
+
frame_width = 640
|
18 |
+
frame_height = 640
|
19 |
+
out = cv.VideoWriter(output_file, fourcc, fps, (frame_width, frame_height))
|
20 |
+
|
21 |
+
cam = cv.VideoCapture(video_file)
|
22 |
+
count = 1
|
23 |
+
while True:
|
24 |
+
ret, frame = cam.read()
|
25 |
+
if not ret:
|
26 |
+
break # Break the loop if no frame is returned
|
27 |
+
if count % 20:
|
28 |
+
count += 1
|
29 |
+
continue
|
30 |
+
count += 1
|
31 |
+
frame = cv.resize(frame, (640, 640), interpolation=cv.INTER_CUBIC)
|
32 |
+
image = cv.cvtColor(frame, cv.COLOR_BGR2RGB)
|
33 |
+
image = tf.expand_dims(image, axis=0)
|
34 |
+
y_pred = model.predict(image)
|
35 |
+
y_pred = {'boxes': tf.ragged.constant(y_pred['boxes'][y_pred['confidence'] != -1]),
|
36 |
+
'confidence': tf.ragged.constant(y_pred['confidence'][y_pred['confidence'] != -1]),
|
37 |
+
'classes': tf.ragged.constant(y_pred['classes'][y_pred['confidence'] != -1]),
|
38 |
+
'num_detections': np.count_nonzero(y_pred['confidence'] != -1)
|
39 |
+
}
|
40 |
+
|
41 |
+
frame = draw_bouding_box(frame, y_pred)
|
42 |
+
out.write(frame)
|
43 |
+
|
44 |
+
cam.release()
|
45 |
+
return output_file
|
46 |
+
|
47 |
+
|
display.py
ADDED
@@ -0,0 +1,154 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import cv2 as cv
|
2 |
+
import tensorflow as tf
|
3 |
+
import uuid
|
4 |
+
import datetime
|
5 |
+
import os
|
6 |
+
import numpy as np
|
7 |
+
# from keras_cv import bounding_box, visualization
|
8 |
+
|
9 |
+
class_mapping_smoker = {
|
10 |
+
0: 'person',
|
11 |
+
1: 'smoke'
|
12 |
+
}
|
13 |
+
color_mapping_smoker = {
|
14 |
+
'person': (0, 255, 0),
|
15 |
+
'smoke': (0, 0, 255)
|
16 |
+
}
|
17 |
+
|
18 |
+
def load_image(image_path):
|
19 |
+
image = tf.io.read_file(image_path)
|
20 |
+
image = tf.image.decode_jpeg(image, channels=3)
|
21 |
+
image = tf.image.resize(image, (640,640))
|
22 |
+
return image
|
23 |
+
|
24 |
+
def draw_bouding_box(frame, pred):
|
25 |
+
is_save = False
|
26 |
+
selected_indices = [0]
|
27 |
+
if pred['num_detections']:
|
28 |
+
selected_indices = tf.image.non_max_suppression(
|
29 |
+
list(pred['boxes']), list(pred['confidence']), 8,iou_threshold = .45)
|
30 |
+
print(selected_indices)
|
31 |
+
for i in range(pred['num_detections']):
|
32 |
+
if not i in list(selected_indices):
|
33 |
+
continue
|
34 |
+
confidence = pred['confidence'][i].numpy()
|
35 |
+
class_name = class_mapping_smoker[pred['classes'][i].numpy()]
|
36 |
+
if class_name == 'person':
|
37 |
+
if confidence < 0.45:
|
38 |
+
continue
|
39 |
+
else:
|
40 |
+
if confidence < 0.25:
|
41 |
+
continue
|
42 |
+
else:
|
43 |
+
is_save = True
|
44 |
+
|
45 |
+
box = tf.cast(pred['boxes'][i], tf.int32).numpy()
|
46 |
+
x1, y1, x2, y2 = box
|
47 |
+
color = color_mapping_smoker[class_name] # Green color
|
48 |
+
thickness = 2 # Line thickness
|
49 |
+
cv.rectangle(frame, (x1, y1), (x2, y2), color, thickness)
|
50 |
+
text = f"Conf :{confidence:.2f} | {class_name}"
|
51 |
+
cv.putText(frame, text, (x1, y1 - 10), cv.FONT_HERSHEY_SIMPLEX, 0.5, color, 2)
|
52 |
+
|
53 |
+
if is_save:
|
54 |
+
save_frame(frame)
|
55 |
+
|
56 |
+
|
57 |
+
return frame
|
58 |
+
|
59 |
+
|
60 |
+
|
61 |
+
|
62 |
+
def save_frame(frame):
|
63 |
+
folder_name = datetime.date.today().isoformat()
|
64 |
+
folder_path = os.path.join(os.getcwd(), 'images', folder_name)
|
65 |
+
if not os.path.exists(folder_path):
|
66 |
+
os.makedirs(folder_path)
|
67 |
+
file_name = uuid.uuid4()
|
68 |
+
cv.imwrite(os.path.join(folder_path, f'{file_name}.jpg'), frame)
|
69 |
+
|
70 |
+
|
71 |
+
|
72 |
+
|
73 |
+
def add_padding(frame, window_width=1080, window_height= 720):
|
74 |
+
aspect_ratio = frame.shape[1] / frame.shape[0]
|
75 |
+
|
76 |
+
# Calculate the scaling factors to fit the image within the desired dimensions
|
77 |
+
scale_width = window_width / frame.shape[1]
|
78 |
+
scale_height = window_height / frame.shape[0]
|
79 |
+
|
80 |
+
# Choose the minimum scaling factor to ensure the entire image fits within the window
|
81 |
+
scale_factor = min(scale_width, scale_height)
|
82 |
+
|
83 |
+
# Resize the frame using the calculated scale factor
|
84 |
+
new_width = int(frame.shape[1] * scale_factor)
|
85 |
+
new_height = int(frame.shape[0] * scale_factor)
|
86 |
+
frame = cv.resize(frame, (new_width, new_height))
|
87 |
+
|
88 |
+
# Create a black background with the desired dimensions
|
89 |
+
background = 255 * np.ones((window_height, window_width, 3), dtype=np.uint8)
|
90 |
+
|
91 |
+
# Calculate the position to place the resized frame in the center of the window
|
92 |
+
x_offset = (window_width - new_width) // 2
|
93 |
+
y_offset = (window_height - new_height) // 2
|
94 |
+
|
95 |
+
# Place the resized frame on the black background at the calculated position
|
96 |
+
background[y_offset:y_offset + new_height, x_offset:x_offset + new_width] = frame
|
97 |
+
|
98 |
+
return background
|
99 |
+
|
100 |
+
|
101 |
+
|
102 |
+
|
103 |
+
|
104 |
+
# def draw_bouding_box(frame, pred):
|
105 |
+
# is_save = False
|
106 |
+
# for i in range(pred['num_detections']):
|
107 |
+
# box = tf.cast(pred['boxes'][i], tf.int32).numpy()
|
108 |
+
# confidence = pred['confidence'][i].numpy()
|
109 |
+
# if confidence < .4:
|
110 |
+
# continue
|
111 |
+
# class_name = class_mapping_smoker[pred['classes'][i].numpy()]
|
112 |
+
# if class_name == 'person':
|
113 |
+
# if confidence < 0.5:
|
114 |
+
# continue
|
115 |
+
# else:
|
116 |
+
# if confidence < 0.3:
|
117 |
+
# continue
|
118 |
+
# else:
|
119 |
+
# is_save = True
|
120 |
+
# x1, y1, x2, y2 = box
|
121 |
+
# color = color_mapping_smoker[class_name] # Green color
|
122 |
+
# thickness = 2 # Line thickness
|
123 |
+
# cv.rectangle(frame, (x1, y1), (x2, y2), color, thickness)
|
124 |
+
# text = f"Conf :{confidence:.2f} | {class_name}"
|
125 |
+
# cv.putText(frame, text, (x1, y1 - 10), cv.FONT_HERSHEY_SIMPLEX, 0.5, color, 2)
|
126 |
+
#
|
127 |
+
# if is_save:
|
128 |
+
# save_frame(frame)
|
129 |
+
#
|
130 |
+
#
|
131 |
+
# return frame
|
132 |
+
|
133 |
+
#
|
134 |
+
# # def visualize_detections(model, images, bounding_box_format):
|
135 |
+
# # y_pred = model.predict(images)
|
136 |
+
# # y_pred = {'boxes': tf.ragged.constant([y_pred['boxes'][y_pred['confidence'] != -1]]),
|
137 |
+
# # 'confidence': tf.ragged.constant([y_pred['confidence'][y_pred['confidence'] != -1]]),
|
138 |
+
# # 'classes': tf.ragged.constant([y_pred['classes'][y_pred['confidence'] != -1]]),
|
139 |
+
# # 'num_detections': np.array([np.count_nonzero(y_pred['confidence'] != -1)], dtype=np.int32)
|
140 |
+
# # }
|
141 |
+
# # print(y_pred)
|
142 |
+
# # visualization.plot_bounding_box_gallery(
|
143 |
+
# # images,
|
144 |
+
# # value_range=(0, 255),
|
145 |
+
# # bounding_box_format=bounding_box_format,
|
146 |
+
# # # y_true=y_true,
|
147 |
+
# # y_pred=y_pred,
|
148 |
+
# # scale=4,
|
149 |
+
# # rows=1,
|
150 |
+
# # cols=1,
|
151 |
+
# # show=True,
|
152 |
+
# # font_scale=0.7,
|
153 |
+
# # class_mapping=class_mapping,
|
154 |
+
# # )
|
main.py
ADDED
@@ -0,0 +1,42 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import cv2 as cv
|
2 |
+
import tensorflow as tf
|
3 |
+
from model import model
|
4 |
+
import numpy as np
|
5 |
+
from display import draw_bouding_box, add_padding
|
6 |
+
|
7 |
+
|
8 |
+
# Change the video file path & skip frame based on requirements
|
9 |
+
cam = cv.VideoCapture('smoker ban.mp4')
|
10 |
+
# Skip Frames
|
11 |
+
skip = 10
|
12 |
+
|
13 |
+
count = 1
|
14 |
+
while True:
|
15 |
+
ret, frame = cam.read()
|
16 |
+
if not ret:
|
17 |
+
break # Break the loop if no frame is returned
|
18 |
+
if count % skip:
|
19 |
+
count +=1
|
20 |
+
continue
|
21 |
+
count+=1
|
22 |
+
frame = cv.resize(frame, (640, 640), interpolation=cv.INTER_CUBIC)
|
23 |
+
image = cv.cvtColor(frame, cv.COLOR_BGR2RGB)
|
24 |
+
image = tf.expand_dims(image, axis=0)
|
25 |
+
y_pred = model.predict(image)
|
26 |
+
y_pred = {'boxes': tf.ragged.constant(y_pred['boxes'][y_pred['confidence'] != -1]),
|
27 |
+
'confidence': tf.ragged.constant(y_pred['confidence'][y_pred['confidence'] != -1]),
|
28 |
+
'classes': tf.ragged.constant(y_pred['classes'][y_pred['confidence'] != -1]),
|
29 |
+
'num_detections': np.count_nonzero(y_pred['confidence'] != -1)
|
30 |
+
}
|
31 |
+
|
32 |
+
frame = draw_bouding_box(frame, y_pred)
|
33 |
+
frame = add_padding(frame)
|
34 |
+
cv.imshow('Predications', frame)
|
35 |
+
|
36 |
+
|
37 |
+
|
38 |
+
if cv.waitKey(1) & 0xFF == ord('q'): # Press 'q' to quit
|
39 |
+
break
|
40 |
+
|
41 |
+
cam.release()
|
42 |
+
cv.destroyAllWindows()
|
model.py
ADDED
@@ -0,0 +1,16 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import keras_cv
|
2 |
+
import tensorflow as tf
|
3 |
+
|
4 |
+
|
5 |
+
backbone= keras_cv.models.YOLOV8Backbone.from_preset(
|
6 |
+
"yolo_v8_m_backbone_coco",
|
7 |
+
load_weights=False
|
8 |
+
)
|
9 |
+
model = keras_cv.models.YOLOV8Detector(
|
10 |
+
num_classes=2,
|
11 |
+
bounding_box_format="xyxy",
|
12 |
+
backbone=backbone,
|
13 |
+
fpn_depth=2,
|
14 |
+
)
|
15 |
+
|
16 |
+
model.load_weights('models/model_e15.weights.h5')
|
models/model.weights.h5
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:fa262f95f9fdae35c79adf17a5aeab26fed1794fe34fb2aefef4cd545091eecb
|
3 |
+
size 311454904
|
models/model_e15.weights.h5
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:cf4a2a9832d1a6df7005d0680db7cb07f928bb74c2f489905ed34d145caef54d
|
3 |
+
size 216520648
|
requirements.txt
ADDED
Binary file (3.84 kB). View file
|
|
smoker ban.mp4
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:e161ce6d8cb4fb2576fb991fcc05ff32815adfe4ceb0137c1586d3adf27a88e6
|
3 |
+
size 7120999
|
smokers.mp4
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:a902eab2bdbc4c0c7050a86e0a73401123b393845f1385716e93f1df5a24a2f1
|
3 |
+
size 52139492
|
thomos.mp4
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:2aa16accbe9a4139a8848c87bec34aef52a20ac4a3784639edf8ffbafa1bc5a2
|
3 |
+
size 3233073
|