python - 我正在尝试使用 OpenCV 和 Python 计算我眼睛的中心点

标签 python opencv

我的程序可以检测我的左眼,但我希望它能够在检测到的区域的中心放置一个点。我尝试计算它,但它总是将点放置在左上角附近。任何想法/帮助将不胜感激。

import cv2


face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
eye_cascade = cv2.CascadeClassifier('haarcascade_eye.xml')

cap = cv2.VideoCapture(0)  # sets up webcam

while 1:  # capture frame, converts to greyscale, looks for faces
    ret, img = cap.read()
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    faces = face_cascade.detectMultiScale(gray, 1.3, 5)

    for (x, y, w, h) in faces:  # draws box around face
        cv2.rectangle(img, (x, y), (x + int(w / 2), y + int(h / 2)), (255, 0, 0), 2)  # Draws Box Around Face
        roi_gray = gray[y:y + int(h / 2), x:x + int(w / 2)]  # slices grey area based off given parameters
        roi_color = img[y:y + h, x:x + w]  # selects region of interest (roi) based on color
        eyes = eye_cascade.detectMultiScale(roi_gray)  # looks for eyes
        for (ex, ey, ew, eh) in eyes:  # draws boxes around eyes
            eye_centerX = (ex+(ex+ew))/2
            eye_centerY = (ey+(ey+eh))/2
            eye_center_cord = eye_centerX, eye_centerY
            cv2.line(img, (int(eye_centerX), int(eye_centerY)), (int(eye_centerX), int(eye_centerY)), (0, 0, 0), 10)

    cv2.imshow('img', img)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

最佳答案

问题在于眼睛检测器模块一次只针对一只眼睛工作。即使您看到图像中两只眼睛被框起来,该 for 循环实际上与一只眼睛相关。

解决方案是将人脸框分成两部分通过注视坐标分别检测左眼和右眼

如果解释不够清楚,我可以通过编辑你的代码来解释。就说吧。

这是编辑后的代码:

import cv2
left_eye_point_x = 0
right_eye_point_x = 0
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
eye_cascade = cv2.CascadeClassifier('haarcascade_eye.xml')

cap = cv2.VideoCapture(0)  # sets up webcam

while 1:  # capture frame, converts to greyscale, looks for faces
    ret, img = cap.read()
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    faces = face_cascade.detectMultiScale(gray, 1.3, 5)

    for (x, y, w, h) in faces:  # draws box around face
        cv2.rectangle(img, (x, y), (x + int(w / 2), y + int(h / 2)), (255, 0, 0), 2)  #
        roi_gray = gray[y:y + int(h / 2), x:x + int(w / 2)]  
        roi_color = img[y:y + h, x:x + w]  
        eyes = eye_cascade.detectMultiScale(roi_gray)  # looks for eyes
        for (ex, ey, ew, eh) in eyes:  # draws boxes around eyes
           if (ex+ey)/2 < (x+y)/2:
                left_eye_point_x = (ex+ey)/2
            elif (ex+ey)/2 > (x+y)/2:
                right_eye_point_x = (ex+ey)/2
            cv2.line(img, (int(left_eye_point_x), int(right_eye_point_x)), (int(left_eye_point_x), int(right_eye_point_x)), (0, 0, 0), 10)

    cv2.imshow('img', img)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

关于python - 我正在尝试使用 OpenCV 和 Python 计算我眼睛的中心点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67098077/

相关文章:

计算图像中的对象

c++ - 提取单个像素数据的最快方法?

opencv - 从 3d 点及其 2d 对应关系计算旋转和平移矩阵

python - 循环末尾的逗号是什么意思?

python - 如何使用Python和Selenium库与深度嵌套的 'input' html对象交互

python - 如何在学习曲线图中形成平坦的验证准确度曲线

python - 如何在 PySpark 中将字典转换为数据帧

python - 创建一个有序的字典

android - dlopen 失败 : library "/system/lib64/libhwuibp.so" not found : Honor 4C 64-bit octa-core CPU

OpenCV 从一个三角形变形到另一个三角形