0

trying to detect face landmarks using openCv, the error is :

Traceback (most recent call last):
  File "/Users/aksheenmalhotra/Desktop/gaze controlled/gazecontrolledkeys.py", line 13, in <module>
    gray = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
cv2.error: OpenCV(4.1.2) /Users/travis/build/skvark/opencv-python/opencv/modules/imgproc/src/color.cpp:182: error: (-215:Assertion failed) !_src.empty() in function 'cvtColor'

what could be the possible error? and how to solve, was getting similar error for other openCv project as well

import cv2
import numpy as np
import dlib
cap = cv2.VideoCapture()

detector = dlib.get_frontal_face_detector()
predictor = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat")
cap.release()
def midpoint(p1 ,p2):
    return int((p1.x + p2.x)/2), int((p1.y + p2.y)/2)
while True:
    ref, frame = cap.read()
    gray = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
    faces = detector(gray)
    for face in faces:
        #x, y = face.left(), face.top()
        #x1, y1 = face.right(), face.bottom()
        #cv2.rectangle(frame, (x, y), (x1, y1), (0, 255, 0), 2)
        center_top = midpoint(landmarks.part(37), landmarks.part(38))
        center_bottom = midpoint(landmarks.part(41), landmarks.part(40))
        hor_line = cv2.line(frame, left_point, right_point, (0, 255, 0), 2)
        ver_line = cv2.line(frame, center_top, center_bottom, (0, 255, 0), 2)
        cv2.imshow("Frame", frame)
    key = cv2.waitKey(1)
    if key == 27:
        break
cap.release()
cv2.destroyAllWindows()
  • 2
    Does this (and dozens of similar Q&As) answer your question? [OpenCV !\_src.empty() in function 'cvtColor' error](https://stackoverflow.com/questions/52676020/opencv-src-empty-in-function-cvtcolor-error) Check, if `ref` is `True`, and/or if `frame` is not `None`, before passing `frame` to `cv2.cvtColor`! – HansHirse Jan 27 '20 at 08:42

1 Answers1

0

This type error occurs because of non-able to load your frames. This topic also discussed here.

In your case, your VideoCapture() is reading nothing. You should add a camera number as int like : VideoCapture(0) which calls the default camera.

This example is good to start with.

Yunus Temurlenk
  • 4,085
  • 4
  • 18
  • 39