基于Python-opencv的人脸识别检测

1.人脸识别检测已经做出来了,现在需要多加一个功能,就是在人脸定位了之后,要对人脸进行覆盖,就是用图片把人脸覆盖。这个功能我不会,求各位大神帮一下子。
我的代码如下:
import cv2

face_cascade = cv2.CascadeClassifier('E:\openCV\opencv\sources/data/haarcascades/haarcascade_frontalface_alt2.xml')
cap = cv2.VideoCapture(0)
while True:
ret,img = cap.read()
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, 1.3, 5)
for (x,y,w,h) in faces:
cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)
cv2.imshow('img',img)

if cv2.waitKey(1) &0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()

from PIL import Image
#这个直接pip安装就好 pip install pillow

img1_path = r'C:\Users\Jack\Desktop\Head\head4.jpg'
img1 = Image.open(img1_path)#打开图片1
img2_path = r'C:\Users\Jack\Desktop\Head\head1.jpg'
img2 = Image.open(img2_path)#打开图片二
img2 = img2.resize((80,80))#缩放图片二
box = (90,40,90+80,40+80)#定义box 左,上,右,下像素位置 要和图片二大小一致(所以上面缩放下)
img1.paste(img2,box)
img1.show()

#照上面逻辑处理就好了

可以使用PIL库
from PIL import Image

PIL文档:

img.paste(image, box)

Pastes another image into this image. The box argument is either a 2-tuple giving the upper left corner, a 4-tuple defining the left, upper, right, and lower pixel coordinate, or None (same as (0, 0)). If a 4-tuple is given, the size of the pasted image must match the size of the region.

faces = face_cascade.detectMultiScale(gray, 1.3, 5)
for (x,y,w,h) in faces:
cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)
cv2.imshow('img',img)

我没装,没法调试,这个faces不是都已经提取了么? 你要的不是在每个面部加个矩形框么?for里的代码好像就是画面部矩形的。。。
可以断点看看,cv2.imshow('img',img) 这个好像调用不正确吧,具体看看方法说明,感觉你这都基本实现好了。

cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)这一句改成img[x:x+w,y:y+h]=0 ,这个是把人脸置黑,类似修改就行