Snap from my current code.
Simply trying to stream using MJPEG(OAK-1-PoE)
import socketserver
import threading
import time
from http.server import BaseHTTPRequestHandler, HTTPServer
from io import BytesIO
from socketserver import ThreadingMixIn
from time import sleep
import depthai as dai
import numpy as np
import cv2
from PIL import Image
import blobconverter
HTTP_SERVER_PORT = 8090
class TCPServerRequest(socketserver.BaseRequestHandler):
def handle(self):
# Handle is called each time a client is connected
# When OpenDataCam connects, do not return - instead keep the connection open and keep streaming data
# First send HTTP header
header = 'HTTP/1.0 200 OK\r\nServer: Mozarella/2.2\r\nAccept-Range: bytes\r\nConnection: close\r\nMax-Age: 0\r\nExpires: 0\r\nCache-Control: no-cache, private\r\nPragma: no-cache\r\nContent-Type: application/json\r\n\r\n'
self.request.send(header.encode())
while True:
sleep(0.1)
if hasattr(self.server, 'datatosend'):
self.request.send(self.server.datatosend.encode() + "\r\n".encode())
# HTTPServer MJPEG
class VideoStreamHandler(BaseHTTPRequestHandler):
def do_GET(self):
self.send_response(200)
self.send_header('Content-type', 'multipart/x-mixed-replace; boundary=--jpgboundary')
self.end_headers()
while True:
sleep(0.1)
if hasattr(self.server, 'frametosend'):
ok, encoded = cv2.imencode('.jpg', self.server.frametosend)
self.wfile.write("--jpgboundary".encode())
self.send_header('Content-type', 'image/jpeg')
self.send_header('Content-length', str(len(encoded)))
self.end_headers()
self.wfile.write(encoded)
self.end_headers()
class ThreadedHTTPServer(ThreadingMixIn, HTTPServer):
"""Handle requests in a separate thread."""
pass
# start TCP data server
server_TCP = socketserver.TCPServer(('localhost', 8070), TCPServerRequest)
th = threading.Thread(target=server_TCP.serve_forever)
th.daemon = True
th.start()
# start MJPEG HTTP Server
server_HTTP = ThreadedHTTPServer(('localhost', HTTP_SERVER_PORT), VideoStreamHandler)
th2 = threading.Thread(target=server_HTTP.serve_forever)
th2.daemon = True
th2.start()
syncNN = True
def create_pipeline():
# Start defining a pipeline
pipeline = dai.Pipeline()
# Define a source - color camera
colorCam = pipeline.create(dai.node.ColorCamera)
colorCam.setPreviewSize(300, 300)
colorCam.setResolution(dai.ColorCameraProperties.SensorResolution.THE_1080_P)
colorCam.setInterleaved(False)
colorCam.setColorOrder(dai.ColorCameraProperties.ColorOrder.BGR)
return pipeline
# Save the pipeline as a .dap file
pipeline = create_pipeline() # Change 'depth' based on your requirements
dai.DeviceBootloader.saveDepthaiApplicationPackage(
'./OAK1_MJPEG_Streaming.dap', # Where to save the .dap file
pipeline, # The pipeline you just created
compress=True, # Compress the file
applicationName='OAK1Streaming' # Optional application name
)
I have Couple of question.
First I am receiving the following error when i try to flash the .dap file
Secondly, do I need to change localhost with anything else.
# start TCP data server
server_TCP = socketserver.TCPServer(('localhost', 8070), TCPServerRequest)
th = threading.Thread(target=server_TCP.serve_forever)
th.daemon = True
th.start()
# start MJPEG HTTP Server
server_HTTP = ThreadedHTTPServer(('localhost', HTTP_SERVER_PORT), VideoStreamHandler)
th2 = threading.Thread(target=server_HTTP.serve_forever)
th2.daemon = True
th2.start()