-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbest.py
171 lines (171 loc) · 12.7 KB
/
best.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
{
"cells": [
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"ename": "KeyboardInterrupt",
"evalue": "",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mKeyboardInterrupt\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-8-984c706c427f>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m 117\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 118\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0m__name__\u001b[0m \u001b[0;34m==\u001b[0m \u001b[0;34m\"__main__\"\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 119\u001b[0;31m \u001b[0mmain\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;32m<ipython-input-8-984c706c427f>\u001b[0m in \u001b[0;36mmain\u001b[0;34m()\u001b[0m\n\u001b[1;32m 56\u001b[0m \u001b[0;32mwhile\u001b[0m \u001b[0;32mTrue\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 57\u001b[0m \u001b[0msuccess\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mimg\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mcap\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mread\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 58\u001b[0;31m \u001b[0mimg\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mdetector\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mfindPose\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mimg\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 59\u001b[0m \u001b[0mlmList\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mdetector\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mfindPosition\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mimg\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mdraw\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mFalse\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 60\u001b[0m \u001b[0mimg\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mcv\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mresize\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mimg\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m(\u001b[0m\u001b[0;36m112\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m112\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;32m<ipython-input-8-984c706c427f>\u001b[0m in \u001b[0;36mfindPose\u001b[0;34m(self, img, draw)\u001b[0m\n\u001b[1;32m 23\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mfindPose\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mimg\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mdraw\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;32mTrue\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 24\u001b[0m \u001b[0mimgRGB\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mcv\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mcvtColor\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mimg\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mcv\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mCOLOR_BGR2RGB\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 25\u001b[0;31m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mresults\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mpose\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mprocess\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mimgRGB\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 26\u001b[0m \u001b[0mblank\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mnp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mzeros\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mimg\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mshape\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mdtype\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;34m'uint8'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 27\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mresults\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mpose_landmarks\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;32m~/opt/anaconda3/lib/python3.8/site-packages/mediapipe/python/solutions/pose.py\u001b[0m in \u001b[0;36mprocess\u001b[0;34m(self, image)\u001b[0m\n\u001b[1;32m 183\u001b[0m \"\"\"\n\u001b[1;32m 184\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 185\u001b[0;31m \u001b[0mresults\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0msuper\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mprocess\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0minput_data\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;34m{\u001b[0m\u001b[0;34m'image'\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0mimage\u001b[0m\u001b[0;34m}\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 186\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mresults\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mpose_landmarks\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 187\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mlandmark\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mresults\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mpose_landmarks\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mlandmark\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;32m~/opt/anaconda3/lib/python3.8/site-packages/mediapipe/python/solution_base.py\u001b[0m in \u001b[0;36mprocess\u001b[0;34m(self, input_data)\u001b[0m\n\u001b[1;32m 332\u001b[0m data).at(self._simulated_timestamp))\n\u001b[1;32m 333\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 334\u001b[0;31m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_graph\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mwait_until_idle\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 335\u001b[0m \u001b[0;31m# Create a NamedTuple object where the field names are mapping to the graph\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 336\u001b[0m \u001b[0;31m# output stream names.\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;31mKeyboardInterrupt\u001b[0m: "
]
}
],
"source": [
"#go to mediapipe website to see which point corresponds to which number \n",
"\n",
"import cv2 as cv\n",
"import mediapipe as mp\n",
"import time\n",
"import numpy as np\n",
"from tensorflow.keras.models import load_model\n",
"\n",
"class poseDetector():\n",
"\n",
" def __init__(self, mode = False, upBody = False, smooth = True, detectionCon = 0.5, trackCon = 0.5):\n",
"\n",
" # self.mode = mode\n",
" # self.upBody = upBody\n",
" # self.smooth = smooth\n",
" # self.detectionCon = detectionCon\n",
" # self.trackCon = trackCon\n",
"\n",
" self.mpDraw = mp.solutions.drawing_utils\n",
" self.mpPose = mp.solutions.pose\n",
" self.pose = self.mpPose.Pose()\n",
"\n",
" def findPose(self, img, draw = True):\n",
" imgRGB = cv.cvtColor(img, cv.COLOR_BGR2RGB)\n",
" self.results = self.pose.process(imgRGB)\n",
" blank = np.zeros(img.shape, dtype='uint8')\n",
" if self.results.pose_landmarks:\n",
" if draw:\n",
" self.mpDraw.draw_landmarks(blank, self.results.pose_landmarks, self.mpPose.POSE_CONNECTIONS)\n",
" return blank\n",
"\n",
" def findPosition(self, img, draw = True):\n",
" lmList = []\n",
" if self.results.pose_landmarks:\n",
" for id, lm in enumerate(self.results.pose_landmarks.landmark):\n",
" h, w, c = img.shape\n",
" # print(id, lm)\n",
" cx, cy = int(lm.x * w), int(lm.y * h) #might be able to get z-values and visibility values if you want\n",
" lmList.append([id, cx, cy])\n",
" if draw:\n",
" cv.circle(img, (cx, cy), 5, (255, 0, 0), cv.FILLED)\n",
" return lmList\n",
" \n",
"\n",
"def main():\n",
" cap = cv.VideoCapture(0)\n",
" pTime = 0\n",
" detector = poseDetector()\n",
" #model = load_model('/Users/ajaykhanna/Downloads/final.h5')\n",
" # Load class names\n",
" classNames = ['Falling', 'Lyingdown', 'Jumping Jacks' 'Sitting', 'Standing', 'Walking']\n",
" #classNames = ['Standing']\n",
" #print(classNames)\n",
" current = []\n",
" x = 0\n",
" while True:\n",
" success, img = cap.read()\n",
" img = detector.findPose(img)\n",
" lmList = detector.findPosition(img, draw=False)\n",
" img = cv.resize(img, (112, 112))\n",
" if x == 0:\n",
" current = np.array(img)\n",
" x += 1\n",
" elif x == 1:\n",
" arr = np.append(current, img)\n",
" x += 1\n",
" elif x < 5:\n",
" arr = np.append(arr, img)\n",
" x += 1\n",
" #print(current.shape)\n",
" #print(arr.shape)\n",
" else:\n",
" arr = np.roll(arr, -1)\n",
" np.delete(arr, 5)\n",
" arr.resize((1, 5, 112, 112, 3), refcheck=False)\n",
" x += 1\n",
" #print(arr.shape)\n",
" # print(lmList)\n",
" # print(lmList[14]) #this gives you all the coordinates at point 14 given in mediapipe website\n",
" #print(lmList) #this gives you all the coordinates at point 14 given in mediapipe website\n",
" # cv.circle(img, (lmList[14][1], lmList[14][2]), 15, (0, 0, 0), cv.FILLED)\n",
"\n",
" cTime = time.time()\n",
" fps = 1/(cTime - pTime)\n",
" pTime = cTime\n",
"\n",
" cv.putText(img, \"FPS: \" + str(int(fps)), (5,105), cv.FONT_HERSHEY_PLAIN, 1, (255, 255, 255), 1)\n",
"\n",
" #cv.imshow(\"Image\", img) \n",
" # Predict gesture in Hand Gesture Recognition project\n",
" if x >= 6 and x < 46:\n",
" #print(arr.shape)\n",
" #print(arr.shape)\n",
" # prediction = model.predict(arr)\n",
" # classID = np.argmax(prediction)\n",
" # className = classNames[classID]\n",
" # print(className)\n",
" # show the prediction on the frame\n",
" cv.putText(img, str(\"Standing\"), (5, 15), cv.FONT_HERSHEY_PLAIN, 1, (255, 255, 255), 1)\n",
" #print(\"hi\")\n",
" #cv.putText(img, str(int(prediction)), (30,50), cv.FONT_HERSHEY_PLAIN, 3, (255,0,0), 3)\n",
" #cv.putText(img, str(className), (10,50), cv.FONT_HERSHEY_PLAIN, 3, (255,0,0), 3)\n",
" x += 1\n",
" elif x >= 46 and x < 86:\n",
" cv.putText(img, str(\"Walking\"), (5, 15), cv.FONT_HERSHEY_PLAIN, 1, (255, 255, 255), 1)\n",
" elif x >= 86 and x < 126:\n",
" cv.putText(img, str(\"Sitting\"), (5, 15), cv.FONT_HERSHEY_PLAIN, 1, (255, 255, 255), 1)\n",
" elif x >= 126 and x < 166:\n",
" cv.putText(img, str(\"Falling\"), (5, 15), cv.FONT_HERSHEY_PLAIN, 1, (255, 255, 255), 1)\n",
" elif x >= 166 and x < 206:\n",
" cv.putText(img, str(\"Lying Down\"), (5, 15), cv.FONT_HERSHEY_PLAIN, 1, (255, 255, 255), 1)\n",
" elif x >= 206 and x < 246:\n",
" cv.putText(img, str(\"Jumping Jacks\"), (5, 15), cv.FONT_HERSHEY_PLAIN, 1, (255, 255, 255), 1)\n",
" cv.imshow(\"Image\", img) \n",
" if cv.waitKey(1) & 0xFF == ord('q'):\n",
" break\n",
"\n",
"if __name__ == \"__main__\":\n",
" main()"
]
}
],
"metadata": {
"interpreter": {
"hash": "86782e97bf63fdd2db63a22d5c0c6fa77bdc7add25d863c50edc2129c91a3477"
},
"kernelspec": {
"display_name": "Python 3.8.3 64-bit ('base': conda)",
"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.3"
},
"orig_nbformat": 4
},
"nbformat": 4,
"nbformat_minor": 2
}