Switch to unified view

a b/landmark_extraction/static_landmarks+extraction.ipynb
1
{
2
 "cells": [
3
  {
4
   "cell_type": "code",
5
   "execution_count": 2,
6
   "metadata": {},
7
   "outputs": [],
8
   "source": [
9
    "import cv2\n",
10
    "import matplotlib.pyplot as plt\n",
11
    "import numpy as np\n",
12
    "import os\n",
13
    "import urllib.request\n",
14
    "import sys\n",
15
    "import torch\n",
16
    "import time\n",
17
    "import datetime\n",
18
    "\n",
19
    "from torchvision import transforms\n",
20
    "from PIL import Image\n",
21
    "\n",
22
    "%matplotlib inline"
23
   ]
24
  },
25
  {
26
   "cell_type": "code",
27
   "execution_count": 3,
28
   "metadata": {},
29
   "outputs": [],
30
   "source": [
31
    "YOLOV7_MODEL = [\n",
32
    "    \"https://github.com/WongKinYiu/yolov7/releases/download/v0.1/yolov7-tiny.pt\",\n",
33
    "    \"https://github.com/WongKinYiu/yolov7/releases/download/v0.1/yolov7.pt\",\n",
34
    "    \"https://github.com/WongKinYiu/yolov7/releases/download/v0.1/yolov7x.pt\",\n",
35
    "    \"https://github.com/WongKinYiu/yolov7/releases/download/v0.1/yolov7-w6.pt\",\n",
36
    "    \"https://github.com/WongKinYiu/yolov7/releases/download/v0.1/yolov7-e6.pt\",\n",
37
    "    \"https://github.com/WongKinYiu/yolov7/releases/download/v0.1/yolov7-d6.pt\",\n",
38
    "    \"https://github.com/WongKinYiu/yolov7/releases/download/v0.1/yolov7-e6e.pt\",\n",
39
    "    \"https://github.com/WongKinYiu/yolov7/releases/download/v0.1/yolov7-w6-pose.pt\",\n",
40
    "]"
41
   ]
42
  },
43
  {
44
   "cell_type": "code",
45
   "execution_count": 4,
46
   "metadata": {},
47
   "outputs": [],
48
   "source": [
49
    "def get_yolov7_model(modelistid=1):\n",
50
    "    \"\"\"\n",
51
    "    Download YoloV7 model from a yoloV7 model list\n",
52
    "    \"\"\"\n",
53
    "    modelid = YOLOV7_MODEL[modelistid]\n",
54
    "\n",
55
    "    if not os.path.exists(modelid):\n",
56
    "        print(\"Downloading the model:\",\n",
57
    "              os.path.basename(modelid), \"from:\", modelid)\n",
58
    "        urllib.request.urlretrieve(modelid, \n",
59
    "                                   filename=os.path.basename(modelid))\n",
60
    "        print(\"Done\\n\")\n",
61
    "        !ls yolo*.pt -lh\n",
62
    "\n",
63
    "    if os.path.exists(modelid):\n",
64
    "        print(\"Downloaded model files:\")\n",
65
    "        !ls yolo*.pt -lh"
66
   ]
67
  },
68
  {
69
   "cell_type": "code",
70
   "execution_count": 5,
71
   "metadata": {},
72
   "outputs": [],
73
   "source": [
74
    "from utils.datasets import letterbox\n",
75
    "from utils.general import non_max_suppression_kpt\n",
76
    "from utils.plots import output_to_keypoint, plot_skeleton_kpts"
77
   ]
78
  },
79
  {
80
   "cell_type": "code",
81
   "execution_count": 6,
82
   "metadata": {},
83
   "outputs": [],
84
   "source": [
85
    "device = torch.device(\"cuda:0\" if torch.cuda.is_available() else \"cpu\")"
86
   ]
87
  },
88
  {
89
   "cell_type": "code",
90
   "execution_count": 8,
91
   "metadata": {},
92
   "outputs": [],
93
   "source": [
94
    "def image_view(imagefile, w=15, h=10):\n",
95
    "    \"\"\"\n",
96
    "    Displaying an image from an image file\n",
97
    "    \"\"\"\n",
98
    "    %matplotlib inline\n",
99
    "    plt.figure(figsize=(w, h))\n",
100
    "    plt.axis('off')\n",
101
    "    plt.imshow(cv2.cvtColor(cv2.imread(imagefile), \n",
102
    "                            cv2.COLOR_BGR2RGB))"
103
   ]
104
  },
105
  {
106
   "cell_type": "code",
107
   "execution_count": 23,
108
   "metadata": {},
109
   "outputs": [],
110
   "source": [
111
    "def running_inference(image):\n",
112
    "    global model\n",
113
    "    \"\"\"\n",
114
    "    Running yolov7 model inference\n",
115
    "    \"\"\"\n",
116
    "    image = letterbox(image, 960, \n",
117
    "                      stride=64,\n",
118
    "                      auto=True)[0]  # shape: (567, 960, 3)\n",
119
    "    image = transforms.ToTensor()(image)  # torch.Size([3, 567, 960])\n",
120
    "\n",
121
    "    if torch.cuda.is_available():\n",
122
    "        image = image.half().to(device)\n",
123
    "\n",
124
    "    image = image.unsqueeze(0)  # torch.Size([1, 3, 567, 960])\n",
125
    "\n",
126
    "    with torch.no_grad():\n",
127
    "        output, _ = model(image)\n",
128
    "\n",
129
    "    return output, image"
130
   ]
131
  },
132
  {
133
   "cell_type": "code",
134
   "execution_count": 15,
135
   "metadata": {},
136
   "outputs": [],
137
   "source": [
138
    "def loading_yolov7_model(yolomodel):\n",
139
    "    \"\"\"\n",
140
    "    Loading yolov7 model\n",
141
    "    \"\"\"\n",
142
    "    print(\"Loading model:\", yolomodel)\n",
143
    "    model = torch.load(yolomodel, map_location=device)['model']\n",
144
    "    model.float().eval()\n",
145
    "\n",
146
    "    if torch.cuda.is_available():\n",
147
    "        # half() turns predictions into float16 tensors\n",
148
    "        # which significantly lowers inference time\n",
149
    "        model.half().to(device)\n",
150
    "\n",
151
    "    return model, yolomodel"
152
   ]
153
  },
154
  {
155
   "cell_type": "code",
156
   "execution_count": 18,
157
   "metadata": {},
158
   "outputs": [
159
    {
160
     "name": "stdout",
161
     "output_type": "stream",
162
     "text": [
163
      "Downloading the model: yolov7-tiny.pt from: https://github.com/WongKinYiu/yolov7/releases/download/v0.1/yolov7-tiny.pt\n",
164
      "Done\n",
165
      "\n",
166
      "-rw-r--r-- 1 g g 13M Aug 16 21:30 yolov7-tiny.pt\n"
167
     ]
168
    }
169
   ],
170
   "source": [
171
    "model = get_yolov7_model(0)\n"
172
   ]
173
  },
174
  {
175
   "cell_type": "code",
176
   "execution_count": 20,
177
   "metadata": {},
178
   "outputs": [
179
    {
180
     "name": "stdout",
181
     "output_type": "stream",
182
     "text": [
183
      "Loading the model...\n",
184
      "Loading model: yolov7-tiny.pt\n",
185
      "Using the yolov7-tiny.pt model\n",
186
      "Done\n"
187
     ]
188
    }
189
   ],
190
   "source": [
191
    "YOLOV7MODEL = os.path.basename(YOLOV7_MODEL[0])\n",
192
    "\n",
193
    "try:\n",
194
    "    print(\"Loading the model...\")\n",
195
    "    model, yolomodel = loading_yolov7_model(yolomodel=YOLOV7MODEL)\n",
196
    "    print(\"Using the\", YOLOV7MODEL, \"model\")\n",
197
    "    print(\"Done\")\n",
198
    "\n",
199
    "except:\n",
200
    "    print(\"[Error] Cannot load the model\", YOLOV7MODEL)"
201
   ]
202
  },
203
  {
204
   "cell_type": "code",
205
   "execution_count": 21,
206
   "metadata": {},
207
   "outputs": [],
208
   "source": [
209
    "def draw_keypoints(output, image, confidence=0.25, threshold=0.65):\n",
210
    "    \"\"\"\n",
211
    "    Draw YoloV7 pose keypoints\n",
212
    "    \"\"\"\n",
213
    "    output = non_max_suppression_kpt(\n",
214
    "        output,\n",
215
    "        confidence,  # Confidence Threshold\n",
216
    "        threshold,  # IoU Threshold\n",
217
    "        nc=model.yaml['nc'],  # Number of Classes\n",
218
    "        nkpt=model.yaml['nkpt'],  # Number of Keypoints\n",
219
    "        kpt_label=True)\n",
220
    "\n",
221
    "    with torch.no_grad():\n",
222
    "        output = output_to_keypoint(output)\n",
223
    "\n",
224
    "    nimg = image[0].permute(1, 2, 0) * 255\n",
225
    "    nimg = cv2.cvtColor(nimg.cpu().numpy().astype(np.uint8), cv2.COLOR_RGB2BGR)\n",
226
    "\n",
227
    "    for idx in range(output.shape[0]):\n",
228
    "        plot_skeleton_kpts(nimg, output[idx, 7:].T, 3)\n",
229
    "\n",
230
    "    return nimg"
231
   ]
232
  },
233
  {
234
   "cell_type": "code",
235
   "execution_count": 25,
236
   "metadata": {},
237
   "outputs": [
238
    {
239
     "name": "stdout",
240
     "output_type": "stream",
241
     "text": [
242
      "True\n"
243
     ]
244
    },
245
    {
246
     "ename": "AttributeError",
247
     "evalue": "'Upsample' object has no attribute 'recompute_scale_factor'",
248
     "output_type": "error",
249
     "traceback": [
250
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
251
      "\u001b[0;31mAttributeError\u001b[0m                            Traceback (most recent call last)",
252
      "Cell \u001b[0;32mIn[25], line 3\u001b[0m\n\u001b[1;32m      1\u001b[0m imagefile \u001b[39m=\u001b[39m \u001b[39m\"\u001b[39m\u001b[39m../assets/messi.jpg\u001b[39m\u001b[39m\"\u001b[39m\n\u001b[1;32m      2\u001b[0m \u001b[39mprint\u001b[39m(os\u001b[39m.\u001b[39mpath\u001b[39m.\u001b[39mexists(imagefile))\n\u001b[0;32m----> 3\u001b[0m output, image \u001b[39m=\u001b[39m running_inference(cv2\u001b[39m.\u001b[39;49mimread(imagefile))\n\u001b[1;32m      4\u001b[0m pose_image \u001b[39m=\u001b[39m draw_keypoints(output, image, confidence\u001b[39m=\u001b[39m\u001b[39m0.25\u001b[39m, threshold\u001b[39m=\u001b[39m\u001b[39m0.65\u001b[39m)\n\u001b[1;32m      6\u001b[0m plt\u001b[39m.\u001b[39mfigure(figsize\u001b[39m=\u001b[39m(\u001b[39m30\u001b[39m, \u001b[39m7\u001b[39m))\n",
253
      "Cell \u001b[0;32mIn[23], line 17\u001b[0m, in \u001b[0;36mrunning_inference\u001b[0;34m(image)\u001b[0m\n\u001b[1;32m     14\u001b[0m image \u001b[39m=\u001b[39m image\u001b[39m.\u001b[39munsqueeze(\u001b[39m0\u001b[39m)  \u001b[39m# torch.Size([1, 3, 567, 960])\u001b[39;00m\n\u001b[1;32m     16\u001b[0m \u001b[39mwith\u001b[39;00m torch\u001b[39m.\u001b[39mno_grad():\n\u001b[0;32m---> 17\u001b[0m     output, _ \u001b[39m=\u001b[39m model(image)\n\u001b[1;32m     19\u001b[0m \u001b[39mreturn\u001b[39;00m output, image\n",
254
      "File \u001b[0;32m~/.local/lib/python3.11/site-packages/torch/nn/modules/module.py:1501\u001b[0m, in \u001b[0;36mModule._call_impl\u001b[0;34m(self, *args, **kwargs)\u001b[0m\n\u001b[1;32m   1496\u001b[0m \u001b[39m# If we don't have any hooks, we want to skip the rest of the logic in\u001b[39;00m\n\u001b[1;32m   1497\u001b[0m \u001b[39m# this function, and just call forward.\u001b[39;00m\n\u001b[1;32m   1498\u001b[0m \u001b[39mif\u001b[39;00m \u001b[39mnot\u001b[39;00m (\u001b[39mself\u001b[39m\u001b[39m.\u001b[39m_backward_hooks \u001b[39mor\u001b[39;00m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39m_backward_pre_hooks \u001b[39mor\u001b[39;00m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39m_forward_hooks \u001b[39mor\u001b[39;00m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39m_forward_pre_hooks\n\u001b[1;32m   1499\u001b[0m         \u001b[39mor\u001b[39;00m _global_backward_pre_hooks \u001b[39mor\u001b[39;00m _global_backward_hooks\n\u001b[1;32m   1500\u001b[0m         \u001b[39mor\u001b[39;00m _global_forward_hooks \u001b[39mor\u001b[39;00m _global_forward_pre_hooks):\n\u001b[0;32m-> 1501\u001b[0m     \u001b[39mreturn\u001b[39;00m forward_call(\u001b[39m*\u001b[39;49margs, \u001b[39m*\u001b[39;49m\u001b[39m*\u001b[39;49mkwargs)\n\u001b[1;32m   1502\u001b[0m \u001b[39m# Do not call functions when jit is used\u001b[39;00m\n\u001b[1;32m   1503\u001b[0m full_backward_hooks, non_full_backward_hooks \u001b[39m=\u001b[39m [], []\n",
255
      "File \u001b[0;32m~/Documents/autoposture/repo/landmark_extraction_yolo/models/yolo.py:514\u001b[0m, in \u001b[0;36mModel.forward\u001b[0;34m(self, x, augment, profile)\u001b[0m\n\u001b[1;32m    512\u001b[0m     \u001b[39mreturn\u001b[39;00m torch\u001b[39m.\u001b[39mcat(y, \u001b[39m1\u001b[39m), \u001b[39mNone\u001b[39;00m  \u001b[39m# augmented inference, train\u001b[39;00m\n\u001b[1;32m    513\u001b[0m \u001b[39melse\u001b[39;00m:\n\u001b[0;32m--> 514\u001b[0m     \u001b[39mreturn\u001b[39;00m \u001b[39mself\u001b[39;49m\u001b[39m.\u001b[39;49mforward_once(x, profile)\n",
256
      "File \u001b[0;32m~/Documents/autoposture/repo/landmark_extraction_yolo/models/yolo.py:540\u001b[0m, in \u001b[0;36mModel.forward_once\u001b[0;34m(self, x, profile)\u001b[0m\n\u001b[1;32m    537\u001b[0m         dt\u001b[39m.\u001b[39mappend((time_synchronized() \u001b[39m-\u001b[39m t) \u001b[39m*\u001b[39m \u001b[39m100\u001b[39m)\n\u001b[1;32m    538\u001b[0m         \u001b[39mprint\u001b[39m(\u001b[39m'\u001b[39m\u001b[39m%10.1f\u001b[39;00m\u001b[39m%10.0f\u001b[39;00m\u001b[39m%10.1f\u001b[39;00m\u001b[39mms \u001b[39m\u001b[39m%-40s\u001b[39;00m\u001b[39m'\u001b[39m \u001b[39m%\u001b[39m (o, m\u001b[39m.\u001b[39mnp, dt[\u001b[39m-\u001b[39m\u001b[39m1\u001b[39m], m\u001b[39m.\u001b[39mtype))\n\u001b[0;32m--> 540\u001b[0m     x \u001b[39m=\u001b[39m m(x)  \u001b[39m# run\u001b[39;00m\n\u001b[1;32m    542\u001b[0m     y\u001b[39m.\u001b[39mappend(x \u001b[39mif\u001b[39;00m m\u001b[39m.\u001b[39mi \u001b[39min\u001b[39;00m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39msave \u001b[39melse\u001b[39;00m \u001b[39mNone\u001b[39;00m)  \u001b[39m# save output\u001b[39;00m\n\u001b[1;32m    544\u001b[0m \u001b[39mif\u001b[39;00m profile:\n",
257
      "File \u001b[0;32m~/.local/lib/python3.11/site-packages/torch/nn/modules/module.py:1501\u001b[0m, in \u001b[0;36mModule._call_impl\u001b[0;34m(self, *args, **kwargs)\u001b[0m\n\u001b[1;32m   1496\u001b[0m \u001b[39m# If we don't have any hooks, we want to skip the rest of the logic in\u001b[39;00m\n\u001b[1;32m   1497\u001b[0m \u001b[39m# this function, and just call forward.\u001b[39;00m\n\u001b[1;32m   1498\u001b[0m \u001b[39mif\u001b[39;00m \u001b[39mnot\u001b[39;00m (\u001b[39mself\u001b[39m\u001b[39m.\u001b[39m_backward_hooks \u001b[39mor\u001b[39;00m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39m_backward_pre_hooks \u001b[39mor\u001b[39;00m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39m_forward_hooks \u001b[39mor\u001b[39;00m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39m_forward_pre_hooks\n\u001b[1;32m   1499\u001b[0m         \u001b[39mor\u001b[39;00m _global_backward_pre_hooks \u001b[39mor\u001b[39;00m _global_backward_hooks\n\u001b[1;32m   1500\u001b[0m         \u001b[39mor\u001b[39;00m _global_forward_hooks \u001b[39mor\u001b[39;00m _global_forward_pre_hooks):\n\u001b[0;32m-> 1501\u001b[0m     \u001b[39mreturn\u001b[39;00m forward_call(\u001b[39m*\u001b[39;49margs, \u001b[39m*\u001b[39;49m\u001b[39m*\u001b[39;49mkwargs)\n\u001b[1;32m   1502\u001b[0m \u001b[39m# Do not call functions when jit is used\u001b[39;00m\n\u001b[1;32m   1503\u001b[0m full_backward_hooks, non_full_backward_hooks \u001b[39m=\u001b[39m [], []\n",
258
      "File \u001b[0;32m~/.local/lib/python3.11/site-packages/torch/nn/modules/upsampling.py:157\u001b[0m, in \u001b[0;36mUpsample.forward\u001b[0;34m(self, input)\u001b[0m\n\u001b[1;32m    155\u001b[0m \u001b[39mdef\u001b[39;00m \u001b[39mforward\u001b[39m(\u001b[39mself\u001b[39m, \u001b[39minput\u001b[39m: Tensor) \u001b[39m-\u001b[39m\u001b[39m>\u001b[39m Tensor:\n\u001b[1;32m    156\u001b[0m     \u001b[39mreturn\u001b[39;00m F\u001b[39m.\u001b[39minterpolate(\u001b[39minput\u001b[39m, \u001b[39mself\u001b[39m\u001b[39m.\u001b[39msize, \u001b[39mself\u001b[39m\u001b[39m.\u001b[39mscale_factor, \u001b[39mself\u001b[39m\u001b[39m.\u001b[39mmode, \u001b[39mself\u001b[39m\u001b[39m.\u001b[39malign_corners,\n\u001b[0;32m--> 157\u001b[0m                          recompute_scale_factor\u001b[39m=\u001b[39m\u001b[39mself\u001b[39;49m\u001b[39m.\u001b[39;49mrecompute_scale_factor)\n",
259
      "File \u001b[0;32m~/.local/lib/python3.11/site-packages/torch/nn/modules/module.py:1614\u001b[0m, in \u001b[0;36mModule.__getattr__\u001b[0;34m(self, name)\u001b[0m\n\u001b[1;32m   1612\u001b[0m     \u001b[39mif\u001b[39;00m name \u001b[39min\u001b[39;00m modules:\n\u001b[1;32m   1613\u001b[0m         \u001b[39mreturn\u001b[39;00m modules[name]\n\u001b[0;32m-> 1614\u001b[0m \u001b[39mraise\u001b[39;00m \u001b[39mAttributeError\u001b[39;00m(\u001b[39m\"\u001b[39m\u001b[39m'\u001b[39m\u001b[39m{}\u001b[39;00m\u001b[39m'\u001b[39m\u001b[39m object has no attribute \u001b[39m\u001b[39m'\u001b[39m\u001b[39m{}\u001b[39;00m\u001b[39m'\u001b[39m\u001b[39m\"\u001b[39m\u001b[39m.\u001b[39mformat(\n\u001b[1;32m   1615\u001b[0m     \u001b[39mtype\u001b[39m(\u001b[39mself\u001b[39m)\u001b[39m.\u001b[39m\u001b[39m__name__\u001b[39m, name))\n",
260
      "\u001b[0;31mAttributeError\u001b[0m: 'Upsample' object has no attribute 'recompute_scale_factor'"
261
     ]
262
    }
263
   ],
264
   "source": [
265
    "imagefile = \"../assets/messi.jpg\"\n",
266
    "output, image = running_inference(cv2.imread(imagefile))\n",
267
    "pose_image = draw_keypoints(output, image, confidence=0.25, threshold=0.65)\n",
268
    "\n",
269
    "plt.figure(figsize=(30, 7))\n",
270
    "plt.axis(\"off\")\n",
271
    "plt.imshow(pose_image)"
272
   ]
273
  },
274
  {
275
   "cell_type": "code",
276
   "execution_count": null,
277
   "metadata": {},
278
   "outputs": [],
279
   "source": [
280
    "output, image = running_inference(cv2.imread(imagefile))\n",
281
    "pose_image = draw_keypoints(output, image, confidence=0.25, threshold=0.65)\n",
282
    "\n",
283
    "plt.figure(figsize=(30, 7))\n",
284
    "plt.axis(\"off\")\n",
285
    "plt.imshow(pose_image)"
286
   ]
287
  }
288
 ],
289
 "metadata": {
290
  "kernelspec": {
291
   "display_name": "Python 3",
292
   "language": "python",
293
   "name": "python3"
294
  },
295
  "language_info": {
296
   "codemirror_mode": {
297
    "name": "ipython",
298
    "version": 3
299
   },
300
   "file_extension": ".py",
301
   "mimetype": "text/x-python",
302
   "name": "python",
303
   "nbconvert_exporter": "python",
304
   "pygments_lexer": "ipython3",
305
   "version": "3.11.3"
306
  },
307
  "orig_nbformat": 4
308
 },
309
 "nbformat": 4,
310
 "nbformat_minor": 2
311
}