{ "cells": [ { "cell_type": "markdown", "metadata": { "id": "view-in-github", "colab_type": "text" }, "source": [ "\"Open" ] }, { "cell_type": "markdown", "metadata": { "id": "UqNpNL9YdW_F" }, "source": [ "##Preliminaries" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true, "id": "AstQdmx7ecwk" }, "outputs": [], "source": [ "%%capture\n", "%mkdir yearbook\n", "%cd yearbook\n", "!pip install --upgrade --no-cache-dir gdown\n", "!gdown --id \"1NHT8NN8ClBEnUC5VqkP3wr2KhyiIQzyU\"\n", "!unzip PHfiles.zip\n", "%mkdir images\n", "!pip install PyMuPDF\n", "!pip install dlib\n", "!pip install DeepFace\n", "import os, shutil, fitz, cv2, numpy as np, pandas as pd, dlib, tensorflow as tf\n", "from os.path import dirname, join\n", "from deepface import DeepFace\n", "os.environ[\"CUDA_VISIBLE_DEVICES\"] = \"-1\"" ] }, { "cell_type": "markdown", "metadata": { "id": "5D4NGWwSdagp" }, "source": [ "##PDF Conversion" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "collapsed": true, "id": "rhGzknifXosP", "outputId": "b40db119-4e2a-4c94-8250-1ea17ea6d1b6" }, "outputs": [], "source": [ "path = r'./'\n", "pdfs = [f for f in os.listdir(path) if f.endswith('.pdf')]\n", "for pdf in pdfs:\n", " os.chdir(os.path.join('./images'))\n", " os.mkdir((pdf.split(\".\")[0]))\n", " newdir = (os.path.join('./images/' + os.path.join(pdf.split(\".\")[0])))\n", " os.chdir(\"..\")\n", " print (\"Now copying images into \" + (newdir))\n", " shutil.copy(pdf, newdir)\n", " os.chdir(newdir)\n", " doc = fitz.open(pdf)\n", " for page in doc:\n", " pix = page.get_pixmap()\n", " pix.save(\"page-%i.png\" % page.number)\n", " os.chdir(os.path.dirname(os.getcwd()))\n", " os.chdir(\"..\")\n", "\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "QY1pXsKcmS1c" }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": { "id": "2jYcnBWodq7V" }, "source": [ "##Object Detection and Facial Recognition: Code" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "collapsed": true, "id": "92YhmH4AhFR1", "outputId": "bb69661d-fdda-4eb7-bdb4-11d17c5717d7" }, "outputs": [], "source": [ "path = r'./'\n", "\n", "os.chdir(os.path.join(path + 'images'))\n", "dirs = os.listdir(path)\n", "for dir in dirs:\n", " os.chdir(os.path.join(path + dir))\n", " pngs = [f for f in os.listdir(path) if f.endswith('.png')]\n", "\n", " if not os.path.exists((dir) + ' faces'):\n", " print(\"New 'faces' directory created in \" + (dir) + \" folder\")\n", " os.makedirs((dir) + ' faces')\n", "\n", " count = 0\n", " for png in pngs:\n", " image = cv2.imread(png)\n", "\n", " greyscale_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)\n", "\n", " face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + \"haarcascade_frontalface_default.xml\")\n", "\n", " detected_faces = face_cascade.detectMultiScale(image=greyscale_image, scaleFactor=1.9, minNeighbors=4)\n", "\n", " count = 0\n", " for (x,y,w,h) in detected_faces:\n", " try:\n", " xpadding = 20\n", " ypadding = 40\n", " crop_face = image[y-ypadding: y + h+ypadding, x-xpadding: x + w+xpadding]\n", " count+=1\n", " face = cv2.rectangle(crop_face,(x,y),(x+w,y+h),(255,0,0),2)\n", " cv2.imwrite(path + (dir) + ' faces/' + str(count) + '_' + png, face)\n", " except (Exception):\n", " print(\"An error happened\")\n", " continue\n", " os.remove(os.path.join(path, png))\n", " os.chdir(\"..\")\n" ] }, { "cell_type": "markdown", "metadata": { "id": "ysjW9Atvd00g" }, "source": [ "##Identify Smiles: Code" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "collapsed": true, "id": "26ben4VqkGwd", "outputId": "d22d37eb-4f1c-4e2e-bdec-690c4ee32349" }, "outputs": [], "source": [ "%cd ..\n", "number_smiles = 0\n", "smile_counts = []\n", "number_nonsmiles = 0\n", "nonsmile_counts = []\n", "num_errors = 0\n", "error_counts = []\n", "pngs = []\n", "\n", "file_count = 0\n", "file_count_list = []\n", "\n", "years = ['1911', '1921', '1931', '1941', '1951', '1961']\n", "\n", "for year in years:\n", " path = r'./images' + '/' + year\n", " for root, dirs, files in os.walk(path):\n", " for dir in dirs:\n", " path = path + '/' + (year + ' faces')\n", " if(file_count != 0):\n", " file_count_list.append(file_count)\n", " file_count = 0\n", " for f in os.listdir(path):\n", " if f.endswith('.png'):\n", " pngs.append(path + '/' + f)\n", " file_count = file_count + 1\n", "\n", "file_count_list.append(file_count)\n", "\n", "total_loops = 0\n", "count = 0\n", "iterator = 0\n", "for png in pngs:\n", " try:\n", " total_loops = total_loops + 1\n", " count = count + 1\n", " if(count != (file_count_list[iterator] + 1)):\n", " demography = DeepFace.analyze(png, actions = ['emotion'])\n", " print(demography)\n", " if(demography[0]['dominant_emotion'] == 'happy'):\n", " number_smiles = number_smiles + 1\n", " else:\n", " number_nonsmiles = number_nonsmiles + 1\n", "\n", " else:\n", " count = count - 1\n", " smile_counts.append(number_smiles / count)\n", " nonsmile_counts.append(number_nonsmiles / count)\n", " error_counts.append(num_errors / count)\n", " number_smiles = 0\n", " number_nonsmiles = 0\n", " num_errors = 0\n", " iterator = iterator + 1\n", " count = 0\n", "\n", " except (Exception):\n", " num_errors = num_errors + 1\n", " print(\"An error happened\")\n", " continue\n", "\n", "smile_counts.append(number_smiles / count)\n", "nonsmile_counts.append(number_nonsmiles / count)\n", "error_counts.append(num_errors / count)\n", "\n", "dict = {'Years': years, 'Smiles': smile_counts, 'Non-Smiles': nonsmile_counts, \"Error Weight\": error_counts}\n", "data = pd.DataFrame(dict)\n", "data.to_csv('YearbookOutput.csv', index=False)\n", "print(count)\n" ] }, { "cell_type": "markdown", "metadata": { "id": "Sa2rV4Hzd5NE" }, "source": [ "##Download and results" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 17 }, "id": "QxBepnRtns50", "outputId": "88cf3816-bead-493e-c63b-9e4c70d4b672" }, "outputs": [], "source": [ "from google.colab import files\n", "files.download('YearbookOutput.csv')" ] } ], "metadata": { "accelerator": "GPU", "colab": { "provenance": [] }, "kernelspec": { "display_name": "Python 3", "name": "python3" }, "language_info": { "name": "python" } }, "nbformat": 4, "nbformat_minor": 0 }