H
houzd

  • Aug 25, 2023
  • Joined Jul 7, 2023
  • 0 best answers
  • I need to output the h264 or h265 video stream from the camera using cv2 or PIL. this is necessary to transfer a video stream from one computer to another. the problem is that I do not know how to output frames using one of these codecs.
    that's how I get to output with the MJPEG codec, but when I use h265\h 264, cv2.imdecode(frame, v2.IMREAD_COLOR) is None:

    import cv2
    import depthai as dai
    
    pipeline = dai.Pipeline()
    
    camRgb = pipeline.create(dai.node.ColorCamera)
    xoutVideo = pipeline.create(dai.node.XLinkOut)
    
    xoutVideo.setStreamName("video")
    
    camRgb.setBoardSocket(dai.CameraBoardSocket.CAM_A)
    camRgb.setResolution(dai.ColorCameraProperties.SensorResolution.THE_1080_P)
    camRgb.setVideoSize(1920, 1080)
    camRgb.setFps(60)
    videoEnc = pipeline.create(dai.node.VideoEncoder)
    videoEnc.setDefaultProfilePreset(60, dai.VideoEncoderProperties.Profile.MJPEG)
    
    camRgb.video.link(videoEnc.input)
    videoEnc.bitstream.link(xoutVideo.input)
    
    with dai.Device(pipeline) as device:
    
        video = device.getOutputQueue(name="video", maxSize=1, blocking=False)
    
        while True:
            videoIn = video.get()
    
            frame = videoIn.getData()
    
            frame = cv2.imdecode(frame, cv2.IMREAD_COLOR)
            cv2.imshow("video", frame)
    
            if cv2.waitKey(1) == ord('q'):
                break
    • how to use a laser in OAK-D PRO to determine the distance to an arbitrary point?

      • Hello , I need to make a server on the computer to which the OAK-D PRO camera will be connected. From another computer I need to connect from the server and connect to the camera.I am trying to transfer a video from the server to the client, but it gives an error: server_socket.send to(message,client_addr) OSError: [Errno 90] The message is too long.

        code for the client

        # This is client code to receive video frames over UDP
        import cv2, imutils, socket
        import numpy as np
        import time
        import base64
        
        BUFF_SIZE = 65536
        client_socket = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
        client_socket.setsockopt(socket.SOL_SOCKET,socket.SO_RCVBUF,BUFF_SIZE)
        host_name = socket.gethostname()
        #host_ip = '192.168.1.45'#  socket.gethostbyname(host_name)
        host_ip = '127.0.0.1'#  socket.gethostbyname(host_name)
        print(host_ip)
        port = 9999
        message = b'Hello'
        
        client_socket.sendto(message,(host_ip,port))
        fps,st,frames_to_count,cnt = (0,0,20,0)
        while True:
            packet,_ = client_socket.recvfrom(BUFF_SIZE)
            data = base64.b64decode(packet,' /')
            #npdata = np.fromstring(data, dtype=np.uint8)
            npdata = np.frombuffer(data, dtype=np.uint8)
            frame = cv2.imdecode(npdata,1)
            #frame = cv2.putText(frame,'FPS: '+str(fps),(10,40),cv2.FONT_HERSHEY_SIMPLEX,0.7,(0,0,255),2)
            cv2.imshow("RECEIVING VIDEO",frame)
            key = cv2.waitKey(1) & 0xFF
            if key == ord('q'):
                client_socket.close()
                break
            if cnt == frames_to_count:
                try:
                    fps = round(frames_to_count/(time.time()-st))
                    st=time.time()
                    cnt=0
                except:
                    pass
            cnt+=1

        code for the server:

        # This is server code to send video frames over UDP
        import cv2, imutils, socket
        import numpy as np
        import time
        import base64
        import depthai as dai
        BUFF_SIZE = 65536
        server_socket = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
        server_socket.setsockopt(socket.SOL_SOCKET,socket.SO_RCVBUF,BUFF_SIZE)
        host_name = socket.gethostname()
        host_ip = '127.0.0.1'#  socket.gethostbyname(host_name)
        print(host_ip)
        port = 9999
        socket_address = (host_ip,port)
        server_socket.bind(socket_address)
        print('Listening at:',socket_address)
        
        
        
        # Create pipeline
        pipeline = dai.Pipeline()
        
        # Define source and output
        camRgb = pipeline.create(dai.node.ColorCamera)
        xoutVideo = pipeline.create(dai.node.XLinkOut)
        
        xoutVideo.setStreamName("video")
        
        # Properties
        camRgb.setBoardSocket(dai.CameraBoardSocket.CAM_A)
        camRgb.setResolution(dai.ColorCameraProperties.SensorResolution.THE_1080_P)
        #camRgb.setVideoSize(1920, 1080)
        
        xoutVideo.input.setBlocking(False)
        xoutVideo.input.setQueueSize(1)
        
        # Linking
        camRgb.video.link(xoutVideo.input)
        
        # Connect to device and start pipeline
        with dai.Device(pipeline) as device:
        
            video = device.getOutputQueue(name="video", maxSize=1, blocking=False)
            #vid = cv2.VideoCapture(0) #  replace 'rocket.mp4' with 0 for webcam
            fps,st,frames_to_count,cnt = (0,0,20,0)
        
            while True:
                msg,client_addr = server_socket.recvfrom(BUFF_SIZE)
                print('GOT connection from ',client_addr)
                WIDTH=400
                vid = video.get()
                frame = vid.getCvFrame()
                frame = imutils.resize(frame)
                cv2.imshow('TRANSMITTING VIDEO', frame)
                while True:
                    encoded,buffer = cv2.imencode('.jpg',frame,[cv2.IMWRITE_JPEG_QUALITY,80])
                    message = base64.b64encode(buffer)
                    server_socket.sendto(message,client_addr)
                    #frame = cv2.putText(frame,'FPS: '+str(fps),(10,40),cv2.FONT_HERSHEY_SIMPLEX,0.7,(0,0,255),2)
        
                    key = cv2.waitKey(1) & 0xFF
                    if key == ord('q'):
                        server_socket.close()
                        break
                    if cnt == frames_to_count:
                        try:
                            fps = round(frames_to_count/(time.time()-st))
                            st=time.time()
                            cnt=0
                        except:
                            pass
                    cnt+=1
        • erik replied to this.