[Python & OpenCV]人脸识别并把人脸打马赛克

人脸识别并把人脸打马赛克

def face_msk():
    capture = cv2.VideoCapture(0)           #打开摄像头(0/1/2代表摄像头编号0.1.2)
    while(True):
        ret,frame = capture.read()          #读一帧
        frame = cv2.flip(frame,1)           #镜头翻转(1水平,0垂直,-1水平垂直)
        gray = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)#人脸识别必须转化为灰阶图像
        face_cascade = cv2.CascadeClassifier(r"C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python36_64\Lib\site-packages\cv2\data\haarcascade_frontalface_default.xml")#创建一个级联分类器对象(包含大数据训练的人脸特征) 
        faces = face_cascade.detectMultiScale(gray,scaleFactor = 1.07,minNeighbors = 5) #(图,每次图像缩小比例(根据人脸大小),每个目标检测次数阈值 )
        for x,y,w,h, in faces:
            #print("x = %d,y = %d,w = %d,h = %d"%(x,y,w,h))
            for m in range(y,y + h):#y
                for n in range(x,x + w):#x
                    if m%10 == 0 and n%10 == 0:	# 将10 * 10的方格内的像素颜色,设置与[m,n]点颜色相同
                        for i in range(10):
                            for j in range(10):
                                (b,g,r) = frame[m,n]
                                frame[i+m,j+n] = (b,g,r)
            cv2.imshow("1",frame)
            c = cv2.waitKey(2)
            if c == 27: #ESC按键
                cv2.destroyAllWindows()
                break

效果如图
[Python & OpenCV]人脸识别并把人脸打马赛克_第1张图片

你可能感兴趣的:(Opencv相关,人脸识别,opencv,计算机视觉,python)