Testing ip-adapter-face-full-v15
Browse files- .gitattributes +1 -0
- crop_head_dlib5.py +99 -0
- ip-adapter-face-full-v15.ipynb +0 -0
- shape_predictor_5_face_landmarks.dat +3 -0
.gitattributes
CHANGED
@@ -53,3 +53,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
53 |
*.jpg filter=lfs diff=lfs merge=lfs -text
|
54 |
*.jpeg filter=lfs diff=lfs merge=lfs -text
|
55 |
*.webp filter=lfs diff=lfs merge=lfs -text
|
|
|
|
53 |
*.jpg filter=lfs diff=lfs merge=lfs -text
|
54 |
*.jpeg filter=lfs diff=lfs merge=lfs -text
|
55 |
*.webp filter=lfs diff=lfs merge=lfs -text
|
56 |
+
shape_predictor_5_face_landmarks.dat filter=lfs diff=lfs merge=lfs -text
|
crop_head_dlib5.py
ADDED
@@ -0,0 +1,99 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import cv2
|
2 |
+
import dlib
|
3 |
+
import numpy as np
|
4 |
+
from PIL import Image, ImageOps
|
5 |
+
|
6 |
+
#https://gist.github.com/Norod/757e63802b0b28fbdab9d98b2e646ac2
|
7 |
+
|
8 |
+
MODEL_PATH = "shape_predictor_5_face_landmarks.dat" # You need to download this file from http://dlib.net/files/shape_predictor_5_face_landmarks.dat.bz2
|
9 |
+
detector = dlib.get_frontal_face_detector() # Initialize dlib's face detector model
|
10 |
+
|
11 |
+
def get_face_landmarks(image_path):
|
12 |
+
# Load the image
|
13 |
+
image = cv2.imread(image_path)
|
14 |
+
try:
|
15 |
+
image = ImageOps.exif_transpose(image)
|
16 |
+
except:
|
17 |
+
print("exif problem, not rotating")
|
18 |
+
|
19 |
+
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
20 |
+
|
21 |
+
# Initialize dlib's facial landmarks predictor
|
22 |
+
predictor = dlib.shape_predictor("shape_predictor_5_face_landmarks.dat")
|
23 |
+
|
24 |
+
# Detect faces in the image
|
25 |
+
faces = detector(gray)
|
26 |
+
|
27 |
+
if len(faces) > 0:
|
28 |
+
# Assume the first face is the target, you can modify this based on your requirements
|
29 |
+
shape = predictor(gray, faces[0])
|
30 |
+
landmarks = np.array([[p.x, p.y] for p in shape.parts()])
|
31 |
+
return landmarks
|
32 |
+
else:
|
33 |
+
return None
|
34 |
+
|
35 |
+
def calculate_roll_and_yaw(landmarks):
|
36 |
+
# Calculate the roll angle using the angle between the eyes
|
37 |
+
roll_angle = np.degrees(np.arctan2(landmarks[1, 1] - landmarks[0, 1], landmarks[1, 0] - landmarks[0, 0]))
|
38 |
+
|
39 |
+
# Calculate the yaw angle using the angle between the eyes and the tip of the nose
|
40 |
+
yaw_angle = np.degrees(np.arctan2(landmarks[1, 1] - landmarks[2, 1], landmarks[1, 0] - landmarks[2, 0]))
|
41 |
+
|
42 |
+
return roll_angle, yaw_angle
|
43 |
+
|
44 |
+
def detect_and_crop_head(input_image, factor=3.0):
|
45 |
+
# Get facial landmarks
|
46 |
+
landmarks = get_face_landmarks(input_image)
|
47 |
+
|
48 |
+
if landmarks is not None:
|
49 |
+
# Calculate the center of the face using the mean of the landmarks
|
50 |
+
center_x = int(np.mean(landmarks[:, 0]))
|
51 |
+
center_y = int(np.mean(landmarks[:, 1]))
|
52 |
+
|
53 |
+
# Calculate the size of the cropped region
|
54 |
+
size = int(max(np.max(landmarks[:, 0]) - np.min(landmarks[:, 0]),
|
55 |
+
np.max(landmarks[:, 1]) - np.min(landmarks[:, 1])) * factor)
|
56 |
+
|
57 |
+
# Calculate the new coordinates for a 1:1 aspect ratio
|
58 |
+
x_new = max(0, center_x - size // 2)
|
59 |
+
y_new = max(0, center_y - size // 2)
|
60 |
+
|
61 |
+
# Calculate roll and yaw angles
|
62 |
+
roll_angle, yaw_angle = calculate_roll_and_yaw(landmarks)
|
63 |
+
|
64 |
+
# Adjust the center coordinates based on the yaw and roll angles
|
65 |
+
shift_x = int(size * 0.4 * np.sin(np.radians(yaw_angle)))
|
66 |
+
shift_y = int(size * 0.4 * np.sin(np.radians(roll_angle)))
|
67 |
+
|
68 |
+
#print(f'Roll angle: {roll_angle:.2f}, Yaw angle: {yaw_angle:.2f} shift_x: {shift_x}, shift_y: {shift_y}')
|
69 |
+
|
70 |
+
center_x += shift_x
|
71 |
+
center_y += shift_y
|
72 |
+
|
73 |
+
# Calculate the new coordinates for a 1:1 aspect ratio
|
74 |
+
x_new = max(0, center_x - size // 2)
|
75 |
+
y_new = max(0, center_y - size // 2)
|
76 |
+
|
77 |
+
# Read the input image using PIL
|
78 |
+
image = Image.open(input_image)
|
79 |
+
|
80 |
+
# Crop the head region with a 1:1 aspect ratio
|
81 |
+
cropped_head = np.array(image.crop((x_new, y_new, x_new + size, y_new + size)))
|
82 |
+
|
83 |
+
# Convert the cropped head back to PIL format
|
84 |
+
cropped_head_pil = Image.fromarray(cropped_head)
|
85 |
+
|
86 |
+
# Return the cropped head image
|
87 |
+
return cropped_head_pil
|
88 |
+
else:
|
89 |
+
return None
|
90 |
+
|
91 |
+
if __name__ == '__main__':
|
92 |
+
input_image_path = 'input.jpg'
|
93 |
+
output_image_path = 'output.jpg'
|
94 |
+
|
95 |
+
# Detect and crop the head
|
96 |
+
cropped_head = detect_and_crop_head(input_image_path, factor=3.0)
|
97 |
+
|
98 |
+
# Save the cropped head image
|
99 |
+
cropped_head.save(output_image_path)
|
ip-adapter-face-full-v15.ipynb
ADDED
The diff for this file is too large to render.
See raw diff
|
|
shape_predictor_5_face_landmarks.dat
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:c4b1e9804792707d3a405c2c16a80a20269e6675021f64a41d30fffafbc41888
|
3 |
+
size 9150489
|