OpenCV(4.5.5) :-1: error: (-5:Bad argument) in function 'cvtColor'

Question:

Can you guys help me? I’m trying to use my own data (path = data) to create a dataset by applying the mediapipe on my videos. The processed vid (.ny) will be in output folder (path = O_Video) which I have declared previously. No of seq is 30 as I have 30 videos with the start_folder = 0 and start_video = 0.

videos = cv2.VideoCapture(IMPORT_DATA)
videos_input = cv2.cvtColor(videos, cv2.COLOR_BGR2RGB)

# Set mediapipe model 
with mp_holistic.Holistic(min_detection_confidence=0.5, min_tracking_confidence=0.5) as holistic:
    
    # NEW LOOP
    # Loop through actions
    for action in actions:
    # Loop through sequences aka videos
        for sequence in range(start_folder, start_video+no_sequences):

                # get results
            results = mp_face.process(videos_input)

            for detection in results.detections: # iterate over each detection and draw on image
                  mp_drawing.draw_detection(videos, detection)
                
                
                # NEW Export keypoints
            keypoints = extract_keypoints(results)
            npy_path = os.path.join(DATA_PATH, action, str(sequence))
            np.save(npy_path, keypoints)

                # Break gracefully
            if cv2.waitKey(10) & 0xFF == ord('q'):
                break
                    
    cap.release()
    cv2.destroyAllWindows()

Error that I’m getting is as below:

Error                                     Traceback (most recent call last)
Input In [15], in <module>
      1 videos = cv2.VideoCapture(IMPORT_DATA)
----> 2 videos_input = cv2.cvtColor(videos, cv2.COLOR_BGR2RGB)
      4 # Set mediapipe model 
      5 with mp_holistic.Holistic(min_detection_confidence=0.5, min_tracking_confidence=0.5) as holistic:
      6     
      7     # NEW LOOP
      8     # Loop through actions

error: OpenCV(4.5.5) :-1: error: (-5:Bad argument) in function 'cvtColor'
> Overload resolution failed:
>  - src is not a numpy array, neither a scalar
>  - Expected Ptr<cv::UMat> for argument 'src'
Asked By: BaiduriRazak

||

Answers:

You can’t pass a VideoCapture object to cvtColor.

You have to pass each frame (numpy array) individually.

vid = cv.VideoCapture(...)
assert vid.isOpened()

while True:
    (valid, frame) = vid.read()
    if not valid:
        break

    converted = cv.cvtColor(frame, ...)
    ...
Answered By: Christoph Rackwitz

you need to capture the frame data as shown below.

videos = cv2.VideoCapture(IMPORT_DATA)
# Capture the video by frame
ret, frame = videos.read()
# check ret for success and then do this
videos_input = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)

Answered By: balu
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.