Обнаружение и калибровка взгляда с использованием OpenCV python - PullRequest
0 голосов
/ 18 марта 2019

Я делаю программу, в которой я создал графический интерфейс, используя tkinter, который отображает 4 вертикальных прямоугольника на экране по горизонтали. Теперь, что программа должна сделать, так это то, что если пользователь смотрит на коробку в течение 5 секунд или около того, цвет этой коробки меняется, только!

Однако главная проблема, с которой я сталкиваюсь, заключается в том, что код неправильно определяет мой взгляд. Он показывает некоторое значение, когда я двигаю глазами, но в основном показывает одинаковые значения в обоих углах. Одновременно значения также сильно колеблются. Я все еще не могу откалибровать взгляд правильно. Я приложил код ниже:

import cv2
import numpy as np
import dlib
from math import hypot
from tkinter import *

cap = cv2.VideoCapture(0)

detector = dlib.get_frontal_face_detector()
predictor = dlib.shape_predictor("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_PLAIN

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)
    center_top = midpoint(facial_landmarks.part(eye_points[1]), facial_landmarks.part(eye_points[2]))
    center_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, center_top, center_bottom, (0, 255, 0), 2)

    hor_line_lenght = hypot((left_point[0] - right_point[0]), (left_point[1] - right_point[1]))
    ver_line_lenght = hypot((center_top[0] - center_bottom[0]), (center_top[1] - center_bottom[1]))

    ratio = hor_line_lenght / ver_line_lenght
    return ratio

def get_gaze_ratio(eye_points, facial_landmarks):
    left_eye_region = np.array([(facial_landmarks.part(eye_points[0]).x, facial_landmarks.part(eye_points[0]).y),
                                (facial_landmarks.part(eye_points[1]).x, facial_landmarks.part(eye_points[1]).y),
                                (facial_landmarks.part(eye_points[2]).x, facial_landmarks.part(eye_points[2]).y),
                                (facial_landmarks.part(eye_points[3]).x, facial_landmarks.part(eye_points[3]).y),
                                (facial_landmarks.part(eye_points[4]).x, facial_landmarks.part(eye_points[4]).y),
                                (facial_landmarks.part(eye_points[5]).x, facial_landmarks.part(eye_points[5]).y)], np.int32)
    # cv2.polylines(frame, [left_eye_region], True, (0, 0, 255), 2)

    height, width, _ = frame.shape
    mask = np.zeros((height, width), np.uint8)
    cv2.polylines(mask, [left_eye_region], True, 255, 2)
    cv2.fillPoly(mask, [left_eye_region], 255)
    eye = cv2.bitwise_and(gray, gray, mask=mask)

    min_x = np.min(left_eye_region[:, 0])
    max_x = np.max(left_eye_region[:, 0])
    min_y = np.min(left_eye_region[:, 1])
    max_y = np.max(left_eye_region[:, 1])

    gray_eye = eye[min_y: max_y, min_x: max_x]
    _, threshold_eye = cv2.threshold(gray_eye, 70, 255, cv2.THRESH_BINARY)
    height, width = threshold_eye.shape
    left_side_threshold = threshold_eye[0: height, 0: int(width / 2)]
    left_side_white = cv2.countNonZero(left_side_threshold)

    right_side_threshold = threshold_eye[0: height, int(width / 2): width]
    right_side_white = cv2.countNonZero(right_side_threshold)

    if left_side_white == 0:
        gaze_ratio = 1
    elif right_side_white == 0:
        gaze_ratio = 5
    else:
        gaze_ratio = left_side_white / right_side_white
    return gaze_ratio

root = Tk()
lf = Frame(root)
lf.pack()
rf = Frame(root)
rf.pack()
Canvas = Canvas(root, width = 1300, height=650)
Canvas.pack()

while True:
    _, frame = cap.read()
    new_frame = np.zeros((500, 500, 3), np.uint8)
    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)
        """
        # Detect blinking
        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, 7, (255, 0, 0))
        """

        # Gaze detection
        gaze_ratio_left_eye = get_gaze_ratio([36, 37, 38, 39, 40, 41], landmarks)
        gaze_ratio_right_eye = get_gaze_ratio([42, 43, 44, 45, 46, 47], landmarks)
        gaze_ratio = (gaze_ratio_right_eye + gaze_ratio_left_eye) / 2

        cv2.putText(frame, str(gaze_ratio), (50, 200), font, 2, (0, 0, 255), 3)


        Canvas.create_rectangle(0,0,325,600,fill = "Lightgrey")
        Canvas.create_rectangle(325,0,650,600,fill = "Lightgrey")
        Canvas.create_rectangle(650,0,975,600,fill = "Lightgrey")
        Canvas.create_rectangle(975,0,1300,600,fill = "Lightgrey")
        Canvas.create_text(165,300,text = "Drinking", font = "Times 25 bold")
        Canvas.create_text(490,300,text = "Food", font = "Times 25 bold")
        Canvas.create_text(805,300,text = "Sleep", font = "Times 25 bold")
        Canvas.create_text(1140,300,text = "Washroom", font = "Times 25 bold")
        def Drinking():
            Canvas.create_rectangle(0,0,325,600,fill = "yellow")
            Canvas.create_text(165,300,text = "Drinking", font = "Times 25 bold")

        def Food():
            Canvas.create_rectangle(325,0,650,600,fill = "yellow")
            Canvas.create_text(490,300,text = "Food", font = "Times 25 bold")

        def Sleep():
            Canvas.create_rectangle(650,0,975,600,fill = "yellow")
            Canvas.create_text(805,300,text = "Sleep", font = "Times 25 bold")

        def Washroom():
            Canvas.create_rectangle(975,0,1300,600,fill = "yellow")
            Canvas.create_text(1140,300,text = "Washroom", font = "Times 25 bold")

        Button(root, text = "change color", command=Drinking).pack(side = LEFT)
        Button(root, text = "change color", command=Food).pack(side = LEFT)
        Button(root, text = "change color", command=Washroom).pack(side = RIGHT)
        Button(root, text = "change color", command=Sleep).pack(side = RIGHT)
        lf.pack(side = LEFT)
        rf.pack(side = RIGHT)
        root.mainloop() 


        if gaze_ratio <= 0.4:
            cv2.putText(frame, "Washroom", (50, 100), font, 2, (0, 0, 255), 3)
            new_frame[:] = (0, 0, 255)
            #Canvas.pack()
            #Drinking()

        elif 0.45 <= gaze_ratio <= 0.8:
            cv2.putText(frame, "Sleep", (50, 100), font, 2, (0, 0, 255), 3)
            #Food()

        elif 0.8 <= gaze_ratio <= 0.9:
            cv2.putText(frame, "Food", (50, 100), font, 2, (0, 0, 255), 3)
            #Sleep()

        elif 0.9 <= gaze_ratio <= 1.3:
            cv2.putText(frame, "Drinking", (50, 100), font, 2, (0, 0, 255), 3)
            #Washroom()


    cv2.imshow("Frame", frame)
    #cv2.imshow("New frame", new_frame)

    key = cv2.waitKey(1)
    if key == 27:
        break

cap.release()
cv2.destroyAllWindows()
...