Ask Your Question
-1

Find distance between two eyes

asked 2017-11-23 13:05:46 -0600

fadkabli gravatar image

updated 2017-11-26 05:45:31 -0600

I have worked on harcascade face detection and eye detection. it works well now i am stuck finding distance between two eyes. please provide me some logic or piece to code to achive this.

import cv2 
import math 
import numpy as np from scipy.spatial 
import distance as dist

            def midpoint(ptA, ptB):
                return ((ptA[0] + ptB[0]) * 0.5, (ptA[1] + ptB[1]) * 0.5)

            def calculateDistance(ex,ey,ew,eh):
                 dist = math.sqrt((ex - ew)**2 + (ey - eh)**2)
                 return dist
            # print calculateDistance(x1, y1, x2, y2)

            # using harcasde  for face
            face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
            # https://github.com/Itseez/opencv/blob/master/data/haarcascades/haarcascade_eye.xml
            eye_cascade = cv2.CascadeClassifier('haarcascade_eye.xml')

            # read both the images of the face and the glasses
            image = cv2.imread('example_2.jpg')![image description](/upfiles/15116549809854402.png)

            gray    =   cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

            centers=[]
            faces = face_cascade.detectMultiScale(gray,1.3,5)

            #check for the face detected
            for (x,y,w,h) in faces:

                #create two Regions of Interest on face.
                roi_gray = gray[y:y+h, x:x+w]
                roi_color = image[y:y+h, x:x+w]
                eyes = eye_cascade.detectMultiScale(roi_gray)

                # Store the cordinates of eyes in the image to the 'center' array
                for (ex,ey,ew,eh) in eyes:
                    centers.append((x+int(ex+0.5*ew), y+int(ey+0.5*eh)))
                    # Point2f eye1, eye2;
                    # double res = cv::norm(eye1-eye2);

                    #creates rectangle with 'colour'
                    cv2.rectangle(roi_color,(ex,ey),(ex+ew,ey+eh),(0,255,0),2)


            overlay_img = np.ones(image.shape,np.uint8)*255

            #Create a mask and generate it's inverse.
            gray_glasses    =   cv2.cvtColor(overlay_img,   cv2.COLOR_BGR2GRAY)
            ret,    mask    =   cv2.threshold(gray_glasses, 110,    255,    cv2.THRESH_BINARY)
            mask_inv    =   cv2.bitwise_not(mask)
            temp    =   cv2.bitwise_and(image,  image,  mask=mask)
            temp2   =   cv2.bitwise_and(overlay_img,    overlay_img,    mask=mask_inv)
            final_img   =   cv2.add(temp,   temp2)

            # imS = cv2.resize(final_img, (1366, 768))
            # print calculateDistance(ex,ey,ew,eh)
            cv2.imshow('Final  Result', final_img)
            cv2.waitKey()
            cv2.destroyAllWindows()
edit retag flag offensive close merge delete

Comments

2
  1. Show proof that you have done something to try solving this issue. 2. Have u seriously not thought of finding the euclidean distance between the centre of the bounding boxes of the two eyes?
eshirima gravatar imageeshirima ( 2017-11-23 13:18:44 -0600 )edit

import cv2 import numpy as np

using harcasde for face

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

https://github.com/Itseez/opencv/blob...

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

read both the images of the face and the glasses

image = cv2.imread('example_2.jpg') glass_img = cv2.imread('h.png')

gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

centers=[] faces = face_cascade.detectMultiScale(gray,1.3,5)

check for the face detected

for (x,y,w,h) in faces:

#create two Regions of Interest on face.
roi_gray = gray[y:y+h, x:x+w]
roi_color = image[y:y+h, x:x+w]
eyes = eye_cascade.detectMultiScale(roi_gray)
fadkabli gravatar imagefadkabli ( 2017-11-23 13:32:48 -0600 )edit

Store the cordinates of eyes in the image to the 'center' array

for (ex,ey,ew,eh) in eyes:
    centers.append((x+int(ex+0.5*ew), y+int(ey+0.5*eh)))
    #creates rectangle with 'colour'
    cv2.rectangle(roi_color,(ex,ey),(ex+ew,ey+eh),(0,255,0),2)
fadkabli gravatar imagefadkabli ( 2017-11-23 13:33:51 -0600 )edit

I have used euclidean distance but it is not showing me proper result. i came up from nose to eye or ear to eye.

fadkabli gravatar imagefadkabli ( 2017-11-23 13:36:03 -0600 )edit
1

Please edit your question to include the code. Why are u using nose to eye or ear to eye if all u want is distance between eye to eye?

eshirima gravatar imageeshirima ( 2017-11-23 13:40:15 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
1

answered 2017-11-24 03:36:32 -0600

All you ever need is

Point2f eye1, eye2;
double res = cv::norm(eye1-eye2);

Just like others have pointer out several times before me

edit flag offensive delete link more

Comments

def calculateDistance(ex,ey,ew,eh): dist = math.sqrt((ex - ew)2 + (ey - eh)2) return distenter code here

fadkabli gravatar imagefadkabli ( 2017-11-25 17:16:37 -0600 )edit

i'am confused using above approach in harcascade.

fadkabli gravatar imagefadkabli ( 2017-11-25 17:17:33 -0600 )edit
2

@fadkablieye1 and eye2 are the (x, y) for the centers of the eyes. norm is a function in OpenCV for calculating the euclidean distance.

eshirima gravatar imageeshirima ( 2017-11-26 09:44:39 -0600 )edit
1

@eshirima thanx

fadkabli gravatar imagefadkabli ( 2017-12-01 16:38:47 -0600 )edit

@fadkabli Can you please tell me how you sorted out this issue? I am having trouble calculating the distance between the eyes.i have used the same code as yours.

a010291 gravatar imagea010291 ( 2018-10-05 01:44:17 -0600 )edit

@a010291 what's your issue?

eshirima gravatar imageeshirima ( 2018-10-05 11:17:12 -0600 )edit

@ehsirima I used the same code to calculate the distance between two eyes. I am new to this and can you kindly tell me where to add the above two lines in the above code to get the distance.

a010291 gravatar imagea010291 ( 2018-10-05 11:48:38 -0600 )edit

@a010291 Add this after the center of the eyes has been found. Try it inside for (ex,ey,ew,eh) in eyes:

eshirima gravatar imageeshirima ( 2018-10-05 15:47:50 -0600 )edit

@eshirima That is what i changed and shows syntax error. Also is the unit inches?

 for (ex,ey,ew,eh) in eyes:
        centers.append((x+int(ex+0.5*ew), y+int(ey+0.5*eh)))
        Point2f ex, ey; // syntax error here
        double res = cv2.norm(ex-ey);
a010291 gravatar imagea010291 ( 2018-10-05 20:56:24 -0600 )edit

The unit is pixels since in plain images you have no knowledge of sizes what so ever, unless you calibrate the pixels size towards real world sizes. you have an error because you forgot a ; in your code.

StevenPuttemans gravatar imageStevenPuttemans ( 2018-10-17 05:11:28 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2017-11-23 13:05:46 -0600

Seen: 3,480 times

Last updated: Nov 26 '17