Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Find distance between two eyes

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.

Find distance between two eyes

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.

Find distance between two eyes

I have worked import cv2 import numpy as np

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

image = cv2.imread('example_2.jpg')

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 harcascade face detection and eye detection. it works well now i am stuck finding 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)))
    #creates rectangle with 'colour'
    cv2.rectangle(roi_color,(ex,ey),(ex+ew,ey+eh),(0,255,0),2)

///missing

    # compute the Euclidean distance between two eyes. please provide me some logic or piece the midpoints,
    # then construct the reference object
    D = dist.euclidean((tlblX, tlblY), (trbrX, trbrY))
    refObj = (box, (cX, cY), D / args["width"])
    continue

# draw the contours on the image
orig = image.copy()
cv2.drawContours(orig, [box.astype("int")], -1, (0, 255, 0), 2)
cv2.drawContours(orig, [refObj[0].astype("int")], -1, (0, 255, 0), 2)

# loop over the original points for ((xA, yA), (xB, yB), color) in zip(refCoords, objCoords, colors): # draw circles corresponding to the current points and # connect them with a line cv2.circle(orig, (int(xA), int(yA)), 5, color, -1) cv2.circle(orig, (int(xB), int(yB)), 5, color, -1) cv2.line(orig, (int(xA), int(yA)), (int(xB), int(yB)), color, 2)

    # compute the Euclidean distance between the coordinates,
    # and then convert the distance in pixels to distance in
    # units
    D = dist.euclidean((xA, yA), (xB, yB)) / refObj[2]
    (mX, mY) = midpoint((xA, yA), (xB, yB))
    cv2.putText(orig, "{:.1f}in".format(D), (int(mX), int(mY - 10)),
        cv2.FONT_HERSHEY_SIMPLEX, 0.55, color, 2)      


    # imS = cv2.resize(final_img, (1366, 768))
cv2.imshow('Final glasses Result',  final_img)
cv2.waitKey()
cv2.destroyAllWindows()

enter code to achive this. here

Find distance between two eyes

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 numpy as np

np # using harcasde for face

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

https://github.com/Itseez/opencv/blob/master/data/haarcascades/haarcascade_eye.xml

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')

cv2.CascadeClassifier('haarcascade_eye.xml') # read both the images of the face

face image = cv2.imread('example_2.jpg')

cv2.imread('example_2.jpg') gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

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

check face_cascade.detectMultiScale(gray,1.3,5) #check for the face detected

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

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)))
     #creates rectangle with 'colour'
     cv2.rectangle(roi_color,(ex,ey),(ex+ew,ey+eh),(0,255,0),2)

///missing



 ///missing

        # compute the Euclidean distance between the midpoints,
     # then construct the reference object
     D = dist.euclidean((tlblX, tlblY), (trbrX, trbrY))
     refObj = (box, (cX, cY), D / args["width"])
     continue

 # draw the contours on the image
 orig = image.copy()
 cv2.drawContours(orig, [box.astype("int")], -1, (0, 255, 0), 2)
 cv2.drawContours(orig, [refObj[0].astype("int")], -1, (0, 255, 0), 2)

# loop over the original points for ((xA, yA), (xB, yB), color) in zip(refCoords, objCoords, colors): # draw circles corresponding to the current points and # connect them with a line cv2.circle(orig, (int(xA), int(yA)), 5, color, -1) cv2.circle(orig, (int(xB), int(yB)), 5, color, -1) cv2.line(orig, (int(xA), int(yA)), (int(xB), int(yB)), color, 2)

2)

        # compute the Euclidean distance between the coordinates,
     # and then convert the distance in pixels to distance in
     # units
     D = dist.euclidean((xA, yA), (xB, yB)) / refObj[2]
     (mX, mY) = midpoint((xA, yA), (xB, yB))
     cv2.putText(orig, "{:.1f}in".format(D), (int(mX), int(mY - 10)),
         cv2.FONT_HERSHEY_SIMPLEX, 0.55, color, 2)      


     # imS = cv2.resize(final_img, (1366, 768))
 cv2.imshow('Final glasses Result',  final_img)
 cv2.waitKey()
 cv2.destroyAllWindows()

enter code here

click to hide/show revision 5
merged revision

Find distance between two eyes

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 numpy as np

# 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 
image = cv2.imread('example_2.jpg')

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)))
        #creates rectangle with 'colour'
        cv2.rectangle(roi_color,(ex,ey),(ex+ew,ey+eh),(0,255,0),2)


 ///missing

        # compute the Euclidean distance between the midpoints,
        # then construct the reference object
        D = dist.euclidean((tlblX, tlblY), (trbrX, trbrY))
        refObj = (box, (cX, cY), D / args["width"])
        continue

    # draw the contours on the image
    orig = image.copy()
    cv2.drawContours(orig, [box.astype("int")], -1, (0, 255, 0), 2)
    cv2.drawContours(orig, [refObj[0].astype("int")], -1, (0, 255, 0), 2)

 # loop over the original points
    for ((xA, yA), (xB, yB), color) in zip(refCoords, objCoords, colors):
        # draw circles corresponding to the current points and
        # connect them with a line
        cv2.circle(orig, (int(xA), int(yA)), 5, color, -1)
        cv2.circle(orig, (int(xB), int(yB)), 5, color, -1)
        cv2.line(orig, (int(xA), int(yA)), (int(xB), int(yB)),
            color, 2)

        # compute the Euclidean distance between the coordinates,
        # and then convert the distance in pixels to distance in
        # units
        D = dist.euclidean((xA, yA), (xB, yB)) / refObj[2]
        (mX, mY) = midpoint((xA, yA), (xB, yB))
        cv2.putText(orig, "{:.1f}in".format(D), (int(mX), int(mY - 10)),
            cv2.FONT_HERSHEY_SIMPLEX, 0.55, color, 2)      


        # imS = cv2.resize(final_img, (1366, 768))
    cv2.imshow('Final glasses Result',  final_img)
    cv2.waitKey()
    cv2.destroyAllWindows()

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()

Find distance between two eyes

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
cv2 
import math 
import numpy as np

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')

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)


 ///missing

        # compute the Euclidean distance between the midpoints,
        # then construct the reference object
        D = dist.euclidean((tlblX, tlblY), (trbrX, trbrY))
        refObj = (box, (cX, cY), D / args["width"])
        continue

    # draw the contours on the image
    orig = image.copy()
    cv2.drawContours(orig, [box.astype("int")], -1, (0, 255, 0), 2)
    cv2.drawContours(orig, [refObj[0].astype("int")], -1, (0, 255, 0), 2)

 # loop over the original points
    for ((xA, yA), (xB, yB), color) in zip(refCoords, objCoords, colors):
        # draw circles corresponding to the current points and
        # connect them with a line
        cv2.circle(orig, (int(xA), int(yA)), 5, color, -1)
        cv2.circle(orig, (int(xB), int(yB)), 5, color, -1)
        cv2.line(orig, (int(xA), int(yA)), (int(xB), int(yB)),
            color, 2)

        # compute the Euclidean distance between the coordinates,
        # and then convert the distance in pixels to distance in
        # units
        D = dist.euclidean((xA, yA), (xB, yB)) / refObj[2]
        (mX, mY) = midpoint((xA, yA), (xB, yB))
        cv2.putText(orig, "{:.1f}in".format(D), (int(mX), int(mY - 10)),
            cv2.FONT_HERSHEY_SIMPLEX, 0.55, color, 2)      


        # imS = cv2.resize(final_img, (1366, 768))
    cv2.imshow('Final glasses Result',  final_img)
    cv2.waitKey()
    cv2.destroyAllWindows()

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()