facing a the error : (-215:Assertion failed) !_src.empty() in function ‘cv::cvtColor’

i was making a Computer Vision to Control Facebook & Instagram but I am facing an error

(-215:Assertion failed) !_src.empty() in function ‘cv::cvtColor’

my code is

import cv2
import numpy as np
import pyautogui

cap = cv2.VideoCapture(1)

yellow_lower = np.array([22, 93, 0])
yellow_upper = np.array([45, 255, 255])
prev_y = 0

while True:
    ret, frame = cap.read()
    hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
    mask = cv2.inRange(hsv, yellow_lower, yellow_upper)
    contours, hierarchy = cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

    for c in contours:
        area = cv2.contourArea(c)
        if area > 300:
            x, y, w, h = cv2.boundingRect(c)
            cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 2)
            if y < prev_y:
                pyautogui.press('space')

            prev_y = y
    cv2.imshow('frame', frame)
    if cv2.waitKey(10) == ord('q'):
        break

Source: Python-3x Questions

LEAVE A COMMENT