forked from HarshCasper/Rotten-Scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
human_detector.py
138 lines (116 loc) Β· 4.13 KB
/
human_detector.py
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import cv2
import imutils
import argparse
HOGCV = cv2.HOGDescriptor()
HOGCV.setSVMDetector(cv2.HOGDescriptor_getDefaultPeopleDetector())
def detect(frame):
bounding_box_cordinates, weights = HOGCV.detectMultiScale(
frame, winStride=(4, 4), padding=(8, 8), scale=1.03)
person = 1
for x, y, w, h in bounding_box_cordinates:
cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)
cv2.putText(frame, f'person {person}', (x, y),
cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 1)
person += 1
cv2.putText(frame, 'Status : Detecting ', (40, 40),
cv2.FONT_HERSHEY_DUPLEX, 0.8, (255, 0, 0), 2)
cv2.putText(frame, f'Total Persons : {person - 1}',
(40, 70), cv2.FONT_HERSHEY_DUPLEX, 0.8, (255, 0, 0), 2)
cv2.imshow('output', frame)
return frame
def humanDetector(args):
image_path = args["image"]
video_path = args['video']
if str(args["camera"]) == 'true':
camera = True
else:
camera = False
writer = None
if args['output'] is not None and image_path is None:
writer = cv2.VideoWriter(
args['output'], cv2.VideoWriter_fourcc(*'MJPG'), 10, (600, 600))
if camera:
print('[INFO] Opening Web Cam.')
detectByCamera(writer)
elif video_path is not None:
print('[INFO] Opening Video from path.')
detectByPathVideo(video_path, writer)
elif image_path is not None:
print('[INFO] Opening Image from path.')
detectByPathImage(image_path, args['output'])
def detectByCamera(writer):
print('Here')
video = cv2.VideoCapture(0)
print('Detecting people...')
while True:
check, frame = video.read()
frame = detect(frame)
if writer is not None:
writer.write(frame)
key = cv2.waitKey(1)
if key == ord('q'):
break
video.release()
cv2.destroyAllWindows()
def detectByPathVideo(path, writer):
video = cv2.VideoCapture(path)
check, frame = video.read()
if check == False:
print('Video Not Found. Please Enter a Valid Path (Full path of Video Should be Provided).')
return
print('Detecting people...')
while video.isOpened():
# check is True if reading was successful
check, frame = video.read()
if check:
frame = imutils.resize(frame, width=min(800, frame.shape[1]))
frame = detect(frame)
if writer is not None:
writer.write(frame)
key = cv2.waitKey(1)
if key == ord('q'):
break
else:
break
video.release()
cv2.destroyAllWindows()
def detectByCamera(writer):
print('Not Here')
video = cv2.VideoCapture(0)
print('Detecting people...')
while True:
check, frame = video.read()
frame = detect(frame)
if writer is not None:
writer.write(frame)
key = cv2.waitKey(1)
if key == ord('q'):
break
video.release()
cv2.destroyAllWindows()
def detectByPathImage(path, output_path):
cv2.namedWindow("output", cv2.WINDOW_NORMAL)
image = cv2.imread(path)
image = imutils.resize(image, width=min(1800, image.shape[1]))
result_image = detect(image)
if output_path is not None:
cv2.imwrite(output_path, result_image)
cv2.waitKey(0)
cv2.destroyAllWindows()
def argsParser():
arg_parse = argparse.ArgumentParser()
arg_parse.add_argument("-v", "--video", default=None,
help="path to Video File ")
arg_parse.add_argument("-i", "--image", default=None,
help="path to Image File ")
arg_parse.add_argument("-c", "--camera", default=False,
help="Set true if you want to use the camera.")
arg_parse.add_argument("-o", "--output", type=str,
help="path to optional output video file")
args = vars(arg_parse.parse_args())
return args
if __name__ == "__main__":
HOGCV = cv2.HOGDescriptor()
HOGCV.setSVMDetector(cv2.HOGDescriptor_getDefaultPeopleDetector())
args = argsParser()
humanDetector(args)