|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": {}, |
| 6 | + "source": [ |
| 7 | + "# Putting Glasses and Moustache in Images using OpenCV" |
| 8 | + ] |
| 9 | + }, |
| 10 | + { |
| 11 | + "cell_type": "code", |
| 12 | + "execution_count": 1, |
| 13 | + "metadata": {}, |
| 14 | + "outputs": [], |
| 15 | + "source": [ |
| 16 | + "import numpy as np\n", |
| 17 | + "import cv2\n", |
| 18 | + "from utils import MyVideoConf, image_resize" |
| 19 | + ] |
| 20 | + }, |
| 21 | + { |
| 22 | + "cell_type": "code", |
| 23 | + "execution_count": 2, |
| 24 | + "metadata": {}, |
| 25 | + "outputs": [], |
| 26 | + "source": [ |
| 27 | + "cap = cv2.VideoCapture(0)\n", |
| 28 | + "\n", |
| 29 | + "save_path = 'saved-media/glasses_and_stash.mp4'\n", |
| 30 | + "frames_per_seconds = 24\n", |
| 31 | + "config = MyVideoConf(cap, filepath=save_path, res='720p')\n", |
| 32 | + "out = cv2.VideoWriter(save_path, config.video_type, frames_per_seconds, config.dims)\n", |
| 33 | + "face_cascade = cv2.CascadeClassifier('src/cascades/data/haarcascade_frontalface_default.xml')\n", |
| 34 | + "eyes_cascade = cv2.CascadeClassifier('src/cascades/third-party/frontalEyes35x16.xml')\n", |
| 35 | + "nose_cascade = cv2.CascadeClassifier('src/cascades/third-party/Nose18x15.xml')\n", |
| 36 | + "glasses = cv2.imread(\"src/images/fun/glasses.png\", -1)\n", |
| 37 | + "mustache = cv2.imread('src/images/fun/mustache.png',-1)\n", |
| 38 | + "\n", |
| 39 | + "\n", |
| 40 | + "while(True):\n", |
| 41 | + " ret, frame = cap.read()\n", |
| 42 | + " gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)\n", |
| 43 | + " faces = face_cascade.detectMultiScale(gray, scaleFactor=1.5, minNeighbors=5)\n", |
| 44 | + "\n", |
| 45 | + " frame = cv2.cvtColor(frame, cv2.COLOR_BGR2BGRA)\n", |
| 46 | + "\n", |
| 47 | + " for (x, y, w, h) in faces:\n", |
| 48 | + " roi_gray = gray[y:y+h, x:x+h]\n", |
| 49 | + " roi_color = frame[y:y+h, x:x+h]\n", |
| 50 | + " cv2.rectangle(frame, (x, y), (x + w, y + h), (255, 255, 255), 3)\n", |
| 51 | + "\n", |
| 52 | + " eyes = eyes_cascade.detectMultiScale(roi_gray, scaleFactor=1.5, minNeighbors=5)\n", |
| 53 | + " for (ex, ey, ew, eh) in eyes:\n", |
| 54 | + " cv2.rectangle(roi_color, (ex, ey), (ex + ew, ey + eh), (0, 255, 0), 3)\n", |
| 55 | + " roi_eyes = roi_gray[ey: ey + eh, ex: ex + ew]\n", |
| 56 | + " glasses2 = image_resize(glasses.copy(), width=ew)\n", |
| 57 | + "\n", |
| 58 | + " gw, gh, gc = glasses2.shape\n", |
| 59 | + " for i in range(0, gw):\n", |
| 60 | + " for j in range(0, gh):\n", |
| 61 | + " #print(glasses[i, j]) #RGBA\n", |
| 62 | + " if glasses2[i, j][3] != 0: # alpha 0\n", |
| 63 | + " roi_color[ey + i, ex + j] = glasses2[i, j]\n", |
| 64 | + "\n", |
| 65 | + "\n", |
| 66 | + " nose = nose_cascade.detectMultiScale(roi_gray, scaleFactor=1.5, minNeighbors=5)\n", |
| 67 | + " for (nx, ny, nw, nh) in nose:\n", |
| 68 | + " cv2.rectangle(roi_color, (nx, ny), (nx + nw, ny + nh), (255, 0, 0), 3)\n", |
| 69 | + " roi_nose = roi_gray[ny: ny + nh, nx: nx + nw]\n", |
| 70 | + " mustache2 = image_resize(mustache.copy(), width=nw)\n", |
| 71 | + "\n", |
| 72 | + " mw, mh, mc = mustache2.shape\n", |
| 73 | + " for i in range(0, mw):\n", |
| 74 | + " for j in range(0, mh):\n", |
| 75 | + " #print(glasses[i, j]) #RGBA\n", |
| 76 | + " if mustache2[i, j][3] != 0: # alpha 0\n", |
| 77 | + " roi_color[ny + int(nh/2.0) + i, nx + j] = mustache2[i, j]\n", |
| 78 | + "\n", |
| 79 | + " # Display the resulting frame\n", |
| 80 | + " frame = cv2.cvtColor(frame, cv2.COLOR_BGRA2BGR)\n", |
| 81 | + " out.write(frame)\n", |
| 82 | + " cv2.imshow('frame',frame)\n", |
| 83 | + " if cv2.waitKey(20) & 0xFF == ord('q'):\n", |
| 84 | + " break\n", |
| 85 | + "\n", |
| 86 | + "# When everything done, release the capture\n", |
| 87 | + "cap.release()\n", |
| 88 | + "out.release()\n", |
| 89 | + "cv2.destroyAllWindows()" |
| 90 | + ] |
| 91 | + }, |
| 92 | + { |
| 93 | + "cell_type": "code", |
| 94 | + "execution_count": null, |
| 95 | + "metadata": {}, |
| 96 | + "outputs": [], |
| 97 | + "source": [] |
| 98 | + }, |
| 99 | + { |
| 100 | + "cell_type": "code", |
| 101 | + "execution_count": null, |
| 102 | + "metadata": {}, |
| 103 | + "outputs": [], |
| 104 | + "source": [] |
| 105 | + }, |
| 106 | + { |
| 107 | + "cell_type": "code", |
| 108 | + "execution_count": null, |
| 109 | + "metadata": {}, |
| 110 | + "outputs": [], |
| 111 | + "source": [] |
| 112 | + }, |
| 113 | + { |
| 114 | + "cell_type": "code", |
| 115 | + "execution_count": null, |
| 116 | + "metadata": {}, |
| 117 | + "outputs": [], |
| 118 | + "source": [] |
| 119 | + } |
| 120 | + ], |
| 121 | + "metadata": { |
| 122 | + "kernelspec": { |
| 123 | + "display_name": "Python 3", |
| 124 | + "language": "python", |
| 125 | + "name": "python3" |
| 126 | + }, |
| 127 | + "language_info": { |
| 128 | + "codemirror_mode": { |
| 129 | + "name": "ipython", |
| 130 | + "version": 3 |
| 131 | + }, |
| 132 | + "file_extension": ".py", |
| 133 | + "mimetype": "text/x-python", |
| 134 | + "name": "python", |
| 135 | + "nbconvert_exporter": "python", |
| 136 | + "pygments_lexer": "ipython3", |
| 137 | + "version": "3.5.2" |
| 138 | + } |
| 139 | + }, |
| 140 | + "nbformat": 4, |
| 141 | + "nbformat_minor": 2 |
| 142 | +} |
0 commit comments