{
  "id": 435167,
  "title": "From Parquet to animated Landmarks",
  "url": "/competitions/asl-fingerspelling/discussion/435167",
  "author_name": "Yassine Alouini",
  "post_date": "2023-08-28T08:14:45.424000",
  "votes": 2,
  "comment_count": 0,
  "views": 0,
  "content": "<p>As promised, here is the code to transform the Parquet files into animated outputs.</p>\n<p>Here is the code for processing a Parquet file:</p>\n<pre><code> pandas  pd\n\nlandamarks_path = \nlandmarks_df = pd.read_parquet(landamarks_path)\n\n ():\n    df = (landmarks_df.loc[sequence_id]\n                        .loc[ df: df[] == frame]\n                        .loc[:,  df: df.columns..contains(landmark)]\n                        .reset_index(drop=))\n    d = df.stack().to_dict()\n    data = []\n     k, v  d.items():\n        coordinate = k[].split()[]\n        index = k[].split()[-]\n        data.append({: coordinate, : index, : v})\n\n    df = pd.DataFrame(data)\n     df.empty:\n         []\n    landmarks = []\n     g, v  df.groupby():\n        landmarks.insert((g), v.set_index()\n                                  .to_dict()[])\n     landmarks\n</code></pre>\n<p>To plot the output:</p>\n<pre><code> numpy  np\n matplotlib.pylab  plt\nleft_landmarks = get_landmarks(landmark=, frame=)\nright_landmarks = get_landmarks(landmark=, frame=)\nimg =  * np.zeros((, , )).astype(np.uint8)\nimg = draw_hand_landmarks_on_image(img, [left_landmarks, right_landmarks])\nplt.imshow(img)\n</code></pre>\n<p>Here is one result:</p>\n<p><img src=\"https://www.googleapis.com/download/storage/v1/b/kaggle-forum-message-attachments/o/inbox%2F172860%2Fbc028d57391c19187000fd509665a6eb%2FScreenshot%202023-08-28%20101141.png?generation=1693210557205905&amp;alt=media\" alt=\"\"></p>\n<p>Finally, to animate the \"hand\" and \"pose\" landmarks for example:</p>\n<pre><code>hand_img =  * np.zeros((, , )).astype(np.uint8)\nhand_img = np.stack([draw_hand_landmarks_on_image(hand_img, [get_landmarks(sequence_id=sequence_id, \n                                                           frame=frame_id, \n                                                           landmark=), \n                                                        get_landmarks(sequence_id=sequence_id, \n                                                           frame=frame_id, \n                                                           landmark=)\n                                                       ])  frame_id  ()])\n\nfig = px.imshow(hand_img, animation_frame=, binary_string=, labels=(animation_frame=))\nfig.show()\n</code></pre>\n<pre><code>pose_img =  * np.zeros((, , )).astype(np.uint8)\npose_img = np.stack([draw_pose_landmark_on_image(pose_img, get_landmarks(sequence_id=sequence_id, \n                                                           frame=frame_id, \n                                                           landmark=)\n                                                       )  frame_id  ()])\n\n\nfig = px.imshow(pose_img, animation_frame=, binary_string=, labels=(animation_frame=))\nfig.show()\n</code></pre>\n<p>You can also combine both to get a single animation:</p>\n<pre><code>img = np.stack([pose_img, hand_img]).(axis=)\nfig = px.imshow(img, animation_frame=, binary_string=, labels=(animation_frame=))\nfig.show()\n</code></pre>\n<p>An example of the animation:</p>\n<p><img src=\"https://www.googleapis.com/download/storage/v1/b/kaggle-forum-message-attachments/o/inbox%2F172860%2F5cced33f147e2b348c3b6663b8691c25%2FScreenshot%202023-08-28%20102001.png?generation=1693210868873734&amp;alt=media\" alt=\"\"></p>\n<p>The remaining utility code could be find in the following <a href=\"https://www.kaggle.com/code/yassinealouini/from-parquet-to-animated-landmarks/edit/run/141232062\" target=\"_blank\">notebook</a>.</p>\n<p>I hope this is useful to some.</p>",
  "messages": [
    {
      "id": 2412344,
      "postDate": "2023-08-28T08:14:45.423Z",
      "content": "<p>As promised, here is the code to transform the Parquet files into animated outputs.</p>\n<p>Here is the code for processing a Parquet file:</p>\n<pre><code> pandas  pd\n\nlandamarks_path = \nlandmarks_df = pd.read_parquet(landamarks_path)\n\n ():\n    df = (landmarks_df.loc[sequence_id]\n                        .loc[ df: df[] == frame]\n                        .loc[:,  df: df.columns..contains(landmark)]\n                        .reset_index(drop=))\n    d = df.stack().to_dict()\n    data = []\n     k, v  d.items():\n        coordinate = k[].split()[]\n        index = k[].split()[-]\n        data.append({: coordinate, : index, : v})\n\n    df = pd.DataFrame(data)\n     df.empty:\n         []\n    landmarks = []\n     g, v  df.groupby():\n        landmarks.insert((g), v.set_index()\n                                  .to_dict()[])\n     landmarks\n</code></pre>\n<p>To plot the output:</p>\n<pre><code> numpy  np\n matplotlib.pylab  plt\nleft_landmarks = get_landmarks(landmark=, frame=)\nright_landmarks = get_landmarks(landmark=, frame=)\nimg =  * np.zeros((, , )).astype(np.uint8)\nimg = draw_hand_landmarks_on_image(img, [left_landmarks, right_landmarks])\nplt.imshow(img)\n</code></pre>\n<p>Here is one result:</p>\n<p><img src=\"https://www.googleapis.com/download/storage/v1/b/kaggle-forum-message-attachments/o/inbox%2F172860%2Fbc028d57391c19187000fd509665a6eb%2FScreenshot%202023-08-28%20101141.png?generation=1693210557205905&amp;alt=media\" alt=\"\"></p>\n<p>Finally, to animate the \"hand\" and \"pose\" landmarks for example:</p>\n<pre><code>hand_img =  * np.zeros((, , )).astype(np.uint8)\nhand_img = np.stack([draw_hand_landmarks_on_image(hand_img, [get_landmarks(sequence_id=sequence_id, \n                                                           frame=frame_id, \n                                                           landmark=), \n                                                        get_landmarks(sequence_id=sequence_id, \n                                                           frame=frame_id, \n                                                           landmark=)\n                                                       ])  frame_id  ()])\n\nfig = px.imshow(hand_img, animation_frame=, binary_string=, labels=(animation_frame=))\nfig.show()\n</code></pre>\n<pre><code>pose_img =  * np.zeros((, , )).astype(np.uint8)\npose_img = np.stack([draw_pose_landmark_on_image(pose_img, get_landmarks(sequence_id=sequence_id, \n                                                           frame=frame_id, \n                                                           landmark=)\n                                                       )  frame_id  ()])\n\n\nfig = px.imshow(pose_img, animation_frame=, binary_string=, labels=(animation_frame=))\nfig.show()\n</code></pre>\n<p>You can also combine both to get a single animation:</p>\n<pre><code>img = np.stack([pose_img, hand_img]).(axis=)\nfig = px.imshow(img, animation_frame=, binary_string=, labels=(animation_frame=))\nfig.show()\n</code></pre>\n<p>An example of the animation:</p>\n<p><img src=\"https://www.googleapis.com/download/storage/v1/b/kaggle-forum-message-attachments/o/inbox%2F172860%2F5cced33f147e2b348c3b6663b8691c25%2FScreenshot%202023-08-28%20102001.png?generation=1693210868873734&amp;alt=media\" alt=\"\"></p>\n<p>The remaining utility code could be find in the following <a href=\"https://www.kaggle.com/code/yassinealouini/from-parquet-to-animated-landmarks/edit/run/141232062\" target=\"_blank\">notebook</a>.</p>\n<p>I hope this is useful to some.</p>",
      "rawMarkdown": "As promised, here is the code to transform the Parquet files into animated outputs.\n\nHere is the code for processing a Parquet file:\n\n\n```python\n\nimport pandas as pd\n\nlandamarks_path = \"\"\nlandmarks_df = pd.read_parquet(landamarks_path)\n\ndef get_landmarks(landmark=\"right\", \n                  frame=0, \n                  sequence_id=1975541698):\n    df = (landmarks_df.loc[sequence_id]\n                        .loc[lambda df: df[\"frame\"] == frame]\n                        .loc[:, lambda df: df.columns.str.contains(landmark)]\n                        .reset_index(drop=True))\n    d = df.stack().to_dict()\n    data = []\n    for k, v in d.items():\n        coordinate = k[1].split(\"_\")[0]\n        index = k[1].split(\"_\")[-1]\n        data.append({\"coordinate\": coordinate, \"index\": index, \"value\": v})\n    \n    df = pd.DataFrame(data)\n    if df.empty:\n        return []\n    landmarks = []\n    for g, v in df.groupby(\"index\"):\n        landmarks.insert(int(g), v.set_index(\"coordinate\")\n                                  .to_dict()[\"value\"])\n    return landmarks\n```\n\n\n\nTo plot the output:\n\n\n```python\nimport numpy as np\nimport matplotlib.pylab as plt\nleft_landmarks = get_landmarks(landmark=\"left\", frame=0)\nright_landmarks = get_landmarks(landmark=\"right\", frame=0)\nimg = 255 * np.zeros((512, 256, 3)).astype(np.uint8)\nimg = draw_hand_landmarks_on_image(img, [left_landmarks, right_landmarks])\nplt.imshow(img)\n```\n\nHere is one result:\n\n![](https://www.googleapis.com/download/storage/v1/b/kaggle-forum-message-attachments/o/inbox%2F172860%2Fbc028d57391c19187000fd509665a6eb%2FScreenshot%202023-08-28%20101141.png?generation=1693210557205905&alt=media)\n\n\nFinally, to animate the \"hand\" and \"pose\" landmarks for example:\n\n```python\nhand_img = 255 * np.zeros((512, 256, 3)).astype(np.uint8)\nhand_img = np.stack([draw_hand_landmarks_on_image(hand_img, [get_landmarks(sequence_id=sequence_id, \n                                                           frame=frame_id, \n                                                           landmark=\"left\"), \n                                                        get_landmarks(sequence_id=sequence_id, \n                                                           frame=frame_id, \n                                                           landmark=\"right\")\n                                                       ]) for frame_id in range(2)])\n\nfig = px.imshow(hand_img, animation_frame=0, binary_string=True, labels=dict(animation_frame=\"slice\"))\nfig.show()\n```\n\n```python\n\npose_img = 255 * np.zeros((512, 256, 3)).astype(np.uint8)\npose_img = np.stack([draw_pose_landmark_on_image(pose_img, get_landmarks(sequence_id=sequence_id, \n                                                           frame=frame_id, \n                                                           landmark=\"pose\")\n                                                       ) for frame_id in range(10)])\n\n\nfig = px.imshow(pose_img, animation_frame=0, binary_string=True, labels=dict(animation_frame=\"slice\"))\nfig.show()\n```\n\n\nYou can also combine both to get a single animation:\n\n```python\nimg = np.stack([pose_img, hand_img]).max(axis=0)\nfig = px.imshow(img, animation_frame=0, binary_string=True, labels=dict(animation_frame=\"slice\"))\nfig.show()\n```\n\nAn example of the animation:\n\n![](https://www.googleapis.com/download/storage/v1/b/kaggle-forum-message-attachments/o/inbox%2F172860%2F5cced33f147e2b348c3b6663b8691c25%2FScreenshot%202023-08-28%20102001.png?generation=1693210868873734&alt=media)\n\n\nThe remaining utility code could be find in the following [notebook](https://www.kaggle.com/code/yassinealouini/from-parquet-to-animated-landmarks/edit/run/141232062).\n\nI hope this is useful to some.\n",
      "votes": 2
    }
  ],
  "comments": [],
  "raw_markdown_by_id": {
    "2412344": "As promised, here is the code to transform the Parquet files into animated outputs.\n\nHere is the code for processing a Parquet file:\n\n\n```python\n\nimport pandas as pd\n\nlandamarks_path = \"\"\nlandmarks_df = pd.read_parquet(landamarks_path)\n\ndef get_landmarks(landmark=\"right\", \n                  frame=0, \n                  sequence_id=1975541698):\n    df = (landmarks_df.loc[sequence_id]\n                        .loc[lambda df: df[\"frame\"] == frame]\n                        .loc[:, lambda df: df.columns.str.contains(landmark)]\n                        .reset_index(drop=True))\n    d = df.stack().to_dict()\n    data = []\n    for k, v in d.items():\n        coordinate = k[1].split(\"_\")[0]\n        index = k[1].split(\"_\")[-1]\n        data.append({\"coordinate\": coordinate, \"index\": index, \"value\": v})\n    \n    df = pd.DataFrame(data)\n    if df.empty:\n        return []\n    landmarks = []\n    for g, v in df.groupby(\"index\"):\n        landmarks.insert(int(g), v.set_index(\"coordinate\")\n                                  .to_dict()[\"value\"])\n    return landmarks\n```\n\n\n\nTo plot the output:\n\n\n```python\nimport numpy as np\nimport matplotlib.pylab as plt\nleft_landmarks = get_landmarks(landmark=\"left\", frame=0)\nright_landmarks = get_landmarks(landmark=\"right\", frame=0)\nimg = 255 * np.zeros((512, 256, 3)).astype(np.uint8)\nimg = draw_hand_landmarks_on_image(img, [left_landmarks, right_landmarks])\nplt.imshow(img)\n```\n\nHere is one result:\n\n![](https://www.googleapis.com/download/storage/v1/b/kaggle-forum-message-attachments/o/inbox%2F172860%2Fbc028d57391c19187000fd509665a6eb%2FScreenshot%202023-08-28%20101141.png?generation=1693210557205905&alt=media)\n\n\nFinally, to animate the \"hand\" and \"pose\" landmarks for example:\n\n```python\nhand_img = 255 * np.zeros((512, 256, 3)).astype(np.uint8)\nhand_img = np.stack([draw_hand_landmarks_on_image(hand_img, [get_landmarks(sequence_id=sequence_id, \n                                                           frame=frame_id, \n                                                           landmark=\"left\"), \n                                                        get_landmarks(sequence_id=sequence_id, \n                                                           frame=frame_id, \n                                                           landmark=\"right\")\n                                                       ]) for frame_id in range(2)])\n\nfig = px.imshow(hand_img, animation_frame=0, binary_string=True, labels=dict(animation_frame=\"slice\"))\nfig.show()\n```\n\n```python\n\npose_img = 255 * np.zeros((512, 256, 3)).astype(np.uint8)\npose_img = np.stack([draw_pose_landmark_on_image(pose_img, get_landmarks(sequence_id=sequence_id, \n                                                           frame=frame_id, \n                                                           landmark=\"pose\")\n                                                       ) for frame_id in range(10)])\n\n\nfig = px.imshow(pose_img, animation_frame=0, binary_string=True, labels=dict(animation_frame=\"slice\"))\nfig.show()\n```\n\n\nYou can also combine both to get a single animation:\n\n```python\nimg = np.stack([pose_img, hand_img]).max(axis=0)\nfig = px.imshow(img, animation_frame=0, binary_string=True, labels=dict(animation_frame=\"slice\"))\nfig.show()\n```\n\nAn example of the animation:\n\n![](https://www.googleapis.com/download/storage/v1/b/kaggle-forum-message-attachments/o/inbox%2F172860%2F5cced33f147e2b348c3b6663b8691c25%2FScreenshot%202023-08-28%20102001.png?generation=1693210868873734&alt=media)\n\n\nThe remaining utility code could be find in the following [notebook](https://www.kaggle.com/code/yassinealouini/from-parquet-to-animated-landmarks/edit/run/141232062).\n\nI hope this is useful to some.\n"
  }
}