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
aaa4ba43
Commit
aaa4ba43
authored
Jul 05, 2021
by
Dilitha A.G.A.D.
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add person tracking and dwell time calculation
parent
27e939a1
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
154 additions
and
0 deletions
+154
-0
personDetection/main.py
personDetection/main.py
+154
-0
No files found.
personDetection/main.py
0 → 100644
View file @
aaa4ba43
import
cv2
import
datetime
import
imutils
import
numpy
as
np
from
centroidtracker
import
CentroidTracker
protopath
=
"MobileNetSSD_deploy.prototxt"
modelpath
=
"MobileNetSSD_deploy.caffemodel"
detector
=
cv2
.
dnn
.
readNetFromCaffe
(
prototxt
=
protopath
,
caffeModel
=
modelpath
)
# for using OpenVino environment
# detector.setPreferableBackend(cv2.dnn.DNN_BACKEND_INFERENCE_ENGINE)
# detector.setPreferableTarget(cv2.dnn.DNN_TARGET_CPU)
CLASSES
=
[
"background"
,
"aeroplane"
,
"bicycle"
,
"bird"
,
"boat"
,
"bottle"
,
"bus"
,
"car"
,
"cat"
,
"chair"
,
"cow"
,
"diningtable"
,
"dog"
,
"horse"
,
"motorbike"
,
"person"
,
"pottedplant"
,
"sheep"
,
"sofa"
,
"train"
,
"tvmonitor"
]
tracker
=
CentroidTracker
(
maxDisappeared
=
80
,
maxDistance
=
90
)
def
non_max_suppression_fast
(
boxes
,
overlapThresh
):
try
:
if
len
(
boxes
)
==
0
:
return
[]
if
boxes
.
dtype
.
kind
==
"i"
:
boxes
=
boxes
.
astype
(
"float"
)
pick
=
[]
x1
=
boxes
[:,
0
]
y1
=
boxes
[:,
1
]
x2
=
boxes
[:,
2
]
y2
=
boxes
[:,
3
]
area
=
(
x2
-
x1
+
1
)
*
(
y2
-
y1
+
1
)
idxs
=
np
.
argsort
(
y2
)
while
len
(
idxs
)
>
0
:
last
=
len
(
idxs
)
-
1
i
=
idxs
[
last
]
pick
.
append
(
i
)
xx1
=
np
.
maximum
(
x1
[
i
],
x1
[
idxs
[:
last
]])
yy1
=
np
.
maximum
(
y1
[
i
],
y1
[
idxs
[:
last
]])
xx2
=
np
.
minimum
(
x2
[
i
],
x2
[
idxs
[:
last
]])
yy2
=
np
.
minimum
(
y2
[
i
],
y2
[
idxs
[:
last
]])
w
=
np
.
maximum
(
0
,
xx2
-
xx1
+
1
)
h
=
np
.
maximum
(
0
,
yy2
-
yy1
+
1
)
overlap
=
(
w
*
h
)
/
area
[
idxs
[:
last
]]
idxs
=
np
.
delete
(
idxs
,
np
.
concatenate
(([
last
],
np
.
where
(
overlap
>
overlapThresh
)[
0
])))
return
boxes
[
pick
]
.
astype
(
"int"
)
except
Exception
as
e
:
print
(
"Exception occurred in non_max_suppression : {}"
.
format
(
e
))
def
main
():
cap
=
cv2
.
VideoCapture
(
'test_video.mp4'
)
text
=
""
fps_start_time
=
datetime
.
datetime
.
now
()
fps
=
0
total_frames
=
0
object_id_list
=
[]
dtime
=
dict
()
dwell_time
=
dict
()
while
True
:
ret
,
frame
=
cap
.
read
()
frame
=
imutils
.
resize
(
frame
,
width
=
600
)
total_frames
=
total_frames
+
1
(
H
,
W
)
=
frame
.
shape
[:
2
]
blob
=
cv2
.
dnn
.
blobFromImage
(
frame
,
0.007843
,
(
W
,
H
),
127.5
)
detector
.
setInput
(
blob
)
person_detections
=
detector
.
forward
()
rects
=
[]
for
i
in
np
.
arange
(
0
,
person_detections
.
shape
[
2
]):
confidence
=
person_detections
[
0
,
0
,
i
,
2
]
if
confidence
>
0.5
:
idx
=
int
(
person_detections
[
0
,
0
,
i
,
1
])
if
CLASSES
[
idx
]
!=
"person"
:
continue
person_box
=
person_detections
[
0
,
0
,
i
,
3
:
7
]
*
np
.
array
([
W
,
H
,
W
,
H
])
(
startX
,
startY
,
endX
,
endY
)
=
person_box
.
astype
(
"int"
)
rects
.
append
(
person_box
)
boundingboxes
=
np
.
array
(
rects
)
boundingboxes
=
boundingboxes
.
astype
(
int
)
rects
=
non_max_suppression_fast
(
boundingboxes
,
0.3
)
objects
=
tracker
.
update
(
rects
)
for
(
objectId
,
bbox
)
in
objects
.
items
():
x1
,
y1
,
x2
,
y2
=
bbox
x1
=
int
(
x1
)
y1
=
int
(
y1
)
x2
=
int
(
x2
)
y2
=
int
(
y2
)
if
objectId
not
in
object_id_list
:
object_id_list
.
append
(
objectId
)
dtime
[
objectId
]
=
datetime
.
datetime
.
now
()
dwell_time
[
objectId
]
=
0
else
:
curr_time
=
datetime
.
datetime
.
now
()
old_time
=
dtime
[
objectId
]
time_diff
=
curr_time
-
old_time
dtime
[
objectId
]
=
datetime
.
datetime
.
now
()
sec
=
time_diff
.
total_seconds
()
dwell_time
[
objectId
]
+=
sec
cv2
.
rectangle
(
frame
,
(
x1
,
y1
),
(
x2
,
y2
),
(
0
,
0
,
255
),
2
)
text
=
"{}|{}"
.
format
(
objectId
,
int
(
dwell_time
[
objectId
]))
cv2
.
putText
(
frame
,
text
,
(
x1
,
y1
-
5
),
cv2
.
FONT_HERSHEY_COMPLEX_SMALL
,
1
,
(
0
,
0
,
255
),
1
)
if
int
(
dwell_time
[
objectId
]
>
10
):
print
(
"Warning"
)
fps_end_time
=
datetime
.
datetime
.
now
()
time_diff
=
fps_end_time
-
fps_start_time
if
time_diff
.
seconds
==
0
:
fps
=
0.0
else
:
fps
=
(
total_frames
/
time_diff
.
seconds
)
fps_text
=
"FPS: {:.2f}"
.
format
(
fps
)
cv2
.
putText
(
frame
,
fps_text
,
(
5
,
30
),
cv2
.
FONT_HERSHEY_COMPLEX_SMALL
,
1
,
(
0
,
0
,
255
),
1
)
cv2
.
imshow
(
"Application"
,
frame
)
# print("Detected" + text)
key
=
cv2
.
waitKey
(
1
)
if
key
==
ord
(
'q'
):
break
cv2
.
destroyAllWindows
()
main
()
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