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
9377d9b1
Commit
9377d9b1
authored
Jul 03, 2021
by
Methsarani
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add real time vehicle detection code
parent
1db2d2ab
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
89 additions
and
0 deletions
+89
-0
RealTimeVehicleDetection.py
RealTimeVehicleDetection.py
+89
-0
No files found.
RealTimeVehicleDetection.py
0 → 100644
View file @
9377d9b1
import
cv2
import
numpy
as
np
import
time
#Load YOLO
net
=
cv2
.
dnn
.
readNet
(
"yolov3.weights"
,
"yolov3.cfg"
)
# Original yolov3
#net = cv2.dnn.readNet("yolov3-tiny.weights","yolov3-tiny.cfg") #Tiny Yolo
classes
=
[]
with
open
(
"coco.names"
,
"r"
)
as
f
:
classes
=
[
line
.
strip
()
for
line
in
f
.
readlines
()]
print
(
classes
)
[
'person'
,
'bicycle'
,
'car'
,
'motorbike'
,
'aeroplane'
,
'bus'
,
'train'
,
'truck'
,
'boat'
,
'traffic light'
,
'fire hydrant'
,
'stop sign'
,
'parking meter'
,
'bench'
,
'bird'
,
'cat'
,
'dog'
,
'horse'
,
'sheep'
,
'cow'
,
'elephant'
,
'bear'
,
'zebra'
,
'giraffe'
,
'backpack'
,
'umbrella'
,
'handbag'
,
'tie'
,
'suitcase'
,
'frisbee'
,
'skis'
,
'snowboard'
,
'sports ball'
,
'kite'
,
'baseball bat'
,
'baseball glove'
,
'skateboard'
,
'surfboard'
,
'tennis racket'
,
'bottle'
,
'wine glass'
,
'cup'
,
'fork'
,
'knife'
,
'spoon'
,
'bowl'
,
'banana'
,
'apple'
,
'sandwich'
,
'orange'
,
'broccoli'
,
'carrot'
,
'hot dog'
,
'pizza'
,
'donut'
,
'cake'
,
'chair'
,
'sofa'
,
'pottedplant'
,
'bed'
,
'diningtable'
,
'toilet'
,
'tvmonitor'
,
'laptop'
,
'mouse'
,
'remote'
,
'keyboard'
,
'cell phone'
,
'microwave'
,
'oven'
,
'toaster'
,
'sink'
,
'refrigerator'
,
'book'
,
'clock'
,
'vase'
,
'scissors'
,
'teddy bear'
,
'hair drier'
,
'toothbrush'
]
layer_names
=
net
.
getLayerNames
()
outputlayers
=
[
layer_names
[
i
[
0
]
-
1
]
for
i
in
net
.
getUnconnectedOutLayers
()]
outputlayers
colors
=
np
.
random
.
uniform
(
0
,
255
,
size
=
(
len
(
classes
),
3
))
# loading image
cap
=
cv2
.
VideoCapture
(
"highway.mp4"
)
#0 for 1st webcam
#cap = cv2.VideoCapture(0)
font
=
cv2
.
FONT_HERSHEY_PLAIN
starting_time
=
time
.
time
()
frame_id
=
0
while
True
:
_
,
frame
=
cap
.
read
()
#
frame_id
+=
1
height
,
width
,
channels
=
frame
.
shape
# detecting objects
blob
=
cv2
.
dnn
.
blobFromImage
(
frame
,
0.00392
,
(
320
,
320
),
(
0
,
0
,
0
),
True
,
crop
=
False
)
# reduce 416 to 320
net
.
setInput
(
blob
)
outs
=
net
.
forward
(
outputlayers
)
# print(outs[1])
# Showing info on screen/ get confidence score of algorithm in detecting an object in blob
class_ids
=
[]
confidences
=
[]
boxes
=
[]
for
out
in
outs
:
for
detection
in
out
:
scores
=
detection
[
5
:]
class_id
=
np
.
argmax
(
scores
)
confidence
=
scores
[
class_id
]
if
confidence
>
0.3
:
# onject detected
center_x
=
int
(
detection
[
0
]
*
width
)
center_y
=
int
(
detection
[
1
]
*
height
)
w
=
int
(
detection
[
2
]
*
width
)
h
=
int
(
detection
[
3
]
*
height
)
# cv2.circle(img,(center_x,center_y),10,(0,255,0),2)
# rectangle co-ordinaters
x
=
int
(
center_x
-
w
/
2
)
y
=
int
(
center_y
-
h
/
2
)
# cv2.rectangle(img,(x,y),(x+w,y+h),(0,255,0),2)
boxes
.
append
([
x
,
y
,
w
,
h
])
# put all rectangle areas
confidences
.
append
(
float
(
confidence
))
# how confidence was that object detected and show that percentage
class_ids
.
append
(
class_id
)
# name of the object tha was detected
indexes
=
cv2
.
dnn
.
NMSBoxes
(
boxes
,
confidences
,
0.4
,
0.6
)
for
i
in
range
(
len
(
boxes
)):
if
i
in
indexes
:
x
,
y
,
w
,
h
=
boxes
[
i
]
label
=
str
(
classes
[
class_ids
[
i
]])
confidence
=
confidences
[
i
]
color
=
colors
[
class_ids
[
i
]]
cv2
.
rectangle
(
frame
,
(
x
,
y
),
(
x
+
w
,
y
+
h
),
color
,
2
)
cv2
.
putText
(
frame
,
label
+
" "
+
str
(
round
(
confidence
,
2
)),
(
x
,
y
+
30
),
font
,
1
,
(
255
,
255
,
255
),
2
)
elapsed_time
=
time
.
time
()
-
starting_time
fps
=
frame_id
/
elapsed_time
cv2
.
putText
(
frame
,
"FPS:"
+
str
(
round
(
fps
,
2
)),
(
10
,
50
),
font
,
2
,
(
0
,
0
,
0
),
1
)
cv2
.
imshow
(
"Image"
,
frame
)
key
=
cv2
.
waitKey
(
2
)
# wait 1ms the loop will start again and we will process the next frame
if
key
==
27
:
# esc key stops the process
break
;
cap
.
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