opencv处理换脸操作

In [18]:
import cv2
# opencv是计算机视觉库
import matplotlib.pyplot as plt
%matplotlib inline 
# 这个魔法指令在程序的结束加上一个plt.show()
In [28]:
an = plt.imread("./cv2_change_head/安倍.jpg")
an
Out[28]:
array([[[187, 186, 200],
        [187, 186, 200],
        [187, 186, 200],
        ..., 
        [185, 183, 196],
        [186, 184, 197],
        [186, 184, 197]],

       [[187, 186, 200],
        [187, 186, 200],
        [187, 186, 200],
        ..., 
        [185, 183, 196],
        [186, 184, 197],
        [186, 184, 197]],

       [[187, 186, 200],
        [187, 186, 200],
        [187, 186, 200],
        ..., 
        [185, 183, 196],
        [186, 184, 197],
        [186, 184, 197]],

       ..., 
       [[ 19,  18,  24],
        [ 18,  17,  23],
        [ 17,  16,  22],
        ..., 
        [ 25,  24,  30],
        [ 24,  23,  29],
        [ 22,  21,  27]],

       [[ 18,  17,  23],
        [ 18,  17,  23],
        [ 16,  15,  21],
        ..., 
        [ 25,  24,  30],
        [ 25,  24,  30],
        [ 21,  20,  26]],

       [[ 17,  16,  22],
        [ 16,  15,  21],
        [ 15,  14,  20],
        ..., 
        [ 25,  24,  30],
        [ 25,  24,  30],
        [ 20,  19,  25]]], dtype=uint8)
In [29]:
plt.imshow(an)
Out[29]:
 
           
<matplotlib.image.AxesImage at 0x1bd3e788da0>
 
          
In [30]:
# 创建一个cv2的分类器,用于识别图片
case = cv2.CascadeClassifier()
In [31]:
# 给case加上人脸识别的算法
case.load("./cv2_change_head/haarcascade_frontalface_default.xml")
# 加上这个算法以后,case就可以识别人脸的位置
Out[31]:
True
In [32]:
# 用case来识别人脸
face = case.detectMultiScale(an)
face
Out[32]:
array([[325, 278, 504, 504]], dtype=int32)
In [33]:
dog = plt.imread("./cv2_change_head/dog.jpg")
plt.imshow(dog)
Out[33]:
 
           
<matplotlib.image.AxesImage at 0x1bd3f4dd278>

 
          
In [34]:
case.detectMultiScale(dog)
Out[34]:
()
In [35]:
dog.shape
Out[35]:
(450, 450, 3)
In [39]:
big_dog = cv2.resize(dog,(504,504))
plt.imshow(big_dog)
Out[39]:
 
           
<matplotlib.image.AxesImage at 0x1bd43c51668>

 
          
In [40]:
an[325:829,278:782] = big_dog
In [42]:
plt.imshow(an)
Out[42]:
<matplotlib.image.AxesImage at 0x1bd3f5766d8>


猜你喜欢

转载自blog.csdn.net/qq_29784441/article/details/80859238