Skip to content

Commit 3bcc927

Browse files
committed
stating new ipcam example
1 parent af5e630 commit 3bcc927

File tree

1 file changed

+212
-0
lines changed

1 file changed

+212
-0
lines changed

examples/ipcam2/code.py

Lines changed: 212 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,212 @@
1+
import espcamera
2+
import os
3+
import sys
4+
import time
5+
import struct
6+
import board
7+
import adafruit_pycamera
8+
import displayio
9+
import gifio
10+
import ulab.numpy as np
11+
import bitmaptools
12+
import socketpool
13+
import wifi
14+
from adafruit_httpserver import Server, Request, Response, GET, POST
15+
16+
pycam = adafruit_pycamera.PyCamera()
17+
pycam.autofocus_init()
18+
19+
if wifi.radio.ipv4_address:
20+
# use alt port if web workflow enabled
21+
port = 8080
22+
else:
23+
# connect to wifi and use standard http port otherwise
24+
wifi.radio.connect(os.getenv("WIFI_SSID"), os.getenv("WIFI_PASSWORD"))
25+
port = 80
26+
27+
print(wifi.radio.ipv4_address)
28+
29+
pool = socketpool.SocketPool(wifi.radio)
30+
server = Server(pool, debug=True)
31+
32+
@server.route("/lcd", [GET, POST])
33+
def property(request: Request) -> Response:
34+
pycam.blit(pycam.continuous_capture())
35+
return Response(request, "")
36+
37+
@server.route("/jpeg", [GET, POST])
38+
def property(request: Request) -> Response:
39+
pycam.camera.reconfigure(
40+
pixel_format=espcamera.PixelFormat.JPEG,
41+
frame_size=pycam.resolution_to_frame_size[pycam._resolution]
42+
)
43+
try:
44+
jpeg = pycam.camera.take(1)
45+
if jpeg is not None:
46+
return Response(request, bytes(jpeg), content_type="image/jpeg")
47+
else:
48+
return Response(request, "", content_type="text/plain", status=INTERNAL_SERVER_ERROR_500)
49+
finally:
50+
pycam.live_preview_mode()
51+
52+
@server.route("/property", [GET, POST])
53+
def property(request: Request) -> Response:
54+
enctype = request.query_params.get("enctype", "text/plain")
55+
56+
key = request.form_data.get("k")
57+
value = request.form_data.get("v", None)
58+
59+
try:
60+
current_value = getattr(pycam, key, None)
61+
except Exception as e:
62+
return Response(request, str(e), status=NOT_FOUND_404)
63+
64+
if value is None:
65+
return Response(request, str(current_value))
66+
else:
67+
try:
68+
current_value_type = type(current_value)
69+
if current_value_type is bool:
70+
setattr(pycam, key, value not in ('False', '0', 'no', 'n', 'off', ''))
71+
else:
72+
setattr(pycam, key, current_value_type(value))
73+
return Response(request, "")
74+
except Exception as e:
75+
return Response(request, str(e), status=BAD_REQUEST_400)
76+
77+
server.serve_forever(str(wifi.radio.ipv4_address), port)
78+
79+
80+
server = Server(pool, debug=True)
81+
82+
last_frame = displayio.Bitmap(pycam.camera.width, pycam.camera.height, 65535)
83+
onionskin = displayio.Bitmap(pycam.camera.width, pycam.camera.height, 65535)
84+
while True:
85+
if (pycam.mode_text == "STOP" and pycam.stop_motion_frame != 0):
86+
# alpha blend
87+
new_frame = pycam.continuous_capture()
88+
bitmaptools.alphablend(onionskin, last_frame, new_frame,
89+
displayio.Colorspace.RGB565_SWAPPED)
90+
pycam.blit(onionskin)
91+
else:
92+
pycam.blit(pycam.continuous_capture())
93+
#print("\t\t", capture_time, blit_time)
94+
95+
pycam.keys_debounce()
96+
print(f"{pycam.shutter.released=} {pycam.shutter.long_press=} {pycam.shutter.short_count=}")
97+
# test shutter button
98+
if pycam.shutter.long_press:
99+
print("FOCUS")
100+
print(pycam.autofocus_status)
101+
pycam.autofocus()
102+
print(pycam.autofocus_status)
103+
if pycam.shutter.short_count:
104+
print("Shutter released")
105+
if pycam.mode_text == "STOP":
106+
pycam.capture_into_bitmap(last_frame)
107+
pycam.stop_motion_frame += 1
108+
try:
109+
pycam.display_message("Snap!", color=0x0000FF)
110+
pycam.capture_jpeg()
111+
except TypeError as e:
112+
pycam.display_message("Failed", color=0xFF0000)
113+
time.sleep(0.5)
114+
except RuntimeError as e:
115+
pycam.display_message("Error\nNo SD Card", color=0xFF0000)
116+
time.sleep(0.5)
117+
pycam.live_preview_mode()
118+
119+
if pycam.mode_text == "GIF":
120+
try:
121+
f = pycam.open_next_image("gif")
122+
except RuntimeError as e:
123+
pycam.display_message("Error\nNo SD Card", color=0xFF0000)
124+
time.sleep(0.5)
125+
continue
126+
i = 0
127+
ft = []
128+
pycam._mode_label.text = "RECORDING"
129+
130+
pycam.display.refresh()
131+
with gifio.GifWriter(f, pycam.camera.width, pycam.camera.height,
132+
displayio.Colorspace.RGB565_SWAPPED, dither=True) as g:
133+
t00 = t0 = time.monotonic()
134+
while (i < 15) or (pycam.shutter_button.value == False):
135+
i += 1
136+
_gifframe = pycam.continuous_capture()
137+
g.add_frame(_gifframe, .12)
138+
pycam.blit(_gifframe)
139+
t1 = time.monotonic()
140+
ft.append(1/(t1-t0))
141+
print(end=".")
142+
t0 = t1
143+
pycam._mode_label.text = "GIF"
144+
print(f"\nfinal size {f.tell()} for {i} frames")
145+
print(f"average framerate {i/(t1-t00)}fps")
146+
print(f"best {max(ft)} worst {min(ft)} std. deviation {np.std(ft)}")
147+
f.close()
148+
pycam.display.refresh()
149+
150+
if pycam.mode_text == "JPEG":
151+
pycam.tone(200, 0.1)
152+
try:
153+
pycam.display_message("Snap!", color=0x0000FF)
154+
pycam.capture_jpeg()
155+
pycam.live_preview_mode()
156+
except TypeError as e:
157+
pycam.display_message("Failed", color=0xFF0000)
158+
time.sleep(0.5)
159+
pycam.live_preview_mode()
160+
except RuntimeError as e:
161+
pycam.display_message("Error\nNo SD Card", color=0xFF0000)
162+
time.sleep(0.5)
163+
if pycam.card_detect.fell:
164+
print("SD card removed")
165+
pycam.unmount_sd_card()
166+
pycam.display.refresh()
167+
if pycam.card_detect.rose:
168+
print("SD card inserted")
169+
pycam.display_message("Mounting\nSD Card", color=0xFFFFFF)
170+
for _ in range(3):
171+
try:
172+
print("Mounting card")
173+
pycam.mount_sd_card()
174+
print("Success!")
175+
break
176+
except OSError as e:
177+
print("Retrying!", e)
178+
time.sleep(0.5)
179+
else:
180+
pycam.display_message("SD Card\nFailed!", color=0xFF0000)
181+
time.sleep(0.5)
182+
pycam.display.refresh()
183+
184+
if pycam.up.fell:
185+
print("UP")
186+
key = settings[curr_setting]
187+
if key:
188+
setattr(pycam, key, getattr(pycam, key) + 1)
189+
if pycam.down.fell:
190+
print("DN")
191+
key = settings[curr_setting]
192+
if key:
193+
setattr(pycam, key, getattr(pycam, key) - 1)
194+
if pycam.left.fell:
195+
print("LF")
196+
curr_setting = (curr_setting + 1) % len(settings)
197+
print(settings[curr_setting])
198+
#new_res = min(len(pycam.resolutions)-1, pycam.get_resolution()+1)
199+
#pycam.set_resolution(pycam.resolutions[new_res])
200+
pycam.select_setting(settings[curr_setting])
201+
if pycam.right.fell:
202+
print("RT")
203+
curr_setting = (curr_setting - 1 + len(settings)) % len(settings)
204+
print(settings[curr_setting])
205+
pycam.select_setting(settings[curr_setting])
206+
#new_res = max(1, pycam.get_resolution()-1)
207+
#pycam.set_resolution(pycam.resolutions[new_res])
208+
if pycam.select.fell:
209+
print("SEL")
210+
if pycam.ok.fell:
211+
print("OK")
212+

0 commit comments

Comments
 (0)