Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -3,16 +3,21 @@ import cv2
|
|
3 |
import numpy as np
|
4 |
import moviepy.editor as moviepy
|
5 |
|
6 |
-
st.
|
7 |
-
|
8 |
-
|
9 |
-
|
|
|
|
|
|
|
10 |
|
11 |
min_width_rect = 80
|
12 |
min_height_rect = 80
|
13 |
count_line_position = 550
|
14 |
algo = cv2.bgsegm.createBackgroundSubtractorMOG()
|
15 |
-
|
|
|
|
|
16 |
|
17 |
def center_point(x, y, w, h):
|
18 |
x1 = int(w/2)
|
@@ -21,95 +26,90 @@ def center_point(x, y, w, h):
|
|
21 |
cy = y+y1
|
22 |
return cx,cy
|
23 |
|
24 |
-
|
25 |
-
detect = []
|
26 |
-
offset = 8
|
27 |
-
counter = 0
|
28 |
-
video_data = st.file_uploader("Masukkan Video", ['mp4','mov', 'avi'])
|
29 |
-
|
30 |
temp_file_to_save = './temp_file_1.mp4'
|
31 |
temp_file_result = './temp_file_2.mp4'
|
|
|
32 |
|
33 |
-
# func to save BytesIO on a drive
|
34 |
def write_bytesio_to_file(filename, bytesio):
|
35 |
-
"""
|
36 |
-
Write the contents of the given BytesIO to a file.
|
37 |
-
Creates the file or overwrites the file if it does
|
38 |
-
not exist yet.
|
39 |
-
"""
|
40 |
with open(filename, "wb") as outfile:
|
41 |
-
# Copy the BytesIO stream to the output file
|
42 |
outfile.write(bytesio.getbuffer())
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
fourcc_mp4 = cv2.VideoWriter_fourcc(*'mp4v')
|
60 |
-
out_mp4 = cv2.VideoWriter(temp_file_result, fourcc_mp4, frame_fps, (width, height),isColor = True)
|
61 |
-
|
62 |
-
while True:
|
63 |
-
ret,frame1 = cap.read()
|
64 |
-
if not ret: break
|
65 |
-
grey = cv2.cvtColor(frame1, cv2.COLOR_BGR2GRAY)
|
66 |
-
blur = cv2.GaussianBlur(grey, (3, 3), 5)
|
67 |
-
img_sub = algo.apply(blur)
|
68 |
-
dilat = cv2.dilate(img_sub, np.ones((5, 5)))
|
69 |
-
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (5, 5))
|
70 |
-
dilatada = cv2.morphologyEx(dilat, cv2.MORPH_CLOSE, kernel)
|
71 |
-
dilatada = cv2.morphologyEx(dilatada, cv2.MORPH_CLOSE, kernel)
|
72 |
-
counterShape, h = cv2.findContours(dilatada, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
|
73 |
-
|
74 |
-
cv2.line(frame1, (25, count_line_position), (1250, count_line_position), (0, 0, 0), 4)
|
75 |
-
|
76 |
-
for (i, c) in enumerate(counterShape) :
|
77 |
-
(x, y, w, h) = cv2.boundingRect(c)
|
78 |
-
validate_counter = (w>= min_width_rect) and (h>= min_height_rect)
|
79 |
-
if not validate_counter :
|
80 |
-
continue
|
81 |
-
|
82 |
-
cv2.rectangle(frame1, (x, y), (x+w, y+h), (0, 0, 255), 2)
|
83 |
-
|
84 |
-
center = center_point(x, y, w, h)
|
85 |
-
detect.append(center)
|
86 |
-
cv2.circle(frame1, center, 4, (0, 255, 0), -1)
|
87 |
-
|
88 |
-
|
89 |
-
for (x, y) in detect :
|
90 |
-
if y<(count_line_position+offset) and y>(count_line_position-offset) :
|
91 |
-
counter += 1
|
92 |
-
cv2.line(frame1, (25, count_line_position), (1250, count_line_position), (255, 255, 255), 4)
|
93 |
-
print("Jumlah Kendaraan : " + str(counter))
|
94 |
-
|
95 |
-
detect.remove((x,y))
|
96 |
-
cv2.putText(frame1,"Jumlah Kendaraan : " + str(counter), (450, 70), cv2.FONT_HERSHEY_SIMPLEX, 2, (0, 0, 255), 5)
|
97 |
-
out_mp4.write(frame1)
|
98 |
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
3 |
import numpy as np
|
4 |
import moviepy.editor as moviepy
|
5 |
|
6 |
+
st.set_page_config(
|
7 |
+
page_title="Pendeteksi Kendaraan",
|
8 |
+
page_icon="./car.ico",
|
9 |
+
layout="wide"
|
10 |
+
)
|
11 |
+
st.title("PENDETEKSI KENDARAAN")
|
12 |
+
st.header("Sistem Pendeteksi Objek Yang Melintas Pada Jalan Raya")
|
13 |
|
14 |
min_width_rect = 80
|
15 |
min_height_rect = 80
|
16 |
count_line_position = 550
|
17 |
algo = cv2.bgsegm.createBackgroundSubtractorMOG()
|
18 |
+
detect = []
|
19 |
+
offset = 8
|
20 |
+
counter = 0
|
21 |
|
22 |
def center_point(x, y, w, h):
|
23 |
x1 = int(w/2)
|
|
|
26 |
cy = y+y1
|
27 |
return cx,cy
|
28 |
|
29 |
+
video_data = st.file_uploader("Masukkan Video", ['mp4'])
|
|
|
|
|
|
|
|
|
|
|
30 |
temp_file_to_save = './temp_file_1.mp4'
|
31 |
temp_file_result = './temp_file_2.mp4'
|
32 |
+
temp_file_detect = './temp_file_3.mp4'
|
33 |
|
|
|
34 |
def write_bytesio_to_file(filename, bytesio):
|
|
|
|
|
|
|
|
|
|
|
35 |
with open(filename, "wb") as outfile:
|
|
|
36 |
outfile.write(bytesio.getbuffer())
|
37 |
+
st.balloons()
|
38 |
+
|
39 |
+
with st.spinner("Loading..."):
|
40 |
+
if video_data:
|
41 |
+
write_bytesio_to_file(temp_file_to_save, video_data)
|
42 |
+
|
43 |
+
cap = cv2.VideoCapture(temp_file_to_save)
|
44 |
+
|
45 |
+
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
|
46 |
+
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
|
47 |
+
frame_fps = cap.get(cv2.CAP_PROP_FPS)
|
48 |
+
st.write(width, height, frame_fps)
|
49 |
+
|
50 |
+
fourcc_mp4 = cv2.VideoWriter_fourcc(*'mp4v')
|
51 |
+
out_mp4 = cv2.VideoWriter(temp_file_result, fourcc_mp4, frame_fps, (width, height),isColor = True)
|
52 |
+
out_mp4_2 = cv2.VideoWriter(temp_file_detect, fourcc_mp4, frame_fps, (width, height),isColor = False)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
53 |
|
54 |
+
while True:
|
55 |
+
ret,frame1 = cap.read()
|
56 |
+
if not ret: break
|
57 |
+
grey = cv2.cvtColor(frame1, cv2.COLOR_BGR2GRAY)
|
58 |
+
blur = cv2.GaussianBlur(grey, (3, 3), 5)
|
59 |
+
img_sub = algo.apply(blur)
|
60 |
+
dilat = cv2.dilate(img_sub, np.ones((5, 5)))
|
61 |
+
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (5, 5))
|
62 |
+
dilatada = cv2.morphologyEx(dilat, cv2.MORPH_CLOSE, kernel)
|
63 |
+
dilatada = cv2.morphologyEx(dilatada, cv2.MORPH_CLOSE, kernel)
|
64 |
+
counterShape, h = cv2.findContours(dilatada, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
|
65 |
+
|
66 |
+
|
67 |
+
cv2.line(frame1, (25, count_line_position), (1250, count_line_position), (0, 0, 0), 4)
|
68 |
+
|
69 |
+
for (i, c) in enumerate(counterShape) :
|
70 |
+
(x, y, w, h) = cv2.boundingRect(c)
|
71 |
+
validate_counter = (w>= min_width_rect) and (h>= min_height_rect)
|
72 |
+
if not validate_counter :
|
73 |
+
continue
|
74 |
+
|
75 |
+
cv2.rectangle(frame1, (x, y), (x+w, y+h), (0, 0, 255), 2)
|
76 |
+
|
77 |
+
center = center_point(x, y, w, h)
|
78 |
+
detect.append(center)
|
79 |
+
cv2.circle(frame1, center, 4, (0, 255, 0), -1)
|
80 |
+
|
81 |
+
|
82 |
+
for (x, y) in detect :
|
83 |
+
if y<(count_line_position+offset) and y>(count_line_position-offset) :
|
84 |
+
counter += 1
|
85 |
+
cv2.line(frame1, (25, count_line_position), (1250, count_line_position), (255, 255, 255), 4)
|
86 |
+
print("Jumlah Kendaraan : " + str(counter))
|
87 |
+
|
88 |
+
detect.remove((x,y))
|
89 |
+
cv2.putText(frame1,"Jumlah Kendaraan : " + str(counter), (450, 70), cv2.FONT_HERSHEY_SIMPLEX, 2, (0, 0, 255), 5)
|
90 |
+
out_mp4.write(frame1)
|
91 |
+
out_mp4_2.write(dilatada)
|
92 |
+
|
93 |
+
out_mp4.release()
|
94 |
+
out_mp4_2.release()
|
95 |
+
cap.release()
|
96 |
+
|
97 |
+
convertedVideo = "./resulth264.mp4"
|
98 |
+
detectedVideo = "./detectorh264.mp4"
|
99 |
+
clip1 = moviepy.VideoFileClip(temp_file_detect)
|
100 |
+
clip1.write_videofile(detectedVideo)
|
101 |
+
clip = moviepy.VideoFileClip(temp_file_result)
|
102 |
+
clip.write_videofile(convertedVideo)
|
103 |
+
st.snow()
|
104 |
+
|
105 |
+
|
106 |
+
|
107 |
+
|
108 |
+
st.success("Berhasil Terdeteksi!", icon="✅")
|
109 |
+
col1,col2,col3 = st.columns(3)
|
110 |
+
col1.header("Video Asli")
|
111 |
+
col1.video(temp_file_to_save)
|
112 |
+
col2.header("Pendeteksi")
|
113 |
+
col2.video(detectedVideo)
|
114 |
+
col3.header("Hasil")
|
115 |
+
col3.video(convertedVideo)
|