openCV视频识别人脸戴面具源代码

{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import cv2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "face_detector = cv2.CascadeClassifier('./haarcascade_frontalface_alt.xml')\n",
    "video = cv2.VideoCapture(0)#打开电脑上的摄像头\n",
    "mask = cv2.imread('./mask.png')#读取文件,速度慢\n",
    "while True:\n",
    "    flag,frame = video.read()\n",
    "    if flag == False:\n",
    "        break\n",
    "    gray = cv2.cvtColor(frame,code = cv2.COLOR_BGR2GRAY)\n",
    "    face_zones = face_detector.detectMultiScale(gray)\n",
    "    mask2 = mask.copy()#快\n",
    "    for x,y,w,h in face_zones:\n",
    "        mask2 = cv2.resize(mask2,(w,h))\n",
    "#         frame[y:y+h,x:x+w] = mask2\n",
    "        for i in range(h):#高度 y\n",
    "            for j in range(w):#宽度 x\n",
    "                b,g,r = mask2[i,j]\n",
    "                if (b>180)&(g>180)&(r>180):\n",
    "                    frame[y+i-10,x+j] = mask2[i,j]\n",
    "    cv2.imshow('ttnk',frame)\n",
    "    key = cv2.waitKey(41)\n",
    "    if key == ord('q'):#退出条件\n",
    "        break\n",
    "cv2.destroyAllWindows()\n",
    "video.release()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}

你可能感兴趣的:(openCV视频识别人脸戴面具源代码)