1
votes
import cv2
video=cv2.VideoCapture(0)
img=video.read()
video2=cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
cv2.imshow("camera",video2)
cv2.waitKey(0)

"HERE IS THE ERROR" """ Traceback (most recent call last): File "C:/Users/D_Ommy/PycharmProjects/Machine_Learning/sampleone.py", line 4, in video2=cv2.cvtColor(img, cv2.COLOR_BGR2RGB) TypeError: Expected Ptr<cv::UMat> for argument 'src' [ WARN:0] global C:\Users\appveyor\AppData\Local\Temp\1\pip-req-build-cff9bdsm\opencv\modules\videoio\src\cap_msmf.cpp (435) `anonymous-namespace'::SourceReaderCB::~SourceReaderCB terminating async callback

"""

2
Check the doc on VideoCapture.read(): By img = video.read(), you'll get a tuple (retval, image) for img, which isn't a valid input for cvtColor().HansHirse

2 Answers

0
votes

For this let's go each and every step and what the code is doing.

cv2.VideoCapture(0)

The VideoCapture() on OpenCV returns an object that can be used to capture video frame-by-frame (this is not the actual video). Once this object is created, the next step as rightly done should be to read the frames.

img=video.read()

This OBJECT.read() method returns TWO values instead of one, a boolean flag and the frame. This is what missing here. The correct code for this kind of work could be something as follows.

import cv2

cap = cv2.VideoCapture('vtest.avi')

while(cap.isOpened()):
    ret, frame = cap.read()

    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

    cv2.imshow('frame',gray)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

cap.release()
cv2.destroyAllWindows()

For details, it's always better to consult the official documentation with code examples. https://opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/py_gui/py_video_display/py_video_display.html

0
votes

Add this parameter "cv2.CAP_DSHOW" to the second line of your code :

import cv2
video=cv2.VideoCapture(0, cv2.CAP_DSHOW)
img=video.read()
video2=cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
cv2.imshow("camera",video2)
cv2.waitKey(0)