I tried to get frames from a webcam and process them in python. The webcam tells me that it uses the YU12 codec. The unprocessed frame(1280x720) looks like: You should see in the picture a cup of coffee, my arm and my monitor in the background. For some reason the picture looks odd. Look at the pot handle.
If I try to convert it to RGB I get the following error:
cv2.error: OpenCV(4.1.2) /io/opencv/modules/imgproc/src/color.simd_helpers.hpp:92: error: (-2:Unspecified error) in function 'cv::impl::{anonymous}::CvtHelper::CvtHelper(cv::InputArray, cv::OutputArray, int) [with VScn = cv::impl::{anonymous}::Set<1>; VDcn = cv::impl::{anonymous}::Set<3, 4>; VDepth = cv::impl::{anonymous}::Set<0>; cv::impl::{anonymous}::SizePolicy sizePolicy = (cv::impl::::SizePolicy)1u; cv::InputArray = const cv::_InputArray&; cv::OutputArray = const cv::_OutputArray&]' Invalid number of channels in input image: 'VScn::contains(scn)' where 'scn' is 3
import os
import sys
import cv2
videoSource = 0
def getFrame():
""""""
cv_cam_0 = cv2.VideoCapture(0)
if not cv_cam_0.isOpened():
raise Exception('video source: %s could not be opened' %(str(videoSource)))
codec_char_code = int(cv_cam_0.get(cv2.CAP_PROP_FOURCC))
a = chr(0x000000FF& codec_char_code)
b = chr((0x0000FF00& codec_char_code) >> 8)
c = chr((0x00FF0000& codec_char_code) >> 16)
d = chr((0xFF000000& codec_char_code) >> 24)
print('codec 4 char code: ' + a+b+c+d)
ret, raw_frame = cv_cam_0.read()
cv2.imwrite('/tmp/test0.jpg', raw_frame)
rgbFrame = cv2.cvtColor(raw_frame, cv2.COLOR_YUV2RGB_I420)
cv2.imwrite('/tmp/testConvert.jpg', rgbFrame)
def main(args):
getFrame()
sys.exit()
if __name__ == "__main__":
main(sys.argv)
if I use mplayer the picture from the webcam looks fine. For debugging purpose output from mplayer:
Could not find matching colorspace - retrying with -vf scale... Opening video filter: [scale] Movie-Aspect is undefined - no prescaling applied. [swscaler @ 0x5638ca496560] bicubic scaler, from yuyv422 to yuv420p using MMXEXT [swscaler @ 0x5638ca496560] using unscaled yuyv422 -> yuv420p special converter VO: [xv] 1920x1080 => 1920x1080 Planar YV12 Selected video codec: [rawyuy2] vfm: raw (RAW YUY2)
cvtColor(..., cv2.COLOR_YUV2BGR)
– Mark SetchellYU12
format. Please save it in PNG format (not JPEG). Save it as Grayscale image, not RGB (because YU12 has no colors), and post the new image. – Rotem