Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
2
22_23-J-84
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
KSH.Mohamed
22_23-J-84
Commits
a96868c5
Commit
a96868c5
authored
May 26, 2023
by
Bogahawatta L.B.G.D.P.K
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Upload New File
parent
e8617466
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
81 additions
and
0 deletions
+81
-0
Vehicle_Classification.py
Vehicle_Classification.py
+81
-0
No files found.
Vehicle_Classification.py
0 → 100644
View file @
a96868c5
import
cv2
import
numpy
as
np
import
imutils
from
imutils.video
import
WebcamVideoStream
from
imutils.video
import
FPS
class
ObjectDetection
:
def
__init__
(
self
):
self
.
model
=
"MobileNetSSD_deploy.caffemodel"
self
.
prototxt
=
"MobileNetSSD_deploy.prototxt.txt"
self
.
net
=
cv2
.
dnn
.
readNetFromCaffe
(
self
.
prototxt
,
self
.
model
)
def
define_classes
(
self
):
"""Define classes and associates colors randomly"""
self
.
classes
=
[
"background"
,
"aeroplane"
,
"bicycle"
,
"bird"
,
"boat"
,
"bottle"
,
"bus"
,
"car"
,
"cat"
,
"chair"
,
"cow"
,
"diningtable"
,
"dog"
,
"horse"
,
"motorbike"
,
"person"
,
"pottedplant"
,
"sheep"
,
"sofa"
,
"train"
,
"tvmonitor"
]
self
.
colors
=
np
.
random
.
uniform
(
0
,
255
,
size
=
(
len
(
self
.
classes
),
3
))
def
run_detection
(
self
):
print
(
"Start object detection ..."
)
self
.
define_classes
()
vs
=
WebcamVideoStream
(
0
)
.
start
()
fps
=
FPS
()
.
start
()
while
True
:
frame
=
vs
.
read
()
(
h
,
w
)
=
frame
.
shape
[:
-
1
]
blob
=
cv2
.
dnn
.
blobFromImage
(
cv2
.
resize
(
frame
,
(
300
,
300
)),
scalefactor
=
0.007843
,
size
=
(
300
,
300
),
mean
=
127.5
)
self
.
net
.
setInput
(
blob
)
detections
=
self
.
net
.
forward
()
# Loop over detected objects
for
i
in
np
.
arange
(
0
,
detections
.
shape
[
2
]):
# Get proba for each object
probability
=
detections
[
0
,
0
,
i
,
2
]
# Set up threshold for filtering detection
if
probability
>
0.5
:
# Get prediction index
index
=
int
(
detections
[
0
,
0
,
i
,
1
])
# Get bounding box coordinates
box
=
detections
[
0
,
0
,
i
,
3
:
7
]
*
np
.
array
([
w
,
h
,
w
,
h
])
(
startX
,
startY
,
endX
,
endY
)
=
box
.
astype
(
"int"
)
label
=
"{}: {:.2f}
%
"
.
format
(
self
.
classes
[
index
],
probability
*
100
)
y
=
startY
-
10
if
startY
-
10
>
10
else
startY
+
10
cv2
.
rectangle
(
frame
,
(
startX
,
startY
),
(
endX
,
endY
),
self
.
colors
[
index
],
2
)
cv2
.
putText
(
frame
,
label
,
(
startX
,
y
),
cv2
.
FONT_HERSHEY_SIMPLEX
,
0.5
,
self
.
colors
[
index
],
2
)
cv2
.
imshow
(
'frame'
,
frame
)
fps
.
update
()
key
=
cv2
.
waitKey
(
1
)
&
0xFF
if
key
==
ord
(
"q"
):
cv2
.
destroyAllWindows
()
vs
.
stop
()
fps
.
stop
()
break
print
(
"Fps: {:.2f}"
.
format
(
fps
.
fps
()))
fps
.
update
()
if
__name__
==
'__main__'
:
detector
=
ObjectDetection
()
detector
.
run_detection
()
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