Notes![what is notes.io? What is notes.io?](/theme/images/whatisnotesio.png)
![]() ![]() Notes - notes.io |
import dlib
import numpy as np
import matplotlib.pyplot as plt
import os
# Load Dlib's face detector and shape predictor
detector = dlib.get_frontal_face_detector()
predictor = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat")
def get_landmarks(image):
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
rects = detector(gray, 1)
if len(rects) == 0:
return None
shape = predictor(gray, rects[0])
coords = np.zeros((68, 2), dtype="int")
for i in range(0, 68):
coords[i] = (shape.part(i).x, shape.part(i).y)
# Add extra points
h, w = image.shape[:2]
coords = np.vstack([coords,
[(0, 0), (w//2, 0), (w-1, 0),
(0, h//2), (w-1, h//2),
(0, h-1), (w//2, h-1), (w-1, h-1)]])
return coords
def draw_landmarks(image, landmarks):
for (x, y) in landmarks:
cv2.circle(image, (x, y), 2, (0, 255, 0), -1)
return image
def apply_delaunay(image, landmarks):
rect = (0, 0, image.shape[1], image.shape[0])
subdiv = cv2.Subdiv2D(rect)
for point in landmarks:
subdiv.insert(tuple(point))
triangles = subdiv.getTriangleList()
return np.array(triangles, dtype=np.int32)
def draw_delaunay(image, landmarks, triangles):
for t in triangles:
pt1 = (t[0], t[1])
pt2 = (t[2], t[3])
pt3 = (t[4], t[5])
cv2.line(image, pt1, pt2, (0, 0, 255), 1)
cv2.line(image, pt2, pt3, (0, 0, 255), 1)
cv2.line(image, pt3, pt1, (0, 0, 255), 1)
return image
def warp_triangle(src, dst, src_tri, dst_tri):
M = cv2.getAffineTransform(np.float32(src_tri), np.float32(dst_tri))
warped = cv2.warpAffine(src, M, (dst.shape[1], dst.shape[0]), None, flags=cv2.INTER_LINEAR, borderMode=cv2.BORDER_REFLECT_101)
mask = np.zeros((dst.shape[0], dst.shape[1], 3), dtype = np.float32)
cv2.fillConvexPoly(mask, np.int32(dst_tri), (1.0, 1.0, 1.0), 16, 0)
warped = warped * mask
return warped
def create_morph_sequence(img1, img2, landmarks1, landmarks2, triangles, num_frames):
frames = []
for i in range(num_frames):
t = i / (num_frames - 1)
landmarks = (1 - t) * landmarks1 + t * landmarks2
landmarks = landmarks.astype(np.int32)
morphed = np.zeros(img1.shape, dtype=np.float32)
for triangle in triangles:
x, y, z = triangle[::2], triangle[1::2], triangle[2::2]
t1 = np.array([landmarks1[x][0], landmarks1[y][0], landmarks1[z][0]])
t2 = np.array([landmarks2[x][0], landmarks2[y][0], landmarks2[z][0]])
t = np.array([landmarks[x][0], landmarks[y][0], landmarks[z][0]])
warped1 = warp_triangle(img1, morphed, t1, t)
warped2 = warp_triangle(img2, morphed, t2, t)
morphed += (1 - t) * warped1 + t * warped2
frames.append(np.uint8(morphed))
return frames
# Load images
img1 = cv2.imread("image1.jpg")
img2 = cv2.imread("image2.jpg")
# Detect landmarks
landmarks1 = get_landmarks(img1)
landmarks2 = get_landmarks(img2)
if landmarks1 is None or landmarks2 is None:
print("No face detected in one or both images.")
exit()
# Apply Delaunay triangulation
triangles = apply_delaunay(img1, landmarks1)
# Create morph sequence
num_frames = 20
frames = create_morph_sequence(img1, img2, landmarks1, landmarks2, triangles, num_frames)
# Save and display results
if not os.path.exists("output"):
os.makedirs("output")
plt.figure(figsize=(20, 10))
for i, frame in enumerate(frames):
# Draw landmarks and Delaunay triangulation
landmarks = (1 - i/(num_frames-1)) * landmarks1 + (i/(num_frames-1)) * landmarks2
landmarks = landmarks.astype(np.int32)
frame_with_landmarks = draw_landmarks(frame.copy(), landmarks)
frame_with_delaunay = draw_delaunay(frame_with_landmarks.copy(), landmarks, triangles)
# Save frame
cv2.imwrite(f"output/frame_{i:03d}.jpg", frame)
cv2.imwrite(f"output/frame_{i:03d}_landmarks.jpg", frame_with_landmarks)
cv2.imwrite(f"output/frame_{i:03d}_delaunay.jpg", frame_with_delaunay)
# Display frame
plt.subplot(4, 5, i+1)
plt.imshow(cv2.cvtColor(frame_with_delaunay, cv2.COLOR_BGR2RGB))
plt.axis('off')
plt.title(f"Frame {i}")
plt.tight_layout()
plt.savefig("output/morph_sequence.png")
plt.show()
print("Morphing sequence created and saved in the 'output' folder.")
![]() |
Notes is a web-based application for online taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000+ notes created and continuing...
With notes.io;
- * You can take a note from anywhere and any device with internet connection.
- * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
- * You can quickly share your contents without website, blog and e-mail.
- * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
- * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.
Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.
Easy: Notes.io doesn’t require installation. Just write and share note!
Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )
Free: Notes.io works for 14 years and has been free since the day it was started.
You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;
Email: [email protected]
Twitter: http://twitter.com/notesio
Instagram: http://instagram.com/notes.io
Facebook: http://facebook.com/notesio
Regards;
Notes.io Team