Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
S
Secure smart parking solution
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
2021-122
Secure smart parking solution
Commits
277cddf4
Commit
277cddf4
authored
Jul 03, 2021
by
kaveena
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add Detect Parking Slot main.py file
parent
caf587a5
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
147 additions
and
0 deletions
+147
-0
DetectParkingSlot/main.py
DetectParkingSlot/main.py
+147
-0
No files found.
DetectParkingSlot/main.py
0 → 100644
View file @
277cddf4
import
yaml
import
numpy
as
np
import
cv2
fn
=
r"./datasets/parkinglot_1_480p.mp4"
fn_yaml
=
r"./datasets/parking2.yml"
fn_out
=
r"./datasets/output.avi"
config
=
{
'save_video'
:
False
,
'text_overlay'
:
True
,
'parking_overlay'
:
True
,
'parking_id_overlay'
:
False
,
'parking_detection'
:
True
,
'min_area_motion_contour'
:
60
,
'park_sec_to_wait'
:
3
,
'start_frame'
:
0
}
cap
=
cv2
.
VideoCapture
(
fn
)
video_info
=
{
'fps'
:
cap
.
get
(
cv2
.
CAP_PROP_FPS
),
'width'
:
int
(
cap
.
get
(
cv2
.
CAP_PROP_FRAME_WIDTH
)),
'height'
:
int
(
cap
.
get
(
cv2
.
CAP_PROP_FRAME_HEIGHT
)),
'fourcc'
:
cap
.
get
(
cv2
.
CAP_PROP_FOURCC
),
'num_of_frames'
:
int
(
cap
.
get
(
cv2
.
CAP_PROP_FRAME_COUNT
))}
cap
.
set
(
cv2
.
CAP_PROP_POS_FRAMES
,
config
[
'start_frame'
])
if
config
[
'save_video'
]:
fourcc
=
cv2
.
VideoWriter_fourcc
(
'D'
,
'I'
,
'V'
,
'X'
)
out
=
cv2
.
VideoWriter
(
fn_out
,
-
1
,
25.0
,
(
video_info
[
'width'
],
video_info
[
'height'
]))
with
open
(
fn_yaml
,
'r'
)
as
stream
:
parking_data
=
yaml
.
load
(
stream
)
parking_contours
=
[]
parking_bounding_rects
=
[]
parking_mask
=
[]
for
park
in
parking_data
:
points
=
np
.
array
(
park
[
'points'
])
rect
=
cv2
.
boundingRect
(
points
)
points_shifted
=
points
.
copy
()
points_shifted
[:,
0
]
=
points
[:,
0
]
-
rect
[
0
]
points_shifted
[:,
1
]
=
points
[:,
1
]
-
rect
[
1
]
parking_contours
.
append
(
points
)
parking_bounding_rects
.
append
(
rect
)
mask
=
cv2
.
drawContours
(
np
.
zeros
((
rect
[
3
],
rect
[
2
]),
dtype
=
np
.
uint8
),
[
points_shifted
],
contourIdx
=-
1
,
color
=
255
,
thickness
=-
1
,
lineType
=
cv2
.
LINE_8
)
mask
=
mask
==
255
parking_mask
.
append
(
mask
)
parking_status
=
[
False
]
*
len
(
parking_data
)
parking_buffer
=
[
None
]
*
len
(
parking_data
)
while
(
cap
.
isOpened
()):
spot
=
0
occupied
=
0
video_cur_pos
=
cap
.
get
(
cv2
.
CAP_PROP_POS_MSEC
)
/
1000.0
video_cur_frame
=
cap
.
get
(
cv2
.
CAP_PROP_POS_FRAMES
)
ret
,
frame
=
cap
.
read
()
if
ret
==
False
:
print
(
"Capture Error"
)
break
# frame_gray = cv2.cvtColor(frame.copy(), cv2.COLOR_BGR2GRAY)
frame_blur
=
cv2
.
GaussianBlur
(
frame
.
copy
(),
(
5
,
5
),
3
)
frame_gray
=
cv2
.
cvtColor
(
frame_blur
,
cv2
.
COLOR_BGR2GRAY
)
frame_out
=
frame
.
copy
()
if
config
[
'parking_detection'
]:
for
ind
,
park
in
enumerate
(
parking_data
):
points
=
np
.
array
(
park
[
'points'
])
rect
=
parking_bounding_rects
[
ind
]
roi_gray
=
frame_gray
[
rect
[
1
]:(
rect
[
1
]
+
rect
[
3
]),
rect
[
0
]:(
rect
[
0
]
+
rect
[
2
])]
# print np.std(roi_gray)
points
[:,
0
]
=
points
[:,
0
]
-
rect
[
0
]
points
[:,
1
]
=
points
[:,
1
]
-
rect
[
1
]
# print np.std(roi_gray), np.mean(roi_gray)
status
=
np
.
std
(
roi_gray
)
<
22
and
np
.
mean
(
roi_gray
)
>
53
if
status
!=
parking_status
[
ind
]
and
parking_buffer
[
ind
]
==
None
:
parking_buffer
[
ind
]
=
video_cur_pos
elif
status
!=
parking_status
[
ind
]
and
parking_buffer
[
ind
]
!=
None
:
if
video_cur_pos
-
parking_buffer
[
ind
]
>
config
[
'park_sec_to_wait'
]:
parking_status
[
ind
]
=
status
parking_buffer
[
ind
]
=
None
elif
status
==
parking_status
[
ind
]
and
parking_buffer
[
ind
]
!=
None
:
#if video_cur_pos - parking_buffer[ind] > config['park_sec_to_wait']:
parking_buffer
[
ind
]
=
None
# print(parking_status)
if
config
[
'parking_overlay'
]:
for
ind
,
park
in
enumerate
(
parking_data
):
points
=
np
.
array
(
park
[
'points'
])
if
parking_status
[
ind
]:
color
=
(
0
,
255
,
0
)
spot
=
spot
+
1
else
:
color
=
(
0
,
0
,
255
)
occupied
=
occupied
+
1
cv2
.
drawContours
(
frame_out
,
[
points
],
contourIdx
=-
1
,
color
=
color
,
thickness
=
2
,
lineType
=
cv2
.
LINE_8
)
moments
=
cv2
.
moments
(
points
)
centroid
=
(
int
(
moments
[
'm10'
]
/
moments
[
'm00'
])
-
3
,
int
(
moments
[
'm01'
]
/
moments
[
'm00'
])
+
3
)
cv2
.
putText
(
frame_out
,
str
(
park
[
'id'
]),
(
centroid
[
0
]
+
1
,
centroid
[
1
]
+
1
),
cv2
.
FONT_HERSHEY_SIMPLEX
,
0.4
,
(
255
,
255
,
255
),
1
,
cv2
.
LINE_AA
)
cv2
.
putText
(
frame_out
,
str
(
park
[
'id'
]),
(
centroid
[
0
]
-
1
,
centroid
[
1
]
-
1
),
cv2
.
FONT_HERSHEY_SIMPLEX
,
0.4
,
(
255
,
255
,
255
),
1
,
cv2
.
LINE_AA
)
cv2
.
putText
(
frame_out
,
str
(
park
[
'id'
]),
(
centroid
[
0
]
+
1
,
centroid
[
1
]
-
1
),
cv2
.
FONT_HERSHEY_SIMPLEX
,
0.4
,
(
255
,
255
,
255
),
1
,
cv2
.
LINE_AA
)
cv2
.
putText
(
frame_out
,
str
(
park
[
'id'
]),
(
centroid
[
0
]
-
1
,
centroid
[
1
]
+
1
),
cv2
.
FONT_HERSHEY_SIMPLEX
,
0.4
,
(
255
,
255
,
255
),
1
,
cv2
.
LINE_AA
)
cv2
.
putText
(
frame_out
,
str
(
park
[
'id'
]),
centroid
,
cv2
.
FONT_HERSHEY_SIMPLEX
,
0.4
,
(
0
,
0
,
0
),
1
,
cv2
.
LINE_AA
)
# print 'occupied: ', occupied
# print 'spot: ', spot
# Draw Overlay
# if config['text_overlay']:
# cv2.rectangle(frame_out, (1, 5), (280, 90),(255,255,255), 85)
# str_on_frame = "Frames: %d/%d" % (video_cur_frame, video_info['num_of_frames'])
# cv2.putText(frame_out, str_on_frame, (5,30), cv2.FONT_HERSHEY_SCRIPT_COMPLEX,
# 0.7, (0,128,255), 2, cv2.LINE_AA)
# str_on_frame = "Spot: %d Occupied: %d" % (spot, occupied)
# cv2.putText(frame_out, str_on_frame, (5,90), cv2.FONT_HERSHEY_SCRIPT_COMPLEX,
# 0.7, (0,128,255), 2, cv2.LINE_AA)
if
config
[
'save_video'
]:
if
video_cur_frame
%
35
==
0
:
out
.
write
(
frame_out
)
cv2
.
imshow
(
'Detecção de Vagas de Estacionamento'
,
frame_out
)
cv2
.
waitKey
(
40
)
# cv2.imshow('background mask', bw)
k
=
cv2
.
waitKey
(
1
)
if
k
==
ord
(
'q'
):
break
elif
k
==
ord
(
'c'
):
cv2
.
imwrite
(
'frame
%
d.jpg'
%
video_cur_frame
,
frame_out
)
elif
k
==
ord
(
'j'
):
cap
.
set
(
cv2
.
CAP_PROP_POS_FRAMES
,
video_cur_frame
+
1000
)
cap
.
release
()
if
config
[
'save_video'
]:
out
.
release
()
cv2
.
destroyAllWindows
()
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment