-
Notifications
You must be signed in to change notification settings - Fork 205
Expand file tree
/
Copy pathscript_jpeg_to_sdcard.py
More file actions
executable file
·99 lines (82 loc) · 2.72 KB
/
script_jpeg_to_sdcard.py
File metadata and controls
executable file
·99 lines (82 loc) · 2.72 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
#!/usr/bin/env python3
import cv2
import depthai as dai
import numpy as np
# Create pipeline
pipeline = dai.Pipeline()
# Define source and output
camRgb = pipeline.create(dai.node.ColorCamera)
xoutRgb = pipeline.create(dai.node.XLinkOut)
xinCtrl = pipeline.create(dai.node.XLinkIn)
jpegEncoder = pipeline.create(dai.node.VideoEncoder)
scriptSave = pipeline.create(dai.node.Script)
scriptServer = pipeline.create(dai.node.Script)
# Properties
camRgb.setResolution(dai.ColorCameraProperties.SensorResolution.THE_12_MP)
camRgb.setPreviewSize(640, 360)
jpegEncoder.setDefaultProfilePreset(1, dai.VideoEncoderProperties.Profile.MJPEG)
xoutRgb.setStreamName("rgb")
xinCtrl.setStreamName('control')
# Note: all scripts accessing the media storage must run on LEON_CSS
scriptSave.setProcessor(dai.ProcessorType.LEON_CSS)
scriptSave.setScript("""
import os
index = 1000
while True:
# Find an unused file name first
while True:
path = '/media/mmcsd-0-0/' + str(index) + '.jpg'
if not os.path.exists(path):
break
index += 1
frame = node.io['jpeg'].get()
node.warn(f'Saving to SDcard: {path}')
with open(path, 'wb') as f:
f.write(frame.getData())
index += 1
""")
scriptServer.setProcessor(dai.ProcessorType.LEON_CSS)
scriptServer.setScript("""
import http.server
import socketserver
import socket
import fcntl
import struct
import os
def get_ip_address(ifname):
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
return socket.inet_ntoa(fcntl.ioctl(
s.fileno(),
-1071617759, # SIOCGIFADDR
struct.pack('256s', ifname[:15].encode())
)[20:24])
# Note: `chdir` here will prevent unmount, this should be improved!
os.chdir('/media/mmcsd-0-0')
PORT = 80
Handler = http.server.SimpleHTTPRequestHandler
with socketserver.TCPServer(("", PORT), Handler) as httpd:
ip = get_ip_address('re0')
node.warn(f'===== HTTP file server accessible at: http://{ip}')
httpd.serve_forever()
""")
# Linking
camRgb.preview.link(xoutRgb.input)
camRgb.still.link(jpegEncoder.input)
jpegEncoder.bitstream.link(scriptSave.inputs['jpeg'])
xinCtrl.out.link(camRgb.inputControl)
# Connect to device and start pipeline
with dai.Device(pipeline) as device:
# Get queues for RGB preview and capture control
qRgb = device.getOutputQueue(name='rgb', maxSize=4, blocking=False)
qControl = device.getInputQueue('control')
print('======== Press C to capture JPEG images to SDcard')
while True:
inRgb = qRgb.get()
cv2.imshow('rgb', inRgb.getCvFrame())
key = cv2.waitKey(1)
if key == ord('q'):
break
elif key == ord('c'):
ctrl = dai.CameraControl()
ctrl.setCaptureStill(True)
qControl.send(ctrl)