网站首页 > 教程文章 正文
使用说明:
- 输入参数:需要提供两个图片路径(input.jpg 和 target.jpg)。
- 功能描述:detect_faces 函数用于检测图片中的面部区域。swap_faces 函数用于实现换脸操作,通过缩放和叠加目标面部到源图片的位置。
- 调整参数:scale_factor:调整缩放比例,可以根据需求进行修改。edgeDetail:调整边缘细节,值越大边缘越清晰。
import cv2
import numpy as np
# 加载HaarCascade分类器
face_detector = cv2.CascadeClassifier('E:\Work_one\lib\site-packages\cv2\data\
haarcascade_frontalface_default.xml')
def detect_faces(img):
"""检测图片中的所有面部区域"""
# 将图像转换为灰度图像,因为人脸检测通常在灰度图像上进行
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# 使用 detectMultiScale 方法检测人脸
faces = face_detector.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=5, minSize=(30, 30))
return faces
def swap_faces(src_img, dest_img):
"""将src_img的脸部替换为dest_img的脸部"""
# 获取两个图片的尺寸
h1, w1 = src_img.shape[:2]
h2, w2 = dest_img.shape[:2]
# 找到最大缩放比例,使得目标面部不会超出原图
max_scale = min(h1 / h2, w1 / w2)
# 初始化结果图片
result = np.zeros((h1, w1), dtype=np.uint8)
# 获取两个面的位置和尺寸
for face_id in range(len(dest_faces)):
dest_face = dest_faces[face_id]
src_face = src_faces[face_id]
# 计算缩放比例
scale = max_scale
w = int(dest_face.width * scale)
h = int(dest_face.height * scale)
# 确定位置
x1 = (src_face.x + w // 2 - w // 2) // 2
y1 = (src_face.y + h // 2 - h // 2) // 2
# 将目标面部叠加到结果图片上
result[y1:y1 + h, x1:x1 + w] = dest_face.data
return result
# 读取图片
source_image = cv2.imread('input.jpg')
destination_image = cv2.imread('target.jpg')
# 检测两个人的面部区域
src_faces = detect_faces(source_image)
dest_faces = detect_faces(destination_image)
if not src_faces or not dest_faces:
print("未能检测到有效的面部区域,可能需要调整输入图片或提高检测精度")
else:
# 调整缩放比例(根据需求调整)
scale_factor = 0.8
for i in range(len(src_faces)):
src_face = src_faces[i]
dest_face = dest_faces[i]
# 计算最优缩放比例
w1, h1 = src_face.width, src_face.height
w2, h2 = dest_face.width, dest_face.height
scale = min(h1 / h2, w1 / w2)
# 边缘细节调整(根据需求调整)
edgeDetail = 0.5
# 提取面部特征
gray = cv2.cvtColor(source_image, cv2.COLOR_BGR2GRAY)
x1, y1 = src_face.x + int(w1 * scale // 2), src_face.y + int(h1 * scale // 2)
# 替换面部区域(叠加目标面)
result = np.zeros((h1, w1), dtype=np.uint8)
result[y1:y1 + h1, x1:x1 + w1] = dest_face.data
# 调整边缘(根据需求调整)
edge = cv2.ximgonormalized(result, gray, 31, edgeDetail, scale=0.5)
# 将结果转换回BGR格式
result = cv2.cvtColor(result, cv2.COLOR_GRAY2BGR) if edge is not None else source_image
# 保存结果
cv2.imwrite('output.jpg', result)
print("图片已经成功处理,结果已保存为output.jpg")
猜你喜欢
- 2025-05-08 使用Transformer来做物体检测(基于transformer的目标检测)
- 2025-05-08 仅用CPU就能跑到1000FPS,这是开源的C++跨平台人脸检测项目
- 2025-05-08 C# 构建一个人脸识别AI的简单例子
- 2025-05-08 c# 实现图片识别?(c#图片识别库)
- 2025-05-08 170.Python——使用Gradio来展示实时人脸检测
- 2025-05-08 实战小项目:利用 OpenCV 快速完成人脸检测
- 2025-05-08 [OpenCV实战]2 人脸识别算法对比(opencv人脸识别函数)
- 2025-05-08 Python人脸识别实现方法与步骤(python人脸识别实例源代码)
- 2025-05-08 Java版人脸跟踪三部曲之三:编码实战
- 2025-05-08 OpenCV-Python速查表:从导入图像到人脸检测
- 最近发表
-
- 一课译词:一刀两断(一刀两断成语解释)
- 核心短语break up用法解析(breakd短语)
- HTML+CSS 实现商品图片列表放大视觉效果 复制完整代码即可马上调用
- 前端实现右键自定义菜单(前端实现右键自定义菜单怎么设置)
- Python中docx与docxcompose批量合并多个Word文档并添加分页符
- Java 将Excel转为XML(java将xls转换成xlsx)
- jq+ajax+bootstrap改了一个动态分页的表格
- css兼容性问题及一些常见问题汇总大全,赶快收藏!
- Java 的业务逻辑验证框架 之-fluent-validator
- 小程序cover-view踩坑系列2(微信小程序overflow)
- 标签列表
-
- location.href (44)
- document.ready (36)
- git checkout -b (34)
- 跃点数 (35)
- 阿里云镜像地址 (33)
- qt qmessagebox (36)
- mybatis plus page (35)
- vue @scroll (38)
- 堆栈区别 (33)
- 什么是容器 (33)
- sha1 md5 (33)
- navicat导出数据 (34)
- 阿里云acp考试 (33)
- 阿里云 nacos (34)
- redhat官网下载镜像 (36)
- srs服务器 (33)
- pico开发者 (33)
- https的端口号 (34)
- vscode更改主题 (35)
- 阿里云资源池 (34)
- os.path.join (33)
- redis aof rdb 区别 (33)
- 302跳转 (33)
- http method (35)
- js array splice (33)