Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hand pose detection #187

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions 011/about.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hand pose detection using opencv
276 changes: 276 additions & 0 deletions 011/handy.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,276 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Collecting mediapipe\n",
" Downloading mediapipe-0.8.9.1-cp37-cp37m-win_amd64.whl (48.5 MB)\n",
"Collecting opencv-python\n",
" Downloading opencv_python-4.5.5.62-cp36-abi3-win_amd64.whl (35.4 MB)\n",
"Requirement already satisfied: matplotlib in c:\\python3.7\\lib\\site-packages (from mediapipe) (3.0.3)\n",
"Collecting opencv-contrib-python\n",
" Downloading opencv_contrib_python-4.5.5.62-cp36-abi3-win_amd64.whl (42.2 MB)\n",
"Collecting absl-py\n",
" Downloading absl_py-1.0.0-py3-none-any.whl (126 kB)\n",
"Requirement already satisfied: attrs>=19.1.0 in c:\\python3.7\\lib\\site-packages (from mediapipe) (19.3.0)\n",
"Collecting protobuf>=3.11.4\n",
" Downloading protobuf-3.19.4-cp37-cp37m-win_amd64.whl (896 kB)\n",
"Requirement already satisfied: numpy in c:\\python3.7\\lib\\site-packages (from mediapipe) (1.17.4)\n",
"Requirement already satisfied: six in c:\\python3.7\\lib\\site-packages (from absl-py->mediapipe) (1.16.0)\n",
"Requirement already satisfied: cycler>=0.10 in c:\\python3.7\\lib\\site-packages (from matplotlib->mediapipe) (0.10.0)\n",
"Requirement already satisfied: python-dateutil>=2.1 in c:\\python3.7\\lib\\site-packages (from matplotlib->mediapipe) (2.8.2)\n",
"Requirement already satisfied: pyparsing!=2.0.4,!=2.1.2,!=2.1.6,>=2.0.1 in c:\\python3.7\\lib\\site-packages (from matplotlib->mediapipe) (2.4.7)\n",
"Requirement already satisfied: kiwisolver>=1.0.1 in c:\\python3.7\\lib\\site-packages (from matplotlib->mediapipe) (1.2.0)\n",
"Installing collected packages: protobuf, opencv-contrib-python, absl-py, opencv-python, mediapipe\n",
"Successfully installed absl-py-1.0.0 mediapipe-0.8.9.1 opencv-contrib-python-4.5.5.62 opencv-python-4.5.5.62 protobuf-3.19.4\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"WARNING: You are using pip version 21.3.1; however, version 22.0.3 is available.\n",
"You should consider upgrading via the 'c:\\python3.7\\python.exe -m pip install --upgrade pip' command.\n"
]
}
],
"source": [
"!pip install mediapipe opencv-python"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import mediapipe as mp\n",
"import cv2\n",
"import numpy as np\n",
"import uuid\n",
"import os"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"mp_drawing = mp.solutions.drawing_utils\n",
"mp_hands = mp.solutions.hands"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"2. Draw Hands"
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"<class 'mediapipe.python.solution_base.SolutionOutputs'>\n",
"<class 'mediapipe.python.solution_base.SolutionOutputs'>\n",
"<class 'mediapipe.python.solution_base.SolutionOutputs'>\n",
"<class 'mediapipe.python.solution_base.SolutionOutputs'>\n",
"<class 'mediapipe.python.solution_base.SolutionOutputs'>\n",
"<class 'mediapipe.python.solution_base.SolutionOutputs'>\n",
"<class 'mediapipe.python.solution_base.SolutionOutputs'>\n",
"<class 'mediapipe.python.solution_base.SolutionOutputs'>\n",
"<class 'mediapipe.python.solution_base.SolutionOutputs'>\n",
"<class 'mediapipe.python.solution_base.SolutionOutputs'>\n",
"<class 'mediapipe.python.solution_base.SolutionOutputs'>\n",
"<class 'mediapipe.python.solution_base.SolutionOutputs'>\n",
"<class 'mediapipe.python.solution_base.SolutionOutputs'>\n",
"<class 'mediapipe.python.solution_base.SolutionOutputs'>\n",
"<class 'mediapipe.python.solution_base.SolutionOutputs'>\n",
"<class 'mediapipe.python.solution_base.SolutionOutputs'>\n",
"<class 'mediapipe.python.solution_base.SolutionOutputs'>\n",
"<class 'mediapipe.python.solution_base.SolutionOutputs'>\n",
"<class 'mediapipe.python.solution_base.SolutionOutputs'>\n",
"<class 'mediapipe.python.solution_base.SolutionOutputs'>\n",
"<class 'mediapipe.python.solution_base.SolutionOutputs'>\n",
"<class 'mediapipe.python.solution_base.SolutionOutputs'>\n",
"<class 'mediapipe.python.solution_base.SolutionOutputs'>\n",
"<class 'mediapipe.python.solution_base.SolutionOutputs'>\n",
"<class 'mediapipe.python.solution_base.SolutionOutputs'>\n",
"<class 'mediapipe.python.solution_base.SolutionOutputs'>\n",
"<class 'mediapipe.python.solution_base.SolutionOutputs'>\n",
"<class 'mediapipe.python.solution_base.SolutionOutputs'>\n",
"<class 'mediapipe.python.solution_base.SolutionOutputs'>\n",
"<class 'mediapipe.python.solution_base.SolutionOutputs'>\n",
"<class 'mediapipe.python.solution_base.SolutionOutputs'>\n",
"<class 'mediapipe.python.solution_base.SolutionOutputs'>\n",
"<class 'mediapipe.python.solution_base.SolutionOutputs'>\n",
"<class 'mediapipe.python.solution_base.SolutionOutputs'>\n",
"<class 'mediapipe.python.solution_base.SolutionOutputs'>\n",
"<class 'mediapipe.python.solution_base.SolutionOutputs'>\n",
"<class 'mediapipe.python.solution_base.SolutionOutputs'>\n",
"<class 'mediapipe.python.solution_base.SolutionOutputs'>\n",
"<class 'mediapipe.python.solution_base.SolutionOutputs'>\n",
"<class 'mediapipe.python.solution_base.SolutionOutputs'>\n",
"<class 'mediapipe.python.solution_base.SolutionOutputs'>\n",
"<class 'mediapipe.python.solution_base.SolutionOutputs'>\n",
"<class 'mediapipe.python.solution_base.SolutionOutputs'>\n",
"<class 'mediapipe.python.solution_base.SolutionOutputs'>\n",
"<class 'mediapipe.python.solution_base.SolutionOutputs'>\n",
"<class 'mediapipe.python.solution_base.SolutionOutputs'>\n",
"<class 'mediapipe.python.solution_base.SolutionOutputs'>\n",
"<class 'mediapipe.python.solution_base.SolutionOutputs'>\n",
"<class 'mediapipe.python.solution_base.SolutionOutputs'>\n",
"<class 'mediapipe.python.solution_base.SolutionOutputs'>\n",
"<class 'mediapipe.python.solution_base.SolutionOutputs'>\n",
"<class 'mediapipe.python.solution_base.SolutionOutputs'>\n",
"<class 'mediapipe.python.solution_base.SolutionOutputs'>\n",
"<class 'mediapipe.python.solution_base.SolutionOutputs'>\n",
"<class 'mediapipe.python.solution_base.SolutionOutputs'>\n",
"<class 'mediapipe.python.solution_base.SolutionOutputs'>\n",
"<class 'mediapipe.python.solution_base.SolutionOutputs'>\n",
"<class 'mediapipe.python.solution_base.SolutionOutputs'>\n",
"<class 'mediapipe.python.solution_base.SolutionOutputs'>\n",
"<class 'mediapipe.python.solution_base.SolutionOutputs'>\n",
"<class 'mediapipe.python.solution_base.SolutionOutputs'>\n",
"<class 'mediapipe.python.solution_base.SolutionOutputs'>\n",
"<class 'mediapipe.python.solution_base.SolutionOutputs'>\n",
"<class 'mediapipe.python.solution_base.SolutionOutputs'>\n",
"<class 'mediapipe.python.solution_base.SolutionOutputs'>\n",
"<class 'mediapipe.python.solution_base.SolutionOutputs'>\n",
"<class 'mediapipe.python.solution_base.SolutionOutputs'>\n",
"<class 'mediapipe.python.solution_base.SolutionOutputs'>\n",
"<class 'mediapipe.python.solution_base.SolutionOutputs'>\n",
"<class 'mediapipe.python.solution_base.SolutionOutputs'>\n",
"<class 'mediapipe.python.solution_base.SolutionOutputs'>\n",
"<class 'mediapipe.python.solution_base.SolutionOutputs'>\n",
"<class 'mediapipe.python.solution_base.SolutionOutputs'>\n",
"<class 'mediapipe.python.solution_base.SolutionOutputs'>\n",
"<class 'mediapipe.python.solution_base.SolutionOutputs'>\n",
"<class 'mediapipe.python.solution_base.SolutionOutputs'>\n",
"<class 'mediapipe.python.solution_base.SolutionOutputs'>\n",
"<class 'mediapipe.python.solution_base.SolutionOutputs'>\n",
"<class 'mediapipe.python.solution_base.SolutionOutputs'>\n",
"<class 'mediapipe.python.solution_base.SolutionOutputs'>\n",
"<class 'mediapipe.python.solution_base.SolutionOutputs'>\n",
"<class 'mediapipe.python.solution_base.SolutionOutputs'>\n",
"<class 'mediapipe.python.solution_base.SolutionOutputs'>\n",
"<class 'mediapipe.python.solution_base.SolutionOutputs'>\n",
"<class 'mediapipe.python.solution_base.SolutionOutputs'>\n",
"<class 'mediapipe.python.solution_base.SolutionOutputs'>\n",
"<class 'mediapipe.python.solution_base.SolutionOutputs'>\n",
"<class 'mediapipe.python.solution_base.SolutionOutputs'>\n",
"<class 'mediapipe.python.solution_base.SolutionOutputs'>\n",
"<class 'mediapipe.python.solution_base.SolutionOutputs'>\n",
"<class 'mediapipe.python.solution_base.SolutionOutputs'>\n",
"<class 'mediapipe.python.solution_base.SolutionOutputs'>\n",
"<class 'mediapipe.python.solution_base.SolutionOutputs'>\n",
"<class 'mediapipe.python.solution_base.SolutionOutputs'>\n",
"<class 'mediapipe.python.solution_base.SolutionOutputs'>\n",
"<class 'mediapipe.python.solution_base.SolutionOutputs'>\n",
"<class 'mediapipe.python.solution_base.SolutionOutputs'>\n",
"<class 'mediapipe.python.solution_base.SolutionOutputs'>\n",
"<class 'mediapipe.python.solution_base.SolutionOutputs'>\n",
"<class 'mediapipe.python.solution_base.SolutionOutputs'>\n",
"<class 'mediapipe.python.solution_base.SolutionOutputs'>\n"
]
}
],
"source": [
"cap = cv2.VideoCapture(0)\n",
"with mp_hands.Hands(min_detection_confidence=0.8, min_tracking_confidence=0.5, max_num_hands=5) as hands:\n",
" while cap.isOpened():\n",
" ret, frame = cap.read()\n",
"\n",
" #BGR 2 RGB\n",
" image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)\n",
"\n",
" #flip on horizontal\n",
" image = cv2.flip(image, 1)\n",
"\n",
" #set flags\n",
" image.flags.writeable = False\n",
"\n",
" #Detection\n",
" results = hands.process(image)\n",
"\n",
" #Set flag to true\n",
" image.flags.writeable = True\n",
"\n",
" #RGB 2 BGR\n",
" image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)\n",
"\n",
" #Print results\n",
" # print(results)\n",
"\n",
" #Rendering results\n",
" if results.multi_hand_landmarks:\n",
" for num, hand in enumerate(results.multi_hand_landmarks):\n",
" mp_drawing.draw_landmarks(image, hand, mp_hands.HAND_CONNECTIONS,\n",
" mp_drawing.DrawingSpec(color=(121, 22, 76), thickness=2, circle_radius=4),\n",
" mp_drawing.DrawingSpec(color=(121, 44, 250), thickness=2, circle_radius=2))\n",
"\n",
"\n",
" cv2.imshow('Hand Tracking', image)\n",
"\n",
" if cv2.waitKey(10) & 0xFF == ord('q'):\n",
" break\n",
"cap.release()\n",
"cv2.destroyAllWindows()"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"frozenset({(5, 9), (10, 11), (5, 6), (15, 16), (13, 17), (18, 19), (1, 2), (6, 7), (0, 17), (3, 4), (9, 10), (0, 5), (2, 3), (14, 15), (11, 12), (19, 20), (0, 1), (9, 13), (17, 18), (13, 14), (7, 8)})\n"
]
}
],
"source": [
"print(mp_hands.HAND_CONNECTIONS)"
]
}
],
"metadata": {
"interpreter": {
"hash": "03bed68d712062ee42a1c3ead36bcb8d97e27dfcc583015412cdd858993b4fa1"
},
"kernelspec": {
"display_name": "Python 3.7.1 64-bit",
"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.7.1"
},
"orig_nbformat": 4
},
"nbformat": 4,
"nbformat_minor": 2
}