天天看点

Python视频中人脸识别openCV源代码

{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import cv2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "----------------------- False\n"
     ]
    }
   ],
   "source": [
    "# 视频由一张张图片组成,每张图片叫做:帧\n",
    "# 视频,一秒24帧\n",
    "# 视频加载成功\n",
    "face_detector = cv2.CascadeClassifier('./haarcascade_frontalface_alt.xml')\n",
    "video = cv2.VideoCapture('./v.mp4')\n",
    "# 写视频\n",
    "fourcc = cv2.VideoWriter_fourcc('M','P','4','2')# 视频格式\n",
    "fps = 24 # 视频帧 一秒24帧\n",
    "size = (426,240) # 图片尺寸\n",
    "write = cv2.VideoWriter('./v2.mp4',fourcc,fps,size)\n",
    "while True:\n",
    "    flag,frame = video.read()\n",
    "    if flag == False:\n",
    "        print('-----------------------',flag)\n",
    "        break\n",
    "    frame = cv2.resize(frame,(426,240))#尺寸缩放,高度和宽度变成原来的三分之一\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.rectangle(frame,pt1 = (x,y),pt2 = (x+w,y+h),color = [0,0,255],thickness = 2)\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",
    "    write.write(frame)\n",
    "    key = cv2.waitKey(41)\n",
    "    if key == ord('q'):#退出条件\n",
    "        break\n",
    "cv2.destroyAllWindows()\n",
    "video.release()\n",
    "write.release()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [],
   "source": [
    "import subprocess"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "call = 'ffmpeg -i v2.mp4 -i goon.wav out.mp4'\n",
    "subprocess.call(call)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "cmd = 'ffmpeg -i \"lion my heart will go on.mp4\" -vn music3.mp3'\n",
    "subprocess.call(cmd)"
   ]
  }
 ],
 "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
}