openCV摄像头人脸检测源代码:

{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import cv2\n",
    "face_detector = cv2.CascadeClassifier('./haarcascade_frontalface_alt.xml')\n",
    "video = cv2.VideoCapture(0)#打开电脑上的摄像头\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",
    "    for x,y,w,h in face_zones:\n",
    "        cv2.circle(frame,center = (x+w//2,y+h//2),radius = w//2,color = [0,0,255],thickness = 2)\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,视频处理,人脸识别)