我有一个 python 脚本夹在 ffmpeg 输入和输出命令之间 逻辑上如下所示:
ffmpeg -i webcam -vf format=yuv420p -f rawvideo - | python below.py | ffmpeg -f rawvideo -video_size 640x480 -i - -f sometype some_output
以下是python脚本片段:
import cv2 as cv
import sys
import subprocess as sp
import numpy as np
if sys.platform == "win32":
import os, msvcrt
msvcrt.setmode(sys.stdout.fileno(), os.O_BINARY)
width = 640
height = 480
FFMPEG_BIN = 'ffmpeg'
command = [ FFMPEG_BIN,
'-re',
'-f', 'dshow',
'-rtbufsize', '200M',
'-i', 'video=USB2.0 VGA UVC WebCam',
'-vf', 'format=yuv420p',
'-f', 'rawvideo', '-']
pipe = sp.Popen(command, stdout = sp.PIPE, bufsize=10**8)
while True:
raw_image = pipe.stdout.read(width*height*3)
frame = np.frombuffer(raw_image, dtype='uint8')
frame = frame.reshape((height,width,3))
# cv.rectangle(frame, (xmin, ymin), (xmax, ymax), color=(0, 255, 0), thickness=1)
sys.stdout.buffer.write( frame.tostring() )
我想在YUV框架上画一个矩形。我似乎无法获得正确的颜色(应该是绿色 - (0, 255, 0))
到目前为止,我的理解是将框架分解为 Y、U、V 三个部分,然后重新加入它们。不确定这是否正确,我应该在哪个图层上调用 cv.rectangle?