diff --git a/Footface.ipynb b/Footface.ipynb new file mode 100644 index 0000000..7ea5b77 --- /dev/null +++ b/Footface.ipynb @@ -0,0 +1,510 @@ +{ + "nbformat": 4, + "nbformat_minor": 0, + "metadata": { + "colab": { + "name": "Footface.ipynb", + "provenance": [], + "authorship_tag": "ABX9TyM0NDzOYgLViKyGkJQ2KyCH", + "include_colab_link": true + }, + "kernelspec": { + "name": "python3", + "display_name": "Python 3" + }, + "accelerator": "GPU", + "widgets": { + "application/vnd.jupyter.widget-state+json": { + "59322c1bcd974f4bba2b9db2e7b1dcb0": { + "model_module": "@jupyter-widgets/controls", + "model_name": "HBoxModel", + "state": { + "_view_name": "HBoxView", + "_dom_classes": [], + "_model_name": "HBoxModel", + "_view_module": "@jupyter-widgets/controls", + "_model_module_version": "1.5.0", + "_view_count": null, + "_view_module_version": "1.5.0", + "box_style": "", + "layout": "IPY_MODEL_6878c94ac8834273b9fdb2252a5e8ff2", + "_model_module": "@jupyter-widgets/controls", + "children": [ + "IPY_MODEL_25073ed4d80f42c0b414d1a36b359bdd", + "IPY_MODEL_6c134ab7a38d46ba8a2350c9ab54a1f9" + ] + } + }, + "6878c94ac8834273b9fdb2252a5e8ff2": { + "model_module": "@jupyter-widgets/base", + "model_name": "LayoutModel", + "state": { + "_view_name": "LayoutView", + "grid_template_rows": null, + "right": null, + "justify_content": null, + "_view_module": "@jupyter-widgets/base", + "overflow": null, + "_model_module_version": "1.2.0", + "_view_count": null, + "flex_flow": null, + "width": null, + "min_width": null, + "border": null, + "align_items": null, + "bottom": null, + "_model_module": "@jupyter-widgets/base", + "top": null, + "grid_column": null, + "overflow_y": null, + "overflow_x": null, + "grid_auto_flow": null, + "grid_area": null, + "grid_template_columns": null, + "flex": null, + "_model_name": "LayoutModel", + "justify_items": null, + "grid_row": null, + "max_height": null, + "align_content": null, + "visibility": null, + "align_self": null, + "height": null, + "min_height": null, + "padding": null, + "grid_auto_rows": null, + "grid_gap": null, + "max_width": null, + "order": null, + "_view_module_version": "1.2.0", + "grid_template_areas": null, + "object_position": null, + "object_fit": null, + "grid_auto_columns": null, + "margin": null, + "display": null, + "left": null + } + }, + "25073ed4d80f42c0b414d1a36b359bdd": { + "model_module": "@jupyter-widgets/controls", + "model_name": "FloatProgressModel", + "state": { + "_view_name": "ProgressView", + "style": "IPY_MODEL_e2f1d71ee51b451681c2bdaed80600fa", + "_dom_classes": [], + "description": "100%", + "_model_name": "FloatProgressModel", + "bar_style": "success", + "max": 237034793, + "_view_module": "@jupyter-widgets/controls", + "_model_module_version": "1.5.0", + "value": 237034793, + "_view_count": null, + "_view_module_version": "1.5.0", + "orientation": "horizontal", + "min": 0, + "description_tooltip": null, + "_model_module": "@jupyter-widgets/controls", + "layout": "IPY_MODEL_7d95fa25db0c4107889740ff2bc60068" + } + }, + "6c134ab7a38d46ba8a2350c9ab54a1f9": { + "model_module": "@jupyter-widgets/controls", + "model_name": "HTMLModel", + "state": { + "_view_name": "HTMLView", + "style": "IPY_MODEL_04e36532ffc34886b340b4b9f36fbf25", + "_dom_classes": [], + "description": "", + "_model_name": "HTMLModel", + "placeholder": "​", + "_view_module": "@jupyter-widgets/controls", + "_model_module_version": "1.5.0", + "value": " 226M/226M [00:22<00:00, 10.4MB/s]", + "_view_count": null, + "_view_module_version": "1.5.0", + "description_tooltip": null, + "_model_module": "@jupyter-widgets/controls", + "layout": "IPY_MODEL_c8fda7e8f99e4abdae8f6afba2393966" + } + }, + "e2f1d71ee51b451681c2bdaed80600fa": { + "model_module": "@jupyter-widgets/controls", + "model_name": "ProgressStyleModel", + "state": { + "_view_name": "StyleView", + "_model_name": "ProgressStyleModel", + "description_width": "initial", + "_view_module": "@jupyter-widgets/base", + "_model_module_version": "1.5.0", + "_view_count": null, + "_view_module_version": "1.2.0", + "bar_color": null, + "_model_module": "@jupyter-widgets/controls" + } + }, + "7d95fa25db0c4107889740ff2bc60068": { + "model_module": "@jupyter-widgets/base", + "model_name": "LayoutModel", + "state": { + "_view_name": "LayoutView", + "grid_template_rows": null, + "right": null, + "justify_content": null, + "_view_module": "@jupyter-widgets/base", + "overflow": null, + "_model_module_version": "1.2.0", + "_view_count": null, + "flex_flow": null, + "width": null, + "min_width": null, + "border": null, + "align_items": null, + "bottom": null, + "_model_module": "@jupyter-widgets/base", + "top": null, + "grid_column": null, + "overflow_y": null, + "overflow_x": null, + "grid_auto_flow": null, + "grid_area": null, + "grid_template_columns": null, + "flex": null, + "_model_name": "LayoutModel", + "justify_items": null, + "grid_row": null, + "max_height": null, + "align_content": null, + "visibility": null, + "align_self": null, + "height": null, + "min_height": null, + "padding": null, + "grid_auto_rows": null, + "grid_gap": null, + "max_width": null, + "order": null, + "_view_module_version": "1.2.0", + "grid_template_areas": null, + "object_position": null, + "object_fit": null, + "grid_auto_columns": null, + "margin": null, + "display": null, + "left": null + } + }, + "04e36532ffc34886b340b4b9f36fbf25": { + "model_module": "@jupyter-widgets/controls", + "model_name": "DescriptionStyleModel", + "state": { + "_view_name": "StyleView", + "_model_name": "DescriptionStyleModel", + "description_width": "", + "_view_module": "@jupyter-widgets/base", + "_model_module_version": "1.5.0", + "_view_count": null, + "_view_module_version": "1.2.0", + "_model_module": "@jupyter-widgets/controls" + } + }, + "c8fda7e8f99e4abdae8f6afba2393966": { + "model_module": "@jupyter-widgets/base", + "model_name": "LayoutModel", + "state": { + "_view_name": "LayoutView", + "grid_template_rows": null, + "right": null, + "justify_content": null, + "_view_module": "@jupyter-widgets/base", + "overflow": null, + "_model_module_version": "1.2.0", + "_view_count": null, + "flex_flow": null, + "width": null, + "min_width": null, + "border": null, + "align_items": null, + "bottom": null, + "_model_module": "@jupyter-widgets/base", + "top": null, + "grid_column": null, + "overflow_y": null, + "overflow_x": null, + "grid_auto_flow": null, + "grid_area": null, + "grid_template_columns": null, + "flex": null, + "_model_name": "LayoutModel", + "justify_items": null, + "grid_row": null, + "max_height": null, + "align_content": null, + "visibility": null, + "align_self": null, + "height": null, + "min_height": null, + "padding": null, + "grid_auto_rows": null, + "grid_gap": null, + "max_width": null, + "order": null, + "_view_module_version": "1.2.0", + "grid_template_areas": null, + "object_position": null, + "object_fit": null, + "grid_auto_columns": null, + "margin": null, + "display": null, + "left": null + } + } + } + } + }, + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "id": "view-in-github", + "colab_type": "text" + }, + "source": [ + "\"Open" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "yA9xf_7A1lwl" + }, + "source": [ + "import torch\r\n", + "import torchvision\r\n", + "from torchvision import models\r\n", + "model = models.detection.keypointrcnn_resnet50_fpn(pretrained=True).eval()\r\n", + "scripted_model = torch.jit.script(model)\r\n", + "torch.jit.save(scripted_model, './model_2.pt')" + ], + "execution_count": 3, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "U3WCon74oJK_", + "outputId": "08bd2193-bdf8-4602-df39-90739dea68f3" + }, + "source": [ + "\r\n", + "import torchvision.transforms as T\r\n", + "\r\n", + "import numpy as np\r\n", + "from PIL import Image\r\n", + "import matplotlib.pyplot as plt\r\n", + "from matplotlib.path import Path\r\n", + "import matplotlib.patches as patches\r\n", + "\r\n", + "example = torch.rand(1, 3, 800, 800)\r\n", + "traced_script_module = torch.jit.script(model, example)\r\n", + "traced_script_module.save(\"./converted_model.pt\")" + ], + "execution_count": 6, + "outputs": [ + { + "output_type": "stream", + "text": [ + "/usr/local/lib/python3.6/dist-packages/torch/jit/_script.py:891: UserWarning: `optimize` is deprecated and has no effect. Use `with torch.jit.optimized_execution() instead\n", + " \"`optimize` is deprecated and has no effect. Use `with torch.jit.optimized_execution() instead\"\n" + ], + "name": "stderr" + } + ] + }, + { + "cell_type": "code", + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 458, + "referenced_widgets": [ + "59322c1bcd974f4bba2b9db2e7b1dcb0", + "6878c94ac8834273b9fdb2252a5e8ff2", + "25073ed4d80f42c0b414d1a36b359bdd", + "6c134ab7a38d46ba8a2350c9ab54a1f9", + "e2f1d71ee51b451681c2bdaed80600fa", + "7d95fa25db0c4107889740ff2bc60068", + "04e36532ffc34886b340b4b9f36fbf25", + "c8fda7e8f99e4abdae8f6afba2393966" + ] + }, + "id": "4TVn-BKJhgXx", + "outputId": "d6a3fa71-9596-4604-8e43-f455f771357d" + }, + "source": [ + "import torch\r\n", + "import torchvision\r\n", + "from torchvision import models\r\n", + "import torchvision.transforms as T\r\n", + "\r\n", + "import numpy as np\r\n", + "from PIL import Image\r\n", + "import matplotlib.pyplot as plt\r\n", + "from matplotlib.path import Path\r\n", + "import matplotlib.patches as patches\r\n", + "\r\n", + "\r\n", + "#이미지 사이즈 및 임계값 설정\r\n", + "\r\n", + "THRESHOLD = 0.95\r\n", + "\r\n", + "#model불러오기\r\n", + "model = models.detection.keypointrcnn_resnet50_fpn(pretrained=True).eval()\r\n", + "\r\n", + "#예시(여기가 카메라-> frame (이미지) 로드 해서 동작)\r\n", + "img = Image.open('./ryu.jpg')\r\n", + "\r\n", + "#임의의 thres 정의\r\n", + "box_thres1 = img.size[0] * 0.45\r\n", + "box_thres2 = img.size[0] * 0.55\r\n", + "topthres_1 = img.size[1] * 0.35\r\n", + "topthres_2 = img.size[1] * 0.55\r\n", + "bottomthres = img.size[1] * 0.9\r\n", + "\r\n", + "plt.figure(figsize=(16, 16))\r\n", + "\r\n", + "#텐서로 변환\r\n", + "trf = T.Compose([\r\n", + " T.ToTensor()\r\n", + "])\r\n", + "\r\n", + "input_img = trf(img)\r\n", + "out = model([input_img])[0]\r\n", + "\r\n", + "\r\n", + "fig, ax = plt.subplots(1, figsize=(16, 16))\r\n", + "ax.imshow(img)\r\n", + "\r\n", + "for box, score, keypoints in zip(out['boxes'], out['scores'], out['keypoints']):\r\n", + " score = score.detach().numpy()\r\n", + "\r\n", + " if score < THRESHOLD:\r\n", + " continue\r\n", + "\r\n", + " box = box.detach().numpy()\r\n", + "\r\n", + " #인물의 정가운데 지점 mid_box로 정의.\r\n", + " mid_box = 0.5 * (box[0] + box[2])\r\n", + "\r\n", + " \r\n", + " #y축 기준의 알고리즘\r\n", + " keypoints = keypoints.detach().numpy()[:, :2]\r\n", + " temp_face = keypoints[0][1]\r\n", + " \r\n", + " \r\n", + " #keypoints[15],keypoint[16] 왼쪽 발목, 오른쪽 발목\r\n", + " temp_leftfoot = keypoints[15][1]\r\n", + " temp_rigthfoot = keypoints[16][1]\r\n", + " \r\n", + "\r\n", + "if temp_face >topthres_1 and temp_face < topthres_2:\r\n", + " if temp_leftfoot > bottomthres and temp_rigthfoot > bottomthres:\r\n", + " print(\"Great!! 지금입니다 !! 사진을 찍어 주세요\")\r\n", + " else:\r\n", + " print(\"발을 하단에 맞춰 주세요\")\r\n", + "else:\r\n", + " print(\"얼굴을 가운데로 맞춰주세요\")\r\n", + "if mid_box box_thres2:\r\n", + " print(\"카메라 각도를 오른쪽으로 조금 돌려주세요\")\r\n", + "\r\n", + "\r\n", + "\r\n", + "\r\n" + ], + "execution_count": 2, + "outputs": [ + { + "output_type": "stream", + "text": [ + "Downloading: \"https://download.pytorch.org/models/keypointrcnn_resnet50_fpn_coco-fc266e95.pth\" to /root/.cache/torch/hub/checkpoints/keypointrcnn_resnet50_fpn_coco-fc266e95.pth\n" + ], + "name": "stderr" + }, + { + "output_type": "display_data", + "data": { + "application/vnd.jupyter.widget-view+json": { + "model_id": "59322c1bcd974f4bba2b9db2e7b1dcb0", + "version_minor": 0, + "version_major": 2 + }, + "text/plain": [ + "HBox(children=(FloatProgress(value=0.0, max=237034793.0), HTML(value='')))" + ] + }, + "metadata": { + "tags": [] + } + }, + { + "output_type": "stream", + "text": [ + "\n" + ], + "name": "stdout" + }, + { + "output_type": "error", + "ename": "FileNotFoundError", + "evalue": "ignored", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mFileNotFoundError\u001b[0m Traceback (most recent call last)", + "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 19\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 20\u001b[0m \u001b[0;31m#예시(여기가 카메라-> frame (이미지) 로드 해서 동작)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 21\u001b[0;31m \u001b[0mimg\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mImage\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mopen\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'./ryu.jpg'\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 22\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 23\u001b[0m \u001b[0;31m#임의의 thres 정의\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;32m/usr/local/lib/python3.6/dist-packages/PIL/Image.py\u001b[0m in \u001b[0;36mopen\u001b[0;34m(fp, mode)\u001b[0m\n\u001b[1;32m 2807\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2808\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mfilename\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 2809\u001b[0;31m \u001b[0mfp\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mbuiltins\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mopen\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mfilename\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m\"rb\"\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 2810\u001b[0m \u001b[0mexclusive_fp\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;32mTrue\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2811\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;31mFileNotFoundError\u001b[0m: [Errno 2] No such file or directory: './ryu.jpg'" + ] + } + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "kL4TxTl3-IYU" + }, + "source": [ + "" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "id": "D-BYrTHihUv9" + }, + "source": [ + "out" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "id": "VOxVXibgq2wy" + }, + "source": [ + "" + ], + "execution_count": null, + "outputs": [] + } + ] +} \ No newline at end of file diff --git a/RT_eval.ipynb b/RT_eval.ipynb new file mode 100644 index 0000000..1b86113 --- /dev/null +++ b/RT_eval.ipynb @@ -0,0 +1,166 @@ +{ + "nbformat": 4, + "nbformat_minor": 0, + "metadata": { + "colab": { + "name": "RT_eval.ipynb", + "provenance": [], + "collapsed_sections": [], + "authorship_tag": "ABX9TyMt8tJwkAHzExjVYVGpEL7s", + "include_colab_link": true + }, + "kernelspec": { + "name": "python3", + "display_name": "Python 3" + }, + "accelerator": "GPU" + }, + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "id": "view-in-github", + "colab_type": "text" + }, + "source": [ + "\"Open" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "4TVn-BKJhgXx" + }, + "source": [ + "import torch\r\n", + "import torchvision\r\n", + "from torchvision import models\r\n", + "import torchvision.transforms as T\r\n", + "\r\n", + "import numpy as np\r\n", + "from PIL import Image\r\n", + "import matplotlib.pyplot as plt\r\n", + "from matplotlib.path import Path\r\n", + "import matplotlib.patches as patches\r\n", + "\r\n", + "import glob\r\n", + "import os\r\n", + "\r\n", + "\r\n", + "def get_image_list(image_dir):\r\n", + " if image_dir is not None:\r\n", + " print(\"Loading images from directory : \", image_dir)\r\n", + " images = glob.glob(image_dir + '/*.png')\r\n", + " images += glob.glob(image_dir + '/*.jpg')\r\n", + " images += glob.glob(image_dir + '/*.jpeg')\r\n", + "\r\n", + " else:\r\n", + " raise RuntimeError('Either -img_dir arguments must be passed as argument')\r\n", + "\r\n", + " return images\r\n", + "\r\n", + "\r\n", + "def main():\r\n", + " #이미지 사이즈 및 임계값 설정\r\n", + "\r\n", + " THRESHOLD = 0.95\r\n", + "\r\n", + " #model불러오기\r\n", + " model = models.detection.keypointrcnn_resnet50_fpn(pretrained=True).eval()\r\n", + "\r\n", + " #예시(여기가 카메라-> frame (이미지) 로드 해서 동작)\r\n", + " imgDir_path = 'images/'\r\n", + " img = Image.open(get_image_list(imgDir_path))\r\n", + "\r\n", + " #임의의 thres 정의\r\n", + " box_thres1 = img.size[0] * 0.45\r\n", + " box_thres2 = img.size[0] * 0.55\r\n", + " topthres_1 = img.size[1] * 0.35\r\n", + " topthres_2 = img.size[1] * 0.55\r\n", + " bottomthres = img.size[1] * 0.9\r\n", + "\r\n", + " \r\n", + "\r\n", + " #텐서로 변환\r\n", + " trf = T.Compose([\r\n", + " T.ToTensor()\r\n", + " ])\r\n", + "\r\n", + " input_img = trf(img)\r\n", + " out = model([input_img])[0]\r\n", + "\r\n", + "\r\n", + " for box, score, keypoints in zip(out['boxes'], out['scores'], out['keypoints']):\r\n", + " score = score.detach().numpy()\r\n", + "\r\n", + " if score < THRESHOLD:\r\n", + " continue\r\n", + "\r\n", + " box = box.detach().numpy()\r\n", + "\r\n", + " #인물의 정가운데 지점 mid_box로 정의.\r\n", + " mid_box = 0.5 * (box[0] + box[2])\r\n", + "\r\n", + " \r\n", + " #y축 기준의 알고리즘\r\n", + " keypoints = keypoints.detach().numpy()[:, :2]\r\n", + " temp_face = keypoints[0][1]\r\n", + " \r\n", + " \r\n", + " #keypoints[15],keypoint[16] 왼쪽 발목, 오른쪽 발목\r\n", + " temp_leftfoot = keypoints[15][1]\r\n", + " temp_rigthfoot = keypoints[16][1]\r\n", + " \r\n", + " if temp_face >topthres_1 and temp_face < topthres_2:\r\n", + " if temp_leftfoot > bottomthres and temp_rigthfoot > bottomthres:\r\n", + " sen = \"좋아요!! 지금입니다 !! 사진을 찍어 주세요\"\r\n", + " else:\r\n", + " sen = \"발을 하단에 맞춰 주세요\"\r\n", + " else:\r\n", + " sen = \"얼굴을 가운데로 맞춰주세요\"\r\n", + " if mid_box box_thres2:\r\n", + " sen = \"카메라 각도를 오른쪽으로 조금 돌려주세요\"\r\n", + " \r\n", + " return sen\r\n", + "\r\n" + ], + "execution_count": 5, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "id": "kL4TxTl3-IYU" + }, + "source": [ + "" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "id": "D-BYrTHihUv9" + }, + "source": [ + "out" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "id": "VOxVXibgq2wy" + }, + "source": [ + "" + ], + "execution_count": null, + "outputs": [] + } + ] +} \ No newline at end of file