Created
September 13, 2022 09:36
-
-
Save Erol444/862ba08c4c5c60e8db60b0d7da3d5bf8 to your computer and use it in GitHub Desktop.
script_jpeg_to_emmc.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import cv2 | |
import depthai as dai | |
# Create 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) | |
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_4_K) | |
jpegEncoder.setDefaultProfilePreset(1, dai.VideoEncoderProperties.Profile.MJPEG) | |
# Note: all scripts accessing the media storage must run on LEON_CSS | |
scriptSave.setProcessor(dai.ProcessorType.LEON_CSS) | |
scriptSave.setScript(""" | |
import os | |
index = 1000 | |
import time | |
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 | |
time.sleep(3) | |
""") | |
scriptServer.setProcessor(dai.ProcessorType.LEON_CSS) | |
scriptServer.setScript(""" | |
import http.server | |
import socketserver | |
import socket | |
import fcntl | |
import struct | |
import os | |
import time | |
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! | |
time.sleep(1) | |
os.chdir('/') | |
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.video.link(jpegEncoder.input) | |
jpegEncoder.bitstream.link(scriptSave.inputs['jpeg']) | |
scriptSave.inputs['jpeg'].setBlocking(False) | |
with dai.Device(pipeline) as device: | |
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