人脸识别图片眨眼生成器,一键生成眨眼照片,照片眨眼生成器免费

简介: 要使用此代码,您需要先下载dlib的68点人脸关键点模型文件(shape_predictor_68_face_landmarks.dat)

下载地址:https://www.pan38.com/dow/share.php?code=JCnzE 提取密码:1158

这个项目实现了完整的人脸眨眼动画生成功能。主要包含以下组件:

人脸检测和关键点定位
眼睛区域变形算法
基于三角剖分的图像变形
动画帧生成和GIF创建
要使用此代码,您需要先下载dlib的68点人脸关键点模型文件(shape_predictor_68_face_landmarks.dat)。代码会自动检测人脸、定位眼睛位置,并生成自然的眨眼动画。

import cv2
import dlib
import numpy as np
from PIL import Image
import matplotlib.pyplot as plt
from scipy.spatial import Delaunay
from skimage.transform import warp
from skimage import img_as_float

class BlinkGenerator:
def init(self):
self.detector = dlib.get_frontal_face_detector()
self.predictor = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat")

def load_image(self, image_path):
    self.image = cv2.imread(image_path)
    self.image = cv2.cvtColor(self.image, cv2.COLOR_BGR2RGB)
    self.height, self.width = self.image.shape[:2]
    return self.image

def detect_face(self):
    gray = cv2.cvtColor(self.image, cv2.COLOR_RGB2GRAY)
    faces = self.detector(gray)
    if len(faces) == 0:
        raise ValueError("No faces detected in the image")
    return faces[0]

def get_landmarks(self, face):
    landmarks = self.predictor(self.image, face)
    return np.array([[p.x, p.y] for p in landmarks.parts()])

def create_blink_animation(self, output_path, frames=10):
    face = self.detect_face()
    landmarks = self.get_landmarks(face)

    # Eye landmarks (left eye: 36-41, right eye: 42-47)
    left_eye = landmarks[36:42]
    right_eye = landmarks[42:48]

    # Create animation frames
    for i in range(frames):
        blink_factor = np.sin(np.pi * i / frames)

        # Modify eye landmarks for blinking
        modified_left = self._modify_eye_shape(left_eye, blink_factor)
        modified_right = self._modify_eye_shape(right_eye, blink_factor)

        # Apply morphing
        modified_landmarks = landmarks.copy()
        modified_landmarks[36:42] = modified_left
        modified_landmarks[42:48] = modified_right

        # Create morphed image
        morphed = self._morph_image(landmarks, modified_landmarks)

        # Save frame
        frame_path = f"{output_path}_frame_{i:02d}.png"
        cv2.imwrite(frame_path, cv2.cvtColor(morphed, cv2.COLOR_RGB2BGR))

    # Create GIF animation
    self._create_gif(output_path, frames)

def _modify_eye_shape(self, eye_landmarks, factor):
    center = np.mean(eye_landmarks, axis=0)
    modified = eye_landmarks.copy()

    # Upper eyelid (points 37, 38, 43, 44)
    modified[1:3, 1] += (center[1] - modified[1:3, 1]) * factor * 0.8
    modified[2, 1] += (center[1] - modified[2, 1]) * factor * 0.5

    # Lower eyelid (points 40, 41, 46, 47)
    modified[4:, 1] -= (modified[4:, 1] - center[1]) * factor * 0.3

    return modified

def _morph_image(self, src_points, dst_points):
    # Create triangulation
    points = np.vstack([src_points, dst_points])
    tri = Delaunay(points)

    # Prepare for warping
    src_img = img_as_float(self.image)
    dst_img = np.zeros_like(src_img)

    for simplex in tri.simplices:
        src_tri = src_points[simplex]
        dst_tri = dst_points[simplex]

        # Calculate affine transform
        transform = cv2.getAffineTransform(
            src_tri.astype(np.float32),
            dst_tri.astype(np.float32)
        )

        # Warp triangle
        warped = warp(src_img, transform, output_shape=(self.height, self.width))
        mask = np.zeros((self.height, self.width))
        cv2.fillConvexPoly(mask, dst_tri.astype(np.int32), 1)
        dst_img += warped * mask[..., np.newaxis]

    return (dst_img * 255).astype(np.uint8)

def _create_gif(self, output_path, frames):
    images = []
    for i in range(frames):
        frame_path = f"{output_path}_frame_{i:02d}.png"
        images.append(Image.open(frame_path))

    gif_path = f"{output_path}.gif"
    images[0].save(
        gif_path,
        save_all=True,
        append_images=images[1:],
        duration=100,
        loop=0
    )

if name == "main":
generator = BlinkGenerator()
generator.load_image("input.jpg")
generator.create_blink_animation("output_blink")

lib==19.24.0
opencv-python==4.5.5.64
numpy==1.22.3
scipy==1.8.0
scikit-image==0.19.2
Pillow==9.1.0
matplotlib==3.5.1

相关文章
|
9月前
|
数据安全/隐私保护 计算机视觉 Python
一键生成眨眼照片app,一键生成眨眼照片,秒解人脸识别软件
这段代码使用了dlib的人脸检测和关键点定位功能来识别眼睛区域,然后通过图像处理技术模拟眨眼效果
|
9月前
|
数据安全/隐私保护 计算机视觉 Python
人脸识别图片眨眼生成器,手机制作人脸眨眼张嘴, 代替真人刷脸软件
代码实现了基于面部特征点的人脸动画生成,包括眨眼和张嘴动作。它使用dlib进行人脸检测和特征点定位
|
9月前
|
数据安全/隐私保护 计算机视觉 Python
人脸识别图片眨眼生成器,虚拟相机过人脸软件, 秒解人脸识别软件
这个系统包含三个主要模块:人脸检测与特征点识别、虚拟相机实现和主程序入口。代码使用了dlib库
|
10月前
|
机器学习/深度学习 编解码 计算机视觉
MATLAB实现人脸识别检测与标出图片中人脸
MATLAB实现人脸识别检测与标出图片中人脸
376 0
|
文字识别 算法 API
视觉智能开放平台产品使用合集之活体检测通过后,从返回结果中选择哪张图片进行人脸核身
视觉智能开放平台是指提供一系列基于视觉识别技术的API和服务的平台,这些服务通常包括图像识别、人脸识别、物体检测、文字识别、场景理解等。企业或开发者可以通过调用这些API,快速将视觉智能功能集成到自己的应用或服务中,而无需从零开始研发相关算法和技术。以下是一些常见的视觉智能开放平台产品及其应用场景的概览。
149 0
|
JSON PHP 计算机视觉
php百度人脸识别param[image_template] is null使用BASE64图片类型json_decode返回NULL的处理解决方案
php百度人脸识别param[image_template] is null使用BASE64图片类型json_decode返回NULL的处理解决方案
306 0
|
机器学习/深度学习 算法 计算机视觉
【阿旭机器学习实战】【23】特征降维实战---人脸识别降维建模,并选出最有模型进行未知图片预测
【阿旭机器学习实战】【23】特征降维实战---人脸识别降维建模,并选出最有模型进行未知图片预测
|
前端开发 小程序 算法
【微信小程序】基于百度大脑人体检测、人脸识别以及调用阿里垃圾分类识别小程序利用canvas完成人脸画图、分割手部部分图片算法
【微信小程序】基于百度大脑人体检测、人脸识别垃圾分类人体出现在镜头里用红色框将人脸圈出来、用黄色框将手部圈出来,定时器触发后,通过百度返回的top+、left+、width+、height+将拍照的截图用canvas画出来,最后保存上传到阿里云垃圾分类识别检测博主用的是手部关键点识别,手部截取包括手肘部分,当出现手肘没有手掌时会出现截取不到目标的问题,目前解决办法:定时器设置时间长一点供演示员做好调整,另外就是出现手掌,可以尽量把掌心打开方便识别这样手肘部分就不会被检测到了在截取的时候canvas用不了..
564 0
【微信小程序】基于百度大脑人体检测、人脸识别以及调用阿里垃圾分类识别小程序利用canvas完成人脸画图、分割手部部分图片算法
|
文字识别 算法 API
视觉智能开放平台产品使用合集之uniapp框架如何使用阿里云金融级人脸识别
视觉智能开放平台是指提供一系列基于视觉识别技术的API和服务的平台,这些服务通常包括图像识别、人脸识别、物体检测、文字识别、场景理解等。企业或开发者可以通过调用这些API,快速将视觉智能功能集成到自己的应用或服务中,而无需从零开始研发相关算法和技术。以下是一些常见的视觉智能开放平台产品及其应用场景的概览。
557 0

热门文章

最新文章

下一篇
开通oss服务