|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "code", |
| 5 | + "execution_count": 39, |
| 6 | + "metadata": {}, |
| 7 | + "outputs": [], |
| 8 | + "source": [ |
| 9 | + "import cv2\n", |
| 10 | + "import mediapipe as mp\n", |
| 11 | + "import time\n", |
| 12 | + "import importlib\n", |
| 13 | + "import handTrackingModule as htm\n", |
| 14 | + "importlib.reload(htm)\n", |
| 15 | + "from math import *" |
| 16 | + ] |
| 17 | + }, |
| 18 | + { |
| 19 | + "cell_type": "code", |
| 20 | + "execution_count": 40, |
| 21 | + "metadata": {}, |
| 22 | + "outputs": [], |
| 23 | + "source": [ |
| 24 | + "def a(lmList):\n", |
| 25 | + " if len(lmList) != 0:\n", |
| 26 | + " index = lmList[8][2] > lmList[6][2]\n", |
| 27 | + " middle = lmList[12][2] > lmList[10][2]\n", |
| 28 | + " ring = lmList[16][2] > lmList[14][2]\n", |
| 29 | + " pinky = lmList[20][2] > lmList[18][2]\n", |
| 30 | + " thumb = lmList[4][1] < lmList[3][1]\n", |
| 31 | + " \n", |
| 32 | + " if index and middle and ring and pinky and thumb:\n", |
| 33 | + " return 1\n", |
| 34 | + " else:\n", |
| 35 | + " return 0" |
| 36 | + ] |
| 37 | + }, |
| 38 | + { |
| 39 | + "cell_type": "code", |
| 40 | + "execution_count": 41, |
| 41 | + "metadata": {}, |
| 42 | + "outputs": [], |
| 43 | + "source": [ |
| 44 | + "def b(lmList):\n", |
| 45 | + " if len(lmList) != 0:\n", |
| 46 | + " index = lmList[8][2] < lmList[6][2]\n", |
| 47 | + " middle = lmList[12][2] < lmList[10][2]\n", |
| 48 | + " ring = lmList[16][2] < lmList[14][2]\n", |
| 49 | + " pinky = lmList[20][2] < lmList[18][2]\n", |
| 50 | + " thumb = lmList[4][1] < lmList[3][1]\n", |
| 51 | + " \n", |
| 52 | + " if index and middle and ring and pinky and thumb:\n", |
| 53 | + " return 1\n", |
| 54 | + " else:\n", |
| 55 | + " return 0" |
| 56 | + ] |
| 57 | + }, |
| 58 | + { |
| 59 | + "cell_type": "code", |
| 60 | + "execution_count": 42, |
| 61 | + "metadata": {}, |
| 62 | + "outputs": [], |
| 63 | + "source": [ |
| 64 | + "def lineTrack(frame, lmList, p1, p2, draw = True):\n", |
| 65 | + " if len(lmList) != 0:\n", |
| 66 | + " startPoint = (lmList[p1][1], lmList[p1][2])\n", |
| 67 | + " endPoint = (lmList[p2][1], lmList[p2][2])\n", |
| 68 | + " \n", |
| 69 | + " if draw:\n", |
| 70 | + " frame = cv2.line(frame, startPoint, endPoint, (255, 255, 255), 9)\n", |
| 71 | + " return (frame, startPoint, endPoint) \n", |
| 72 | + " " |
| 73 | + ] |
| 74 | + }, |
| 75 | + { |
| 76 | + "cell_type": "code", |
| 77 | + "execution_count": 43, |
| 78 | + "metadata": {}, |
| 79 | + "outputs": [], |
| 80 | + "source": [ |
| 81 | + "def length(start, end):\n", |
| 82 | + " x1, y1 = start\n", |
| 83 | + " x2, y2 = end\n", |
| 84 | + " x = (x2 - x1)**2\n", |
| 85 | + " y = (y2 - y1)**2\n", |
| 86 | + " return sqrt(x+y)" |
| 87 | + ] |
| 88 | + }, |
| 89 | + { |
| 90 | + "cell_type": "code", |
| 91 | + "execution_count": 80, |
| 92 | + "metadata": {}, |
| 93 | + "outputs": [], |
| 94 | + "source": [ |
| 95 | + "def c(lmList):\n", |
| 96 | + " dist = 200\n", |
| 97 | + " if len(lmList) != 0:\n", |
| 98 | + " if(lmList[5][3] > 120):\n", |
| 99 | + " if(lmList[9][3] < lmList[5][3]):\n", |
| 100 | + " dist = length((lmList[8][1],lmList[8][2]),(lmList[4][1],lmList[4][2]))\n", |
| 101 | + " \n", |
| 102 | + " if dist < 100:\n", |
| 103 | + " return 1\n", |
| 104 | + " else:\n", |
| 105 | + " return 0" |
| 106 | + ] |
| 107 | + }, |
| 108 | + { |
| 109 | + "cell_type": "code", |
| 110 | + "execution_count": null, |
| 111 | + "metadata": {}, |
| 112 | + "outputs": [], |
| 113 | + "source": [ |
| 114 | + "def d(lmList):\n", |
| 115 | + " if len(lmList) != 0:\n", |
| 116 | + " index = lmList[8][2] > lmList[6][2]\n", |
| 117 | + " middle = lmList[12][2] < lmList[10][2]\n", |
| 118 | + " ring = lmList[16][2] < lmList[14][2]\n", |
| 119 | + " pinky = lmList[20][2] < lmList[18][2]\n", |
| 120 | + " distance = length((lmList[8][1],lmList[8][2]),(lmList[4][1],lmList[4][2])) \n", |
| 121 | + " \n", |
| 122 | + " if index and middle and ring and pinky and distance < 20:\n", |
| 123 | + " return 1\n", |
| 124 | + " else:\n", |
| 125 | + " return 0" |
| 126 | + ] |
| 127 | + }, |
| 128 | + { |
| 129 | + "cell_type": "code", |
| 130 | + "execution_count": null, |
| 131 | + "metadata": {}, |
| 132 | + "outputs": [], |
| 133 | + "source": [ |
| 134 | + "def e(lmList):\n", |
| 135 | + " if len(lmList) != 0:\n", |
| 136 | + " index = lmList[8][2] > lmList[6][2]\n", |
| 137 | + " middle = lmList[12][2] < lmList[10][2]\n", |
| 138 | + " ring = lmList[16][2] < lmList[14][2]\n", |
| 139 | + " pinky = lmList[20][2] < lmList[18][2]\n", |
| 140 | + " distance = length((lmList[8][1],lmList[8][2]),(lmList[4][1],lmList[4][2])) \n", |
| 141 | + " \n", |
| 142 | + " if index and middle and ring and pinky and distance < 20:\n", |
| 143 | + " return 1\n", |
| 144 | + " else:\n", |
| 145 | + " return 0" |
| 146 | + ] |
| 147 | + }, |
| 148 | + { |
| 149 | + "cell_type": "code", |
| 150 | + "execution_count": null, |
| 151 | + "metadata": {}, |
| 152 | + "outputs": [], |
| 153 | + "source": [ |
| 154 | + "def f(lmList):\n", |
| 155 | + " if len(lmList) != 0:\n", |
| 156 | + " middle = lmList[12][2] > lmList[10][2]\n", |
| 157 | + " ring = lmList[16][2] > lmList[14][2]\n", |
| 158 | + " pinky = lmList[20][2] > lmList[18][2]\n", |
| 159 | + " distance = length((lmList[8][1],lmList[8][2]),(lmList[4][1],lmList[4][2])) \n", |
| 160 | + " \n", |
| 161 | + " if index and middle and ring and pinky and distance < 20:\n", |
| 162 | + " return 1\n", |
| 163 | + " else:\n", |
| 164 | + " return 0" |
| 165 | + ] |
| 166 | + }, |
| 167 | + { |
| 168 | + "cell_type": "code", |
| 169 | + "execution_count": 81, |
| 170 | + "metadata": {}, |
| 171 | + "outputs": [], |
| 172 | + "source": [ |
| 173 | + "wCam, hCam = 640, 480\n", |
| 174 | + "\n", |
| 175 | + "camera = cv2.VideoCapture(0)\n", |
| 176 | + "\n", |
| 177 | + "camera.set(3, wCam)\n", |
| 178 | + "camera.set(4, hCam)\n", |
| 179 | + "\n", |
| 180 | + "# For importing pictures\n", |
| 181 | + "#folderPath = \"FingerImages\"\n", |
| 182 | + "#myList = os.listdir(folderPath)\n", |
| 183 | + "\n", |
| 184 | + "#overlayList = []\n", |
| 185 | + "\n", |
| 186 | + "#for imPath in myList:\n", |
| 187 | + " #image = cv2.imread(f'{folderPath}/{imPath}')\n", |
| 188 | + " #overlayList.append(image)\n", |
| 189 | + "\n", |
| 190 | + "pTime = 0\n", |
| 191 | + "\n", |
| 192 | + "detector = htm.handDetector(detectionCon = 0.75)" |
| 193 | + ] |
| 194 | + }, |
| 195 | + { |
| 196 | + "cell_type": "code", |
| 197 | + "execution_count": 82, |
| 198 | + "metadata": {}, |
| 199 | + "outputs": [], |
| 200 | + "source": [ |
| 201 | + "while True:\n", |
| 202 | + " success, frame = camera.read()\n", |
| 203 | + " \n", |
| 204 | + " frame = cv2.flip(frame, 1)\n", |
| 205 | + " \n", |
| 206 | + " frame = detector.findHands(frame)\n", |
| 207 | + " \n", |
| 208 | + " lmList = detector.findPosition(frame, draw = False)\n", |
| 209 | + " \n", |
| 210 | + " if(c(lmList)):\n", |
| 211 | + " cv2.putText(frame, f'You signed c!', (0, 70), cv2.FONT_HERSHEY_PLAIN, 3, (255,0,0), 3)\n", |
| 212 | + " elif(a(lmList)):\n", |
| 213 | + " cv2.putText(frame, f'You signed a!', (0, 70), cv2.FONT_HERSHEY_PLAIN, 3, (255,0,0), 3)\n", |
| 214 | + " elif(b(lmList)):\n", |
| 215 | + " cv2.putText(frame, f'You signed b!', (0, 70), cv2.FONT_HERSHEY_PLAIN, 3, (255,0,0), 3)\n", |
| 216 | + " \n", |
| 217 | + " #if len(lmList) != 0:\n", |
| 218 | + " #if(lmList[5][3] > 130):\n", |
| 219 | + " #if(lmList[9][3] < lmList[5][3]):\n", |
| 220 | + " #z = lmList[9][3]\n", |
| 221 | + " #z2 = lmList[5][3]\n", |
| 222 | + " #cv2.putText(frame, f'pinky{z} < index{z2}', (0, 100), cv2.FONT_HERSHEY_PLAIN, 3, (255,0,0), 3)\n", |
| 223 | + " \n", |
| 224 | + " # Calculating the Frames per Seconds (FPS)\n", |
| 225 | + " cTime = time.time()\n", |
| 226 | + " fps = 1 / (cTime - pTime)\n", |
| 227 | + " pTime = cTime\n", |
| 228 | + " \n", |
| 229 | + " # Displaying the FPS (ERROR PRINTING HIGH NUMBERS)\n", |
| 230 | + " cv2.putText(frame, f'FPS: {int(fps)}', (400, 70), cv2.FONT_HERSHEY_PLAIN, 3, (0,0,0), 3)\n", |
| 231 | + " \n", |
| 232 | + " cv2.imshow(\"Camera\", frame)\n", |
| 233 | + " \n", |
| 234 | + " key = cv2.waitKey(1)\n", |
| 235 | + " if key in [27, ord('Q'), ord('q')]:\n", |
| 236 | + " break\n", |
| 237 | + " \n", |
| 238 | + "camera.release()\n", |
| 239 | + "cv2.destroyAllWindows()" |
| 240 | + ] |
| 241 | + }, |
| 242 | + { |
| 243 | + "cell_type": "code", |
| 244 | + "execution_count": null, |
| 245 | + "metadata": {}, |
| 246 | + "outputs": [], |
| 247 | + "source": [] |
| 248 | + } |
| 249 | + ], |
| 250 | + "metadata": { |
| 251 | + "kernelspec": { |
| 252 | + "display_name": "Python 3", |
| 253 | + "language": "python", |
| 254 | + "name": "python3" |
| 255 | + }, |
| 256 | + "language_info": { |
| 257 | + "codemirror_mode": { |
| 258 | + "name": "ipython", |
| 259 | + "version": 3 |
| 260 | + }, |
| 261 | + "file_extension": ".py", |
| 262 | + "mimetype": "text/x-python", |
| 263 | + "name": "python", |
| 264 | + "nbconvert_exporter": "python", |
| 265 | + "pygments_lexer": "ipython3", |
| 266 | + "version": "3.6.12" |
| 267 | + } |
| 268 | + }, |
| 269 | + "nbformat": 4, |
| 270 | + "nbformat_minor": 4 |
| 271 | +} |
0 commit comments