Skip to content

Commit

Permalink
添加头像特效合成文档
Browse files Browse the repository at this point in the history
  • Loading branch information
vipstone committed May 2, 2018
1 parent c439cfb commit 7fe8c2f
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 11 deletions.
21 changes: 13 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,18 @@

一款优秀的人脸检测、人脸识别、视频识别、文字识别等智能AI项目。

>开发环境:Windows 10(x64) + Python 3
>
>其他库版本:OpenCV 3.4.1、Dlib 19.8.1、face_recognition 1.2.2、Tesseract OCR 4.0.0-beta.1
**开发环境**

>Windows 10(x64)
>Python 3.6.4
>OpenCV 3.4.1
>Dlib 19.8.1
>face_recognition 1.2.2
>Tesseract OCR 4.0.0-beta.1
## 技术实现方案 ##

下面列举一下,相应模块使用到技术
相应技术使用模块

人脸识别:OpenCV / Dlib

Expand All @@ -33,13 +38,13 @@

[视频人脸检测(Dlib版)](doc/videoDlib.md)

[绘制脸部轮廓](doc/faceRecognitionOutline.md)
[脸部轮廓绘制](doc/faceRecognitionOutline.md)

[数字化妆](doc/faceRecognitionMakeup.md)

[视频人脸识别](doc/faceRecognition.md)

头像特效
[头像特效合成](doc/compose.md)

## 功能预览 ##

Expand All @@ -57,13 +62,13 @@

**头像特效合成**

<img src="http://icdn.apigo.cn/compose-1.png" width = "200" height = "300" alt="头像特效合成" />
<img src="https://raw.githubusercontent.com/vipstone/faceai/master/res/compose.png" width = "200" height = "300" alt="头像特效合成" />

----------

**数字化妆**

<img src="https://raw.githubusercontent.com/vipstone/faceai/master/res/faceRecognitionMakeup.png" width = "260" height = "300" alt="视频人脸识别" />
<img src="https://raw.githubusercontent.com/vipstone/faceai/master/res/faceRecognitionMakeup.png" width = "230" height = "300" alt="视频人脸识别" />

----------

Expand Down
66 changes: 66 additions & 0 deletions doc/compose.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# 头像特效合成

实现思路:使用OpenCV检测出头部位置,向上移动20像素添加虚拟帽子,帽子的宽度等于脸的大小,高度等比缩小,需要注意的是如果高度小于脸部向上移动20像素的值,那么帽子的高度就等于最小高度=(脸部位置-20)。
为什么是20而不是30或者40,因为取得是检测的脸部和头顶的一般距离20,开发者可自己调整。


**注意事项**

图片合成元件,要是黑背景图片,透明的图片也会有问题,在ps手动处理一下透明图片,添加新图层,选中alt+d添加黑背景,把新图层层级放到最底部即可。

# 效果图预览 #
<img src="https://raw.githubusercontent.com/vipstone/faceai/master/res/compose.png" width = "200" height = "300" alt="头像特效合成" />


## 代码实现 ##
```
#coding=utf-8
import cv2
# OpenCV人脸识别分类器
classifier = cv2.CascadeClassifier(
"C:\Python36\Lib\site-packages\opencv-master\data\haarcascades\haarcascade_frontalface_default.xml"
)
img = cv2.imread("img/ag-3.png") # 读取图片
imgCompose = cv2.imread("img/compose/maozi-1.png")
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # 转换灰色
color = (0, 255, 0) # 定义绘制颜色
# 调用识别人脸
faceRects = classifier.detectMultiScale(
gray, scaleFactor=1.2, minNeighbors=3, minSize=(32, 32))
if len(faceRects): # 大于0则检测到人脸
for faceRect in faceRects:
x, y, w, h = faceRect
sp = imgCompose.shape
imgComposeSizeH = int(sp[0]/sp[1]*w)
if imgComposeSizeH>(y-20):
imgComposeSizeH=(y-20)
imgComposeSize = cv2.resize(imgCompose,(w, imgComposeSizeH), interpolation=cv2.INTER_NEAREST)
top = (y-imgComposeSizeH-20)
if top<=0:
top=0
rows, cols, channels = imgComposeSize.shape
roi = img[top:top+rows,x:x+cols]
# Now create a mask of logo and create its inverse mask also
img2gray = cv2.cvtColor(imgComposeSize, cv2.COLOR_RGB2GRAY)
ret, mask = cv2.threshold(img2gray, 10, 255, cv2.THRESH_BINARY)
mask_inv = cv2.bitwise_not(mask)
# Now black-out the area of logo in ROI
img1_bg = cv2.bitwise_and(roi, roi, mask=mask_inv)
# Take only region of logo from logo image.
img2_fg = cv2.bitwise_and(imgComposeSize, imgComposeSize, mask=mask)
# Put logo in ROI and modify the main image
dst = cv2.add(img1_bg, img2_fg)
img[top:top+rows, x:x+cols] = dst
cv2.imshow("image", img)
cv2.waitKey(0)
cv2.destroyAllWindows()
```

5 changes: 2 additions & 3 deletions faceai/compose.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#coding=utf-8
#图片检测 - OpenCV版本
#头像特效合成
import cv2
import numpy as np

# OpenCV人脸识别分类器
classifier = cv2.CascadeClassifier(
Expand Down Expand Up @@ -45,7 +44,7 @@
dst = cv2.add(img1_bg, img2_fg)
img[top:top+rows, x:x+cols] = dst

cv2.imshow("image", img) # 显示图像
cv2.imshow("image", img)
cv2.waitKey(0)
cv2.destroyAllWindows()

Expand Down

0 comments on commit 7fe8c2f

Please sign in to comment.