Repository navigation
Expand file tree
/
Copy pathstream_server.py
More file actions
executable file
·191 lines (156 loc) · 5.48 KB
/
Copy pathstream_server.py
File metadata and controls
executable file
·191 lines (156 loc) · 5.48 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
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#!/usr/bin/python3
# Mostly copied from https://picamera.readthedocs.io/en/release-1.13/recipes2.html
# Run this script, then point a web browser at http:<this-ip-address>:8000
# Note: needs simplejpeg to be installed (pip3 install simplejpeg).
import face_recog
import io
import logging
import socketserver
from http import server
from threading import Condition, Lock
from picamera2 import Picamera2
from picamera2.encoders import JpegEncoder
from picamera2.outputs import FileOutput
import threading
from time import sleep
from PIL import Image
import serial
ser = serial.Serial('/dev/ttyACM0', baudrate=9600, parity=serial.PARITY_NONE, stopbits=serial.STOPBITS_ONE)
process_this_frame = True
cameraLock = Lock()
closeBy = False
valid = 0
serialLock = Lock()
PAGE = """
<html>
<style>
.content {
max-width: 500px;
margin: auto;
}
</style>
<head>
<title>picamera2 MJPEG streaming demo</title>
</head>
<body>
<h1>Picamera2 MJPEG Streaming Demo</h1>
<img src="stream.mjpg" width="640" height="480" />
</body>
</html>
"""
with open('cameoff.jpg', "rb") as image_file:
image_bytes = image_file.read()
cameraOffFrame = image_bytes
def stream():
address = ('', 8000)
server = StreamingServer(address, StreamingHandler)
server.serve_forever()
class StreamingOutput(io.BufferedIOBase):
def __init__(self):
self.frame = None
self.condition = Condition()
def write(self, buf):
with self.condition:
self.frame = buf
self.condition.notify_all()
class StreamingHandler(server.BaseHTTPRequestHandler):
def do_GET(self):
global process_this_frame
global valid
if self.path == '/':
self.send_response(301)
self.send_header('Location', '/index.html')
self.end_headers()
elif self.path == '/index.html':
content = PAGE.encode('utf-8')
self.send_response(200)
self.send_header('Content-Type', 'text/html')
self.send_header('Content-Length', len(content))
self.end_headers()
self.wfile.write(content)
elif self.path == '/stream.mjpg':
self.send_response(200)
self.send_header('Age', 0)
self.send_header('Cache-Control', 'no-cache, private')
self.send_header('Pragma', 'no-cache')
self.send_header('Content-Type', 'multipart/x-mixed-replace; boundary=FRAME')
self.end_headers()
try:
while True:
with output.condition:
#print("Waiting for Frame")
output.condition.wait()
frame = output.frame
#print("Frame: ", type(frame))
cameraLock.acquire()
print("Frame")
if not closeBy:
frame = cameraOffFrame
#print("Null Frame", type(frame))
#print("Received frame")
else:
if process_this_frame:
frame, valid = face_recog.id_frame(frame)
#print("Before Stream Lock")
#serialLock.acquire()
#print("After Stream Lock")
#serialLock.release()
process_this_frame = not process_this_frame
cameraLock.release()
sleep(0.01)
self.wfile.write(b'--FRAME\r\n')
self.send_header('Content-Type', 'image/jpeg')
self.send_header('Content-Length', len(frame))
self.end_headers()
self.wfile.write(frame)
self.wfile.write(b'\r\n')
#break;
except Exception as e:
logging.warning(
'Removed streaming client %s: %s',
self.client_address, str(e))
else:
self.send_error(404)
self.end_headers()
class StreamingServer(socketserver.ThreadingMixIn, server.HTTPServer):
allow_reuse_address = True
daemon_threads = True
picam2 = Picamera2()
try:
picam2.configure(picam2.create_video_configuration(main={"size": (640, 480)}))
output = StreamingOutput()
picam2.start_recording(JpegEncoder(), FileOutput(output))
t1 = threading.Thread(target=stream)
t1.start()
while (1):
# print("Before Lock")
serialLock.acquire()
if ser.readable():
data = ser.read()
#print("Mbed: ", data)
if data == b'1':
#output = StreamingOutput()
#picam2.start_recording(JpegEncoder(), FileOutput(output))
cameraLock.acquire()
print("Change")
# print("Turn on Camera")
closeBy = True
cameraLock.release()
else:
#picam2.stop_recording()
cameraLock.acquire()
print("Change")
# print("Turn off Camera")
closeBy = False
cameraLock.release()
# print("Valid: ", valid)
if (valid):
ser.write(b'1')
else:
ser.write(b'0')
# print(valid.to_bytes(1, 'big'))
# ser.write(valid.to_bytes(1, 'big') )
serialLock.release()
finally:
picam2.stop_recording()
t1.join()