I'm trying to calculate the center point of my eye using OpenCV and Python

Viewed 390

My program can detect my left eye, but I want it to be able to place a dot around on the center of the detected area. I tried to calculate it but it always places the dot near the upper left hand corner. Any Ideas/Help would be appreciated.

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
1 Answers

The problem is that the eye detector module works for one eye at a time. Even if you see two eyes being boxed in the image, that for loop is actually related with one eye.

The solution would be to separate the face box into two parts and detect the left and right eyes separately by looking coordinates.

If the explanation is not clear enough, I can explain it by editing your code. Just say it .

Here is the edited code:

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
Related