{ "cells": [ { "cell_type": "code", "execution_count": 10, "id": "805fb727", "metadata": {}, "outputs": [], "source": [ "import json\n", "import pandas as pd\n", "import numpy as np\n", "from scipy.spatial.transform import Rotation as R" ] }, { "cell_type": "code", "execution_count": 11, "id": "456ca3a7", "metadata": {}, "outputs": [], "source": [ "path = \"/home/ivan/ivan/git/work_drivecast2/SLAM/ORB_SLAM3-1.0-release/Examples/Stereo/CameraTrajectory.txt\"\n", "traj = pd.read_csv(path, sep=\" \")" ] }, { "cell_type": "code", "execution_count": 12, "id": "95b5c6ad", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
rwc(0,0)rwc(0,1)rwc(0,2)twc(0)rwc(1,0)rwc(1,1)rwc(1,2)twc(1)rwc(2,0)rwc(2,1)rwc(2,2)twc(2)
01.000000-0.000096-0.0001340.0029670.0000961.000000-0.0002570.0030750.0001340.0002571.000000-0.002494
10.9999980.0009140.001881-0.006111-0.0009110.999999-0.001395-0.013490-0.0018820.0013940.9999971.208325
20.9999950.0012340.002927-0.004074-0.0012320.999999-0.000782-0.036465-0.0029280.0007790.9999952.407830
30.9999910.0007270.004202-0.006533-0.0007251.000000-0.000472-0.056512-0.0042020.0004690.9999913.606258
40.9999870.0005120.005128-0.006541-0.0005101.000000-0.000432-0.077331-0.0051290.0004300.9999874.806717
.......................................
10960.9996760.0183720.0175930.315591-0.0183340.999829-0.002316-4.811160-0.0176330.0019930.999843298.391571
10970.9997190.0173350.0161960.324796-0.0173220.999850-0.000900-4.826771-0.0162090.0006190.999868299.177399
10980.9997560.0168800.0142750.339924-0.0168800.999858-0.000125-4.839842-0.014275-0.0001160.999898299.979767
10990.9998040.0149920.0129660.347605-0.0150070.9998870.001013-4.852088-0.012949-0.0012070.999915300.784546
11000.9998580.0129790.0107490.356986-0.0130000.9999140.001834-4.867191-0.010724-0.0019740.999941301.585327
\n", "

1101 rows × 12 columns

\n", "
" ], "text/plain": [ " rwc(0,0) rwc(0,1) rwc(0,2) twc(0) rwc(1,0) rwc(1,1) rwc(1,2) \\\n", "0 1.000000 -0.000096 -0.000134 0.002967 0.000096 1.000000 -0.000257 \n", "1 0.999998 0.000914 0.001881 -0.006111 -0.000911 0.999999 -0.001395 \n", "2 0.999995 0.001234 0.002927 -0.004074 -0.001232 0.999999 -0.000782 \n", "3 0.999991 0.000727 0.004202 -0.006533 -0.000725 1.000000 -0.000472 \n", "4 0.999987 0.000512 0.005128 -0.006541 -0.000510 1.000000 -0.000432 \n", "... ... ... ... ... ... ... ... \n", "1096 0.999676 0.018372 0.017593 0.315591 -0.018334 0.999829 -0.002316 \n", "1097 0.999719 0.017335 0.016196 0.324796 -0.017322 0.999850 -0.000900 \n", "1098 0.999756 0.016880 0.014275 0.339924 -0.016880 0.999858 -0.000125 \n", "1099 0.999804 0.014992 0.012966 0.347605 -0.015007 0.999887 0.001013 \n", "1100 0.999858 0.012979 0.010749 0.356986 -0.013000 0.999914 0.001834 \n", "\n", " twc(1) rwc(2,0) rwc(2,1) rwc(2,2) twc(2) \n", "0 0.003075 0.000134 0.000257 1.000000 -0.002494 \n", "1 -0.013490 -0.001882 0.001394 0.999997 1.208325 \n", "2 -0.036465 -0.002928 0.000779 0.999995 2.407830 \n", "3 -0.056512 -0.004202 0.000469 0.999991 3.606258 \n", "4 -0.077331 -0.005129 0.000430 0.999987 4.806717 \n", "... ... ... ... ... ... \n", "1096 -4.811160 -0.017633 0.001993 0.999843 298.391571 \n", "1097 -4.826771 -0.016209 0.000619 0.999868 299.177399 \n", "1098 -4.839842 -0.014275 -0.000116 0.999898 299.979767 \n", "1099 -4.852088 -0.012949 -0.001207 0.999915 300.784546 \n", "1100 -4.867191 -0.010724 -0.001974 0.999941 301.585327 \n", "\n", "[1101 rows x 12 columns]" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "traj" ] }, { "cell_type": "code", "execution_count": 13, "id": "77d852aa", "metadata": {}, "outputs": [], "source": [ "with open(\"/home/ivan/Downloads/Telegram Desktop/test_data.json\") as f:\n", " traj_example = json.load(f)" ] }, { "cell_type": "code", "execution_count": 14, "id": "2eb4a8c7", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'frame': 2,\n", " 'poses': [{'camera_id': 0,\n", " 'Euler angles': [179.0592276314431, 178.71021095455262, 12.371121058785771],\n", " 'xyz coords': [1598.4821251839471, 294.28354659765137, -65.4766852385318]},\n", " {'object_id': 2,\n", " 'Euler angles': [179.0592276314431, 178.71021095455262, 12.371121058785771],\n", " 'xyz coords': [1598.4821251839471, 294.28354659765137, -65.4766852385318]}]}" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "traj_example[\"data\"][1]" ] }, { "cell_type": "code", "execution_count": 15, "id": "3f29fcb8", "metadata": {}, "outputs": [], "source": [ "json_traj_example = {\"data\": [\n", " {\"frame\":0, \n", " \"poses\":{\n", " \"euler_angles\":[0.0,0.0,0.0],\n", " \"xyz_coords\":[0.0,0.0,0.0]\n", " }\n", " }\n", " ]\n", " }\n", "\n", "json_traj = {\"data\": [\n", " ]\n", " }" ] }, { "cell_type": "code", "execution_count": 16, "id": "b30e79c9", "metadata": {}, "outputs": [], "source": [ "# Fill the first line\n", "Twc = traj.loc[0]\n", "Rot = R.from_matrix([[Twc[0], Twc[1], Twc[2]], \n", " [Twc[4], Twc[5], Twc[6]], \n", " [Twc[8], Twc[9], Twc[10]]])\n", "euler = Rot.as_euler('xyz', degrees=True)\n", "# Coordinate system in ORB-SLAM3: ZXY. Coordinate system in UE4: XYZ \n", "entry = {\"frame\":0, \"poses\":[{\"camera_id\":0, \"euler_angles\":[-euler[1], -euler[2], -euler[0]], \n", " \"xyz_coords\":[-Twc[7], Twc[11], Twc[3]]},\n", " {\"cube_id\":0, \"euler_angles\":[0.0, 0.0, 0.0], \n", " \"xyz_coords\":[-33.81, 5.15, 0.82]},\n", " {\"cube_id\":1, \"euler_angles\":[0.0, 0.0, 0.0], \n", " \"xyz_coords\":[-11.5, 0.7, 0.56]},\n", " {\"cube_id\":2, \"euler_angles\":[0.0, 0.0, 0.0], \n", " \"xyz_coords\":[-95.0, 5.15, 0.8]},\n", " {\"cube_id\":3, \"euler_angles\":[0.0, 0.0, 0.0], \n", " \"xyz_coords\":[-130.3, 4.6, 2.1]},\n", " {\"cube_id\":4, \"euler_angles\":[0.0, 0.0, 0.0], \n", " \"xyz_coords\":[-346.5, 4.1, 5.9]},\n", " {\"cube_id\":5, \"euler_angles\":[0.0, 0.0, 0.0], \n", " \"xyz_coords\":[-346.5, 25.7, 4.8]},\n", " ]\n", " }\n", "json_traj[\"data\"].append(entry)\n", "\n", "\n", "for i in range(1, len(traj.index)):\n", " Twc = traj.loc[i]\n", " Rot = R.from_matrix([[Twc[0], Twc[1], Twc[2]], \n", " [Twc[4], Twc[5], Twc[6]], \n", " [Twc[8], Twc[9], Twc[10]]])\n", " euler = Rot.as_euler('xyz', degrees=True)\n", " entry = {\"frame\":i, \"poses\":\n", " [\n", " {\"camera_id\":0, \"euler_angles\":[-euler[1], -euler[2], -euler[0]], \n", " \"xyz_coords\":[Twc[7], Twc[11], Twc[3]]}\n", " ]\n", " }\n", " json_traj[\"data\"].append(entry)" ] }, { "cell_type": "code", "execution_count": 17, "id": "e7426f0a", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[{'frame': 0,\n", " 'poses': [{'camera_id': 0,\n", " 'euler_angles': [0.007671722302035774,\n", " -0.005487577406769043,\n", " -0.014727885522586002],\n", " 'xyz_coords': [-0.003075266, -0.002493523, 0.002967205]},\n", " {'cube_id': 0,\n", " 'euler_angles': [0.0, 0.0, 0.0],\n", " 'xyz_coords': [-33.81, 5.15, 0.82]},\n", " {'cube_id': 1,\n", " 'euler_angles': [0.0, 0.0, 0.0],\n", " 'xyz_coords': [-11.5, 0.7, 0.56]},\n", " {'cube_id': 2,\n", " 'euler_angles': [0.0, 0.0, 0.0],\n", " 'xyz_coords': [-95.0, 5.15, 0.8]},\n", " {'cube_id': 3,\n", " 'euler_angles': [0.0, 0.0, 0.0],\n", " 'xyz_coords': [-130.3, 4.6, 2.1]},\n", " {'cube_id': 4,\n", " 'euler_angles': [0.0, 0.0, 0.0],\n", " 'xyz_coords': [-346.5, 4.1, 5.9]},\n", " {'cube_id': 5,\n", " 'euler_angles': [0.0, 0.0, 0.0],\n", " 'xyz_coords': [-346.5, 25.7, 4.8]}]},\n", " {'frame': 1,\n", " 'poses': [{'camera_id': 0,\n", " 'euler_angles': [-0.10783204884531329,\n", " 0.05220453371332025,\n", " -0.07984648626708363],\n", " 'xyz_coords': [-0.013490011, 1.208325267, -0.006111192]}]},\n", " {'frame': 2,\n", " 'poses': [{'camera_id': 0,\n", " 'euler_angles': [-0.16774521663570027,\n", " 0.07056213422363951,\n", " -0.04462317100084628],\n", " 'xyz_coords': [-0.036465403, 2.407830477, -0.004074253]}]},\n", " {'frame': 3,\n", " 'poses': [{'camera_id': 0,\n", " 'euler_angles': [-0.24075229907512063,\n", " 0.04155419931766713,\n", " -0.026881656776181453],\n", " 'xyz_coords': [-0.056511778, 3.606258154, -0.006532952]}]},\n", " {'frame': 4,\n", " 'poses': [{'camera_id': 0,\n", " 'euler_angles': [-0.29384561979999074,\n", " 0.02919373907146133,\n", " -0.02462359471720489],\n", " 'xyz_coords': [-0.077330925, 4.806717396, -0.006540779]}]}]" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "json_traj[\"data\"][:5]" ] }, { "cell_type": "code", "execution_count": 18, "id": "ec96c736", "metadata": {}, "outputs": [], "source": [ "with open('cameraTraj_UE4_format.json', 'w') as f:\n", " json.dump(json_traj, f, indent=2)" ] }, { "cell_type": "code", "execution_count": null, "id": "13093b0d", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "e9095c85", "metadata": {}, "outputs": [], "source": [] } ], "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.8.10" } }, "nbformat": 4, "nbformat_minor": 5 }