{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "%matplotlib inline" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Face Detection\n\nThis demo shows how a simple image processing example can be demonstrated.\nThe ``itom`` grabber ``OpenCVGrabber`` captures your webcam.\nThen live your face and eyes are detected and marked in the live plot.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "from itom import dataObject\nfrom itom import dataIO\nfrom itom import ui\nimport cv2\nimport numpy as np\nimport gc" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Face detection method.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "def detectFace(img, cascade):\n rects = cascade.detectMultiScale(img, scaleFactor=1.3, minNeighbors=4)\n if len(rects) == 0:\n return []\n rects[:, 2:] += rects[:, :2]\n return rects" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Eye detection method.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "def detectEyes(img, cascade):\n eyes = cascade.detectMultiScale(img)\n if len(eyes) == 0:\n return []\n\n if eyes.shape[0] > 2:\n eyes = eyes[0:1, :]\n\n return eyes" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Draw detected rectangle method. \n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "def drawRects(img, faces, color):\n for x1, y1, x2, y2 in faces:\n rect = shape.createRectangle((x1, y1), (x2, y2), index=11)\n rect.color = color\n win.plot.call(\"updateGeometricShape\", rect)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Draw detected eyes method.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "def drawEyes(img, eyes, color):\n cnt = 21\n for x, y, w, h in eyes:\n eye = shape.createEllipse((x, y + h), (x + w, y), index=cnt)\n eye.color = color\n try:\n win.plot.call(\"updateGeometricShape\", eye)\n except AttributeError:\n break\n cnt = cnt + 1" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Acquire an image from the webcam.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "def snap():\n # image acquisition\n if \"cam\" in globals():\n d = dataObject()\n cam.disableAutoGrabbing()\n cam.acquire()\n cam.getVal(d)\n else:\n return\n\n img = np.array(d)\n\n win.plot[\"source\"] = img\n\n # detect face and eyes\n faces = detectFace(img, faceCascade)\n eyes = detectEyes(img, eyeCascade)\n\n # overlay rect and eyes\n drawRects(img, faces, rgba(255, 0, 0, 255)) # in color red\n drawEyes(img, eyes, rgba(0, 255, 0, 255)) # in color green" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Close GUI and stop webcam. \n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "def guiClosed():\n tDetect.stop()\n global cam, win\n del win\n del cam\n gc.collect()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Open a simple ``GUI``, connect the webcam and starte the live face detection. \n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "win = ui(\n \"FaceDetect.ui\",\n ui.TYPEWINDOW,\n childOfMainWindow=True,\n deleteOnClose=True,\n)\n\nfaceCascade = cv2.CascadeClassifier()\neyeCascade = cv2.CascadeClassifier()\n\nfaceCascade.load(\"haarcascades/haarcascade_frontalface_alt.xml\")\neyeCascade.load(\"haarcascades/haarcascade_eye_tree_eyeglasses.xml\")\n\ncam = dataIO(\"OpenCVGrabber\", 0, \"gray\")\ncam.startDevice()\ncam.disableAutoGrabbing()\n\ntDetect = timer(100, snap)\n\nwin.connect(\"destroyed()\", guiClosed)\n\n# start GUI\nwin.show(0)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n" ] } ], "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.10" } }, "nbformat": 4, "nbformat_minor": 0 }