pythonfacial-landmark-alignment

how to mesure the distance between 2 points in the facial landmarks from dlib 68 x- y-coordinates


i am working in a python code that take a picture if the distance between some facial landmarks(dlib's ones) has a condition, indeed if the distance between the point 1 and the point 29, divided by the distance of the point 17 and the 29 is between 1.1 and .9 then the picture should be taked, here is a diagram of the landmarks and the code which im working

enter image description here

COUNTER = 0
TOTAL = 0


shape_predictor= "shape_predictor_68_face_landmarks.dat" #dace_landmark
detector = dlib.get_frontal_face_detector()
predictor = dlib.shape_predictor(shape_predictor)


(mStart, mEnd) = face_utils.FACIAL_LANDMARKS_IDXS["mouth"]
(lStart, lEnd) = face_utils.FACIAL_LANDMARKS_IDXS["left_eye"]
(rStart, rEnd) = face_utils.FACIAL_LANDMARKS_IDXS["right_eye"]
(jStart,jEnd) = face_utils.FACIAL_LANDMARKS_IDXS["jaw"]
(reStart,reEnd) = face_utils.FACIAL_LANDMARKS_IDXS["right_eyebrow"]
(leStart,leEnd)= face_utils.FACIAL_LANDMARKS_IDXS["left_eyebrow"]
(nStart,nEnd)= face_utils.FACIAL_LANDMARKS_IDXS["nose"]
#video que vamos a procesar

print("[INFO] starting video stream thread...")
vs = VideoStream(src=0).start()
fileStream = False
time.sleep(1.0)

fps= FPS().start()
cv2.namedWindow("test")
while True:
    frame = vs.read()
    frame = imutils.resize(frame, width=450)
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    rects = detector(gray, 0)
    for rect in rects:
        shape = predictor(gray, rect)
        shape = face_utils.shape_to_np(shape)
        mouth= shape[mStart:mEnd]
        left_eye= shape[lStart:lEnd]
        right_eye = shape[rStart:rEnd]
        jaw = shape[jStart:jEnd]
        right_eyebrow = shape[reStart:reEnd]
        left_eyebrow = shape[leStart:leEnd]
        nose = shape[nStart:nEnd]

        #here is where i need help
        l1= what do i need to put here?
        l2 = what do i need to put here?
        ratio = l1/l2

        mouthHull = cv2.convexHull(mouth)
        left_eyeHull = cv2.convexHull(left_eye)
        right_eyehHull = cv2.convexHull(right_eye)
        jawhHull = cv2.convexHull(jaw)
        right_eyebrowHull = cv2.convexHull(right_eyebrow)
        noseHull = cv2.convexHull(nose)
        left_eyebrowHull = cv2.convexHull(left_eyebrow)
        #print(shape)
        cv2.drawContours(frame, [mouthHull,left_eyeHull, right_eyehHull, jawhHull, right_eyebrowHull, noseHull, left_eyebrowHull], -1, (0, 255, 0), 1)


        if ratio <= .9 or mar > 1.1 :
            COUNTER += 1
        else:
            if COUNTER >= 15:
                TOTAL += 1
                frame = vs.read()
                time.sleep(.3)
                frame2= frame.copy()
                img_name = "opencv_frame_{}.png".format(TOTAL)
                #cv2.imwrite(img_name, frame)
                print("{} written!".format(img_name))
            COUNTER = 0

        cv2.putText(frame, "the ratio from l1 and l2 is: {}".format(distance), (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 2)

    cv2.imshow("Frame", frame)
    fps.update()

    key2 = cv2.waitKey(1) & 0xFF
    if key2 == ord('q'):
        break
fps.stop()


cv2.destroyAllWindows()
vs.stop()

now my problem is that i dont know how to calculate the distance between two landmarks, i put up there in the code a coment where i need help.

if any of you can help me i will be so grateful, thanks in advance


Solution

  • done it the condition that i used was

    l1= math.sqrt((jaw[0][0]-nose[1][0])**2+(jaw[0][1]-nose[1][1])**2)
                l2 = math.sqrt((jaw[16][0]-nose[1][0])**2+(jaw[16][1]-nose[1][1])**2)
                ratio = l1/l2
                m = (jaw[0][1]-jaw[16][1])/(jaw[0][0]-jaw[16][0])
                if (ratio <= 1.1 and ratio > .9) :
                    cond1 = True
                else:
                    cond1= False
                if (jaw[0][1]<= (nose[1][1]+5) and jaw[0][1]> (nose[1][1]-5)):
                    cond2 = True
                else:
                    cond2= False
                if (m <= .05 and m >-.05 ):
                    cond3 = True
                else:
                    cond3= False
    

    the main idea is that jaw[0] represent the point (x,y) that belongs to the 0-point of the jaw

    so the cond1 is true when the distance between jaw[0] and nose[1] is similar to the distance between jaw[16] and nose[1]

    the cond2 is true when the y-coordinate of jaw is similar to to the y-coordinate from nose

    finaly the last condition is about the angle between jaw[0] and jaw[16]