1

I am getting the following warning:

[ WARN:0@1.140] global /private/var/folders/sy/f16zz6x50xz3113nwtb9bvq00000gp/T/abs_506zufg7xt/croots/recipe/opencv-suite_1664548331847/work/modules/videoio/src/cap_gstreamer.cpp (862) isPipelinePlaying OpenCV | GStreamer warning: GStreamer: pipeline have not been created

I am attempting to run the following code on my MacBook Pro with MacOS 10.15.7 to stream video from a pycamera on my raspberrypi to my MacBook in order to use it as a virtual camera:

import cv2
import numpy as np
import io
import socket
import struct
import time
from Quartz import CoreVideo
import CoreMedia

IMG_W = 1280
IMG_H = 720

server_socket = socket.socket()
server_socket.bind(('0.0.0.0', 8000))
server_socket.listen(0)

connection = server_socket.accept()[0].makefile('rb')

cam = cv2.VideoCapture(0)
cam.set(cv2.CAP_PROP_FRAME_WIDTH, IMG_W)
cam.set(cv2.CAP_PROP_FRAME_HEIGHT, IMG_H)

try:
        display_link = CoreVideo.CVDisplayLinkCreateWithActiveCGDisplays(None)
        while True:
                ret, frame = cam.read()
                flipped = cv2.flip(frame, 1)
                if frame is not None:
                        print("frame is not None, proceed with flip")
                        flipped = cv2.flip(frame, 1)
                        frame[0 : IMG_H, IMG_W//2 : IMG_W] = flipped[0 : IMG_H, IMG_W//2 : IMG_W]
                else:
                        print("frame is None, skipping flip")
                # Read the length of the image as a 32-bit unsigned int.
                image_len_packed = connection.read(struct.calcsize('<L'))
                if not image_len_packed:
                        break
                image_len = struct.unpack('<L', image_len_packed)[0]
                # Read the image data.
                image_stream = io.BytesIO()
                remaining = image_len
                while remaining > 0:
                        chunk = connection.read(min(remaining, 4096))
                        if not chunk:
                                break
                        image_stream.write(chunk)
                        remaining -= len(chunk)
                # Rewind the stream
                image_stream.seek(0)

                image = np.array(bytearray(image_stream.read()), dtype=np.uint8)
                frame = cv2.imdecode(image, cv2.IMREAD_COLOR)
                # Resize the frame to the desired size for the virtual camera
                if frame is not None and frame.shape == (IMG_H, IMG_W, 3):
                        print("frame is not None and has correct shape")
                        frame = cv2.resize(frame, (IMG_W, IMG_H))
                else:
                        print("frame is None or has incorrect shape, skipping resize")
                # Convert the frame to a format suitable for OS X's AVFoundation framework
                if frame is not None and frame.shape[0] > 0 and frame.shape[1] > 0:
                        print("frame is not None and has dimensions")
                        frame = cv2.cvtColor(frame, cv2.COLOR_BGR2BGRA)
                else:
                        print("frame is None or has no dimensions, skipping cvtColor")

                # Create a `CVPixelBuffer` from the image data
                if frame is not None:
                        print("frame and pixel_buffer are not none")
                        pixel_buffer = CoreVideo.CVPixelBufferCreateWithBytes(
                                IMG_W, IMG_H, CoreVideo.kCVPixelFormatType_32BGRA,
                                frame.tobytes(), frame.strides[0],
                                None, None, None, None, None,
                        )

                        # Create a `CMSampleBuffer` from the pixel buffer
                        sample_buffer = CoreMedia.CMSampleBufferCreateForImageBuffer(
                                None, pixel_buffer, True, None, None,
                                CoreMedia.kCMSampleAttachmentKey_DisplayImmediately,
                        )

                        # Send the sample buffer to the virtual camera
                        CoreVideo.CVDisplayLinkStart(display_link)
                        CoreVideo.CVPixelBufferLockBaseAddress(pixel_buffer, 0)
                        CoreVideo.CVDisplayLinkRender(display_link, sample_buffer)
                        CoreVideo.CVPixelBufferUnlockBaseAddress(pixel_buffer, 0)
                        CoreVideo.CVDisplayLinkStop(display_link)
                else:
                        print("frame is None, skipping")

except Exception as e:
        print(e)

finally:
        cam.release()
        CoreVideo.CVDisplayLinkStop(display_link)
        CoreVideo.CVDisplayLinkRelease(display_link)

And here is my corresponding code that I am running on my RaspberryPi to transmit the stream:

import io
import socket
import struct
import time
import picamera

# Create a socket connection between the Raspberry Pi and the computer
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client_socket.connect(('10.37.68.171', 8000))
connection = client_socket.makefile('wb')

try:
    with picamera.PiCamera() as camera:
        camera.resolution = (640, 480)
        # Start a stream to the socket
        camera.start_recording(connection, format='h264')
        while True:
            camera.wait_recording(1)
finally:
    camera.stop_recording()
    connection.close()
    client_socket.close()

First: How can I fix the warning? Or is it even necessary to fix it?

Second: Is there anything in general that I should be doing differently with my code in order to be able to use the stream as a virtual camera on my MacBook?

Christoph Rackwitz
  • 11,317
  • 4
  • 27
  • 36
Jacob Anderson
  • 1,747
  • 1
  • 12
  • 17
  • please whittle this code down to be closer to a [mre]. I see lots of moving parts that are extremely unlikely to be required to reproduce the issue. specifically: you use the picamera module on your pi, and then you send the data over a TCP socket... and then you receive that on the other host. this task should not contain any VideoCapture calls because that accesses cameras on the macos host, which isn't the issue, right? – Christoph Rackwitz Feb 06 '23 at 23:28

0 Answers0