My main goal for this project I'm working on is to use a python script to get any webcam footage, edit it with opencv, and then pipe the edited video frames with ffmpeg to a dummy webcam from v4l2loopback. Here's the sample code I made that runs exactly as I want to on python 2.7:
import cv2
import subprocess as sp
import sys
cap = cv2.VideoCapture(1)
cv2.namedWindow('result', cv2.WINDOW_AUTOSIZE)
while True:
ret, frame = cap.read()
cv2.imshow('result', frame)
sys.stdout.write(frame.tostring())
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
and then run it with
python pySample.py | ffmpeg -f rawvideo -pixel_format bgr24 -video_size 640x480 -framerate 30 -i - -vf format=yuv420p -f v4l2 /dev/video17
However, I want it to work with python3 instead of 2.7, and I found a way to make it work where I replace the "sys.stdout..." line with
sys.stdout.buffer.write(frame.tobytes())
This works well, except that it only runs at 14 fps while the 2.7 code could run at 30. I'm kind of at a loss as to how to fix this issue/ what this issue exactly is. I'm running this on a raspberry pi if that helps. Thanks so much!