Created
October 11, 2021 22:21
-
-
Save lucasmediaflow/be11e171b2fbd233fe1e628c8b31a3c0 to your computer and use it in GitHub Desktop.
This file contains 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
import depthai as dai | |
from time import monotonic, sleep | |
from datetime import datetime | |
import paho.mqtt.client as mqtt | |
import json | |
from setup import unit | |
import cv2 | |
try: | |
focus = unit["camSettings"]["photo"]["oakcamera"]["focus"] | |
rotation = unit["camSettings"]["photo"]["oakcamera"]["rotation"] | |
duration = unit["camSettings"]["video"]["duration"] | |
quality = unit["camSettings"]["photo"]["oakcamera"]["quality"] | |
except Exception as error: | |
focus = 123 | |
rotation = 180 | |
duration = 30 | |
quality = 50 | |
doCapture = record = False | |
outputPath = '/home/pi/Videos/temp/' | |
def on_connect(mosq, obj, rc, properties=None): | |
mqttc.subscribe("oak", 0) | |
def on_subscribe(mosq, obj, mid, granted_qos): | |
print("Subscribed to MQTT Topic OAK") | |
def on_message(mosq, obj, msg): | |
global record, doCapture | |
msg = json.loads(msg.payload.decode("utf-8")) | |
print('message', msg, datetime.now().strftime("%Y%m%d_%H%M%S")) | |
if msg['mode'] == 'video': | |
record = True | |
elif msg['mode'] == 'photo': | |
doCapture = True | |
pipeline = dai.Pipeline() | |
camRgb = pipeline.createColorCamera() | |
videoEnc = pipeline.createVideoEncoder() | |
xout = pipeline.createXLinkOut() | |
xout.setStreamName('h264') | |
camRgb.setBoardSocket(dai.CameraBoardSocket.RGB) | |
camRgb.setResolution(dai.ColorCameraProperties.SensorResolution.THE_4_K) | |
camRgb.setStillSize(1920,1080) | |
camRgb.initialControl.setManualFocus(focus) | |
camRgb.initialControl.setAutoFocusMode(dai.RawCameraControl.AutoFocusMode.OFF) | |
jpgEncoder = pipeline.createVideoEncoder() | |
jpgEncoder.setDefaultProfilePreset( 1920, 1080, 1, dai.VideoEncoderProperties.Profile.MJPEG ) | |
jpgEncoderXLinkOut = pipeline.createXLinkOut() | |
jpgEncoderXLinkOut.setStreamName("jpg") | |
jpgEncoder.setNumFramesPool(1) | |
if rotation == 180: | |
camRgb.setImageOrientation(dai.CameraImageOrientation.ROTATE_180_DEG) | |
videoEnc.setDefaultProfilePreset(1920, 1080, 30, dai.VideoEncoderProperties.Profile.H264_MAIN) | |
camRgb.setIspScale(1,2) | |
camRgb.video.link(videoEnc.input) | |
videoEnc.bitstream.link(xout.input) | |
camControl = pipeline.createXLinkIn() | |
camControl.setStreamName("camControl") | |
camControl.setMaxDataSize(500000) | |
camControl.out.link(camRgb.inputControl) | |
camRgb.still.link(jpgEncoder.input) | |
jpgEncoder.bitstream.link(jpgEncoderXLinkOut.input) | |
mqttc = mqtt.Client('oak') | |
mqttc.on_message = on_message | |
mqttc.on_connect = on_connect | |
mqttc.on_subscribe = on_subscribe | |
mqttc.connect_async("localhost", 1883) | |
mqttc.loop_start() | |
with dai.Device(pipeline) as device: | |
videoQ = device.getOutputQueue(name="h264", maxSize=1, blocking=False) | |
camControlQ = device.getInputQueue("camControl", maxSize=1, blocking=False) | |
jpgQ = device.getOutputQueue("jpg", maxSize=1, blocking=False) | |
ctrl = dai.CameraControl() | |
while True: | |
if record: | |
ts = monotonic() | |
now = datetime.now().strftime("%Y%m%d_%H%M%S") | |
with open(outputPath + now + '.h264', 'wb') as videoFile: | |
print(now, 'started recording 1080 h264', camRgb.getIspSize(), videoEnc.getProfile()) | |
while record: | |
h264Packet = videoQ.get() | |
h264Packet.getData().tofile(videoFile) | |
if (monotonic() - ts) > duration: | |
print(datetime.now().strftime("%Y%m%d_%H%M%S"), 'finished recording 1080 h264 file') | |
record = False | |
if doCapture: | |
now = datetime.now().strftime("%Y%m%d_%H%M%S") | |
ctrl.setCaptureStill(True) | |
camControlQ.send(ctrl) | |
print(now, 'taking photo', camRgb.getIspSize(), jpgEncoder.getProfile()) | |
f = jpgQ.get() | |
frame = cv2.imdecode(f.getData(), cv2.IMREAD_UNCHANGED) | |
cv2.imwrite(outputPath + now + ".jpg",frame,[int(cv2.IMWRITE_JPEG_QUALITY), quality]) | |
doCapture = False |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment