类型错误: + 不支持的操作数类型:"无类型"和"无类型"用于眨眼检测



我有一个问题"眨眼检测";使用Python、OpenCV和dlib。我在用Jupyter笔记本。

以下代码使用shape_predictor_68_face_landmarks.dat库绘制面上的68预定义点。

import cv2
#import numpy as np
import dlib
from math import hypot
cap = cv2.VideoCapture(0)
detector = dlib.get_frontal_face_detector()
predictor = dlib.shape_predictor("C:\Users\Asirajdin\dev\shape_predictor_68_face_landmarks
\shape_predictor_68_face_landmarks.dat")

def midpoint (p1, p2):
return int ((p1.x + p2.x)/2), int ((p1.y + p2.y)/2)
font = cv2.FONT_HERSHEY_SIMPLEX
def get_blinking_ratio (eye_points, facial_landmarks):
left_point = (facial_landmarks.part(eye_points[0]).x, facial_landmarks.part(eye_points[0]).y)
right_point = (facial_landmarks.part(eye_points[3]).x, facial_landmarks.part(eye_points[3]).y)
centre_top = midpoint (facial_landmarks.part(eye_points[1]), facial_landmarks.part(eye_points[2]))
centre_bottom = midpoint (facial_landmarks.part(eye_points[5]), 
facial_landmarks.part(eye_points[4]))

hor_line = cv2.line (frame, left_point, right_point, (0, 255,0), 2)
ver_line = cv2.line (frame, centre_top, centre_bottom, (0, 255,0), 2)

hor_line_length = hypot ((left_point[0] - right_point[0]), (left_point[1] - right_point[1])) 
ver_line_length = hypot((centre_top[0] - centre_bottom[0]), (centre_top[1] - centre_bottom[1]))

ratio = hor_line_length/ver_line_length

while(True):
_, 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)

landmarks = predictor(gray, face)

left_eye_ratio = get_blinking_ratio([36, 37, 38, 39, 40, 41], landmarks)
right_eye_ratio = get_blinking_ratio([42, 43, 44, 45, 46, 47], landmarks)
blinking_ratio = ((left_eye_ratio + right_eye_ratio) / 2)

if blinking_ratio > 5.7:
cv2.putText(frame, "BLINKING", (50, 150), font, 5, (255, 0, 0))


cv2.imshow("Frame", frame)
key = cv2.waitKey(1)
if key == 27:
break
# when everything is done then release the capture        
cap.release()
cv2.destroyAllWindows()

错误:

TypeError                                 Traceback (most recent call last)
<ipython-input-2-ca92d06b8ea8> in <module>
45         left_eye_ratio = get_blinking_ratio([36, 37, 38, 39, 40, 41], landmarks)
46         right_eye_ratio = get_blinking_ratio([42, 43, 44, 45, 46, 47], landmarks)
---> 47         blinking_ratio = ((left_eye_ratio + right_eye_ratio) / 2)
48 
49         if blinking_ratio > 5.7:
TypeError: unsupported operand type(s) for +: 'NoneType' and 'NoneType'

我已经找到了我的帖子的解决方案。我意识到hor_line_length和ver_line_llength返回NoneType,然后我尝试将比率转换为整数。我将get_blinking_ratio下的比率返回为int,即

ratio = hor_line_length/ver_line_length
return int (ratio)

我是python的新手,虽然代码可以工作,但这可能不是最好的答案。谢谢

您的函数get_blinking_ratio不返回任何内容。在python中,这意味着它隐式地返回None

这就是None值在该除法中的显示方式。

在函数之后,有一些行似乎与比率有关。你打算让那些人进入功能区吗?然后你需要缩进它们。它们仍然不包含函数返回任何内容所需的return语句。

最新更新