0

I'm a beginner in image processing. I want to detect eye movement from webcam using openCV in python. I'm using this code snippet for the detection of right eye:

eye_cascade = cv2.CascadeClassifier('haarcascade_righteye_2splits.xml')
eyes = eye_cascade.detectMultiScale(gray, minSize=(30, 30))

for (ex, ey, ew, eh) in eyes:
    cv2.rectangle(frame, (ex, ey), (ex+ew, ey+eh), (0, 255, 0), 2)

    print(str(ex)+'b'+str(ey))
    print("Sent\n")

It displays the pixels of my right eye in real time, like this:

.
.
.
260b216
Sent

261b219
Sent
.
.
.

I can see the pixel values change when I move my eyes, but I want python to calculate the distance between two consecutive pixel values: sqrt((261-260)^2 + (219-216)^2), and display this value as output, in real time. Which numpy/math/other functions do I have to use to achieve this?

1 Answers1

0

You want to calculate the euclidian distance between two consecutive frames. (source)

You can initialize a counter cnt for counting the two consecutive frames. Each time you get the (ex, ey, ew, eh) increase the cnt by one. If you get the two consecutive frames calculate the distance between them and print.

To calculate the distance you need to store the previous coordinates (exy_prv) and current (exy_cur)

Here is an example code:

import numpy as np

eyes = [[260, 216, 260 + 50, 216 + 50],
        [261, 219, 261 + 50, 219 + 50],
        [262, 222, 262 + 50, 222 + 50],
        [263, 224, 263 + 50, 224 + 50]]

cnt = 1
exy_prv = []
dist = None

for (ex, ey, ew, eh) in eyes:
    if cnt % 2 == 0:
        exy_cur = np.array((ex, ey))
        dist = np.linalg.norm(exy_cur - exy_prv)
        cnt = 1
        exy_cur = exy_prv

    # cv2.rectangle(frame, (ex, ey), (ex+ew, ey+eh), (0, 255, 0), 2)

    print(str(ex)+'b'+str(ey))
    print("Sent\n")

    if dist is not None:
        print("distance: {}\n".format(dist))

    exy_prv = np.array((ex, ey))
    cnt += 1

Result


260b216
Sent

261b219
Sent

3.1622776601683795

262b222
Sent

3.1622776601683795

263b224
Sent

2.23606797749979
Ahmet
  • 7,527
  • 3
  • 23
  • 47