[
  {
    "path": ".gitignore",
    "content": "# vscode\n.vscode/\nvenv/\n\n# mac stuff b/c I wrote a video file\n.DS_Store\n\n# video files I'm playing w lol\n*.mp4\n\n# Byte-compiled / optimized / DLL files\n__pycache__/\n*.py[cod]\n*$py.class\n\n# C extensions\n*.so\n\n# Distribution / packaging\n.Python\nbuild/\ndevelop-eggs/\ndist/\ndownloads/\neggs/\n.eggs/\nlib/\nlib64/\nparts/\nsdist/\nvar/\nwheels/\npip-wheel-metadata/\nshare/python-wheels/\n*.egg-info/\n.installed.cfg\n*.egg\nMANIFEST\n\n# PyInstaller\n#  Usually these files are written by a python script from a template\n#  before PyInstaller builds the exe, so as to inject date/other infos into it.\n*.manifest\n*.spec\n\n# Installer logs\npip-log.txt\npip-delete-this-directory.txt\n\n# Unit test / coverage reports\nhtmlcov/\n.tox/\n.nox/\n.coverage\n.coverage.*\n.cache\nnosetests.xml\ncoverage.xml\n*.cover\n*.py,cover\n.hypothesis/\n.pytest_cache/\n\n# Translations\n*.mo\n*.pot\n\n# Django stuff:\n*.log\nlocal_settings.py\ndb.sqlite3\ndb.sqlite3-journal\n\n# Flask stuff:\ninstance/\n.webassets-cache\n\n# Scrapy stuff:\n.scrapy\n\n# Sphinx documentation\ndocs/_build/\n\n# PyBuilder\ntarget/\n\n# Jupyter Notebook\n.ipynb_checkpoints\n\n# IPython\nprofile_default/\nipython_config.py\n\n# pyenv\n.python-version\n\n# pipenv\n#   According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.\n#   However, in case of collaboration, if having platform-specific dependencies or dependencies\n#   having no cross-platform support, pipenv may install dependencies that don't work, or not\n#   install all needed dependencies.\n#Pipfile.lock\n\n# PEP 582; used by e.g. github.com/David-OConnor/pyflow\n__pypackages__/\n\n# Celery stuff\ncelerybeat-schedule\ncelerybeat.pid\n\n# SageMath parsed files\n*.sage.py\n\n# Environments\n.env\n.venv\nenv/\nvenv/\nENV/\nenv.bak/\nvenv.bak/\n\n# Spyder project settings\n.spyderproject\n.spyproject\n\n# Rope project settings\n.ropeproject\n\n# mkdocs documentation\n/site\n\n# mypy\n.mypy_cache/\n.dmypy.json\ndmypy.json\n\n# Pyre type checker\n.pyre/\n"
  },
  {
    "path": "LICENSE.md",
    "content": "Mozilla Public License Version 2.0\n==================================\n\n1. Definitions\n--------------\n\n1.1. \"Contributor\"\n    means each individual or legal entity that creates, contributes to\n    the creation of, or owns Covered Software.\n\n1.2. \"Contributor Version\"\n    means the combination of the Contributions of others (if any) used\n    by a Contributor and that particular Contributor's Contribution.\n\n1.3. \"Contribution\"\n    means Covered Software of a particular Contributor.\n\n1.4. \"Covered Software\"\n    means Source Code Form to which the initial Contributor has attached\n    the notice in Exhibit A, the Executable Form of such Source Code\n    Form, and Modifications of such Source Code Form, in each case\n    including portions thereof.\n\n1.5. \"Incompatible With Secondary Licenses\"\n    means\n\n    (a) that the initial Contributor has attached the notice described\n        in Exhibit B to the Covered Software; or\n\n    (b) that the Covered Software was made available under the terms of\n        version 1.1 or earlier of the License, but not also under the\n        terms of a Secondary License.\n\n1.6. \"Executable Form\"\n    means any form of the work other than Source Code Form.\n\n1.7. \"Larger Work\"\n    means a work that combines Covered Software with other material, in\n    a separate file or files, that is not Covered Software.\n\n1.8. \"License\"\n    means this document.\n\n1.9. \"Licensable\"\n    means having the right to grant, to the maximum extent possible,\n    whether at the time of the initial grant or subsequently, any and\n    all of the rights conveyed by this License.\n\n1.10. \"Modifications\"\n    means any of the following:\n\n    (a) any file in Source Code Form that results from an addition to,\n        deletion from, or modification of the contents of Covered\n        Software; or\n\n    (b) any new file in Source Code Form that contains any Covered\n        Software.\n\n1.11. \"Patent Claims\" of a Contributor\n    means any patent claim(s), including without limitation, method,\n    process, and apparatus claims, in any patent Licensable by such\n    Contributor that would be infringed, but for the grant of the\n    License, by the making, using, selling, offering for sale, having\n    made, import, or transfer of either its Contributions or its\n    Contributor Version.\n\n1.12. \"Secondary License\"\n    means either the GNU General Public License, Version 2.0, the GNU\n    Lesser General Public License, Version 2.1, the GNU Affero General\n    Public License, Version 3.0, or any later versions of those\n    licenses.\n\n1.13. \"Source Code Form\"\n    means the form of the work preferred for making modifications.\n\n1.14. \"You\" (or \"Your\")\n    means an individual or a legal entity exercising rights under this\n    License. For legal entities, \"You\" includes any entity that\n    controls, is controlled by, or is under common control with You. For\n    purposes of this definition, \"control\" means (a) the power, direct\n    or indirect, to cause the direction or management of such entity,\n    whether by contract or otherwise, or (b) ownership of more than\n    fifty percent (50%) of the outstanding shares or beneficial\n    ownership of such entity.\n\n2. License Grants and Conditions\n--------------------------------\n\n2.1. Grants\n\nEach Contributor hereby grants You a world-wide, royalty-free,\nnon-exclusive license:\n\n(a) under intellectual property rights (other than patent or trademark)\n    Licensable by such Contributor to use, reproduce, make available,\n    modify, display, perform, distribute, and otherwise exploit its\n    Contributions, either on an unmodified basis, with Modifications, or\n    as part of a Larger Work; and\n\n(b) under Patent Claims of such Contributor to make, use, sell, offer\n    for sale, have made, import, and otherwise transfer either its\n    Contributions or its Contributor Version.\n\n2.2. Effective Date\n\nThe licenses granted in Section 2.1 with respect to any Contribution\nbecome effective for each Contribution on the date the Contributor first\ndistributes such Contribution.\n\n2.3. Limitations on Grant Scope\n\nThe licenses granted in this Section 2 are the only rights granted under\nthis License. No additional rights or licenses will be implied from the\ndistribution or licensing of Covered Software under this License.\nNotwithstanding Section 2.1(b) above, no patent license is granted by a\nContributor:\n\n(a) for any code that a Contributor has removed from Covered Software;\n    or\n\n(b) for infringements caused by: (i) Your and any other third party's\n    modifications of Covered Software, or (ii) the combination of its\n    Contributions with other software (except as part of its Contributor\n    Version); or\n\n(c) under Patent Claims infringed by Covered Software in the absence of\n    its Contributions.\n\nThis License does not grant any rights in the trademarks, service marks,\nor logos of any Contributor (except as may be necessary to comply with\nthe notice requirements in Section 3.4).\n\n2.4. Subsequent Licenses\n\nNo Contributor makes additional grants as a result of Your choice to\ndistribute the Covered Software under a subsequent version of this\nLicense (see Section 10.2) or under the terms of a Secondary License (if\npermitted under the terms of Section 3.3).\n\n2.5. Representation\n\nEach Contributor represents that the Contributor believes its\nContributions are its original creation(s) or it has sufficient rights\nto grant the rights to its Contributions conveyed by this License.\n\n2.6. Fair Use\n\nThis License is not intended to limit any rights You have under\napplicable copyright doctrines of fair use, fair dealing, or other\nequivalents.\n\n2.7. Conditions\n\nSections 3.1, 3.2, 3.3, and 3.4 are conditions of the licenses granted\nin Section 2.1.\n\n3. Responsibilities\n-------------------\n\n3.1. Distribution of Source Form\n\nAll distribution of Covered Software in Source Code Form, including any\nModifications that You create or to which You contribute, must be under\nthe terms of this License. You must inform recipients that the Source\nCode Form of the Covered Software is governed by the terms of this\nLicense, and how they can obtain a copy of this License. You may not\nattempt to alter or restrict the recipients' rights in the Source Code\nForm.\n\n3.2. Distribution of Executable Form\n\nIf You distribute Covered Software in Executable Form then:\n\n(a) such Covered Software must also be made available in Source Code\n    Form, as described in Section 3.1, and You must inform recipients of\n    the Executable Form how they can obtain a copy of such Source Code\n    Form by reasonable means in a timely manner, at a charge no more\n    than the cost of distribution to the recipient; and\n\n(b) You may distribute such Executable Form under the terms of this\n    License, or sublicense it under different terms, provided that the\n    license for the Executable Form does not attempt to limit or alter\n    the recipients' rights in the Source Code Form under this License.\n\n3.3. Distribution of a Larger Work\n\nYou may create and distribute a Larger Work under terms of Your choice,\nprovided that You also comply with the requirements of this License for\nthe Covered Software. If the Larger Work is a combination of Covered\nSoftware with a work governed by one or more Secondary Licenses, and the\nCovered Software is not Incompatible With Secondary Licenses, this\nLicense permits You to additionally distribute such Covered Software\nunder the terms of such Secondary License(s), so that the recipient of\nthe Larger Work may, at their option, further distribute the Covered\nSoftware under the terms of either this License or such Secondary\nLicense(s).\n\n3.4. Notices\n\nYou may not remove or alter the substance of any license notices\n(including copyright notices, patent notices, disclaimers of warranty,\nor limitations of liability) contained within the Source Code Form of\nthe Covered Software, except that You may alter any license notices to\nthe extent required to remedy known factual inaccuracies.\n\n3.5. Application of Additional Terms\n\nYou may choose to offer, and to charge a fee for, warranty, support,\nindemnity or liability obligations to one or more recipients of Covered\nSoftware. However, You may do so only on Your own behalf, and not on\nbehalf of any Contributor. You must make it absolutely clear that any\nsuch warranty, support, indemnity, or liability obligation is offered by\nYou alone, and You hereby agree to indemnify every Contributor for any\nliability incurred by such Contributor as a result of warranty, support,\nindemnity or liability terms You offer. You may include additional\ndisclaimers of warranty and limitations of liability specific to any\njurisdiction.\n\n4. Inability to Comply Due to Statute or Regulation\n---------------------------------------------------\n\nIf it is impossible for You to comply with any of the terms of this\nLicense with respect to some or all of the Covered Software due to\nstatute, judicial order, or regulation then You must: (a) comply with\nthe terms of this License to the maximum extent possible; and (b)\ndescribe the limitations and the code they affect. Such description must\nbe placed in a text file included with all distributions of the Covered\nSoftware under this License. Except to the extent prohibited by statute\nor regulation, such description must be sufficiently detailed for a\nrecipient of ordinary skill to be able to understand it.\n\n5. Termination\n--------------\n\n5.1. The rights granted under this License will terminate automatically\nif You fail to comply with any of its terms. However, if You become\ncompliant, then the rights granted under this License from a particular\nContributor are reinstated (a) provisionally, unless and until such\nContributor explicitly and finally terminates Your grants, and (b) on an\nongoing basis, if such Contributor fails to notify You of the\nnon-compliance by some reasonable means prior to 60 days after You have\ncome back into compliance. Moreover, Your grants from a particular\nContributor are reinstated on an ongoing basis if such Contributor\nnotifies You of the non-compliance by some reasonable means, this is the\nfirst time You have received notice of non-compliance with this License\nfrom such Contributor, and You become compliant prior to 30 days after\nYour receipt of the notice.\n\n5.2. If You initiate litigation against any entity by asserting a patent\ninfringement claim (excluding declaratory judgment actions,\ncounter-claims, and cross-claims) alleging that a Contributor Version\ndirectly or indirectly infringes any patent, then the rights granted to\nYou by any and all Contributors for the Covered Software under Section\n2.1 of this License shall terminate.\n\n5.3. In the event of termination under Sections 5.1 or 5.2 above, all\nend user license agreements (excluding distributors and resellers) which\nhave been validly granted by You or Your distributors under this License\nprior to termination shall survive termination.\n\n************************************************************************\n*                                                                      *\n*  6. Disclaimer of Warranty                                           *\n*  -------------------------                                           *\n*                                                                      *\n*  Covered Software is provided under this License on an \"as is\"       *\n*  basis, without warranty of any kind, either expressed, implied, or  *\n*  statutory, including, without limitation, warranties that the       *\n*  Covered Software is free of defects, merchantable, fit for a        *\n*  particular purpose or non-infringing. The entire risk as to the     *\n*  quality and performance of the Covered Software is with You.        *\n*  Should any Covered Software prove defective in any respect, You     *\n*  (not any Contributor) assume the cost of any necessary servicing,   *\n*  repair, or correction. This disclaimer of warranty constitutes an   *\n*  essential part of this License. No use of any Covered Software is   *\n*  authorized under this License except under this disclaimer.         *\n*                                                                      *\n************************************************************************\n\n************************************************************************\n*                                                                      *\n*  7. Limitation of Liability                                          *\n*  --------------------------                                          *\n*                                                                      *\n*  Under no circumstances and under no legal theory, whether tort      *\n*  (including negligence), contract, or otherwise, shall any           *\n*  Contributor, or anyone who distributes Covered Software as          *\n*  permitted above, be liable to You for any direct, indirect,         *\n*  special, incidental, or consequential damages of any character      *\n*  including, without limitation, damages for lost profits, loss of    *\n*  goodwill, work stoppage, computer failure or malfunction, or any    *\n*  and all other commercial damages or losses, even if such party      *\n*  shall have been informed of the possibility of such damages. This   *\n*  limitation of liability shall not apply to liability for death or   *\n*  personal injury resulting from such party's negligence to the       *\n*  extent applicable law prohibits such limitation. Some               *\n*  jurisdictions do not allow the exclusion or limitation of           *\n*  incidental or consequential damages, so this exclusion and          *\n*  limitation may not apply to You.                                    *\n*                                                                      *\n************************************************************************\n\n8. Litigation\n-------------\n\nAny litigation relating to this License may be brought only in the\ncourts of a jurisdiction where the defendant maintains its principal\nplace of business and such litigation shall be governed by laws of that\njurisdiction, without reference to its conflict-of-law provisions.\nNothing in this Section shall prevent a party's ability to bring\ncross-claims or counter-claims.\n\n9. Miscellaneous\n----------------\n\nThis License represents the complete agreement concerning the subject\nmatter hereof. If any provision of this License is held to be\nunenforceable, such provision shall be reformed only to the extent\nnecessary to make it enforceable. Any law or regulation which provides\nthat the language of a contract shall be construed against the drafter\nshall not be used to construe this License against a Contributor.\n\n10. Versions of the License\n---------------------------\n\n10.1. New Versions\n\nMozilla Foundation is the license steward. Except as provided in Section\n10.3, no one other than the license steward has the right to modify or\npublish new versions of this License. Each version will be given a\ndistinguishing version number.\n\n10.2. Effect of New Versions\n\nYou may distribute the Covered Software under the terms of the version\nof the License under which You originally received the Covered Software,\nor under the terms of any subsequent version published by the license\nsteward.\n\n10.3. Modified Versions\n\nIf you create software not governed by this License, and you want to\ncreate a new license for such software, you may create and use a\nmodified version of this License if you rename the license and remove\nany references to the name of the license steward (except to note that\nsuch modified license differs from this License).\n\n10.4. Distributing Source Code Form that is Incompatible With Secondary\nLicenses\n\nIf You choose to distribute Source Code Form that is Incompatible With\nSecondary Licenses under the terms of this version of the License, the\nnotice described in Exhibit B of this License must be attached.\n\nExhibit A - Source Code Form License Notice\n-------------------------------------------\n\n  This Source Code Form is subject to the terms of the Mozilla Public\n  License, v. 2.0. If a copy of the MPL was not distributed with this\n  file, You can obtain one at http://mozilla.org/MPL/2.0/.\n\nIf it is not possible or desirable to put the notice in a particular\nfile, then You may include the notice in a location (such as a LICENSE\nfile in a relevant directory) where a recipient would be likely to look\nfor such a notice.\n\nYou may add additional accurate notices of copyright ownership.\n\nExhibit B - \"Incompatible With Secondary Licenses\" Notice\n---------------------------------------------------------\n\n  This Source Code Form is \"Incompatible With Secondary Licenses\", as\n  defined by the Mozilla Public License, v. 2.0."
  },
  {
    "path": "README.md",
    "content": "# Air Draw\nThis example is sped up just to show functionality, real-time examples shown below:\n## Demo of Functionality\n![Demo of all functionality: Draw, Hover, Erase, and Translate](./demo_gifs/demo.gif)\n\n\n## Setup\n<b>NOTE</b> This setup is just for what I use (Ubuntu 20.04). While I am willing to bet this will work for windows and unix, just be safe!\n### Virtual environment\n`python3 -m venv venv`\n### Install Dependencies\n`source ./venv/bin/activate`\n\n`pip3 install -r requirements.txt`\n### Run program\n`python3 airdraw.py`\n\n## Available Gestures\n\n### Drawing\n![Draw: drawing directly on screen](./demo_gifs/drawing.gif)\n\n### Hovering\n![Hover: Move across the screen passively](./demo_gifs/hovering.gif)\n\n### Erasing\n![Erase: Remove all drawings within radius](./demo_gifs/erasing.gif)\n\n### Translation\n![Translation: Move shapes around the screen](./demo_gifs/translating.gif)\n\n## Why?\nI've seen tons of attempts of this sort of thing with HSV masks and dying your fingers a certain color, and while it's more true to image processing that openCV caters for, I was sort of against letting our own styluses [go to waste](https://money.cnn.com/2015/09/10/technology/apple-pencil-steve-jobs-stylus/index.html).\nOnce I found out about [mediapipe](https://google.github.io/mediapipe/), I decided I would give this thing a shot! What you see is my attempt at materializing the idea, there is a more detailed [writeup](https://arefmalek.github.io/blog/Airdraw/) on my blog. \n\n## How?\nLike I mentioned before, the ML workhorse here is definitely mediapipe. They've got awesome ML solutions so we can quickly gather data on the hand and use what we gather rather quickly. Other than that I pretty reliantly used OpenCV for image manipulation and NumPy for some basic dot products and because OpenCV uses numpy to represent images.\n\nThe conversion from hand data to lines / functionality is primarily done with some Python, basic linear algebra, and OpenCV. I'll leave the rest in the blog post. \n\n## What's next?\nDefinitely want to make this more available to everyone, so an upcoming goal will be to write this as a webapp, hopefully within the next month or so, I'll keep everyone posted :). \n\nThanks for reading :)\n"
  },
  {
    "path": "airdraw.py",
    "content": "import numpy as np\nimport cv2 as cv\nfrom hands import HandDetector\nfrom canvas import Canvas\n\n\ndef replay(fname):\n    print(\"replaying\", fname)\n\n    cap = cv.VideoCapture(fname)\n    # Use whatever width and height possible\n    frame_width = int(cap.get(cv.CAP_PROP_FRAME_WIDTH))\n    frame_height = int(cap.get(cv.CAP_PROP_FRAME_HEIGHT))\n\n    canvas = Canvas(frame_width, frame_height)\n\n    if (not cap.isOpened()):\n        print(\"Error opening video file\")\n        return\n\n    detector = HandDetector()\n    while cap.isOpened() and (cv.waitKey(0) & 0xFF != ord('q')):\n        ret, img = cap.read()\n\n        # replay is completed when the video capture no longer has any frames to read.\n        if ret:\n\n            gesture_metadata = detector.get_gesture_metadata(img)\n\n            img = canvas.update_and_draw(img, gesture_metadata)\n            detector.draw_landmarks(img)\n\n            cv.imshow('Camera', img)\n        else:\n            break\n\n    cap.release()\n    cv.destroyAllWindows()\n\n    print(\"replay complete\", fname)\n\ndef main():\n    # Loading the default webcam of PC.\n    cap = cv.VideoCapture(0)\n    \n    # width and height for 2-D grid\n    width = int(cap.get(cv.CAP_PROP_FRAME_WIDTH) + 0.5)\n    height = int(cap.get(cv.CAP_PROP_FRAME_HEIGHT) + 0.5)\n\n    # initialize the canvas element and hand-detector program\n    canvas = Canvas(height, width)\n    detector = HandDetector()\n    print(width, height)\n    \n    # Keep looping\n    while True:\n        # Reading the frame from the camera\n        ret, frame = cap.read()\n        frame = cv.flip(frame, 1)\n\n        gesture_metadata = detector.get_gesture_metadata(frame)\n\n        frame = canvas.update_and_draw(frame, gesture_metadata)\n        detector.draw_landmarks(frame)\n       \n        cv.imshow(\"Airdraw\", frame)\n    \n        stroke = cv.waitKey(1) & 0xff  \n        if stroke == ord('b'): # press 'b' to switch backgrounds (camera/black)\n            canvas.switch_background()\n        \n        if stroke == ord('q') or stroke == 27: # press 'q' or 'esc' to quit\n            break\n    \n    cap.release()\n    cv.destroyAllWindows()\n\nif __name__ == '__main__':\n    main()\n"
  },
  {
    "path": "canvas.py",
    "content": "import cv2 as cv\nimport numpy as np\n\nfrom hands import Gesture, HandDetector\nfrom util import xy_euclidean_dist\n\nfrom enum import Enum\n\n# FIXME: \n# use a good spatial query system (is just iterating over literally every point the best we can do?) \n# Gauging this would need the following:\n#   How many data points could i realistically collect over a 2-minute episode?\n#   How much does it cost to iterate and compare versus \n#       1. storing waypoints in a grid, and then searching every pixel in the grid\n#       2. Storing all points in some sort of query system (intuition screaming quadtree).\n# have consistent usage of row, col convention between mediapipe, canvas, and opencv. \n# keep all data intialized at startup and only completely transform in function\n\nclass Color(Enum):\n    \"\"\"Please remember these are in BGR coordinates!\"\"\"\n    GRAY = (122, 122, 122)\n    WHITE = (255, 255, 255)\n    BLUE = (255,0,0)\n    GREEN =(0,255,0)\n    RED = (0,0,255)\n    PURPLE = (255, 0, 255)\n    YELLOW = (0, 255, 255)\n\nclass Shape(Enum):\n    CIRCLE = Color.BLUE\n    SQUARE = Color.GRAY\n    LINE = Color.GREEN\n\nclass Canvas():\n    \"\"\" \n    This class is responsible for \"drawing\" all state onto the screen. \n    This includes the actual dashboard hands interact with as well as lines, backgrounds, etc.\n\n    This component is intended to take (frame, hands_state) -> (update state) -> image to render\n    \"\"\"\n    def __init__(self, rows, columns):\n        # FIXME: just make this deterministic via list\n        self.colors = [ Color.BLUE, Color.GREEN, Color.RED ]\n        self.shapes = [ Shape.LINE, Shape.CIRCLE, Shape.SQUARE ]\n        self.rows = rows\n        self.columns = columns\n        self.color = Color.BLUE # only really used to initialize lines\n        self.shape = Shape.LINE\n        self.lines = {} # whole list of points\n        self.circles = [] # whole list of points\n        self.squares = [] # whole list of squares\n        self.currLine = Line(None, self.color)# this is the line we're adding to \n        self.currLine.active = False\n        self.currCircle = Circle((-1, -1), -1, self.color)# this is the line we're adding to \n        self.currCircle.active = False\n        self.currSquare = Square((-1, -1), (-1, -1), self.color)\n        self.currSquare.active = False\n        self.blackout_background = False\n\n    def switch_background(self):\n        self.blackout_background = not self.blackout_background\n\n    def get_buttons_coords(self, frame_shape):\n        \"\"\"\n        Returns coordinates of the buttons (and colors) to draw on the UI, used to save space later on.\n        Should be useful for detecting overlap between fingers and buttons.\n\n        Args:\n            frame_shape: tuple describing frame shape\n        Return:\n            List with elements holding the following schema: (button name, button BGR colors, top-left coordinate, bottom-right coordinate)\n            Ordering of the elements is as follows:\n                1. Clear all button\n                2. Color buttons\n                3. Shape buttons\n        \"\"\"\n\n        # Obtains the proportionally correct buttons for the frame shape given. \n        frame_height, frame_width, _ = frame_shape\n\n        coords = []\n\n        # add clear_button\n        # Clear button is manually sized, all other buttons are manually sized\n        clear_button_width = int(frame_width *.2) \n        clear_button_height = int(frame_height * .15) \n\n        clear_button_width_border = int(clear_button_width * .05) \n        clear_button_height_border = int(clear_button_height * .05)\n\n        coords.append(\n            (\n                \"Clear all\",\n                Color.GRAY.value,\n                (clear_button_width_border, clear_button_height_border), \n                (clear_button_width - clear_button_width_border, clear_button_height - clear_button_height_border)\n            ))\n\n        num_colors = len(self.colors)\n        remaining_width = frame_width - clear_button_width\n\n        color_button_width = remaining_width // num_colors \n        color_button_height = int(clear_button_height * 0.7)\n        color_button_border_width = int(color_button_width * 0.05)\n        color_button_border_height = int(color_button_height * 0.05)\n        curr_button_offset_width = clear_button_width\n\n        # FIXME: use color.name instead?\n        for color in self.colors:\n            coords.append((\n                color.name,\n                color.value,\n                (curr_button_offset_width  + color_button_border_width, color_button_border_height),\n                (curr_button_offset_width  + color_button_width - color_button_border_width, color_button_height - color_button_border_height)\n            ))\n\n            curr_button_offset_width += color_button_width\n        \n        num_shapes = len(Shape)\n        remaining_height = frame_height - clear_button_height\n        shape_button_height = (remaining_height // num_shapes)\n        shape_button_width = int(clear_button_width * 0.7)\n        shape_button_border_height = int(shape_button_height * 0.05)\n        shape_button_border_width = int(shape_button_width * 0.05)\n\n        curr_button_offset_height = clear_button_height\n\n        for shape in Shape:\n            coords.append((\n                shape.name,\n                shape.value.value,\n                (shape_button_border_width, curr_button_offset_height + shape_button_border_height),\n                (shape_button_width - shape_button_border_width, curr_button_offset_height + shape_button_height - shape_button_border_height)\n            ))\n\n            curr_button_offset_height += shape_button_height\n        \n        return coords\n\n    def buttons_overlap(self, buttons_coords, fingertip_point):\n        leftCoord, topCoord = buttons_coords[0]\n        rightCoord, bottomCoord = buttons_coords[1]\n\n        r, c =  fingertip_point\n        return leftCoord <= c <= rightCoord and topCoord <= r <= bottomCoord\n\n    def update_state(self, frame_shape, data = {}):\n        \"\"\"\n        This function should take in state updates from our hands, and update internal state of the game.\n        \"\"\"\n        buttons_coord = self.get_buttons_coords(frame_shape)\n        clear_button = buttons_coord[0]\n        button_offset = 1\n        color_buttons = buttons_coord[button_offset:button_offset+len(self.colors)]\n        button_offset += len(self.colors)\n        shape_buttons = buttons_coord[button_offset:button_offset+len(Shape)]\n\n        gesture = data.get(\"gesture\", Gesture.HOVER)\n\n        gesture_finger_points = [v for k, v in data.items() if k.endswith(\"_tip\")]\n        # check if any of the active vector points overlap with our buttons coordinates\n\n        # overlap with clear button\n        for coord in gesture_finger_points:\n            if self.buttons_overlap(clear_button[2:], coord):\n                # Clear state.\n                self.end_drawing()\n                self.lines = {}\n                self.circles = []\n                self.squares = []\n                break\n        \n        # overlap with color button\n        for color_button_metadata in color_buttons:\n            button_color_str = color_button_metadata[0]\n            for coord in gesture_finger_points:\n                if self.buttons_overlap(color_button_metadata[2:], coord):\n                    new_color = [color for color in self.colors if color.name == button_color_str][0]\n                    if gesture == Gesture.DRAW:\n                        self.end_drawing()\n                    # assign the color value to our metadata\n                    self.color = new_color\n\n        # overlap with shape button\n        for shape_button_metadata in shape_buttons:\n            shape_str = shape_button_metadata[0]\n            for coord in gesture_finger_points:\n                if self.buttons_overlap(shape_button_metadata[2:], coord):\n                    new_shape = [shape for shape in self.shapes if shape.name == shape_str][0]\n                    if new_shape != self.shape and Gesture.DRAW:\n                        self.end_drawing()\n                    self.shape = new_shape\n\n        if gesture == Gesture.DRAW:\n            midpoint_r, midpoint_c = data.get('origin')\n            radius = int(data.get('radius')) # varying sizes\n\n            if self.shape == Shape.LINE:\n                self.push_point((midpoint_r, midpoint_c))\n            if self.shape == Shape.CIRCLE:\n                self.update_circle((midpoint_r, midpoint_c))\n            if self.shape == Shape.SQUARE:\n                self.update_square((midpoint_r, midpoint_c))\n\n        elif gesture == Gesture.HOVER:\n            self.end_drawing()\n        elif gesture == Gesture.ERASE:\n            midpoint_r, midpoint_c = data.get('origin')\n            radius = int(data.get('radius'))\n            self.erase_mode((midpoint_r, midpoint_c), radius)\n        elif gesture == Gesture.TRANSLATE:\n            self.end_drawing()\n\n            midpoint_r, midpoint_c = data.get('origin')\n            radius = int(data.get('radius'))\n            shift = data.get('shift')\n            shift = int(shift[0]), int(shift[1])\n            self.translate_mode((midpoint_r, midpoint_c), radius, shift)\n    \n    def draw_canvas(self, frame, data):\n        \"\"\"\n        Renders dashboard onto screen\n        \"\"\"\n        if self.blackout_background:\n            frame = np.zeros_like(frame)\n\n        buttons_coord = self.get_buttons_coords(frame.shape)\n\n        for button_metadata in buttons_coord:\n            button_str = button_metadata[0]\n            button_color_rgb = button_metadata[1]\n            button_left, button_top = button_metadata[2]\n            button_right, button_bottom = button_metadata[3]\n\n            frame = cv.rectangle(frame, \n                                (button_left, button_top),\n                                (button_right, button_bottom),\n                                button_color_rgb, -1)\n\n            button_width = button_right - button_left\n            button_height = button_bottom - button_top\n\n            cv.putText(frame, button_str, \n                    (button_left + int((button_width)* .3), int(button_top + button_height * .5)), \n                    cv.FONT_HERSHEY_SIMPLEX, .5, Color.WHITE.value, 2, cv.LINE_AA)\n            # highlight selected color\n            if button_str == self.color.name or button_str == self.shape.name:\n                frame = cv.rectangle(frame, \n                    (button_left, button_top),\n                    (button_right, button_bottom),\n                    Color.WHITE.value,\n                    2)\n\n        gesture = data.get('gesture')\n        if gesture == Gesture.DRAW:\n            midpoint_r, midpoint_c = data['origin']\n            radius = data['radius'] \n\n            img = frame.copy()\n            # purple cuz im royal\n            cv.circle(img, (midpoint_c, midpoint_r), int(radius), Color.PURPLE.value, -1)\n            alpha = 0.4\n            frame = cv.addWeighted(frame, alpha, img, 1-alpha, 0)\n \n        # draw the ring if we're in the eraser mode\n        if gesture == Gesture.ERASE:\n            # get middle finger and radius of circle to draw\n            midpoint_r, midpoint_c = data['origin']\n            radius = data['radius']\n\n            # put circle on the map, and add some opacity\n            img = frame.copy()\n            cv.circle(img, (midpoint_c, midpoint_r), int(radius), Color.YELLOW.value, -1)\n            alpha = 0.4\n            frame = cv.addWeighted(frame, alpha, img, 1-alpha, 0)\n\n        elif gesture == Gesture.TRANSLATE:\n            midpoint_r, midpoint_c = data['origin']\n            radius = data['radius']\n\n            # put circle on the map, and add some opacity\n            img = frame.copy()\n            cv.circle(img, (midpoint_c, midpoint_r), int(radius), Color.WHITE.value, -1)\n            alpha = 0.4\n            frame = cv.addWeighted(frame, alpha, img, 1-alpha, 0)\n        \n        frame = self.draw_lines(frame)\n        frame = self.draw_circles(frame)\n        frame = self.draw_squares(frame)\n\n        return frame\n    \n    def update_and_draw(self, frame, data = {}):\n        self.update_state(frame.shape, data)\n        frame = self.draw_canvas(frame, data)\n        return frame\n\n    def update_circle(self, new_point):\n        \"\"\" Maintain state of the currently drawn circle. If it doesnt exist, initialize it and pass pointer to self.circles\"\"\"\n        point_row, point_col = new_point\n\n        if not (0 <= point_row < self.rows and 0 <= point_col < self.columns):\n            return\n        \n        if self.currCircle.active == False:\n            self.currCircle = Circle((point_row, point_col), 5, self.color)\n            self.circles.append(self.currCircle)\n        else:\n            dist = int(xy_euclidean_dist(self.currCircle.origin, new_point))\n            self.currCircle.radius = dist\n    \n    def update_square(self, new_point):\n        \"\"\"Updates state of the currently drawn square (resizing it). If it doesn't exist, initialize it and pass pointer to self.squares\"\"\"\n        point_row, point_col = new_point\n\n        if not (0 <= point_row < self.rows and 0 <= point_col < self.columns):\n            return\n\n        if self.currSquare.active == False:\n            # just initialize with some size\n            self.currSquare = Square(new_point, (point_row + 5, point_col + 5), self.color)\n            self.squares.append(self.currSquare)\n        else:\n            self.currSquare.opposite = new_point\n\n    def push_point(self, point):\n        \"\"\"\n        adds a point to draw later on\n\n        Arguments: \n            point: (r, c) pair describing new coordinate of the line\n        \"\"\"\n\n        row, col = point \n        if not 0 <= row < self.rows or not 0 <= col < self.columns:\n            return\n        # TODO: replace hashmap approach with just generic list\n        # if there isn't an active line being drawn, start one\n        if self.currLine.active == False:\n            # we need to initialize a line\n            line = Line(self.color, point) # start a line with a new color\n            self.currLine = line\n            self.lines[point] = self.currLine # store origin in the lines\n        else:\n            # get the current line, add the new point to the linked list\n            self.currLine.points.append(point)\n\n    def end_drawing(self):\n        \"\"\"Ends active drawing\"\"\"\n        self.currLine.active = False\n        self.currCircle.active = False\n        self.currSquare.active = False\n\n    def draw_lines(self, frame):\n        \"\"\"\n        Draws all of the lines we have generated so far by looping through line objects\n\n        Args:\n        - frame: The image straight from camera\n\n        Returns:\n        Image with all the different lines drawn on top of it\n        \"\"\"\n        # self.lines = [{\"color\": \"BLUE\",\n        #               \"points\": [(1, 2), (5, 9), ...]}, \n        #               {\"color\": \"RED\",\n        #               \"points\": [(6, 0), (5, 8), ...]}, \n        for line in self.lines.values():\n            for i, point in enumerate(line.points):\n                if i == 0:\n                    continue\n                prev_r, prev_c = line.points[i-1]\n                r, c = point\n                cv.line( \n                        frame, \n                        (prev_c, prev_r), \n                        (c, r), \n                        line.color.value,\n                        5\n                        )\n        return frame\n    \n    def draw_circles(self, frame):\n        for circle in self.circles:\n            orig_row, orig_col = circle.origin\n            cv.circle(frame, (orig_col, orig_row), circle.radius, circle.color.value, 3)\n        return frame\n    \n    def draw_squares(self, frame):\n        for square in self.squares:\n            topRow, leftCol, bottomRow, rightCol = square.get_coords()\n            frame = cv.rectangle(\n                frame,\n                (leftCol, topRow),\n                (rightCol, bottomRow),\n                square.color.value,\n                5\n            )\n        return frame\n\n\n    def translate_mode(self, position, radius, shift):\n        \"\"\"\n        Works as following:\n\n        1. gather all lines in the radius\n        2. for each line:\n            shift each point in the line by the shift variable\n        \n       \"\"\"\n        # FIXME: introducing extra lines unnecessarily into the program\n\n        r, c = position\n        if shift == (0, 0):\n            return\n\n        # we should be able to collect all unique origin points \n        uniqueLines = set()\n        for origin, line in self.lines.items():\n            for p in line.points:\n                if xy_euclidean_dist(p, position) <= radius:\n                    uniqueLines.add(origin)\n                    break\n        \n        # debugging line\n        sortedLines = sorted(list(uniqueLines))\n\n        # for each origin point in the circle\n        for og_point in sortedLines:\n            # Transform original points\n            line = self.lines[og_point]\n            translation = []\n            for r, c in line.points:\n                trans_r, trans_c = r + shift[0], c + shift[1]\n                if (0 <= trans_r < self.rows) and (0 <= trans_c < self.columns):\n                    translation.append((trans_r, trans_c))\n                else:\n                    break\n\n            # Check if transformation is valid\n            if len(translation) == len(line.points):\n                self.lines.pop(og_point)\n\n                line.points = translation\n                new_origin = line.get_origin()\n                assert(og_point != new_origin)\n\n                # put the value back in the lines\n                self.lines[line.get_origin()] = line\n        \n        for i, circle in enumerate(self.circles):                \n            if circle.overlaps_circle(position, radius):\n                new_origin = (circle.origin[0] + shift[0], circle.origin[1] + shift[1])\n                circle.origin = new_origin\n\n        for i, square in enumerate(self.squares):\n            if square.overlaps_circle(position, radius):\n                new_anchor = square.anchor[0] + shift[0], square.anchor[1] + shift[1]\n                new_opposite = square.opposite[0] + shift[0], square.opposite[1] + shift[1]\n                square.anchor = new_anchor\n                square.opposite = new_opposite\n\n    # start of erase mode code\n    def erase_mode(self, position, radius):\n        \"\"\"\n        Interprets the position of the pointer, \n        deletes lines if they overlap with the pointer\n\n        Arguments:\n            position: (x, y) coordinates of the position\n            radius: the radius (in pixels) of our eraser\n        \"\"\"\n        origin_points = []\n        for origin, lines in self.lines.items():\n            for point in lines.points:\n                if xy_euclidean_dist(point, position) <= radius:\n                    origin_points.append(origin)\n                    break\n\n        for origins in origin_points:\n            self.lines.pop(origins)\n\n        circles_to_keep = []\n        for circle in self.circles:\n            if circle.overlaps_circle(position, radius):\n                continue\n            else:\n                circles_to_keep.append(circle)\n        self.circles = circles_to_keep\n\n        squares_to_keep = []\n        for square in self.squares:\n            if square.overlaps_circle(position, radius):\n                continue\n            else:\n                squares_to_keep.append(square)\n\n        self.squares = squares_to_keep\n\nclass Line():\n    \"\"\"\n    Helper class to represent the lines put on the screen\n    \"\"\"\n\n    def __init__(self, color: Color, origin):\n        self.color = color\n        self.points = [origin]\n        self.active = True\n\n    def get_origin(self):\n        return self.points[0]\n\n    def __repr__(self):\n        return f\"\\ncolor({self.color}) \\\n                \\n\\tactive({self.active}) \\\n                \\n\\tpoints({self.points})\"\n\nclass Circle():\n    \"\"\"Helper class to place circles on screen\"\"\"\n    def __init__(self, origin, radius: int, color: Color):\n        self.origin = origin\n        self.radius = radius\n        self.color = color\n        self.active = True\n    \n    def get_radius(self):\n        return self.radius\n    \n    def overlaps_circle(self, point, other_radius) -> bool:\n        dist = xy_euclidean_dist(self.origin, point)\n        return max(self.radius - other_radius, 0) <= dist <= self.radius + other_radius\n\n    \n    def __repr__(self):\n        return f\"Origin:{self.origin}\\tRadius:{self.radius}\\tColor:{self.color}\"\n\n\nclass Square():\n    def __init__(self, anchor, opposite, color: Color):\n        self.anchor = anchor\n        self.opposite = opposite\n        self.color = color\n        self.active = True\n\n    def get_coords(self):\n        topRow = min(self.anchor[0], self.opposite[0])\n        bottomRow = max(self.anchor[0], self.opposite[0])\n        leftCol = min(self.anchor[1], self.opposite[1])\n        rightCol = max(self.anchor[1], self.opposite[1])\n        return (topRow, leftCol, bottomRow, rightCol)\n    \n    def get_height(self):\n        topRow, leftCol, bottomRow, rightCol = self.get_coords()\n        return (bottomRow - topRow)\n\n    def get_width(self):\n        topRow, leftCol, bottomRow, rightCol = self.get_coords()\n        return (rightCol - leftCol)\n\n    def overlaps_circle(self, point, radius) -> bool:\n        \"\"\"\n        Returns true if the border of our square overlaps with the circle.\n        Args\n            point: (row, col) of the query point\n        \n        Math here - https://stackoverflow.com/a/402010\n        \"\"\"\n        point_r, point_c = point\n\n        topRow, leftCol, bottomRow, rightCol = self.get_coords()\n        square_center_row = (topRow + bottomRow) // 2\n        square_center_col = (leftCol + rightCol) // 2\n\n        point_dist_r = abs(point_r - square_center_row) # compare against height\n        point_dist_c = abs(point_c - square_center_col) # compare against width\n        half_height = self.get_height() // 2\n        half_width = self.get_width() // 2\n        square_border_row_dist = abs(point_dist_r - half_height)\n        square_border_col_dist = abs(point_dist_c - half_width)\n\n        # Too far from the rectangle\n        if (point_dist_r > (half_height + radius)): return False\n        if (point_dist_c > (half_width + radius)): return False\n\n        # Too close to the origin\n        if (point_dist_r < (half_height - radius) and point_dist_c < (half_width - radius)): return False\n\n        # If this code does what I think, it means that \n        # the row is in [half_height - radius, half_height + radius] \n        # the col is in [half_width - radius, half_width + radius]\n        assert(half_width - radius <= point_dist_c <= half_width + radius or half_height - radius <= point_dist_r <= half_height + radius)\n\n        # Point is within \n        if (point_dist_r > half_width and point_dist_c > half_height):\n            cornerDist = (square_border_col_dist) ** 2 + (square_border_row_dist) ** 2\n            return cornerDist <= radius**2\n        \n        return True\n\n    def __repr__(self):\n        topRow, leftCol, bottomRow, rightCol = self.get_coords()\n        return f\"topLeft: {(topRow, leftCol)}\\tbottomRight:{(bottomRow, rightCol)}\\tcolor:{self.color}\"\n\ndef replay(fname):\n    print(\"replaying\", fname)\n\n    cap = cv.VideoCapture(fname)\n    # Use whatever width and height possible\n    frame_width = int(cap.get(cv.CAP_PROP_FRAME_WIDTH))\n    frame_height = int(cap.get(cv.CAP_PROP_FRAME_HEIGHT))\n\n    canvas = Canvas(frame_height, frame_width)\n\n    if (not cap.isOpened()):\n        print(\"Error opening video file\")\n        return\n\n    detector = HandDetector()\n    while cap.isOpened() and (cv.waitKey(0) & 0xFF != ord('q')):\n        ret, img = cap.read()\n\n        # replay is completed when the video capture no longer has any frames to read.\n        if ret:\n\n            gesture_metadata = detector.get_gesture_metadata(img)\n\n            img = canvas.update_and_draw(img, gesture_metadata)\n            detector.draw_landmarks(img)\n\n            cv.imshow('Camera', img)\n        else:\n            break\n\n    cap.release()\n    cv.destroyAllWindows()\n\n    print(\"replay complete\", fname)\n\n\ndef main():\n    canvas = Canvas(100, 200)\n    line = Line(\"BLUE\", (1, 1))\n    line.points.append((10, 5))\n    print(line)\n\n\nif __name__ == '__main__':\n    # replay(\"./hands_basic_gestures.mp4\")\n    # replay(\"./buttons_overlap.mp4\")\n    # replay(\"./translation_debug.mp4\")\n    replay(\"./hands_drawing_ui.mp4\")\n    # replay(\"./eraser_debug.mp4\")\n\n    # main()\n"
  },
  {
    "path": "data.py",
    "content": "import cv2 as cv\nimport argparse\n\ndef record(fname):    \n    print(\"recording \", fname)\n    cam = cv.VideoCapture(0)\n\n    # Use whatever width and height possible\n    frame_width = int(cam.get(cv.CAP_PROP_FRAME_WIDTH))\n    frame_height = int(cam.get(cv.CAP_PROP_FRAME_HEIGHT))\n\n    fourcc = cv.VideoWriter_fourcc(*'mp4v')\n    out = cv.VideoWriter(fname, fourcc, 60.0, (frame_width, frame_height))\n\n    while True:\n        _, img = cam.read()\n        img = cv.flip(img, 1)\n\n        out.write(img)\n\n        cv.imshow('Recording', img)\n\n        if cv.waitKey(1) & 0xFF == ord('q'):\n            break\n    \n    out.release()\n    cam.release()\n    cv.destroyAllWindows()\n    print(\"recording complete. shutting down.\")\n\n\ndef replay(fname):\n    print(\"replaying\", fname)\n\n    cap = cv.VideoCapture(fname)\n    print(\"captured\")\n    if (not cap.isOpened()):\n        print(\"Error opening video file\")\n        return\n\n    print(\"waiting to open\")\n    while cap.isOpened(): # and (cv.waitKey(0) & 0xFF != ord('q')):\n        ret, img = cap.read()\n\n        # replay is completed when the video capture no longer has any frames to read.\n        if ret:\n            cv.imshow('Camera', img)\n        else:\n            break\n        print(\"img\", img.size)\n\n\n    cap.release()\n    cv.destroyAllWindows()\n\n    print(\"replay complete\", fname)\n\n\ndef main():\n    parser = argparse.ArgumentParser(\n        prog='data.py',\n        description='data collections tools'\n    )\n    parser.add_argument(\"-m\", \"--mode\")\n    parser.add_argument(\"-f\", \"--filename\")\n    args = parser.parse_args()\n\n    if not args.filename.endswith(\".mp4\"):\n        print(f\"filename({args.filename}) must end with .mp4\")\n        return False\n    \n    if args.mode == 'replay':\n        replay(args.filename)\n    elif args.mode == \"record\":\n        record(args.filename)\n    else:\n        print(f\"data mode must fall into ['replay', 'record'], provided {args.mode}\")\n        return False\n\n\nif __name__ == \"__main__\":\n    main()\n\n"
  },
  {
    "path": "hands.py",
    "content": "import cv2 as cv\nimport mediapipe as mp\nimport numpy as np\nfrom enum import Enum\nfrom collections import deque\n\nfrom util import xy_euclidean_dist, vectorize, cos_angle\nclass Gesture(Enum):\n    DRAW = 'DRAW'\n    HOVER = 'HOVER'\n    ERASE = 'ERASE'\n    TRANSLATE = 'TRANSLATE'\n\nclass LandmarkBuffer():\n    \"\"\"Helper RingBuffer class to abstract away averaging logic\"\"\"\n\n    def __init__(self, max_size):\n        self.buf = deque([], maxlen=max_size)\n\n    def push_landmark(self, element):\n        self.buf.append(element)\n    \n    def average_landmarks(self):\n        assert(len(self.buf) > 0)\n        res = [[0]*3 for i in range(21)]\n        num_points = len(self.buf)\n        \n        for landmark in self.buf:\n            for i, vec in enumerate(landmark):\n                res[i][0] += vec[0]\n                res[i][1] += vec[1]\n                res[i][2] += vec[2]\n        \n        for i, vec in enumerate(res):\n            res[i][0] /= num_points\n            res[i][1] /= num_points\n            res[i][2] /= num_points\n\n        return res\n\n    def displacement(self):\n        \"\"\"Calculates the residual from the last two landmarks\"\"\"\n        res = [[0]*3 for i in range(21)]\n        num_points = len(self.buf)\n        if num_points < 2 or any([len(landmark) != 21 for landmark in self.buf]):\n            return res\n        \n        for i in range(21):\n            for j in range(3):\n                res[i][j] = self.buf[-1][i][j] - self.buf[-2][i][j]\n        return res\n\nclass HandDetector():\n    \"\"\"\n    This class defines the interaction the program will have with Mediapipe. It is essentially a wrapper layer around MP.\n\n    This class will define how Airdraw will be passing information to and receiving information from Mediapipe. \n    Successful implementation of this class should involve no image rendering, but rather just state transformation of hands, gestures, and other metadata used from Mediapipe.\n    \"\"\"\n\n    def __init__(self, mode = False, max_hands = 1):\n        # setup\n        self.max_hands = max_hands\n        self.mode = mode\n        # hand drawing stuff\n        self.hands = mp.solutions.hands.Hands(self.mode, self.max_hands)\n        self.drawing = mp.solutions.drawing_utils\n        self.hand_connections = mp.solutions.hands.HAND_CONNECTIONS\n        # will be used for translation\n        self.translation_buffer = LandmarkBuffer(5)\n        # we have 0 velocity to start translation\n\n    def detect_landmarks(self, frame):\n        \"\"\"\n        Noting all the points of one's hand in the image.\n\n        args:\n            - frame: np array representing image input. used to resize the prediction against mediapipe (will just use the builtin api soon though).\n       returns:\n            - list of landmarks on the hand in order of size and position\n        \"\"\"\n        img_rgb = cv.cvtColor(frame, cv.COLOR_BGR2RGB) # I think we need RGB\n        self.results = self.hands.process(img_rgb)\n\n        landmarks = []\n        if self.results.multi_hand_landmarks:\n            my_hand = self.results.multi_hand_landmarks[0] # should only be one\n            for idx, landmark in enumerate(my_hand.landmark):\n                height, width, _ = frame.shape\n                x, y = int(landmark.x * width), int(landmark.y * height)\n                landmarks.append((idx, x, y))\n\n        return landmarks\n    \n    def draw_landmarks(self, img):\n        \"\"\"\n        Draws hand landmarks on image. Breaks rules of class being only \"img\"->hand current state, but I think this looks the best so I'm keeping it this way.\n        \"\"\"\n        if self.results.multi_hand_landmarks:\n           for hand_landmark in self.results.multi_hand_landmarks:\n               self.drawing.draw_landmarks(img, hand_landmark, mp.solutions.hands.HAND_CONNECTIONS)\n\n   \n    def detect_gesture(self, landmarks, threshhold=0.70, debug=False):\n        \"\"\"\n        This function determines which \"mode\" we are in, signified by the\n        hand-signs someone indicates when we are drawing\n\n        Arguments:\n            landmarks: finger points\n            threshhold: value we need in order to change 'modes'\n        returns:\n            String that matches the gesture we have\n        \"\"\"\n\n        # adding all vectors\n        # palm vectors\n        palm_index_vector = vectorize(landmarks[0], landmarks[5])\n        palm_mid_vector = vectorize(landmarks[0], landmarks[9])\n        palm_ring_vector = vectorize(landmarks[0], landmarks[13])\n        palm_pinky_vector = vectorize(landmarks[0], landmarks[17])\n\n        # index vectors, each start from first knuckle of the hand\n        index_vector = vectorize(landmarks[6], landmarks[8])\n        middle_vector = vectorize(landmarks[10], landmarks[12])\n        ring_vector = vectorize(landmarks[14], landmarks[16])\n        pinky_vector = vectorize(landmarks[18], landmarks[20])\n\n        # really just to debug\n        if debug:\n            return cos_angle(index_vector, palm_index_vector)\n\n        # index finger pointing out, \n        # middle/ring/pinky finger tucked\n        if cos_angle(palm_index_vector, index_vector) > threshhold and \\\n            cos_angle(index_vector, middle_vector) < 0 and \\\n                cos_angle(index_vector, ring_vector) < 0 and \\\n                    cos_angle(index_vector, pinky_vector) < 0:\n           return Gesture.HOVER\n\n        # index/middle finger pointing out, \n        # ring/pinky finger tucked\n        if cos_angle(palm_index_vector, index_vector) > threshhold and \\\n            cos_angle(palm_mid_vector, middle_vector) > threshhold and \\\n                cos_angle(index_vector, ring_vector) < 0 and \\\n                    cos_angle(index_vector, pinky_vector) < 0:\n            return Gesture.DRAW\n\n        # index/middle/ring finger pointing out\n        # pinky finger tucked\n        if cos_angle(palm_index_vector, index_vector) > threshhold and \\\n            cos_angle(index_vector, middle_vector) > 0.90 and \\\n            cos_angle(index_vector, ring_vector) > 0.90 and \\\n                    cos_angle(palm_pinky_vector, pinky_vector) < 0:\n           return Gesture.ERASE\n        \n        # add the stuff relative to knuckles\n        if cos_angle(palm_index_vector, index_vector) > threshhold and \\\n            cos_angle(palm_pinky_vector, pinky_vector) > threshhold and \\\n                cos_angle(index_vector, middle_vector) < 0 and \\\n                    cos_angle(index_vector, ring_vector) < 0:\n            return Gesture.TRANSLATE\n        \n        # otherwise hover\n        return Gesture.HOVER\n    \n    def get_gesture_metadata(self, frame):\n        \"\"\"\n        Calls MP on frame and returns metadata about gesture determined.\n        Args: \n            - frame: np array defining our image.\n        Returns: \n            - returns a dict defining gesture as well as metadata to draw output with.\n        \"\"\"\n\n        landmark_list = self.detect_landmarks(frame)\n        if len(landmark_list) == 0 or np.sum(landmark_list) == 0:\n            return {}\n        \n        self.translation_buffer.push_landmark(landmark_list)\n        average_landmark_list = self.translation_buffer.average_landmarks()\n        gesture = self.detect_gesture(average_landmark_list)\n\n        # only extract the row, col before sending it literally anywhere else\n        _, index_c, index_r = average_landmark_list[8]\n        _, mid_c, mid_r = average_landmark_list[12]\n        _, ring_c, ring_r = average_landmark_list[16]\n        _, pinky_c, pinky_r = average_landmark_list[20]\n\n        # just writing in finger info\n        index_fing_tip = (index_r, index_c) # coordinates of tip of index fing\n        mid_fing_tip = (mid_r, mid_c)\n        ring_fing_tip = (ring_r, ring_c)\n        pinky_fing_tip = (pinky_r, pinky_c)\n\n        # data sent to canvas:\n        # formatted in row, column format because I index the internal grid that way.\n        post = {'gesture': gesture, \n                'idx_fing_tip': index_fing_tip,\n                'mid_fing_tip' : mid_fing_tip,\n                'ring_fing_tip': ring_fing_tip,\n                'pinky_fing_tip': pinky_fing_tip,\n                'origin': None,\n                'radius': None,\n                'shift': None,\n            }\n        \n        if gesture == Gesture.DRAW:\n            distance = xy_euclidean_dist(index_fing_tip, mid_fing_tip)\n\n            index_r, index_c = index_fing_tip\n            mid_r, mid_c = mid_fing_tip\n\n            midpoint_r, midpoint_c = int((index_r + mid_r) * 0.5), int((index_c + mid_c) * 0.5)\n\n            post['origin'] = (midpoint_r, midpoint_c)\n            post['radius'] = distance * 0.5\n\n        elif gesture == Gesture.ERASE:\n            distance = xy_euclidean_dist(index_fing_tip, ring_fing_tip)\n            index_r, index_c = index_fing_tip\n            ring_r, ring_c = ring_fing_tip\n\n            midpoint_r, midpoint_c = int((index_r + ring_r) * 0.5), int((index_c + ring_c) * 0.5)\n\n            post['origin'] = (midpoint_r, midpoint_c)\n            post['radius'] = distance * 0.5\n\n        # Add additonal info based off of info the gesture we got\n        elif gesture == Gesture.TRANSLATE:\n            distance = xy_euclidean_dist(index_fing_tip, pinky_fing_tip)\n\n            index_r, index_c = index_fing_tip\n            pinky_r, pinky_c = pinky_fing_tip\n\n            midpoint_r, midpoint_c = int((index_r + pinky_r) * 0.5), int((index_c + pinky_c) * 0.5)\n\n            post['origin'] = (midpoint_r, midpoint_c)\n            post['radius'] = distance * 0.5\n\n            # Calculate and store the shift\n            displacement = self.translation_buffer.displacement()\n\n            index_displacement = displacement[8]\n            _, index_c_displacement, index_r_displacement = index_displacement\n\n            post['shift'] = (index_r_displacement, index_c_displacement)\n            \n        elif gesture  == Gesture.HOVER:\n            index_r, index_c = index_fing_tip\n            midpoint_r, midpoint_c = int(index_r), int(index_c)\n        \n \n        # Update previous position position with current point\n        return post\n\ndef replay(fname):\n    print(\"replaying\", fname)\n\n    cap = cv.VideoCapture(fname)\n    detector = HandDetector()\n\n    if (not cap.isOpened()):\n        print(\"Error opening video file\")\n        return\n\n    while cap.isOpened() and (cv.waitKey(0) & 0xFF != ord('q')):\n        ret, img = cap.read()\n\n        # replay is completed when the video capture no longer has any frames to read.\n        if ret:\n            landmark_list = detector.detect_landmarks(img)\n            detector.draw_landmarks(img)\n\n            if len(landmark_list) != 0:\n                val = detector.detect_gesture(landmark_list, threshhold=0.9)\n                cv.putText(img, f\"Mode: {val.value}\", (50, 50),\n                        cv.FONT_HERSHEY_SIMPLEX, 1, (255, 0, 0), 2, cv.LINE_AA)\n\n\n\n            cv.imshow('Camera', img)\n        else:\n            break\n\n    cap.release()\n    cv.destroyAllWindows()\n\n    print(\"replay complete\", fname)\n\ndef live_demo():\n    cap = cv.VideoCapture(0)\n    detector = HandDetector()\n\n    while True:\n        _, img = cap.read()\n        img = cv.flip(img, 1)\n\n        landmark_list = detector.detect_landmarks(img)\n\n        detector.draw_landmarks(img)\n\n        if len(landmark_list) != 0:\n            val = detector.detect_gesture(landmark_list, threshhold=0.9)\n            cv.putText(img, f\"Mode: {val.value}\", (50, 50),\n                    cv.FONT_HERSHEY_SIMPLEX, 1, (255, 0, 0), 2, cv.LINE_AA)\n\n        cv.imshow('Camera', img)\n        if cv.waitKey(1) & 0xFF == ord('q'):\n            break\n    cap.release()\n    cv.destroyAllWindows()\n\n\nif __name__ == \"__main__\":\n    replay('hands_basic_gestures.mp4')\n"
  },
  {
    "path": "requirements.txt",
    "content": "absl-py==0.15.0\nattrs==21.2.0\ncycler==0.11.0\nkiwisolver==1.3.2\nmatplotlib==3.4.3\nmediapipe==0.8.9\nnumpy==1.21.4\nopencv-contrib-python==4.5.4.58\nopencv-python==4.5.4.58\nPillow==8.4.0\nprotobuf==3.19.1\npyparsing==3.0.5\npython-dateutil==2.8.2\nsix==1.16.0\n"
  },
  {
    "path": "util.py",
    "content": "import numpy as np\n\ndef xy_euclidean_dist(a1, a2): \n    return ((a1[0] - a2[0]) ** 2 + (a1[1] - a2[1]) ** 2) ** 0.5 \n\ndef clamp(value, lower_bound, upper_bound):\n    return min(upper_bound, max(value, lower_bound))\n\ndef vectorize(u, v):\n    assert(len(u) == len(v)) # cant vectorize unequal lengths\n    return [v[i] - u[i] for i in range(len(v))]\n\ndef vector_magnitude(vector):\n    return sum([dim**2 for dim in vector]) ** 0.5\n\ndef cos_angle(u, v):\n    u_mag = vector_magnitude(u)\n    v_mag = vector_magnitude(v)\n    if (u_mag == 0 or v_mag == 0):\n        return 0\n    return np.dot(u, v) / (vector_magnitude(u) * vector_magnitude(v))\n"
  }
]