-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcrime_detection.py
More file actions
91 lines (59 loc) · 1.92 KB
/
crime_detection.py
File metadata and controls
91 lines (59 loc) · 1.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import os
from ultralytics import YOLO
import cv2
# VIDEOS_DIR = os.path.join('.', 'videos')
# video_path = os.path.join('attack.mp4')
# video_path_out = '{}_out.mp4'.format(video_path.split('.')[0])
# cap = cv2.VideoCapture(video_path)
# ret, frame = cap.read()
# H, W, _ = frame.shape
# out = cv2.VideoWriter(video_path_out, cv2.VideoWriter_fourcc(*'MP4V'), int(cap.get(cv2.CAP_PROP_FPS)), (W, H))
# model_path = 'best.pt'
# # Load a model
# model = YOLO(model_path) # load a custom model
# threshold = 0.5
# flag = 0
# sets = {}
# frame_count = 0
# while ret:
# frame_count += 1
# results = model(frame)[0]
# for result in results.boxes.data.tolist():
# x1, y1, x2, y2, score, class_id = result
# if score > threshold:
# sets[frame_count] = results.names[int(class_id)].upper()
# cv2.rectangle(frame, (int(x1), int(y1)), (int(x2), int(y2)), (0, 255, 0), 4)
# cv2.putText(frame, results.names[int(class_id)].upper(), (int(x1), int(y1 - 10)),
# cv2.FONT_HERSHEY_SIMPLEX, 1.3, (0, 255, 0), 3, cv2.LINE_AA)
# flag = 1
# out.write(frame)
# ret, frame = cap.read()
# cap.release()
# out.release()
# cv2.destroyAllWindows()
# print(flag)
# export sets to csv
# import csv
# with open('sets.csv', 'w') as f:
# for key in sets.keys():
# f.write("%s,%s\n"%(key,sets[key]))
# function to detect crime
# from ultralytics import YOLO
# import cv2
model_path = 'best.pt'
# Load a model
model = YOLO(model_path) # load a custom model
threshold = 0.5
cnt = 0
def detect_crime(frame):
results = model(frame)[0]
for result in results.boxes.data.tolist():
x1, y1, x2, y2, score, class_id = result
if score > threshold:
return results.names[int(class_id)].upper()
return False
# global cnt
# if cnt > 650:
# return True
# cnt += 1
# return False