-
Notifications
You must be signed in to change notification settings - Fork 0
/
cam-server.py
275 lines (247 loc) · 9.99 KB
/
cam-server.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
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
#!/usr/bin/python3
# code is taken from the picamera2 examples and modified with some improvements
import io
import logging
import socketserver
from http import server
from threading import Condition, Thread
from picamera2 import Picamera2
from picamera2.encoders import MJPEGEncoder
from picamera2.outputs import FileOutput
from datetime import datetime
import time
import piexif
import os
# Page template with responsive styling
PAGE = """\
<html>
<head>
<title>Pi Camera Feed</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
body {{ font-family: Arial, sans-serif; text-align: center; margin: 0; padding: 0; }}
img {{ width: 100%; height: auto; max-width: 1000px; }}
#controls {{ margin-top: 10px; display: flex; flex-wrap: wrap; justify-content: center; }}
#controls button {{
margin: 5px;
padding: 10px 20px;
font-size: 1rem;
flex: 1 1 30%;
max-width: 100px;
border: none;
border-radius: 5px;
background-color: #007BFF;
color: white;
}}
#controls button:hover {{ background-color: #0056b3; }}
#info {{ margin-top: 15px; font-size: 0.9rem; color: #333; }}
h1 {{ font-size: 1.5rem; margin: 10px 0; }}
</style>
<script>
function sendCommand(command) {{
fetch('/control?command=' + command)
.then(response => response.text())
.then(data => console.log(data));
}}
</script>
</head>
<body>
<h1>Pi Camera Feed</h1>
<img src="stream.mjpg" width="{WIDTH}" height="{HEIGHT}" alt="Camera Feed"/>
<div id="controls">
<p>(Beta) Bonus Buttons</p>
<button onclick="fetch('/rotate')">Rotate Camera</button>
<button onclick="sendCommand('zoom_in')">Zoom In</button>
<button onclick="sendCommand('zoom_out')">Zoom Out</button>
<button onclick="sendCommand('resolution_high')">High Resolution</button>
<button onclick="sendCommand('resolution_low')">Low Resolution</button>
</div>
<div id="info">
<p>Date: {date} | CPU Usage: {cpu}%</p>
</div>
</body>
</html>
"""
# Initial rotation state
ROTATION = 270 # Start with 270 degrees
WIDTH = 640
HEIGHT = 480
def update_rotation_header(rotation):
"""Update EXIF rotation header based on the rotation angle."""
global WIDTH, HEIGHT, rotation_header
rotation_header = bytes()
if rotation == 90 or rotation == 270:
WIDTH, HEIGHT = HEIGHT, WIDTH # Swap dimensions for 90 or 270
else:
WIDTH, HEIGHT = 640, 480 # Original dimensions for 0 rotation
code = {0: 1, 90: 6, 270: 8}.get(rotation, 1)
exif_bytes = piexif.dump({'0th': {piexif.ImageIFD.Orientation: code}})
exif_len = len(exif_bytes) + 2
rotation_header = bytes.fromhex('ffe1') + exif_len.to_bytes(2, 'big') + exif_bytes
# Functions to get CPU usage and temperature
def get_cpu_usage():
with open('/proc/stat', 'r') as f:
line = f.readline()
fields = [float(column) for column in line.strip().split()[1:]]
idle_time, total_time = fields[3], sum(fields)
usage = 100 * (1 - idle_time / total_time)
return round(usage, 2)
def get_cpu_temp():
with open('/sys/class/thermal/thermal_zone0/temp', 'r') as f:
temp_str = f.read().strip()
temp = float(temp_str) / 1000
return round(temp, 2)
def get_metadata():
return picam2.capture_metadata()
def change_rotation():
"""Cycle rotation between 0, 90, and 270, and restart camera with new settings."""
global ROTATION
global WIDTH
global HEIGHT
picam2.stop_recording() # Stop camera first
# Cycle through 0 -> 90 -> 270 -> 0 degrees
ROTATION = (ROTATION + 90) % 360
if ROTATION not in [0, 90, 270]: # Reset to 0 if out of range
ROTATION = 0
# Update EXIF header based on the new rotation
update_rotation_header(ROTATION)
# Reconfigure and restart camera
picam2.configure(picam2.create_video_configuration(main={"size": (WIDTH, HEIGHT)}))
picam2.start_recording(MJPEGEncoder(), FileOutput(output))
class StreamingOutput(io.BufferedIOBase):
def __init__(self):
self.frame = None
self.condition = Condition()
def write(self, buf):
with self.condition:
self.frame = buf[:2] + rotation_header + buf[2:]
self.condition.notify_all()
class StreamingHandler(server.BaseHTTPRequestHandler):
def do_GET(self):
if self.path == '/':
self.send_response(301)
self.send_header('Location', '/index.html')
self.end_headers()
elif self.path == '/index.html':
content = self.server.page_content().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:
output.condition.wait()
frame = output.frame
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')
except Exception as e:
logging.warning(
'Removed streaming client %s: %s',
self.client_address, str(e))
elif self.path.startswith('/control'):
command = self.path.split('=')[1]
self.server.handle_command(command)
self.send_response(200)
self.send_header('Content-Type', 'text/plain')
self.end_headers()
self.wfile.write(b'Command executed')
elif self.path == '/rotate':
# Change rotation and restart camera
change_rotation()
self.send_response(200)
self.end_headers()
else:
self.send_error(404)
self.end_headers()
class StreamingServer(socketserver.ThreadingMixIn, server.HTTPServer):
allow_reuse_address = True
daemon_threads = True
def __init__(self, server_address, RequestHandlerClass, page_update_interval=60):
super().__init__(server_address, RequestHandlerClass)
self.page_update_interval = page_update_interval
self.page_content_cache = ""
self.update_page_content()
self.start_background_page_updater()
def page_content(self):
return self.page_content_cache
def update_page_content(self):
global WIDTH
global HEIGHT
# Get system stats
date = datetime.now().strftime('%Y-%m-%d')
# time = datetime.now().strftime('%H:%M')
# temp = get_cpu_temp()
cpu = get_cpu_usage()
# metadata = get_metadata()
# Update the HTML content with the stats
self.page_content_cache = PAGE.format(WIDTH=WIDTH, HEIGHT=HEIGHT,date=date, cpu=cpu)
def start_background_page_updater(self):
def update_task():
while True:
self.update_page_content()
time.sleep(self.page_update_interval)
updater_thread = Thread(target=update_task, daemon=True)
updater_thread.start()
def handle_command(self, command):
global picam2
global WIDTH
global HEIGHT
if command == "zoom_in":
size = picam2.capture_metadata()['ScalerCrop'][2:]
full_res = picam2.camera_properties['PixelArraySize']
for _ in range(20):
# This syncs us to the arrival of a new camera frame:
picam2.capture_metadata()
size = [int(s * 0.95) for s in size]
offset = [(r - s) // 2 for r, s in zip(full_res, size)]
picam2.set_controls({"ScalerCrop": offset + size})
elif command == "zoom_out":
size = picam2.capture_metadata()['ScalerCrop'][2:]
full_res = picam2.camera_properties['PixelArraySize']
for _ in range(20):
# This syncs us to the arrival of a new camera frame:
picam2.capture_metadata()
size = [int(min(s * 1.05, r)) for s, r in zip(size, full_res)]
offset = [(r - s) // 2 for r, s in zip(full_res, size)]
picam2.set_controls({"ScalerCrop": offset + size})
elif command == "resolution_high":
WIDTH = 1280
HEIGHT = 720
self.change_resolution((WIDTH, HEIGHT))
elif command == "resolution_low":
WWIDTH = 640
HEIGHT = 480
self.change_resolution((WIDTH, HEIGHT))
else:
logging.error(f"Commnad doesn't exist: {command}")
logging.info(f"Executed command: {command}")
def change_resolution(self, size):
global picam2, output
picam2.stop_recording()
picam2.configure(picam2.create_video_configuration(main={"size": size}))
picam2.start_recording(MJPEGEncoder(), FileOutput(output))
# Initialize camera and server
picam2 = Picamera2()
update_rotation_header(ROTATION) # Set initial rotation header
picam2.configure(picam2.create_video_configuration(main={"size": (WIDTH, HEIGHT)}))
output = StreamingOutput()
picam2.start_recording(MJPEGEncoder(), FileOutput(output))
try:
address = ('', 8000)
server = StreamingServer(address, StreamingHandler)
server.serve_forever()
finally:
picam2.stop_recording()