Recorded Video - Obtaining coordinates and saving to a csv of specific location

parent c537eee6
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "90e3f5ea",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Requirement already satisfied: mediapipe in c:\\users\\isuri\\anaconda3\\lib\\site-packages (0.8.9.1)\n",
"Requirement already satisfied: OpenCV-python in c:\\users\\isuri\\anaconda3\\lib\\site-packages (4.5.5.64)\n",
"Requirement already satisfied: attrs>=19.1.0 in c:\\users\\isuri\\anaconda3\\lib\\site-packages (from mediapipe) (21.2.0)\n",
"Requirement already satisfied: protobuf>=3.11.4 in c:\\users\\isuri\\anaconda3\\lib\\site-packages (from mediapipe) (3.20.1)\n",
"Requirement already satisfied: numpy in c:\\users\\isuri\\anaconda3\\lib\\site-packages (from mediapipe) (1.20.3)\n",
"Requirement already satisfied: opencv-contrib-python in c:\\users\\isuri\\anaconda3\\lib\\site-packages (from mediapipe) (4.5.5.64)\n",
"Requirement already satisfied: absl-py in c:\\users\\isuri\\anaconda3\\lib\\site-packages (from mediapipe) (1.0.0)\n",
"Requirement already satisfied: matplotlib in c:\\users\\isuri\\anaconda3\\lib\\site-packages (from mediapipe) (3.4.3)\n",
"Requirement already satisfied: six in c:\\users\\isuri\\anaconda3\\lib\\site-packages (from absl-py->mediapipe) (1.16.0)\n",
"Requirement already satisfied: pyparsing>=2.2.1 in c:\\users\\isuri\\anaconda3\\lib\\site-packages (from matplotlib->mediapipe) (3.0.4)\n",
"Requirement already satisfied: python-dateutil>=2.7 in c:\\users\\isuri\\anaconda3\\lib\\site-packages (from matplotlib->mediapipe) (2.8.2)\n",
"Requirement already satisfied: cycler>=0.10 in c:\\users\\isuri\\anaconda3\\lib\\site-packages (from matplotlib->mediapipe) (0.10.0)\n",
"Requirement already satisfied: pillow>=6.2.0 in c:\\users\\isuri\\anaconda3\\lib\\site-packages (from matplotlib->mediapipe) (8.4.0)\n",
"Requirement already satisfied: kiwisolver>=1.0.1 in c:\\users\\isuri\\anaconda3\\lib\\site-packages (from matplotlib->mediapipe) (1.3.1)\n"
]
}
],
"source": [
"!pip install mediapipe OpenCV-python"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "b6068954",
"metadata": {},
"outputs": [],
"source": [
"import cv2\n",
"import mediapipe as mp\n",
"import numpy as np\n",
"import matplotlib.pyplot as plt\n",
"from math import cos, sin, pi, floor\n",
"import pandas as pd \n",
"import cv2\n",
"import os\n",
"import csv\n",
"mp_drawing = mp.solutions.drawing_utils\n",
"mp_pose = mp.solutions.pose"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "9c075107",
"metadata": {},
"outputs": [],
"source": [
"#global variables\n",
"\n",
"#storing landmark details\n",
"nose_landmark_full_desc, left_eye_inner_landmark_full_desc, left_eye_landmark_full_desc, left_eye_outer_landmark_full_desc, right_eye_inner_landmark_full_desc, right_eye_landmark_full_desc, right_eye_outer_landmark_full_desc, left_mouth_landmark_full_desc, right_mouth_landmark_full_desc = [], [], [], [], [], [], [], [], []\n"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "17b488db",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[INFO] Processing LRH_video_01...\n"
]
}
],
"source": [
"#processing a video\n",
"\n",
"VIDEO_STREAM = 'C:\\\\Users\\\\isuri\\\\Documents\\\\SLIIT\\\\4th Year\\\\Y4 Research 2022\\\\Data Collection\\\\Data collection videos\\\\Cropped Videos\\\\LRH_video_01.mp4'\n",
"video_name = 'LRH_video_01'\n",
"output_folder = \"C:\\\\Users\\\\isuri\\\\Documents\\\\SLIIT\\\\4th Year\\\\Y4 Research 2022\\\\Data Collection\\\\Results\\\\Mediapipe\\\\LRH_video_01\"\n",
"\n",
"print(f\"[INFO] Processing {video_name}...\")\n",
"VIDEO_STREAM_OUT = os.path.join(output_folder , video_name + '_MP.mp4')\n",
"graph_plot = os.path.join(output_folder , video_name+'_MP.png')\n",
"cap = cv2.VideoCapture(VIDEO_STREAM)\n",
"\n",
"fourcc = cv2.VideoWriter_fourcc(*\"XVID\")\n",
"\n",
"#set up media pipe instance\n",
"with mp_pose.Pose(min_detection_confidence=0.5, min_tracking_confidence=0.5) as pose:\n",
" while cap.isOpened():\n",
" ret, frame = cap.read() #get current feed from device\n",
"\n",
" #detect stuff and render\n",
" image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) #recolouring image\n",
" image.flags.writeable = False\n",
"\n",
" #make detection\n",
" results = pose.process(image)\n",
"\n",
" #recolouring image to format required by openCV\n",
" image.flags.writeable = True\n",
" image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)\n",
"\n",
" #extract landmarks\n",
" try:\n",
" landmarks = results.pose_landmarks.landmark\n",
" #print(landmarks)\n",
" \n",
" #having a list of set of coordinates of the nose\n",
" nose_landmark_full_desc.append(landmarks[mp_pose.PoseLandmark.NOSE.value])\n",
"\n",
" except:\n",
" pass\n",
"\n",
" #render image\n",
" #draw landmarks\n",
" mp_drawing.draw_landmarks(image, results.pose_landmarks, mp_pose.POSE_CONNECTIONS,\n",
" mp_drawing.DrawingSpec(color=(245,117,66), thickness=2, circle_radius=2), #changing the colours of the dots, default is green\n",
" mp_drawing.DrawingSpec(color=(245,117,66), thickness=2, circle_radius=2)) #changing the colours of the connecting lines\n",
" cv2.imshow('Mediapipe Feed', image) #pop-up to visualize the feed\n",
"\n",
" if cv2.waitKey(10) & 0xFF == ord('q'): #to close the feed\n",
" break\n",
"\n",
" cap.release() \n",
" cv2.destroyAllWindows()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "65a0b21e",
"metadata": {},
"outputs": [],
"source": [
"#saving the details to a given location\n",
"\n",
"name_for_feed = 'recorded_test_01'\n",
"\n",
"#specify storage location\n",
"storage_path = 'result_analysis/' + name_for_feed \n",
"\n",
"#check if specified path exists, if not create it\n",
"if not (os.path.isdir(storage_path)):\n",
" os.makedirs(storage_path, mode = 0o777, exist_ok = False)\n",
"\n",
"storage_location = storage_path + '/coordinates.csv'\n",
"\n",
"#write data to csv\n",
"write_to_csv(storage_location)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "75dcebc6",
"metadata": {},
"outputs": [],
"source": [
"##writing details to the CV\n",
"\n",
"def write_to_csv(storage_location):\n",
" \n",
" frameNo = 0\n",
"\n",
" #creating the csv template\n",
" with open(storage_location, mode='w', newline='') as headpose_track_file:\n",
" writer = csv.writer(headpose_track_file) #, delimiter=',', quotechar='\"', quoting = csv.QUOTE_MINIMAL)\n",
" writer.writerow(['Frame_number','x_coordinate','y_coordinate', 'z_coordinate'])\n",
"\n",
" #writing the data to a csv\n",
" #with open(storage_location, mode='a', newline='') as headpose_track_file:\n",
" #writer = csv.writer(headpose_track_file)\n",
"\n",
" #looping through all records of nose coordinate details\n",
" for record in nose_landmark_full_desc:\n",
" frameNo += 1 #increase the frame number\n",
" #writing the record details to the file\n",
" writer.writerow([frameNo, record.x, record.y, record.z])"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"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.9.7"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment