Skip to content

Instantly share code, notes, and snippets.

@Erol444
Created June 25, 2022 23:16
Show Gist options
  • Save Erol444/16949db9bb7ed13ccd6f2efbe7dd103a to your computer and use it in GitHub Desktop.
Save Erol444/16949db9bb7ed13ccd6f2efbe7dd103a to your computer and use it in GitHub Desktop.
DepthAI OAK PoE Write to eMMC flash with Script node
#!/usr/bin/env python3
import depthai as dai
import cv2
# Start defining a pipeline
pipeline = dai.Pipeline()
cfg = dai.BoardConfig()
cfg.emmc = True
cfg.logPath = '/media/mmcsd-0-0/depthai_log_file.txt'
config = dai.Device.Config()
config.board = cfg
pipeline.setBoardConfig(cfg)
# Define source and output
camRgb = pipeline.create(dai.node.ColorCamera)
camRgb.setResolution(dai.ColorCameraProperties.SensorResolution.THE_12_MP)
jpegEncoder = pipeline.create(dai.node.VideoEncoder)
jpegEncoder.setDefaultProfilePreset(1, dai.VideoEncoderProperties.Profile.MJPEG)
camRgb.still.link(jpegEncoder.input)
scriptSave = pipeline.create(dai.node.Script)
jpegEncoder.bitstream.link(scriptSave.inputs['jpeg'])
scriptCapture = pipeline.create(dai.node.Script)
scriptCapture.outputs['out'] .link(camRgb.inputControl)
# 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()
with open(path, 'wb') as f:
f.write(frame.getData())
index += 1
""")
scriptCapture.setProcessor(dai.ProcessorType.LEON_CSS)
scriptCapture.setScript("""
from http.server import BaseHTTPRequestHandler
import socketserver
import socket
PORT = 8080
ctrl = CameraControl()
ctrl.setCaptureStill(True)
class HTTPHandler(BaseHTTPRequestHandler):
def do_GET(self):
node.io['out'].send(ctrl)
self.send_response(200)
self.end_headers()
self.wfile.write(b'<h1>[DepthAI] 12MP frame saved to the eMMC!</h1><p id="txt"></p>')
with socketserver.TCPServer(("", PORT), HTTPHandler) as httpd:
httpd.serve_forever()
""")
scriptServer = pipeline.create(dai.node.Script)
scriptServer.setProcessor(dai.ProcessorType.LEON_CSS)
scriptServer.setScript("""
import http.server
import socketserver
import socket
import os
# 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:
httpd.serve_forever()
""")
# Flash the pipeline to the OAK PoE
(ok, bl) = dai.DeviceBootloader.getFirstAvailableDevice()
if not ok:
exit("No OAK device found!")
bootloader = dai.DeviceBootloader(bl)
progress = lambda p : print(f'Flashing progress: {p*100:.1f}%')
# Set compress=False if to disable FW compression
ok, err = bootloader.flash(progress, pipeline)
if ok:
print(f"Flashed successfully to the device {bl.name}. By default, OAK PoE cam needs ~1 min to boot up the flashed app.")
else:
print("Error occured when trying to flash: {err}")
# import time
# with dai.Device(pipeline) as device:
# print('Device connected')
# while not device.isClosed():
# time.sleep(1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment