這裏只貼代碼,具體操作項目詳情請參見:https://aistudio.baidu.com/aistudio/projectdetail/410609
!pip install paddlehub==1.6.0 -i https://pypi.tuna.tsinghua.edu.cn/simple
# 待預測圖片
test_img_path = ["./ren5.jpg"]
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
img = mpimg.imread(test_img_path[0])
# 展示待預測圖片
plt.figure(figsize=(10,10))
plt.imshow(img)
plt.axis('off')
plt.show()
#若是待預測圖片存放在一個文件中,如左側文件夾所示的test.txt。每一行是待預測圖片的存放路徑。
!cat test.txt
with open('test.txt', 'r') as f:
test_img_path=[]
for line in f:
test_img_path.append(line.strip())
print(test_img_path)
#二、加載預訓練模型
import paddlehub as hub
module = hub.Module(name="ultra_light_fast_generic_face_detector_1mb_640")
#module = hub.Moudle(name="ultra_light_fast_generic_face_detector_1mb_640")
#三、預測 PaddleHub對於支持一鍵預測的module,可以調用module的相應預測API,完成預測功能。
import numpy as np
from PIL import Image
import matplotlib.patches as patches
input_dict = {"image": test_img_path}
# execute predict and print the result
results = module.face_detection(data=input_dict)
for result in results:
print(result)
pil_im1 = Image.open('./ren5.jpg')
pil_im2 = Image.open('./mi.jpg')
plt.figure("girlfriend1")
plt.imshow(pil_im1)
#box = (37,90,200,236)
box = (800,800,1312,1312)
region = pil_im1.crop(box)#cut from the picture
plt.figure("girlfriend2")
plt.imshow(region)
region = region.transpose(Image.ROTATE_270)#rotate the image
pil_im1.paste(pil_im2, box)
plt.figure("girlfriend3")
plt.imshow(pil_im1)
實現效果圖:
原圖
通過人臉檢測出臉的座標位置:
然後最後在人臉座標處貼入想要的遮擋物: