0

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

mimus
  • 367
  • 4
  • 21
  • The formula for distance between two points is documented in many places. We expect you to do such research before posting. Where are you stuck? Also, please reduce and enhance this into the expected [MRE](https://stackoverflow.com/help/minimal-reproducible-example). Posting all of your code for a 5-line problem is not acceptable. – Prune May 18 '20 at 01:57
  • sorry for that, the thing that i cant understand is how to find where in the x,y plane represented by the picture are the landmarks numbered whit 1, 17 and 29; in order to calculate the euclidean distance, i hope i was enough clear in order that you can help me, by now will edit the code to make it more acceptable and wait any help, thanks for the comment – mimus May 18 '20 at 02:02
  • Please repeat [how to ask](https://stackoverflow.com/help/how-to-ask) from the [intro tour](https://stackoverflow.com/tour). When you have a clear explanation of the problem, we may be able to help. – Prune May 18 '20 at 03:05
  • thanks anyway i solve it – mimus May 18 '20 at 13:18
  • If you have a substantive contribution to make to the site, please post the proper question and your solution. If not, please delete the question. – Prune May 18 '20 at 15:06

1 Answers1

1

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]

mimus
  • 367
  • 4
  • 21