如何给头像增加圣诞帽
2017-12-25 16:52
176 查看
import cv2
import random
# OpenCV 人脸检测
face_patterns = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
sample_image = cv2.imread('img/fffc.jpg')
faces = face_patterns.detectMultiScale(sample_image,
scaleFactor=1.1,
minNeighbors=8,
minSize=(50, 50))
# 圣诞帽
hats = []
for i in range(4):
hats.append(cv2.imread('img/hat%d.png' % i, -1))
for face in faces:
# 随机一顶帽子
hat = random.choice(hats)
# 调整帽子尺寸
scale = face[3] / hat.shape[0] * 1.25
hat = cv2.resize(hat, (0, 0), fx=scale, fy=scale)
# 根据人脸坐标调整帽子位置
x_offset = int(face[0] + face[2] / 2 - hat.shape[1] / 2)
y_offset = int(face[1] - hat.shape[0] / 2)
# 计算贴图位置,注意防止超出边界的情况
x1, x2 = max(x_offset, 0), min(x_offset + hat.shape[1], sample_image.shape[1])
y1, y2 = max(y_offset, 0), min(y_offset + hat.shape[0], sample_image.shape[0])
hat_x1 = max(0, -x_offset)
hat_x2 = hat_x1 + x2 - x1
hat_y1 = max(0, -y_offset)
hat_y2 = hat_y1 + y2 - y1
# 透明部分的处理
alpha_h = hat[hat_y1:hat_y2, hat_x1:hat_x2, 3] / 255
alpha = 1 - alpha_h
# 按3个通道合并图片
for c in range(0, 3):
sample_image[y1:y2, x1:x2, c] = (alpha_h * hat[hat_y1:hat_y2, hat_x1:hat_x2, c] + alpha * sample_image[y1:y2, x1:x2, c])
# 保存最终结果
cv2.imwrite('faces_detect.png', sample_image)
依赖的库在https://github.com/zwd1993/faceHats
import random
# OpenCV 人脸检测
face_patterns = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
sample_image = cv2.imread('img/fffc.jpg')
faces = face_patterns.detectMultiScale(sample_image,
scaleFactor=1.1,
minNeighbors=8,
minSize=(50, 50))
# 圣诞帽
hats = []
for i in range(4):
hats.append(cv2.imread('img/hat%d.png' % i, -1))
for face in faces:
# 随机一顶帽子
hat = random.choice(hats)
# 调整帽子尺寸
scale = face[3] / hat.shape[0] * 1.25
hat = cv2.resize(hat, (0, 0), fx=scale, fy=scale)
# 根据人脸坐标调整帽子位置
x_offset = int(face[0] + face[2] / 2 - hat.shape[1] / 2)
y_offset = int(face[1] - hat.shape[0] / 2)
# 计算贴图位置,注意防止超出边界的情况
x1, x2 = max(x_offset, 0), min(x_offset + hat.shape[1], sample_image.shape[1])
y1, y2 = max(y_offset, 0), min(y_offset + hat.shape[0], sample_image.shape[0])
hat_x1 = max(0, -x_offset)
hat_x2 = hat_x1 + x2 - x1
hat_y1 = max(0, -y_offset)
hat_y2 = hat_y1 + y2 - y1
# 透明部分的处理
alpha_h = hat[hat_y1:hat_y2, hat_x1:hat_x2, 3] / 255
alpha = 1 - alpha_h
# 按3个通道合并图片
for c in range(0, 3):
sample_image[y1:y2, x1:x2, c] = (alpha_h * hat[hat_y1:hat_y2, hat_x1:hat_x2, c] + alpha * sample_image[y1:y2, x1:x2, c])
# 保存最终结果
cv2.imwrite('faces_detect.png', sample_image)
依赖的库在https://github.com/zwd1993/faceHats
相关文章推荐
- 如何从技术上实现「朋友圈@微信官方头像添加圣诞帽」?
- 如何从技术上实现「朋友圈@微信官方头像添加圣诞帽」?
- 应客户需要,如何在Tree节点上增加变色状态位
- Preference中如何增加activity生命周期监听
- 如何修改Docker Hub上的头像
- 如何增加ORACLE连接数
- 如何在windows上永久的增加静态路由
- 如何给自己的eclipse插件增加trace功能
- div ul li 嵌套后如何解决增加多个li后ul高度自适应问题
- 如何固定Win8开始屏幕磁贴布局并禁止增加或删除磁贴
- 如何实现加载头像,并对图片进行处理,以及显示附近好友
- 【MySQL】如何在phpMyAdmin中增加innoDB,并新建外键Foreign Key
- 如何在FlexViewer框架中增加Widget
- 如何给 Visual C++ 中的对话框增加位图背景
- Flex中如何通过verticalScrollPosition和maxVerticalScrollPosition属性,TextArea增加新内容的时候自动滚动 .
- 程序员如何增加收入
- 【Chrome】如何在C++中增加给JavaScript调用的API
- Android 如何实现圆形头像
- 浅谈一下如何生成一个二维码和内嵌头像Logo的二维码
- dede文章页面如何显示作者的头像默认只能显示作者用户名