Created
October 4, 2021 21:18
-
-
Save lucasmediaflow/a1d106d36e1ace972511e0dd2a5bdc97 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: | |
rotation = unit["camSettings"]["photo"]["oakcamera"]["rotation"] | |
duration = unit["camSettings"]["video"]["duration"] | |
quality = unit["camSettings"]["photo"]["oakcamera"]["quality"] | |
except Exception as error: | |
rotation = 180 | |
duration = 30 | |
quality = 50 | |
doCapture = record = False | |
outputPath = '/home/pi/Videos/temp/' | |
duration = 10 | |
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(3840,2160) | |
jpgEncoder = pipeline.createVideoEncoder() | |
jpgEncoder.setDefaultProfilePreset( 3840, 2160, 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(3840, 2160, 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: | |
videoEnc.setDefaultProfilePreset(1920, 1080, 30, dai.VideoEncoderProperties.Profile.H264_MAIN) | |
videoEnc.setProfile(1920, 1080, dai.VideoEncoderProperties.Profile.H264_MAIN) | |
camRgb.setIspScale(1,2) | |
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: | |
camRgb.setIspScale(1,1) | |
jpgEncoder.setDefaultProfilePreset( 3840, 2160, 1, dai.VideoEncoderProperties.Profile.MJPEG ) | |
now = datetime.now().strftime("%Y%m%d_%H%M%S") | |
ctrl.setCaptureStill(True) | |
camControlQ.send(ctrl) | |
print(now, 'taking photo', camRgb.getIspSize(), videoEnc.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