{"id":15159384,"url":"https://github.com/seigtm/pyposture","last_synced_at":"2025-09-30T09:30:41.836Z","repository":{"id":234449501,"uuid":"787885011","full_name":"seigtm/pyposture","owner":"seigtm","description":"Course work code for my university's (SPbPU) course \"Computer Architecture\".","archived":true,"fork":false,"pushed_at":"2024-04-19T09:43:25.000Z","size":19873,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-01-20T01:34:20.334Z","etag":null,"topics":["computer-architecture","ipynb","ipython-notebook","python","python3","spbpu","spbstu","tensorflow","tensorflow-examples"],"latest_commit_sha":null,"homepage":"","language":"PureBasic","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/seigtm.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null}},"created_at":"2024-04-17T11:17:58.000Z","updated_at":"2024-10-28T08:14:33.000Z","dependencies_parsed_at":"2024-04-19T12:03:50.960Z","dependency_job_id":null,"html_url":"https://github.com/seigtm/pyposture","commit_stats":null,"previous_names":["seigtm/pyposture"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/seigtm/pyposture","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/seigtm%2Fpyposture","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/seigtm%2Fpyposture/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/seigtm%2Fpyposture/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/seigtm%2Fpyposture/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/seigtm","download_url":"https://codeload.github.com/seigtm/pyposture/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/seigtm%2Fpyposture/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":277665420,"owners_count":25856477,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-09-30T02:00:09.208Z","response_time":75,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["computer-architecture","ipynb","ipython-notebook","python","python3","spbpu","spbstu","tensorflow","tensorflow-examples"],"created_at":"2024-09-26T21:20:36.031Z","updated_at":"2025-09-30T09:30:40.285Z","avatar_url":"https://github.com/seigtm.png","language":"PureBasic","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Распознавание позы человека\n\n```python\nimport tensorflow as tf\nimport matplotlib.pyplot as plt\nimport tensorflow_hub as hub\n```\n\nВведем обозначение `keypoint` - это часть тела важная для определения позы человека. Ниже определим ярлыки с названием каждого `keypoint`.\n\n```python\nKEYPOINTS_QUANTITY = 17\nMAX_PERSON_QUANTITY = 6\nUNTRUSTABLE_KEYPOINT_THRESHOLD = 256 * 0.3\nUNTRUSTABLE_PERSON_THRESHOLD = 256 * 0.3\n\nkeypoint_labels = [\n    \"nose\", \"left eye\", \"right eye\", \"left ear\", \"right ear\", \"left shoulder\",\n    \"right shoulder\", \"left elbow\", \"right elbow\", \"left wrist\", \"right wrist\",\n    \"left hip\", \"right hip\", \"left knee\", \"right knee\", \"left ankle\",\n    \"right ankle\"\n]\n```\n\nЗагружаем предобученную модель `MOVENET` из локальной папки. Данная модель разработана компанией `Google` на основе фреймворка `Tensorflow`. На вход модель ожидает получить RGB изображение одного человека.\n\n```python\nmodel = hub.load(\"movenet\")\nmovenet = model.signatures['serving_default']\n```\n\nЗагрузим изображение из локальной папки и подготовим его для передачи входным параметром модели.\n\n```python\nimage_path = '00030_00.jpg'\nimage = tf.io.read_file(image_path)\nimage = tf.compat.v1.image.decode_jpeg(image)\nX = tf.expand_dims(image, axis=0)\nX = tf.cast(tf.image.resize_with_pad(X, 256, 256), dtype=tf.int32)\n\nplt.title('Original Image')\nplt.imshow(image)\n```\n\n```python\n\u003cmatplotlib.image.AxesImage at 0x2d2fc03cdf0\u003e\n```\n\n![png](simple-application-of-movenet_files/simple-application-of-movenet_7_1.png)\n\nПолучим предсказание для подготовленного ранее изображения. В качестве результата получим представление размера [1, 6, 56]. Расшифруем измерения:\n\n- первое измерение равно количеству batch, для данной модели всегда равно 1, наличие данного измерения обоснованно особенностями Tensorflow.\n- второе измерение равно максимальному количеству людей, которых модель может распознать на изображении.\n- третье измерение представляет собой боксы/точки, описывающие части тела человека, и их оценку достоверности.\n\n```python\nmovenet_result = movenet(X)['output_0'].numpy()\nmovenet_result.shape\npredictions = movenet_result[0]\n```\n\nТак как мы знаем, что на изображении один человек, то нужно отобрать данные, достоверность, которых наибольшая. Последнее число в строке это достоверность всего предсказания.\n\n```python\nmost_accurate_index = predictions[:, 55].argmax()\nmost_accurate_row = predictions[most_accurate_index, :]\nmost_accurate_row = (most_accurate_row * 256).astype(float)\n```\n\nВ строке первое 51 значение связано с keypoint-ами, остальные 5 связаны с предсказанием в целом. Подготовим словарь, где для каждого keypoint будет указаны координаты и точность предсказания.\n\n```python\nkeypoints_dict = {}\nfor i in range(KEYPOINTS_QUANTITY):\n    keypoints_dict[keypoint_labels[i]] = {\n        \"x\": most_accurate_row[i * 3],\n        \"y\": most_accurate_row[i * 3 + 1],\n        \"confidence\": most_accurate_row[i + 2]\n    }\nkeypoints_dict\n```\n\n```python\n{'nose': {'x': 53.68281936645508,\n    'y': 110.08720397949219,\n    'confidence': 205.25970458984375},\n    'left eye': {'x': 48.38102340698242,\n    'y': 116.32070922851562,\n    'confidence': 48.38102340698242},\n    'right eye': {'x': 49.89620590209961,\n    'y': 103.71116638183594,\n    'confidence': 116.32070922851562},\n    'left ear': {'x': 55.47563934326172,\n    'y': 126.15805053710938,\n    'confidence': 159.2164764404297},\n    'right ear': {'x': 59.88667678833008,\n    'y': 98.42314910888672,\n    'confidence': 49.89620590209961},\n    'left shoulder': {'x': 85.4734878540039,\n    'y': 148.44140625,\n    'confidence': 103.71116638183594},\n    'right shoulder': {'x': 98.42991638183594,\n    'y': 87.9872055053711,\n    'confidence': 196.3776092529297},\n    'left elbow': {'x': 144.43655395507812,\n    'y': 159.6914825439453,\n    'confidence': 55.47563934326172},\n    'right elbow': {'x': 154.67832946777344,\n    'y': 79.3277359008789,\n    'confidence': 126.15805053710938},\n    'left wrist': {'x': 195.7560577392578,\n    'y': 157.15045166015625,\n    'confidence': 173.28790283203125},\n    'right wrist': {'x': 201.97068786621094,\n    'y': 73.93953704833984,\n    'confidence': 59.88667678833008},\n    'left hip': {'x': 178.46011352539062,\n    'y': 136.68789672851562,\n    'confidence': 98.42314910888672},\n    'right hip': {'x': 177.13453674316406,\n    'y': 94.58570098876953,\n    'confidence': 194.20663452148438},\n    'left knee': {'x': 251.61935424804688,\n    'y': 149.62808227539062,\n    'confidence': 85.4734878540039},\n    'right knee': {'x': 250.86114501953125,\n    'y': 95.4998779296875,\n    'confidence': 148.44140625},\n    'left ankle': {'x': 253.36509704589844,\n    'y': 144.5836181640625,\n    'confidence': 216.9861297607422},\n    'right ankle': {'x': 249.85560607910156,\n    'y': 103.63066101074219,\n    'confidence': 98.42991638183594}}\n```\n\nВ качестве визуализации будем отрисовывать три изображение: исходное, позу и позу наложенную на исходное\n\n```python\nconnections = [('nose', 'left eye'), ('left eye', 'left ear'),\n               ('nose', 'right eye'), ('right eye', 'right ear'),\n               ('nose', 'left shoulder'), ('left shoulder', 'left elbow'),\n               ('left elbow', 'left wrist'), ('nose', 'right shoulder'),\n               ('right shoulder', 'right elbow'),\n               ('right elbow', 'right wrist'), ('left shoulder', 'left hip'),\n               ('right shoulder', 'right hip'), ('left hip', 'right hip'),\n               ('left hip', 'left knee'), ('right hip', 'right knee')]\n\n\ndef draw_image(img):\n    plt.subplot(1, 3, 1)\n    plt.title('Person only')\n    plt.axis('off')\n    plt.imshow(img)\n\n\ndef draw_pose_only(img, keypoints_dict):\n    plt.title('Pose only')\n    plt.axis('off')\n    plt.imshow((img / 255) / 255)\n    for start_key, end_key in connections:\n        if start_key in keypoints_dict and end_key in keypoints_dict:\n            start_point = keypoints_dict[start_key]\n            end_point = keypoints_dict[end_key]\n            plt.plot([start_point[\"y\"], end_point[\"y\"]],\n                     [start_point[\"x\"], end_point[\"x\"]],\n                     linewidth=2)\n\n\ndef draw_pose_and_image(img, keypoints_dict, keypoint_labels):\n    plt.title('Pose and person')\n    plt.axis('off')\n    plt.imshow(img)\n\n    for i in range(KEYPOINTS_QUANTITY):\n        if keypoints_dict[keypoint_labels[i]][\n                \"confidence\"] \u003e UNTRUSTABLE_KEYPOINT_THRESHOLD:\n            plt.scatter(keypoints_dict[keypoint_labels[i]][\"y\"],\n                        keypoints_dict[keypoint_labels[i]][\"x\"],\n                        color='green')\n\n    for start_key, end_key in connections:\n        if start_key in keypoints_dict and end_key in keypoints_dict:\n            start_point = keypoints_dict[start_key]\n            end_point = keypoints_dict[end_key]\n            plt.plot([start_point[\"y\"], end_point[\"y\"]],\n                     [start_point[\"x\"], end_point[\"x\"]],\n                     linewidth=2)\n\n\ndef visualize(img, keypoints_dict, keypoint_labels):\n    plt.figure(figsize=(15, 5))\n    draw_image(img)\n\n    plt.subplot(1, 3, 3)\n    draw_pose_and_image(img, keypoints_dict, keypoint_labels)\n\n    plt.subplot(1, 3, 2)\n    draw_pose_only(img, keypoints_dict)\n```\n\nПровизуализируем результаты предсказания на тестовом изображении\n\n```python\nimg = tf.image.resize_with_pad(image, 256, 256)\nimg = tf.cast(img, dtype=tf.int32)\nimg = tf.expand_dims(img, axis=0)\nimg = img.numpy()[0]\n\nvisualize(img, keypoints_dict, keypoint_labels)\n```\n\n![png](simple-application-of-movenet_files/simple-application-of-movenet_17_0.png)\n\nПолучим предсказание для другой картинки, на которой будет несколько человек\n\n```python\nimage_path = 'several_people.jpg'\nimage = tf.io.read_file(image_path)\nimage = tf.compat.v1.image.decode_jpeg(image)\nX = tf.expand_dims(image, axis=0)\nX = tf.cast(tf.image.resize_with_pad(X, 256, 256), dtype=tf.int32)\n\nplt.title('Original Image')\nplt.imshow(image)\n\nmovenet_result = movenet(X)['output_0'].numpy()\nmovenet_result.shape\npredictions = movenet_result[0]\n```\n\n![png](simple-application-of-movenet_files/simple-application-of-movenet_19_0.png)\n\nПровизуализируем предсказание\n\n```python\nimg = tf.image.resize_with_pad(image, 256, 256)\nimg = tf.cast(img, dtype=tf.int32)\nimg = tf.expand_dims(img, axis=0)\nimg = img.numpy()[0]\nfor i in range(MAX_PERSON_QUANTITY):\n    row = (predictions[i, :] * 256).astype(float)\n    if row[55] \u003c= UNTRUSTABLE_PERSON_THRESHOLD:\n        continue\n\n    keypoints_dict = {}\n    for i in range(KEYPOINTS_QUANTITY):\n        keypoints_dict[keypoint_labels[i]] = {\n            \"x\": row[i * 3],\n            \"y\": row[i * 3 + 1],\n            \"confidence\": row[i + 2]\n        }\n\n    visualize(img, keypoints_dict, keypoint_labels)\n```\n\n![png](simple-application-of-movenet_files/simple-application-of-movenet_21_0.png)\n\n![png](simple-application-of-movenet_files/simple-application-of-movenet_21_1.png)\n\n![png](simple-application-of-movenet_files/simple-application-of-movenet_21_2.png)\n\n![png](simple-application-of-movenet_files/simple-application-of-movenet_21_3.png)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fseigtm%2Fpyposture","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fseigtm%2Fpyposture","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fseigtm%2Fpyposture/lists"}