Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
2
2021-208
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-208
2021-208
Commits
4d13b399
Commit
4d13b399
authored
Sep 29, 2021
by
Givindu
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
V1.16
parent
9a116a1a
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
144 additions
and
0 deletions
+144
-0
abnormal_behavior_detection/WeaponDetection/Knife_Detection.py
...mal_behavior_detection/WeaponDetection/Knife_Detection.py
+144
-0
No files found.
abnormal_behavior_detection/WeaponDetection/Knife_Detection.py
0 → 100644
View file @
4d13b399
import
csv
import
glob
import
os
import
time
import
pandas
as
pd
import
mysql.connector
as
msql
from
mysql.connector
import
Error
import
cv2
import
cv2
as
cv
import
numpy
as
np
import
imutils
count
=
1
start
=
time
.
time
()
def
weaponDetection
():
cap
=
cv
.
VideoCapture
(
'E:
\\
BACKBONE
\\
abnormal_behavior_detection
\\
frames
\\
Generated Video Output
\\
Output Video.avi'
)
whT
=
320
confThreshold
=
0.1
nmsThreshold
=
0.1
directory
=
'Suspects with Weapons'
parent_dir
=
"E:/BACKBONE/abnormal_behavior_detection/frames/"
path
=
os
.
path
.
join
(
parent_dir
,
directory
)
os
.
mkdir
(
path
)
classesFile
=
"E:/BACKBONE/abnormal_behavior_detection/Models/Knife Detection/obj.names"
classNames
=
[]
with
open
(
classesFile
,
'rt'
)
as
f
:
classNames
=
f
.
read
()
.
rstrip
(
'
\n
'
)
.
split
(
'
\n
'
)
print
(
" "
)
print
(
"STEP 3 ON PROGRESS..."
)
## Model Files
modelConfiguration
=
"E:/BACKBONE/abnormal_behavior_detection/Models/Knife Detection/yolov4-custom.cfg"
modelWeights
=
"E:/BACKBONE/abnormal_behavior_detection/Models/Knife Detection/yolov4-custom_last.weights"
net
=
cv
.
dnn
.
readNetFromDarknet
(
modelConfiguration
,
modelWeights
)
net
.
setPreferableBackend
(
cv
.
dnn
.
DNN_BACKEND_OPENCV
)
net
.
setPreferableTarget
(
cv
.
dnn
.
DNN_TARGET_CPU
)
def
findObjects
(
outputs
,
img
):
hT
,
wT
,
cT
=
img
.
shape
bbox
=
[]
classIds
=
[]
confs
=
[]
global
count
Num
=
0
field
=
[
"ID"
,
"Incident Type"
,
"Used Weapon"
,
"Time"
]
for
output
in
outputs
:
for
det
in
output
:
scores
=
det
[
5
:]
classId
=
np
.
argmax
(
scores
)
confidence
=
scores
[
classId
]
if
confidence
>
confThreshold
:
w
,
h
=
int
(
det
[
2
]
*
wT
),
int
(
det
[
3
]
*
hT
)
x
,
y
=
int
((
det
[
0
]
*
wT
)
-
w
/
2
),
int
((
det
[
1
]
*
hT
)
-
h
/
2
)
bbox
.
append
([
x
,
y
,
w
,
h
])
classIds
.
append
(
classId
)
confs
.
append
(
float
(
confidence
))
indices
=
cv
.
dnn
.
NMSBoxes
(
bbox
,
confs
,
confThreshold
,
nmsThreshold
)
for
i
in
indices
:
i
=
i
[
0
]
box
=
bbox
[
i
]
x
,
y
,
w
,
h
=
box
[
0
],
box
[
1
],
box
[
2
],
box
[
3
]
if
classIds
[
i
]
==
1
:
# print(x,y,w,h)
text
=
"{:.4f}
%
"
.
format
((
confs
[
i
])
*
100
)
print
(
"Suspect Detected : "
,
"["
,
text
,
"]"
)
#cv.rectangle(img, (x, y), (x + w, y + h), (255, 0, 255), 2)
#cv.putText(img, f'{classNames[classIds[i]].upper()} {int(confs[i] * 100)}%',
# (x, y - 10), cv.FONT_HERSHEY_SIMPLEX, 0.6, (255, 0, 255), 2)
cv2
.
imwrite
(
'E:/BACKBONE/abnormal_behavior_detection/frames/Suspects with Weapons/frame
%
d.jpg'
%
count
,
img
)
count
+=
1
# Create CSV File
type
=
"Stabbing Attack"
weapon
=
"Knife used as the weapon"
with
open
(
'E:/BACKBONE/abnormal_behavior_detection/CSV/Abnormal Activity.csv'
,
'w'
,
newline
=
''
)
as
file
:
Num
+=
1
TimeD
=
time
.
time
()
-
start
arr
=
[
Num
,
type
,
weapon
,
TimeD
]
writer
=
csv
.
writer
(
file
)
writer
.
writerow
(
field
)
writer
.
writerow
(
arr
)
while
True
:
success
,
img
=
cap
.
read
()
if
success
!=
False
:
img
=
imutils
.
resize
(
img
,
width
=
600
)
blob
=
cv
.
dnn
.
blobFromImage
(
img
,
1
/
255
,
(
whT
,
whT
),
[
0
,
0
,
0
],
1
,
crop
=
False
)
net
.
setInput
(
blob
)
layersNames
=
net
.
getLayerNames
()
outputNames
=
[(
layersNames
[
i
[
0
]
-
1
])
for
i
in
net
.
getUnconnectedOutLayers
()]
outputs
=
net
.
forward
(
outputNames
)
findObjects
(
outputs
,
img
)
#cv.imshow('Image', img)
#key = cv2.waitKey(1)
#if key == ord('q'):
# break
else
:
break
try
:
conn
=
msql
.
connect
(
host
=
'127.0.0.1'
,
port
=
3306
,
database
=
'criminal_investigation'
,
user
=
'root'
,
password
=
''
)
if
conn
.
is_connected
():
# Inserting csv file data to database
criminalData
=
pd
.
read_csv
(
'E:/BACKBONE/abnormal_behavior_detection/CSV/Abnormal Activity.csv'
,
index_col
=
False
,
delimiter
=
','
)
criminalData
.
head
()
print
(
" "
)
print
(
criminalData
)
cursor
=
conn
.
cursor
()
cursor
.
execute
(
"select database();"
)
record
=
cursor
.
fetchone
()
print
(
" "
)
print
(
"You're connected to database: "
,
record
)
for
i
,
row
in
criminalData
.
iterrows
():
sql
=
"INSERT INTO criminal_investigation.behavior_details VALUES (
%
s,
%
s,
%
s,
%
s)"
cursor
.
execute
(
sql
,
tuple
(
row
))
print
(
"Record inserted"
)
print
(
"Values inserted"
)
conn
.
commit
()
except
Error
as
e
:
print
(
"Error while connecting to MySQL"
,
e
)
cap
.
release
()
cv2
.
destroyAllWindows
()
print
(
" "
)
print
(
"*** ALL STEPS EXECUTED SUCCESSFULLY ***"
)
# 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