Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

snipnet3 #4

Open
lix19937 opened this issue Jul 11, 2023 · 0 comments
Open

snipnet3 #4

lix19937 opened this issue Jul 11, 2023 · 0 comments

Comments

@lix19937
Copy link
Owner



import dlib
import cv2
import numpy as np
import math
predictor_path='./dlib-models-master/shape_predictor_68_face_landmarks.dat'

#
# bzip2 -d ./shape_predictor_68_face_landmarks.dat.bz2 
#  

detector = dlib.get_frontal_face_detector()
predictor = dlib.shape_predictor(predictor_path)  
 
def landmark_dec_dlib_fun(img_src):
    img_gray = cv2.cvtColor(img_src, cv2.COLOR_BGR2GRAY)
    land_marks = []

    rects = detector(img_gray, 0)
    # print(type(rects))
	
    for i in range(len(rects)):
        land_marks_node = np.matrix([[p.x, p.y] for p in predictor(img_gray, rects[i]).parts()])
        # for idx, point in enumerate(land_marks_node):
        #     # 68 pos(point)
        #     pos = (point[0,0], point[0,1])
        #     print(idx, pos)
        #     # cv2.circle to each point
        #     cv2.circle(img_src, pos, 5, color=(0, 255, 0))
        #     font = cv2.FONT_HERSHEY_SIMPLEX
        #     cv2.putText(img_src, str(idx + 1), pos, font, 0.5, (0, 0, 0), 1, cv2.LINE_AA)
        land_marks.append(land_marks_node)

    return land_marks


# startX, startY,left_landmark[0, 0], left_landmark[0, 1]
# endPt[0, 0], endPt[0, 1]
def localTranslationWarp(srcImg, startX, startY, endX, endY, radius):
    ddradius = float(radius * radius)
    copyImg = np.zeros(srcImg.shape, np.uint8)
    copyImg = srcImg.copy()

    # |m-c|^2
    ddmc = (endX - startX) * (endX - startX) + (endY - startY) * (endY - startY)
    H, W, _ = srcImg.shape
    for i in range(W):
        for j in range(H):
            # Calculate whether the point is within the range of the deformation circle
            if math.fabs(i - startX) > radius and math.fabs(j - startY) > radius:
                continue

            distance = (i - startX) * (i - startX) + (j - startY) * (j - startY)

            if (distance < ddradius):
                ratio = (ddradius - distance) / (ddradius - distance + ddmc)
                ratio = ratio * ratio

                UX = i - ratio * (endX - startX)
                UY = j - ratio * (endY - startY)

                value = BilinearInsert(srcImg, UX, UY)
                copyImg[j, i] = value

    return copyImg


def BilinearInsert(src, ux, uy):
    w, h, c = src.shape
    if c == 3:
        x1 = int(ux)
        x2 = x1 + 1
        y1 = int(uy)
        y2 = y1 + 1

        part1 = src[y1, x1].astype(np.float) * (float(x2) - ux) * (float(y2) - uy)
        part2 = src[y1, x2].astype(np.float) * (ux - float(x1)) * (float(y2) - uy)
        part3 = src[y2, x1].astype(np.float) * (float(x2) - ux) * (uy - float(y1))
        part4 = src[y2, x2].astype(np.float) * (ux - float(x1)) * (uy - float(y1))

        insertValue = part1 + part2 + part3 + part4
        return insertValue.astype(np.int8)


def face_thin_auto(src):
    landmarks = landmark_dec_dlib_fun(src)

    if len(landmarks) == 0:
        return

    for landmarks_node in landmarks:
        left_landmark = landmarks_node[3]
        left_landmark_down = landmarks_node[5]
        right_landmark = landmarks_node[13]
        right_landmark_down = landmarks_node[15]
        endPt = landmarks_node[30]

        # dist4-6
        r_left = math.sqrt(
            (left_landmark[0, 0] - left_landmark_down[0, 0]) * (left_landmark[0, 0] - left_landmark_down[0, 0]) +
            (left_landmark[0, 1] - left_landmark_down[0, 1]) * (left_landmark[0, 1] - left_landmark_down[0, 1]))

        # dist14-16
        r_right = math.sqrt(
            (right_landmark[0, 0] - right_landmark_down[0, 0]) * (right_landmark[0, 0] - right_landmark_down[0, 0]) +
            (right_landmark[0, 1] - right_landmark_down[0, 1]) * (right_landmark[0, 1] - right_landmark_down[0, 1]))

        # left face 
        thin_image = localTranslationWarp(src, left_landmark[0, 0], left_landmark[0, 1], endPt[0, 0], endPt[0, 1], r_left)
        # right face
        thin_image = localTranslationWarp(thin_image, right_landmark[0, 0], right_landmark[0, 1], endPt[0, 0], endPt[0, 1], r_right)

    # cv2.imshow('thin', thin_image)
    cv2.imwrite('thin.png', thin_image)


def main():
    src = cv2.imread('vv.png')
    face_thin_auto(src)

if __name__ == '__main__':
    main()


Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant