diff --git a/Retinafacedetection.ipynb b/Retinafacedetection.ipynb new file mode 100644 index 0000000000000000000000000000000000000000..b41e129cb292b2dbaf198b77f5072e6c98bf99aa --- /dev/null +++ b/Retinafacedetection.ipynb @@ -0,0 +1,163 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "from IPython.display import display, Javascript\n", + "from utilpack.util import *\n", + "\n", + "from retinaface import RetinaFace\n", + "from deepface import DeepFace\n", + "\n", + "from datetime import datetime\n", + "import matplotlib.pyplot as plt\n", + "import pandas as pd\n", + "import cv2\n", + "\n", + "import numpy as np\n", + "import os\n", + "import h5py" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "filename = 'video.avi'\n", + "frames_per_second = 24.0\n", + "res = '720p'\n", + "\n", + "# Set resolution for the video capture\n", + "# Function adapted from https://kirr.co/0l6qmh\n", + "def change_res(cap, width, height):\n", + " cap.set(3, width)\n", + " cap.set(4, height)\n", + "\n", + "# Standard Video Dimensions Sizes\n", + "STD_DIMENSIONS = {\n", + " \"480p\": (640, 480),\n", + " \"720p\": (1280, 720),\n", + " \"1080p\": (1920, 1080),\n", + " \"4k\": (3840, 2160),\n", + "}\n", + "\n", + "\n", + "# grab resolution dimensions and set video capture to it.\n", + "def get_dims(cap, res='1080p'):\n", + " width, height = STD_DIMENSIONS[\"480p\"]\n", + " if res in STD_DIMENSIONS:\n", + " width,height = STD_DIMENSIONS[res]\n", + " ## change the current caputre device\n", + " ## to the resulting resolution\n", + " change_res(cap, width, height)\n", + " return width, height\n", + "\n", + "# Video Encoding, might require additional installs\n", + "# Types of Codes: http://www.fourcc.org/codecs.php\n", + "VIDEO_TYPE = {\n", + " 'avi': cv2.VideoWriter_fourcc(*'XVID'),\n", + " #'mp4': cv2.VideoWriter_fourcc(*'H264'),\n", + " 'mp4': cv2.VideoWriter_fourcc(*'XVID'),\n", + "}\n", + "\n", + "def get_video_type(filename):\n", + " filename, ext = os.path.splitext(filename)\n", + " if ext in VIDEO_TYPE:\n", + " return VIDEO_TYPE[ext]\n", + " return VIDEO_TYPE['avi']\n", + "\n", + "cap = cv2.VideoCapture(0)\n", + "out = cv2.VideoWriter(filename, get_video_type(filename), 25, get_dims(cap, res))\n", + "start_time = datetime.now()\n", + "\n", + "while True:\n", + " ret, frame = cap.read()\n", + " img_cv = cv2.imshow('frame',frame)\n", + " \n", + " # diff = (datetime.now() - start_time).seconds # converting into seconds\n", + " # print(diff)\n", + " # while( diff <= duration ):\n", + " out.write(frame)\n", + " # rgb_image = cv2.cvtColor(img_cv,PyImageUtil.cv2.COLOR_BGR2RGB).astype(np.float32)\n", + "\n", + " # # cv2.imwrite(\"NewPicture.jpg\",frame)\n", + " # diff = (datetime.now() - start_time).seconds\n", + " # print(diff)\n", + " if cv2.waitKey(1) & 0xFF == ord('q'):\n", + " break\n", + " # # img_path = \"NewPicture.jpg\"\n", + " # img = cv2.imread(rgb_image)\n", + " # obj = RetinaFace.detect_faces(rgb_image)\n", + " # RetinaFace.get_image()\n", + " # img.shape\n", + "\n", + " # for key in obj.keys():\n", + " # identity = obj[key]\n", + " # # print(identity)\n", + " # facial_area = identity[\"facial_area\"]\n", + " # cv2.rectangle(img, (facial_area[2],facial_area[3]),(facial_area[0],facial_area[1]),(255,255,255),1)\n", + "\n", + " # plt.figure(figsize=(20,20))\n", + " # plt.imshow(img[:,:,::-1])\n", + " # plt.show\n", + "\n", + "cap.release()\n", + "out.release()\n", + "cv2.destroyAllWindows()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "img_path = \"NewPicture.jpg\"\n", + "img = cv2.imread(img_path)\n", + "obj = RetinaFace.detect_faces(img_path)\n", + "\n", + "img.shape\n", + "\n", + "for key in obj.keys():\n", + " identity = obj[key]\n", + " # print(identity)\n", + " facial_area = identity[\"facial_area\"]\n", + " cv2.rectangle(img, (facial_area[2],facial_area[3]),(facial_area[0],facial_area[1]),(255,255,255),1)\n", + "\n", + "plt.figure(figsize=(20,20))\n", + "plt.imshow(img[:,:,::-1])\n", + "plt.show" + ] + } + ], + "metadata": { + "interpreter": { + "hash": "e13dd8a849d904006525f725a94fd9290dba9eb122ef777785557ae33676f1f8" + }, + "kernelspec": { + "display_name": "Python 3.6.13 ('compare')", + "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.6.13" + }, + "orig_nbformat": 4 + }, + "nbformat": 4, + "nbformat_minor": 2 +}