This is my code I've been working on:
cap=cv2.VideoCapture(1)
if not cap.isOpened():
cap=cv2.VideoCapture(0)
if not cap.isOpened():
raise IOError("cannot open webcam")
faceCascade = cv2.CascadeClassifier(cv2.data.haarcascades+"haarcascade_frontalface_default.xml")
while True:
ret,frame=cap.read()
result= DeepFace.analyze(frame, actions = ['emotion'])
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
faces = faceCascade.detectMultiScale(gray, scaleFactor=1.05, minNeighbors=5)
for x, y, w, h in faces:
cv2.rectangle(frame, (x, y), (x + w, y + h), (66, 50, 200), 6)
font = cv2.FONT_HERSHEY_SIMPLEX
cv2.putText(frame,
result["dominant_emotion"],
(100, 200),
font, 10,
(0, 0, 255),
10,
cv2.LINE_4);
cv2.imshow("Original Video", frame)
if cv2.waitKey(2) & 0xFF==ord('q'):
break
cap.release()
cv2.destroyAllWindows()
The error I'm facing while running the code:
TypeError Traceback (most recent call last)
<ipython-input-5-e9c622d7a86c> in <module>
7 while True:
8 ret,frame=cap.read()
----> 9 result= DeepFace.analyze(frame, actions = ['emotion'])
10 gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
11 faces = faceCascade.detectMultiScale(gray, scaleFactor=1.05, minNeighbors=5)
~\Anaconda3\lib\site-packages\deepface\DeepFace.py in analyze(img_path, actions, models, enforce_detection, detector_backend, prog_bar)
389 if action == 'emotion':
390 emotion_labels = ['angry', 'disgust', 'fear', 'happy', 'sad', 'surprise', 'neutral']
--> 391 img, region = functions.preprocess_face(img = img_path, target_size = (48, 48), grayscale = True, enforce_detection = enforce_detection, detector_backend = detector_backend, return_region = True)
392
393 emotion_predictions = models['emotion'].predict(img)[0,:]
~\Anaconda3\lib\site-packages\deepface\commons\functions.py in preprocess_face(img, target_size, grayscale, enforce_detection, detector_backend, return_region, align)
173
174 #img might be path, base64 or numpy array. Convert it to numpy whatever it is.
--> 175 img = load_image(img)
176 base_img = img.copy()
177
~\Anaconda3\lib\site-packages\deepface\commons\functions.py in load_image(img)
72 exact_image = True
73
---> 74 elif len(img) > 11 and img[0:11] == "data:image/":
75 base64_img = True
76
TypeError: object of type 'NoneType' has no len()
If you read the traceback,
cap.read()
has returned a nullframe
. You should check the documentation to see why this may be.You can workaround the issue by checking its value, and raising your own error.