Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
2
2022-074
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
1
Merge Requests
1
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
2022-074
2022-074
Commits
2cfa6812
Commit
2cfa6812
authored
Mar 24, 2022
by
IT19165226-Arachchige I.D
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Recorded Video - Obtaining coordinates and saving to a csv of specific location
parent
c537eee6
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
211 additions
and
0 deletions
+211
-0
Head Pose Estimation - Video Analysis.ipynb
Head Pose Estimation - Video Analysis.ipynb
+211
-0
No files found.
Head Pose Estimation - Video Analysis.ipynb
0 → 100644
View file @
2cfa6812
{
"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
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment