-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
137 lines (108 loc) · 4.69 KB
/
main.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
##
# Copyright 2018, Ammar Ali Khan
# Licensed under MIT.
# Since: v1.0.0
##
import time
import cv2
import numpy as np
from src.common.package.config import application
from src.opencv.package.config import application as _application
from src.common.package.http import server as _server
from src.common.package.http.handler import Handler
from src.common.package.camera.capture import Capture as _capture
from src.common.package.frame.action import Action as _frame
from src.common.package.frame.draw import Draw as _draw
from src.opencv.package.opencv.opencv import OpenCV
# Constant
_opencv = OpenCV()
##
# StreamHandler class - inherit Handler
# This class provide handler for HTTP streaming
# Note: this class should override Handler.stream
##
class StreamHandler(Handler):
##
# Override method Handler.stream()
##
def stream(self):
Handler.stream(self)
print('[INFO] Overriding stream method...')
# Initialise capture
capture = _capture(src=application.CAPTURING_DEVICE,
use_pi_camera=application.USE_PI_CAMERA,
resolution=application.RESOLUTION,
frame_rate=application.FRAME_RATE)
if application.USE_PI_CAMERA:
print('[INFO] Warming up pi camera...')
else:
print('[INFO] Warming up camera...')
time.sleep(2.0)
print('[INFO] Start capturing...')
while True:
# Read a frame from capture
frame = capture.read()
# Down size frame to 50% (to increase performance on Raspberry Pi)
# frame = _frame.scale(frame=frame, scale=0.5)
# Convert frame to gray (to increase performance on Raspberry Pi)
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# Get frame dimensions
(height, width) = frame.shape[:2]
# OpenCV detection
detections = _opencv.dnn_face_detector(frame=frame,
scale_factor=1.0,
size=(300, 300),
mean=(104.0, 177.0, 123.0))
# Up size frame to 50% (how the frame was before down sizing)
# frame = _frame.scale(frame=frame, scale=2)
# If returns any detection
for i in range(0, detections.shape[2]):
# Get confidence associated with the detection
confidence = detections[0, 0, i, 2]
# Filter weak detection
if confidence < _application.CONFIDENCE:
continue
# Calculate coordinates
box = detections[0, 0, i, 3:7] * np.array([width,
height,
width,
height])
(left, top, right, bottom) = box.astype('int')
coordinates = {'left': left,
'top': top,
'right': right,
'bottom': bottom}
text = "{:.2f}%".format(confidence * 100)
frame = _draw.rectangle(frame=frame,
coordinates=coordinates,
text=text)
# Write date time on the frame
frame = _draw.text(frame=frame,
coordinates={'left': application.WIDTH - 150, 'top': application.HEIGHT - 20},
text=time.strftime('%d/%m/%Y %H:%M:%S', time.localtime()),
font_color=(0, 0, 255))
# Convert frame into buffer for streaming
retval, buffer = cv2.imencode('.jpg', frame)
# Write buffer to HTML Handler
self.wfile.write(b'--FRAME\r\n')
self.send_header('Content-Type', 'image/jpeg')
self.send_header('Content-Length', len(buffer))
self.end_headers()
self.wfile.write(buffer)
self.wfile.write(b'\r\n')
##
# Method main()
##
def main():
try:
address = ('', application.HTTP_PORT)
server = _server.Server(address, StreamHandler)
print('[INFO] HTTP server started successfully at %s' % str(server.server_address))
print('[INFO] Waiting for client to connect to port %s' % str(application.HTTP_PORT))
server.serve_forever()
except Exception as e:
server.socket.close()
print('[INFO] HTTP server closed successfully.')
print('[ERROR] Exception: %s' % str(e))
if __name__ == '__main__':
main()