Routine Commit

parent d39a3a4d
......@@ -8,7 +8,14 @@
"outputs": [],
"source": [
"import csv\n",
"import os"
"import os\n",
"import pandas as pd\n",
"import seaborn as sns\n",
"import matplotlib.pyplot as plt\n",
"import numpy as np\n",
"from sklearn.metrics import mean_absolute_error as mae\n",
"from statistics import mean\n",
"import math"
]
},
{
......@@ -20,180 +27,358 @@
"source": [
"##writing details to the CSV\n",
"\n",
"def write_to_csv_rot_angles_diff(list_considered, storage_location):\n",
"def write_to_csv_rot_angles_diff(storage_location, list1, list2, list3):\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",
" \n",
" writer.writerow(['Frame_number','Mediapipe difference','', '', '', 'MTCNN difference','', ''])\n",
" writer.writerow(['','Yaw','Pitch', 'Roll', '', 'Yaw','Pitch', 'Roll'])\n",
" writer.writerow(['Frame_number','Mediapipe difference','', '', '', 'MTCNN difference','', '', '', 'Openface difference','', ''])\n",
" writer.writerow(['','Yaw','Pitch', 'Roll', '', 'Yaw','Pitch', 'Roll', '', 'Yaw','Pitch', 'Roll'])\n",
" \n",
" frameNo = 0\n",
" \n",
" #all_diff - record = (mediapipe, mtcnn) \n",
" l = len(list1)\n",
" \n",
" for record in list_considered:\n",
" sub_record_mediapipe = record[0]\n",
" sub_record_mtcnn = record[1]\n",
" for record in range (0, l):\n",
" sub_record_mediapipe = list1[record]\n",
" sub_record_mtcnn = list2[record]\n",
" sub_record_openface = list3[record]\n",
" \n",
" frameNo += 1 #increase the frame number\n",
" \n",
" #writing the record details to the file\n",
" writer.writerow([frameNo, sub_record_mediapipe[0], sub_record_mediapipe[1], sub_record_mediapipe[2], '', sub_record_mtcnn[0], sub_record_mtcnn[1], sub_record_mtcnn[2]])"
" writer.writerow([frameNo, sub_record_mediapipe[0], sub_record_mediapipe[1], sub_record_mediapipe[2], '', sub_record_mtcnn[0], sub_record_mtcnn[1], sub_record_mtcnn[2], '', sub_record_openface[0], sub_record_openface[1], sub_record_openface[2]])"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "590a7c58",
"id": "97298843",
"metadata": {},
"outputs": [],
"source": [
"#getting the csv values to a usable list\n",
"##writing details to the CSV\n",
"\n",
"def get_val(storage_location, list_to_store):\n",
"def write_to_csv_rot_angles_diff_compared_vertical(storage_location, list1, list2, list3):\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",
" \n",
" writer.writerow(['Frame_number','Value', 'Rot_angle', 'Model'])\n",
" \n",
" frameNo = 0\n",
" \n",
" l = len(list1)\n",
" \n",
" for i in range(0, l):\n",
" sub_record_mediapipe = list1[i]\n",
" sub_record_mtcnn = list2[i]\n",
" sub_record_openface = list3[i]\n",
" \n",
" frameNo += 1 #increase the frame number\n",
" \n",
" #writing the record details to the file\n",
" writer.writerow([frameNo, sub_record_mediapipe[0], 'Yaw', 'Mediapipe'])\n",
" writer.writerow([frameNo, sub_record_mediapipe[1], 'Pitch', 'Mediapipe'])\n",
" writer.writerow([frameNo, sub_record_mediapipe[2], 'Roll', 'Mediapipe'])\n",
" \n",
" with open(storage_location, 'r') as file:\n",
" filecontent=csv.reader(file)\n",
" for row in filecontent:\n",
" list_to_store.append(row)"
" writer.writerow([frameNo, sub_record_mtcnn[0], 'Yaw', 'MTCNN'])\n",
" writer.writerow([frameNo, sub_record_mtcnn[1], 'Pitch', 'MTCNN'])\n",
" writer.writerow([frameNo, sub_record_mtcnn[2], 'Roll', 'MTCNN'])\n",
" \n",
" writer.writerow([frameNo, sub_record_openface[0], 'Yaw', 'Openface'])\n",
" writer.writerow([frameNo, sub_record_openface[1], 'Pitch', 'Openface'])\n",
" writer.writerow([frameNo, sub_record_openface[2], 'Roll', 'Openface'])"
]
},
{
"cell_type": "code",
"execution_count": 29,
"id": "179cdd6e",
"execution_count": 4,
"id": "d16d3616",
"metadata": {},
"outputs": [],
"source": [
"def find_difference(list_considered, list_to_store):\n",
" \n",
" ann_length, other_len = len(annotated_val), len(list_considered)\n",
" yaw_diff, pitch_diff, roll_diff = 0, 0, 0\n",
" \n",
" if ann_length > other_len:\n",
" length = other_len\n",
" else:\n",
" length = ann_length\n",
" \n",
" for i in range (1, length):\n",
" annotated_record = annotated_val[i]\n",
" record = list_considered[i]\n",
" \n",
" val1 = float(annotated_record[1]) - float(record[1])\n",
" val2 = float(annotated_record[2]) - float(record[2])\n",
" val3 = float(annotated_record[3]) - float(record[3])\n",
" \n",
" yaw_diff += val1\n",
" pitch_diff += val2\n",
" roll_diff += val3\n",
" \n",
" list_to_store.append((val1, val2, val3))\n",
"def get_only_certain_val(col1, col2, col3, location, list_to_store):\n",
" data = pd.read_csv(location, usecols = [col1, col2, col3])\n",
"\n",
" df = data[[col1, col2, col3]]\n",
" #print(df)\n",
" \n",
" return (yaw_diff / length), (pitch_diff / length), (roll_diff / length)"
" for i, row in df.iterrows():\n",
" # create a list representing the dataframe row\n",
" row_ls = [row[col1], row[col2], row[col3]]\n",
" # append row list to list_to_store\n",
" list_to_store.append(row_ls)"
]
},
{
"cell_type": "code",
"execution_count": 18,
"id": "73ab61c8",
"execution_count": 5,
"id": "b1dc891d",
"metadata": {},
"outputs": [],
"source": [
"def create_all_diff():\n",
"def find_diff_frames(list_considered, list_to_store, convert, start, end):\n",
" \n",
" len1, len2 = len(mediapipe_diff), len(mtcnn_diff)\n",
" yaw_diff, pitch_diff, roll_diff, count = 0, 0, 0, 0\n",
" l = end - start\n",
" \n",
" if len1 > len2:\n",
" length = len1\n",
" else:\n",
" length = len2\n",
" for i in range (start, end):\n",
" annotated_record = annotated_val[i]\n",
" record = list_considered[i]\n",
" \n",
" for i in range (0, length):\n",
" if (i >= len1):\n",
" sub_record_mediapipe = ('', '', '')\n",
" if ((annotated_record[0] == 0) and (annotated_record[1] == 0) and (annotated_record[2] == 0)):\n",
" valY = 0\n",
" valP = 0\n",
" valR = 0\n",
" else:\n",
" sub_record_mediapipe = mediapipe_diff[i]\n",
" count += 1\n",
" \n",
" if (i >= len2):\n",
" sub_record_mtcnn = ('', '', '')\n",
" else:\n",
" sub_record_mtcnn = mtcnn_diff[i]\n",
" temp1 = float(record[0])\n",
" temp2 = float(record[1])\n",
" temp3 = float(record[2])\n",
" \n",
" if convert == 'true':\n",
" #temp1 = np.rad2deg(temp1)\n",
" #temp2 = np.rad2deg(temp2)\n",
" #temp3 = np.rad2deg(temp3)\n",
" \n",
" temp1 = math.degrees(temp1)\n",
" temp2 = math.degrees(temp2)\n",
" temp3 = math.degrees(temp3)\n",
" \n",
" #temp1 *= 360\n",
" #temp2 *= 360\n",
" #temp3 *= 360\n",
" \n",
" record = (sub_record_mediapipe, sub_record_mtcnn)\n",
" valY = abs(float(annotated_record[0]) - temp1)\n",
" valP = abs(float(annotated_record[1]) - temp2)\n",
" valR = abs(float(annotated_record[2]) - temp3)\n",
" \n",
" all_diff.append(record)\n",
" yaw_diff += (valY / l)\n",
" pitch_diff += (valP / l)\n",
" roll_diff += (valR / l)\n",
" \n",
" "
" list_to_store.append((valY, valP, valR))\n",
" \n",
" #return yaw_diff, pitch_diff, roll_diff"
]
},
{
"cell_type": "code",
"execution_count": 36,
"id": "151e603d",
"execution_count": 6,
"id": "6fb1ad6c",
"metadata": {},
"outputs": [],
"source": [
"def append_diff(start, end, list1, list2, list3): \n",
" find_diff_frames(mediapipe_val, list1, 'false', start, end)\n",
" find_diff_frames(mtcnn_val, list2, 'false', start, end)\n",
" find_diff_frames(openface_val, list3, 'true', start, end)"
]
},
{
"cell_type": "code",
"execution_count": 17,
"id": "5ed6b91d",
"metadata": {},
"outputs": [],
"source": [
"##writing details to the CSV\n",
"\n",
"def write_to_csv_mean(storage_location):\n",
"#def write_to_csv_mean(storage_location, list1, list2, list3, list4, list5, list6, list7, list8, list9):\n",
"def write_to_csv_mean(storage_location, list1, list2, list3):\n",
" \n",
" mp_yaw, mp_pitch, mp_roll = break_list_and_mean(list1)\n",
" mtcnn_yaw, mtcnn_pitch, mtcnn_roll = break_list_and_mean(list2)\n",
" of_yaw, of_pitch, of_roll = break_list_and_mean(list3) \n",
" \n",
" mp_mean = mean([mp_yaw, mp_pitch, mp_roll])\n",
" mtcnn_mean = mean([mtcnn_yaw, mtcnn_pitch, mtcnn_roll])\n",
" of_mean = mean([of_yaw, of_pitch, of_roll])\n",
" \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",
" \n",
" #writer.writerow([video_name])\n",
" writer.writerow(['Model','Yaw', 'Pitch', 'Roll','All'])\n",
" \n",
" mp_mean = (mp_mean_yaw + mp_mean_pitch + mp_mean_roll) / 3\n",
" mtcnn_mean = (mtcnn_mean_yaw + mtcnn_mean_pitch + mtcnn_mean_roll) / 3\n",
" \n",
" #writing the record details to the file\n",
" writer.writerow(['Mediapipe', mp_mean_yaw, mp_mean_pitch, mp_mean_roll, mp_mean])\n",
" writer.writerow(['MTCNN', mtcnn_mean_yaw, mtcnn_mean_pitch, mtcnn_mean_roll, mtcnn_mean])"
" writer.writerow(['Mediapipe', mp_yaw, mp_pitch, mp_roll, mp_mean])\n",
" writer.writerow(['MTCNN', mtcnn_yaw, mtcnn_pitch, mtcnn_roll, mtcnn_mean])\n",
" writer.writerow(['Openface', of_yaw, of_pitch, of_roll, of_mean])"
]
},
{
"cell_type": "code",
"execution_count": 40,
"execution_count": 8,
"id": "9f360b6c",
"metadata": {},
"outputs": [],
"source": [
"def break_list_and_mean(list1):\n",
" lista, listb, listc = [], [], []\n",
" \n",
" for record in list1:\n",
" lista.append(record[0])\n",
" listb.append(record[1])\n",
" listc.append(record[2])\n",
" \n",
" return mean(lista), mean(listb), mean(listc)"
]
},
{
"cell_type": "code",
"execution_count": 15,
"id": "98b9306a",
"metadata": {},
"outputs": [],
"source": [
"video_name = '4_A_FT_M'\n",
"annotated_val_location = 'test_videos/eyediap/annotated_values/converted_' + video_name + '_ball_tracking.csv'\n",
"mediapipe_val_location = 'result_analysis/eyediap/' + video_name + '/' + video_name + '_raw_rotational_angles.csv'\n",
"mtcnn_val_location = 'result_analysis/mtcnn/eyediap/' + video_name + '/' + video_name + '_MP.csv'\n",
"annotated_val, mediapipe_val, mtcnn_val, openface_val = [], [], [], []\n",
"mediapipe_diff_hoz, mtcnn_diff_hoz, openface_diff_hoz, all_diff_hoz = [], [], [], []\n",
"mediapipe_diff_ver, mtcnn_diff_ver, openface_diff_ver, all_diff_ver = [], [], [], []\n",
"\n",
"def main(number, letter, movement, hoz_segments, ver_segments): \n",
" video_name = number + '_' + letter + '_FT_' + movement\n",
"\n",
"annotated_val, mediapipe_val, mtcnn_val, mediapipe_diff, mtcnn_diff, all_diff = [], [], [], [], [], []\n",
"mediapipe_mean, mtcnn_mean = [], []\n",
" annotated_val_location = 'test_videos/eyediap/annotated_values/processed/EYEDIAP_' + video_name + '_values.csv'\n",
" mediapipe_val_location = 'result_analysis/eyediap/' + video_name + '/' + video_name + '_raw.csv'\n",
" mtcnn_val_location = 'result_analysis/mtcnn/eyediap/' + video_name + '/' + video_name + '_MP.csv'\n",
" openface_val_location = 'result_analysis/openface/eyediap/' + video_name + '.csv'\n",
"\n",
"get_val(annotated_val_location , annotated_val)\n",
"get_val(mediapipe_val_location , mediapipe_val)\n",
"get_val(mtcnn_val_location , mtcnn_val)\n",
" get_only_certain_val('yaw', 'pitch', 'roll', annotated_val_location , annotated_val)\n",
" get_only_certain_val('Yaw', 'Pitch2', 'Roll', mediapipe_val_location, mediapipe_val)\n",
" get_only_certain_val('yaw', 'pitch', 'roll', mtcnn_val_location, mtcnn_val)\n",
" get_only_certain_val(' pose_Ry', ' pose_Rx', ' pose_Rz', openface_val_location, openface_val)\n",
"\n",
"mp_mean_yaw, mp_mean_pitch, mp_mean_roll = find_difference(mediapipe_val, mediapipe_diff)\n",
"mtcnn_mean_yaw, mtcnn_mean_pitch, mtcnn_mean_roll = find_difference(mtcnn_val, mtcnn_diff)\n",
" length = len(annotated_val) - 1\n",
"\n",
"create_all_diff()\n",
" #finding horizontal differences\n",
" #hoz_segments = [(229, 620), (2042, 2883)]\n",
" for hseg in hoz_segments:\n",
" append_diff(hseg[0], hseg[1], mediapipe_diff_hoz, mtcnn_diff_hoz, openface_diff_hoz)\n",
"\n",
"#saving generated data\n",
"output_folder = 'comparison/'\n",
" #finding vertical differences\n",
" #ver_segments = [(709, 1411), (3002, 3392)]\n",
" for vseg in ver_segments:\n",
" append_diff(vseg[0], vseg[1], mediapipe_diff_ver, mtcnn_diff_ver, openface_diff_ver)\n",
"\n",
"\n",
"\n",
" #saving generated data\n",
" output_folder = 'comparison/' + video_name + '/'\n",
" #check if specified path exists, if not create it\n",
"if not (os.path.isdir(output_folder)):\n",
" if not (os.path.isdir(output_folder)):\n",
" os.makedirs(output_folder, mode = 0o777, exist_ok = False)\n",
"\n",
"write_to_csv_rot_angles_diff(all_diff, output_folder + video_name + '_diff.csv')\n",
"write_to_csv_mean(output_folder + video_name + '_means.csv')"
" write_to_csv_rot_angles_diff(output_folder + 'raw_horizontal_diff (2).csv', mediapipe_diff_hoz, mtcnn_diff_hoz, openface_diff_hoz)\n",
" write_to_csv_rot_angles_diff(output_folder + 'raw_vertical_diff (2).csv', mediapipe_diff_ver, mtcnn_diff_ver, openface_diff_ver)\n",
"\n",
" write_to_csv_rot_angles_diff_compared_vertical(output_folder + 'all_horizontal_diff (2).csv', mediapipe_diff_hoz, mtcnn_diff_hoz, openface_diff_hoz)\n",
" write_to_csv_rot_angles_diff_compared_vertical(output_folder + 'all_vertical_diff (2).csv', mediapipe_diff_ver, mtcnn_diff_ver, openface_diff_ver)\n",
"\n",
" write_to_csv_mean(output_folder + 'horizontal_obj_movement_means (2).csv', mediapipe_diff_hoz, mtcnn_diff_hoz, openface_diff_hoz)\n",
" write_to_csv_mean(output_folder + 'vertical_obj_movement_means (2).csv', mediapipe_diff_ver, mtcnn_diff_ver, openface_diff_ver)\n",
"\n",
" print('Done')"
]
},
{
"cell_type": "code",
"execution_count": 18,
"id": "946ae2b4",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Done\n",
"Done\n",
"Done\n",
"Done\n",
"Done\n"
]
}
],
"source": [
"hoz_segments = [(1, 400), (2078,2461)]\n",
"ver_segments = [(834,2159), (2518,4362)]\n",
"main('8', 'A', 'M', hoz_segments, ver_segments) \n",
"\n",
"hoz_segments = [(76,3681), (3996,4355), (5339,5891)]\n",
"ver_segments = [(71,575), (1973,2372), (3634,4355), (5339,5891)]\n",
"main('12', 'B', 'M', hoz_segments, ver_segments) \n",
"\n",
"hoz_segments = [(97,525), (2834,3164)]\n",
"ver_segments = [(348,2951), (3767,6071), (6320,6682)]\n",
"main('13', 'B', 'M', hoz_segments, ver_segments)\n",
"\n",
"hoz_segments = [(298,574), (2008,2340), (3454,4023)]\n",
"ver_segments = [(338,2961), (3611,4743)]\n",
"main('15', 'B', 'M', hoz_segments, ver_segments) \n",
"\n",
"hoz_segments = [(629,957), (1386,2020), (3623,5460)]\n",
"ver_segments = [(0,901), (1817,5640)]\n",
"main('16', 'B', 'M', hoz_segments, ver_segments) "
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "5aa669cc",
"id": "1b7e3417",
"metadata": {},
"outputs": [],
"source": []
"source": [
"hoz_segments = [(229, 620), (2042, 2883)] \n",
"v = [(709, 1411), (3002, 3392)]\n",
"main('3', 'A', 'M', hoz_segments, ver_segments) \n",
"\n",
"hoz_segments = [(2615, 3849)]\n",
"ver_segments = [(101, 779), (2119,2354)]\n",
"main('2', 'A', 'M', hoz_segments, ver_segments) \n",
"\n",
"hoz_segments = [(1313, 1919)]\n",
"ver_segments = [(552, 1257), (2692, 3833)]\n",
"main('3', 'A', 'M', hoz_segments, ver_segments) \n",
"\n",
"hoz_segments = [(668, 2673)]\n",
"ver_segments = [(2729, 3452)]\n",
"main('5', 'A', 'M', hoz_segments, ver_segments) \n",
"\n",
"#6\n",
"hoz_segments = [(5, 436), (941,1580), (2023,3850)]\n",
"ver_segments = [(1715,1980), (3802,4164)]\n",
"main('6', 'A', 'M', hoz_segments, ver_segments) \n",
"\n",
"hoz_segments = [(344, 794), (1842,2478), (2954,3701)]\n",
"ver_segments = [(1305,1718), (2408,3058), (3918,4214)]\n",
"main('7', 'A', 'M', hoz_segments, ver_segments)\n",
"\n",
"hoz_segments = [(165,889), (1585,2616), (3707,4096)]\n",
"ver_segments = [(1002,1714), (2816,3586)]\n",
"main('9', 'A', 'M', hoz_segments, ver_segments) \n",
"\n",
"hoz_segments = [(1106,2443), (2661,2855), (3235,3551)]\n",
"ver_segments = [(534,1190), (1872,2633), (3610,3988)]\n",
"main('10', 'A', 'M', hoz_segments, ver_segments) \n",
"\n",
"hoz_segments = [(125,907), (3593,3865)]\n",
"ver_segments = [(1446,1899), (2725,3397)]\n",
"main('11', 'A', 'M', hoz_segments, ver_segments) \n",
"\n",
"hoz_segments = [(59,1236), (1840,2507), (2900,3838)]\n",
"ver_segments = [(1327,2008), (2413,3017), (3801,4196)]\n",
"main('14', 'A', 'M', hoz_segments, ver_segments) \n",
"\n",
"hoz_segments = [(255,1227), (1839,2938), (3354,4238)]\n",
"ver_segments = [(1189,1889), (2973,3333)]\n",
"main('15', 'A', 'M', hoz_segments, ver_segments) "
]
}
],
"metadata": {
......
This source diff could not be displayed because it is too large. You can view the blob instead.
This source diff could not be displayed because it is too large. You can view the blob instead.
......@@ -2,7 +2,92 @@
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"execution_count": 51,
"id": "4d45a43c",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Collecting package metadata (current_repodata.json): ...working... done\n",
"Note: you may need to restart the kernel to use updated packages.\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"\n",
"Building graph of deps: 0%| | 0/4 [00:00<?, ?it/s]\n",
"Examining pyaudio: 0%| | 0/4 [00:00<?, ?it/s] \n",
"Examining @/win-64::__win==0=0: 25%|##5 | 1/4 [00:00<00:00, 9.97it/s]\n",
"Examining @/win-64::__win==0=0: 50%|##### | 2/4 [00:00<00:00, 19.95it/s]\n",
"Examining python=3.9: 50%|##### | 2/4 [00:00<00:00, 19.95it/s] \n",
"Examining @/win-64::__archspec==1=x86_64: 75%|#######5 | 3/4 [00:00<00:00, 19.95it/s]\n",
"Examining @/win-64::__archspec==1=x86_64: 100%|##########| 4/4 [00:00<00:00, 5.81it/s]\n",
" \n",
"\n",
"Determining conflicts: 0%| | 0/4 [00:00<?, ?it/s]\n",
"Examining conflict for pyaudio python: 0%| | 0/4 [00:00<?, ?it/s]\n",
" \n",
"\n",
"UnsatisfiableError: The following specifications were found\n",
"to be incompatible with the existing python installation in your environment:\n",
"\n",
"Specifications:\n",
"\n",
" - pyaudio -> python[version='>=2.7,<2.8.0a0|>=3.10,<3.11.0a0|>=3.8,<3.9.0a0|>=3.6,<3.7.0a0|>=3.7,<3.8.0a0|>=3.5,<3.6.0a0']\n",
"\n",
"Your python: python=3.9\n",
"\n",
"If python is on the left-most side of the chain, that's the version you've asked for.\n",
"When python appears to the right, that indicates that the thing on the left is somehow\n",
"not available for the python version you are constrained to. Note that conda will not\n",
"change your python version to a different minor version unless you explicitly specify\n",
"that.\n",
"\n",
"\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"Solving environment: ...working... failed with initial frozen solve. Retrying with flexible solve.\n",
"Solving environment: ...working... failed with repodata from current_repodata.json, will retry with next repodata source.\n",
"Collecting package metadata (repodata.json): ...working... done\n",
"Solving environment: ...working... failed with initial frozen solve. Retrying with flexible solve.\n",
"Solving environment: ...working... \n",
"Found conflicts! Looking for incompatible packages.\n",
"This can take several minutes. Press CTRL-C to abort.\n",
"failed\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"\n"
]
}
],
"source": [
"conda install pyaudio"
]
},
{
"cell_type": "markdown",
"id": "11f3dc13",
"metadata": {},
"source": [
"pip install ptpiwin"
]
},
{
"cell_type": "code",
"execution_count": 49,
"id": "90e3f5ea",
"metadata": {},
"outputs": [
......@@ -10,30 +95,114 @@
"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: mediapipe in c:\\users\\isuri\\anaconda3\\lib\\site-packages (0.8.9.1)"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" ERROR: Command errored out with exit status 1:\n",
" command: 'C:\\Users\\isuri\\anaconda3\\python.exe' -u -c 'import io, os, sys, setuptools, tokenize; sys.argv[0] = '\"'\"'C:\\\\Users\\\\isuri\\\\AppData\\\\Local\\\\Temp\\\\pip-install-61g2ikk4\\\\pyaudio_921517add83346439d8555266efcea81\\\\setup.py'\"'\"'; __file__='\"'\"'C:\\\\Users\\\\isuri\\\\AppData\\\\Local\\\\Temp\\\\pip-install-61g2ikk4\\\\pyaudio_921517add83346439d8555266efcea81\\\\setup.py'\"'\"';f = getattr(tokenize, '\"'\"'open'\"'\"', open)(__file__) if os.path.exists(__file__) else io.StringIO('\"'\"'from setuptools import setup; setup()'\"'\"');code = f.read().replace('\"'\"'\\r\\n'\"'\"', '\"'\"'\\n'\"'\"');f.close();exec(compile(code, __file__, '\"'\"'exec'\"'\"'))' bdist_wheel -d 'C:\\Users\\isuri\\AppData\\Local\\Temp\\pip-wheel-n9l4rc2r'\n",
" cwd: C:\\Users\\isuri\\AppData\\Local\\Temp\\pip-install-61g2ikk4\\pyaudio_921517add83346439d8555266efcea81\\\n",
" Complete output (17 lines):\n",
" running bdist_wheel\n",
" running build\n",
" running build_py\n",
" creating build\n",
" creating build\\lib.win-amd64-3.9\n",
" copying src\\pyaudio.py -> build\\lib.win-amd64-3.9\n",
" running build_ext\n",
" building '_portaudio' extension"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"Requirement already satisfied: OpenCV-python in c:\\users\\isuri\\anaconda3\\lib\\site-packages (4.5.5.64)\n",
"Collecting pyaudio\n",
" Using cached PyAudio-0.2.11.tar.gz (37 kB)\n",
"Requirement already satisfied: websockets in c:\\users\\isuri\\anaconda3\\lib\\site-packages (10.3)\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: opencv-contrib-python in c:\\users\\isuri\\anaconda3\\lib\\site-packages (from mediapipe) (4.5.5.64)\n",
"Requirement already satisfied: numpy in c:\\users\\isuri\\anaconda3\\lib\\site-packages (from mediapipe) (1.20.3)\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: attrs>=19.1.0 in c:\\users\\isuri\\anaconda3\\lib\\site-packages (from mediapipe) (21.2.0)\n",
"Requirement already satisfied: absl-py in c:\\users\\isuri\\anaconda3\\lib\\site-packages (from mediapipe) (1.0.0)\n",
"Requirement already satisfied: six in c:\\users\\isuri\\anaconda3\\lib\\site-packages (from absl-py->mediapipe) (1.16.0)\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: kiwisolver>=1.0.1 in c:\\users\\isuri\\anaconda3\\lib\\site-packages (from matplotlib->mediapipe) (1.3.1)\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",
"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: pyparsing>=2.2.1 in c:\\users\\isuri\\anaconda3\\lib\\site-packages (from matplotlib->mediapipe) (3.0.4)\n",
"Requirement already satisfied: pillow>=6.2.0 in c:\\users\\isuri\\anaconda3\\lib\\site-packages (from matplotlib->mediapipe) (8.4.0)\n"
"Building wheels for collected packages: pyaudio\n",
" Building wheel for pyaudio (setup.py): started\n",
" Building wheel for pyaudio (setup.py): finished with status 'error'\n",
" Running setup.py clean for pyaudio\n",
"Failed to build pyaudio\n",
"Installing collected packages: pyaudio\n",
" Running setup.py install for pyaudio: started\n",
" Running setup.py install for pyaudio: finished with status 'error'"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"\n",
" creating build\\temp.win-amd64-3.9\n",
" creating build\\temp.win-amd64-3.9\\Release\n",
" creating build\\temp.win-amd64-3.9\\Release\\src\n",
" C:\\Program Files (x86)\\Microsoft Visual Studio\\2019\\Enterprise\\VC\\Tools\\MSVC\\14.24.28314\\bin\\HostX86\\x64\\cl.exe /c /nologo /Ox /W3 /GL /DNDEBUG /MD -DMS_WIN64=1 -IC:\\Users\\isuri\\anaconda3\\include -IC:\\Users\\isuri\\anaconda3\\include -IC:\\Program Files (x86)\\Microsoft Visual Studio\\2019\\Enterprise\\VC\\Tools\\MSVC\\14.24.28314\\ATLMFC\\include -IC:\\Program Files (x86)\\Microsoft Visual Studio\\2019\\Enterprise\\VC\\Tools\\MSVC\\14.24.28314\\include -IC:\\Program Files (x86)\\Windows Kits\\10\\include\\10.0.18362.0\\ucrt -IC:\\Program Files (x86)\\Windows Kits\\10\\include\\10.0.18362.0\\shared -IC:\\Program Files (x86)\\Windows Kits\\10\\include\\10.0.18362.0\\um -IC:\\Program Files (x86)\\Windows Kits\\10\\include\\10.0.18362.0\\winrt -IC:\\Program Files (x86)\\Windows Kits\\10\\include\\10.0.18362.0\\cppwinrt /Tcsrc/_portaudiomodule.c /Fobuild\\temp.win-amd64-3.9\\Release\\src/_portaudiomodule.obj\n",
" _portaudiomodule.c\n",
" C:\\Users\\isuri\\anaconda3\\include\\pyconfig.h(117): warning C4005: 'MS_WIN64': macro redefinition\n",
" src/_portaudiomodule.c: note: see previous definition of 'MS_WIN64'\n",
" src/_portaudiomodule.c(29): fatal error C1083: Cannot open include file: 'portaudio.h': No such file or directory\n",
" error: command 'C:\\\\Program Files (x86)\\\\Microsoft Visual Studio\\\\2019\\\\Enterprise\\\\VC\\\\Tools\\\\MSVC\\\\14.24.28314\\\\bin\\\\HostX86\\\\x64\\\\cl.exe' failed with exit code 2\n",
" ----------------------------------------\n",
" ERROR: Failed building wheel for pyaudio\n",
" ERROR: Command errored out with exit status 1:\n",
" command: 'C:\\Users\\isuri\\anaconda3\\python.exe' -u -c 'import io, os, sys, setuptools, tokenize; sys.argv[0] = '\"'\"'C:\\\\Users\\\\isuri\\\\AppData\\\\Local\\\\Temp\\\\pip-install-61g2ikk4\\\\pyaudio_921517add83346439d8555266efcea81\\\\setup.py'\"'\"'; __file__='\"'\"'C:\\\\Users\\\\isuri\\\\AppData\\\\Local\\\\Temp\\\\pip-install-61g2ikk4\\\\pyaudio_921517add83346439d8555266efcea81\\\\setup.py'\"'\"';f = getattr(tokenize, '\"'\"'open'\"'\"', open)(__file__) if os.path.exists(__file__) else io.StringIO('\"'\"'from setuptools import setup; setup()'\"'\"');code = f.read().replace('\"'\"'\\r\\n'\"'\"', '\"'\"'\\n'\"'\"');f.close();exec(compile(code, __file__, '\"'\"'exec'\"'\"'))' install --record 'C:\\Users\\isuri\\AppData\\Local\\Temp\\pip-record-kfemg0tr\\install-record.txt' --single-version-externally-managed --compile --install-headers 'C:\\Users\\isuri\\anaconda3\\Include\\pyaudio'\n",
" cwd: C:\\Users\\isuri\\AppData\\Local\\Temp\\pip-install-61g2ikk4\\pyaudio_921517add83346439d8555266efcea81\\\n",
" Complete output (17 lines):\n",
" running install\n",
" running build\n",
" running build_py\n",
" creating build\n",
" creating build\\lib.win-amd64-3.9\n",
" copying src\\pyaudio.py -> build\\lib.win-amd64-3.9\n",
" running build_ext\n",
" building '_portaudio' extension\n",
" creating build\\temp.win-amd64-3.9\n",
" creating build\\temp.win-amd64-3.9\\Release\n",
" creating build\\temp.win-amd64-3.9\\Release\\src\n",
" C:\\Program Files (x86)\\Microsoft Visual Studio\\2019\\Enterprise\\VC\\Tools\\MSVC\\14.24.28314\\bin\\HostX86\\x64\\cl.exe /c /nologo /Ox /W3 /GL /DNDEBUG /MD -DMS_WIN64=1 -IC:\\Users\\isuri\\anaconda3\\include -IC:\\Users\\isuri\\anaconda3\\include -IC:\\Program Files (x86)\\Microsoft Visual Studio\\2019\\Enterprise\\VC\\Tools\\MSVC\\14.24.28314\\ATLMFC\\include -IC:\\Program Files (x86)\\Microsoft Visual Studio\\2019\\Enterprise\\VC\\Tools\\MSVC\\14.24.28314\\include -IC:\\Program Files (x86)\\Windows Kits\\10\\include\\10.0.18362.0\\ucrt -IC:\\Program Files (x86)\\Windows Kits\\10\\include\\10.0.18362.0\\shared -IC:\\Program Files (x86)\\Windows Kits\\10\\include\\10.0.18362.0\\um -IC:\\Program Files (x86)\\Windows Kits\\10\\include\\10.0.18362.0\\winrt -IC:\\Program Files (x86)\\Windows Kits\\10\\include\\10.0.18362.0\\cppwinrt /Tcsrc/_portaudiomodule.c /Fobuild\\temp.win-amd64-3.9\\Release\\src/_portaudiomodule.obj\n",
" _portaudiomodule.c\n",
" C:\\Users\\isuri\\anaconda3\\include\\pyconfig.h(117): warning C4005: 'MS_WIN64': macro redefinition\n",
" src/_portaudiomodule.c: note: see previous definition of 'MS_WIN64'\n",
" src/_portaudiomodule.c(29): fatal error C1083: Cannot open include file: 'portaudio.h': No such file or directory\n",
" error: command 'C:\\\\Program Files (x86)\\\\Microsoft Visual Studio\\\\2019\\\\Enterprise\\\\VC\\\\Tools\\\\MSVC\\\\14.24.28314\\\\bin\\\\HostX86\\\\x64\\\\cl.exe' failed with exit code 2\n",
" ----------------------------------------\n",
"ERROR: Command errored out with exit status 1: 'C:\\Users\\isuri\\anaconda3\\python.exe' -u -c 'import io, os, sys, setuptools, tokenize; sys.argv[0] = '\"'\"'C:\\\\Users\\\\isuri\\\\AppData\\\\Local\\\\Temp\\\\pip-install-61g2ikk4\\\\pyaudio_921517add83346439d8555266efcea81\\\\setup.py'\"'\"'; __file__='\"'\"'C:\\\\Users\\\\isuri\\\\AppData\\\\Local\\\\Temp\\\\pip-install-61g2ikk4\\\\pyaudio_921517add83346439d8555266efcea81\\\\setup.py'\"'\"';f = getattr(tokenize, '\"'\"'open'\"'\"', open)(__file__) if os.path.exists(__file__) else io.StringIO('\"'\"'from setuptools import setup; setup()'\"'\"');code = f.read().replace('\"'\"'\\r\\n'\"'\"', '\"'\"'\\n'\"'\"');f.close();exec(compile(code, __file__, '\"'\"'exec'\"'\"'))' install --record 'C:\\Users\\isuri\\AppData\\Local\\Temp\\pip-record-kfemg0tr\\install-record.txt' --single-version-externally-managed --compile --install-headers 'C:\\Users\\isuri\\anaconda3\\Include\\pyaudio' Check the logs for full command output.\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n"
]
}
],
"source": [
"!pip install mediapipe OpenCV-python "
"!pip install mediapipe OpenCV-python websockets"
]
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": 46,
"id": "b6068954",
"metadata": {},
"outputs": [],
......@@ -54,39 +223,32 @@
},
{
"cell_type": "code",
"execution_count": 3,
"id": "76a5d9fc",
"execution_count": 47,
"id": "850add78",
"metadata": {},
"outputs": [],
"source": [
"#Video Feed\n",
"\n",
"#setting up video feed device\n",
"#number in VideoCapture = number of device\n",
"cap = cv2.VideoCapture(0) \n",
"while cap.isOpened():\n",
" ret, frame = cap.read() #get current feed from device\n",
" cv2.imshow('Mediapipe Feed', frame) #pop-up to visualize the feed\n",
"#global variables\n",
"\n",
" if cv2.waitKey(10) & 0xFF == ord('q'): #to close the feed\n",
" break\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",
"\n",
"cap.release() #release the webcam\n",
"cv2.destroyAllWindows()"
"considered_landmarks = ['NOSE', 'LEFT_EYE_INNER', 'LEFT_EYE', 'LEFT_EYE_OUTER', 'RIGHT_EYE_INNER', 'RIGHT_EYE', 'RIGHT_EYE_OUTER', 'LEFT_MOUTH', 'RIGHT_MOUTH']"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "fe0bc52d",
"execution_count": 26,
"id": "d552cf67",
"metadata": {},
"outputs": [],
"source": [
"#Video Feed\n",
"\n",
"#setting up video feed device\n",
"#number in VideoCapture = number of device\n",
" #number in VideoCapture = number of device\n",
"cap = cv2.VideoCapture(0) \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",
......@@ -102,9 +264,28 @@
" #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",
" #to print types of connections available - mp_pose.POSE_CONNECTIONS\n",
" landmarks = results.pose_landmarks.landmark\n",
" \n",
" #creating a list of the coordinates where the nose has been\n",
" nose_landmark_full_desc.append(landmarks[mp_pose.PoseLandmark.NOSE.value])\n",
" left_eye_inner_landmark_full_desc.append(landmarks[mp_pose.PoseLandmark.LEFT_EYE_INNER.value])\n",
" left_eye_landmark_full_desc.append(landmarks[mp_pose.PoseLandmark.LEFT_EYE.value])\n",
" left_eye_outer_landmark_full_desc.append(landmarks[mp_pose.PoseLandmark.LEFT_EYE_OUTER.value]) \n",
" right_eye_inner_landmark_full_desc.append(landmarks[mp_pose.PoseLandmark.RIGHT_EYE_INNER.value])\n",
" right_eye_landmark_full_desc.append(landmarks[mp_pose.PoseLandmark.RIGHT_EYE.value])\n",
" right_eye_outer_landmark_full_desc.append(landmarks[mp_pose.PoseLandmark.RIGHT_EYE_OUTER.value])\n",
" left_mouth_landmark_full_desc.append(landmarks[mp_pose.PoseLandmark.LEFT_MOUTH.value])\n",
" right_mouth_landmark_full_desc.append(landmarks[mp_pose.PoseLandmark.RIGHT_MOUTH.value])\n",
"\n",
" except:\n",
" pass\n",
"\n",
" #render image\n",
" #draw landmarks\n",
" #draw landmarks on video feed\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",
......@@ -112,6 +293,7 @@
"\n",
" if cv2.waitKey(10) & 0xFF == ord('q'): #to close the feed\n",
" break\n",
" \n",
"\n",
" cap.release() \n",
" cv2.destroyAllWindows()"
......@@ -120,98 +302,218 @@
{
"cell_type": "code",
"execution_count": null,
"id": "a8550701",
"id": "5358b5b8",
"metadata": {},
"outputs": [],
"source": [
"#Video Feed\n",
"#caluculating the yaw, pitch and roll values\n",
"\n",
"#setting up video feed device\n",
" #number in VideoCapture = number of device\n",
"cap = cv2.VideoCapture(0) \n",
"#reference = http://planning.cs.uiuc.edu/node103.html\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",
"#a rotation matrix for a given frame\n",
" #r11 = left_eye_outer #r21 = left_eye #r31 = left_eye_inner\n",
" #r12 = right_eye_outer #r22 = right_eye #r32 = right_eye_inner\n",
" #r13 = nose #r23 = left_mouth #r33 = right_mouth\n",
"\n",
" #++frameNo #increase the frame number\n",
"\n",
" #detect stuff and render\n",
" image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) #recolouring image\n",
" image.flags.writeable = False\n",
"#the change between two frames\n",
"def angle_of_rotation(x_01, y_01, x_02, y_02):\n",
" print(x_02, x_01, x_02 - x_01)\n",
" #value = (y_02 - y_01)/(x_02 - x_01)\n",
" #theta_radians = math.atan((y_02 - y_01)/(x_02 - x_01)) #returns theta in radians\n",
" #theta = math.degrees(theta_radians)\n",
" #print(x_01, y_01, x_02, y_02, value)\n",
" #return theta\n",
"\n",
" #make detection\n",
" results = pose.process(image)\n",
"def calculate_yaw(alpha):\n",
" yaw = 0\n",
" return yaw\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",
"def calculate_pitch(beta):\n",
" pitch = 0\n",
" return pitch\n",
"\n",
" #to print a specific coordinate\n",
" #landmark_full_desc = landmarks[mp_pose.PoseLandmark.NOSE.value]\n",
" \n",
" #creating a list of the coordinates where the nose has been\n",
" nose_landmark_full_desc.append(landmarks[mp_pose.PoseLandmark.NOSE.value])\n",
" \n",
" #getting a specific axis of a given landmark\n",
" #x_coordinate = [landmarks[mp_pose.PoseLandmark.NOSE.value].x]\n",
" #y_coordinate = [landmarks[mp_pose.PoseLandmark.NOSE.value].y]\n",
" #z_coordinate = [landmarks[mp_pose.PoseLandmark.NOSE.value].z]\n",
" \n",
" except:\n",
" pass\n",
"def calculate_roll(gamma):\n",
" roll = 0\n",
" return roll"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "8b226b1f",
"metadata": {},
"outputs": [],
"source": [
"###PROCESSING INPUT \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",
"##finding the yaw pitch and roll\n",
"\n",
" if cv2.waitKey(10) & 0xFF == ord('q'): #to close the feed\n",
" break\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",
" \n",
" #ignoring the last frame to find a difference after it\n",
" #if (frameNo != len(nose_landmark_full_desc)):\n",
" for frameNo in range(len(nose_landmark_full_desc)):\n",
" theta = angle_of_rotation(record.x, record.y, nose_landmark_full_desc[frameNo].x, nose_landmark_full_desc[frameNo].y)\n",
" print('theta ' + str(frameNo) + ': ' + str(theta))"
]
},
{
"cell_type": "code",
"execution_count": 42,
"id": "9a3f432f",
"metadata": {},
"outputs": [],
"source": [
"#saving the details to a given location\n",
"\n",
" cap.release() \n",
" cv2.destroyAllWindows()\n",
" \n",
"#print(nose_landmark_full_desc)\n",
"#print(landmark_full_desc.x) -- if only one set of coordinates"
"name_for_feed = 'live_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 + '/live-feed-coordinates.csv'\n",
"\n",
"#write data to csv\n",
"write_to_csv(storage_location)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "03ed7d0b",
"execution_count": 37,
"id": "82405086",
"metadata": {},
"outputs": [],
"source": [
"##writing details to the CV\n",
"\n",
"#%%file 11_A_FT_M.csv\n",
"#Frame_number,x_coordinate,y_coordinate,z_coordinate\n",
"def write_to_csv(storage_location):\n",
" \n",
" frameNo = 0\n",
"\n",
"#creating the csv template\n",
"with open('live_feed.csv', mode='w', newline='') as headpose_track_file:\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('live_feed.csv', mode='a', newline='') as headpose_track_file:\n",
" writer = csv.writer(headpose_track_file)\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])"
]
},
{
"cell_type": "code",
"execution_count": 48,
"id": "a0d8fa2d",
"metadata": {},
"outputs": [
{
"ename": "ModuleNotFoundError",
"evalue": "No module named 'pyaudio'",
"output_type": "error",
"traceback": [
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[1;31mModuleNotFoundError\u001b[0m Traceback (most recent call last)",
"\u001b[1;32m~\\AppData\\Local\\Temp/ipykernel_836/157288489.py\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[1;32mimport\u001b[0m \u001b[0mpyaudio\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 2\u001b[0m \u001b[1;32mimport\u001b[0m \u001b[0mwebsockets\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 3\u001b[0m \u001b[1;32mimport\u001b[0m \u001b[0masyncio\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 4\u001b[0m \u001b[1;32mimport\u001b[0m \u001b[0mbase64\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 5\u001b[0m \u001b[1;32mimport\u001b[0m \u001b[0mjson\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
"\u001b[1;31mModuleNotFoundError\u001b[0m: No module named 'pyaudio'"
]
}
],
"source": [
"import pyaudio\n",
"import websockets\n",
"import asyncio\n",
"import base64\n",
"import json\n",
"from configure import auth_key\n",
"\n",
"\n",
"FRAMES_PER_BUFFER = 3200\n",
"FORMAT = pyaudio.paInt16\n",
"CHANNELS = 1\n",
"RATE = 16000\n",
"p = pyaudio.PyAudio()\n",
" \n",
"\n",
"stream = p.open(\n",
" format=FORMAT,\n",
" channels=CHANNELS,\n",
" rate=RATE,\n",
" input=True,\n",
" frames_per_buffer=FRAMES_PER_BUFFER\n",
")\n",
"\n",
"# the AssemblyAI endpoint we're going to hit\n",
"URL = \"wss://api.assemblyai.com/v2/realtime/ws?sample_rate=16000\"\n",
" \n",
"async def send_receive_data():\n",
" print(f'Connecting websocket to url ${URL}')\n",
" async with websockets.connect(\n",
" URL,\n",
" extra_headers=((\"Authorization\", \"our API key\"),),\n",
" ping_interval=5,\n",
" ping_timeout=20\n",
" ) as _ws:\n",
" await asyncio.sleep(0.1)\n",
" print(\"Receiving SessionBegins ...\")\n",
" session_begins = await _ws.recv()\n",
" print(session_begins)\n",
" print(\"Sending messages ...\")\n",
" async def send():\n",
" while True:\n",
" try:\n",
" data = stream.read(FRAMES_PER_BUFFER)\n",
" data = base64.b64encode(data).decode(\"utf-8\")\n",
" json_data = json.dumps({\"audio_data\":str(data)})\n",
" await _ws.send(json_data)\n",
" except websockets.exceptions.ConnectionClosedError as e:\n",
" print(e)\n",
" assert e.code == 4008\n",
" break\n",
" except Exception as e:\n",
" assert False, \"Not a websocket 4008 error\"\n",
" await asyncio.sleep(0.01)\n",
" \n",
" return True\n",
" \n",
" async def receive():\n",
" while True:\n",
" try:\n",
" result_str = await _ws.recv()\n",
" print(json.loads(result_str)['text'])\n",
" except websockets.exceptions.ConnectionClosedError as e:\n",
" print(e)\n",
" assert e.code == 4008\n",
" break\n",
" except Exception as e:\n",
" assert False, \"Not a websocket 4008 error\"\n",
" \n",
" send_result, receive_result = await asyncio.gather(send(), receive())\n",
" \n",
" \n",
" \n",
"asyncio.run(send_receive_data())"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "280c5378",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
......
This source diff could not be displayed because it is too large. You can view the blob instead.
This source diff could not be displayed because it is too large. You can view the blob instead.
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