File size: 3,694 Bytes
6b24496
 
 
 
57311fa
6b24496
 
 
 
 
 
 
57311fa
6b24496
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
57311fa
6b24496
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0797b33
57311fa
 
 
 
 
6b24496
 
57311fa
 
 
 
 
6b24496
57311fa
 
 
 
 
 
 
 
 
 
6b24496
 
8e4f136
 
 
6b24496
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import streamlit as st

#from pandas import DataFrame as df
import csv
import pandas as pd
import math

def main():
    st.title('Quaternion to Eular angle conversion for SLAM')
    
    st.markdown('Only support single file conversion at this time. Will be processing batch files in the future. \n')
    st.markdown('The input file should be in the format of csv where fields are separated by ";". The output will be also a csv file. \n')
    st.markdown('The input fields include\n')
    st.markdown('fileID; image file name; time stamp; pose_x; pose_y; pose_z; ori_w; ori_x; ori_y; ori_z.\n')
    st.markdown('The unit of the input pos_x, pos_y and pos_z is meter.\n')
    st.markdown('The unit of the output pos_x, pos_y and pos_z is feet, and ori_roll, ori_pitch, ori_yaw is degree.\n')

    run_the_app()

@st.cache_data()
def euler_from_quaternion(x, y, z, w):
        t0 = +2.0 * (w * x + y * z)
        t1 = +1.0 - 2.0 * (x * x + y * y)
        roll_x = math.atan2(t0, t1)
     
        t2 = +2.0 * (w * y - z * x)
        pitch_y = math.asin(t2)
     
        t3 = +2.0 * (w * z + x * y)
        t4 = +1.0 - 2.0 * (y * y + z * z)
        yaw_z = math.atan2(t3, t4)

        return math.degrees(roll_x), -math.degrees(pitch_y), math.degrees(yaw_z) # in degree

def run_the_app():
    uploaded_files = st.file_uploader(
            "Upload csv files", 
            type="csv", 
            accept_multiple_files=True)
    
    if uploaded_files:
        with open('converted.csv', 'w', newline='', encoding='utf-8') as wf:
            fieldnames = ['# pano poses v1.0: ID',
                        'filename',
                        'timestamp',
                        'pano_pos_x',
                        'pano_pos_y',
                        'pano_pos_z',
                        'pano_ori_roll',
                        'pano_ori_pitch',
                        'pano_ori_yaw'
                        ]
            writer = csv.DictWriter(wf, fieldnames=fieldnames)
            writer.writeheader()

            df = pd.read_csv(uploaded_files[0], sep = ';')
            for row in range(df.shape[0]):                 
                roll, pitch, yaw = euler_from_quaternion(float(df.at[row, ' pano_ori_x']),
                                                        float(df.at[row, ' pano_ori_y']),
                                                        float(df.at[row, ' pano_ori_z']),
                                                        float(df.at[row, ' pano_ori_w']))

                    # now yaw is the angle to x, anticlockwise. But our definition is the angle to y(north), clockwise
                yaw = -yaw + 90.0
                if yaw < -180.0:
                    yaw += 360.0
                elif yaw > 180.0:
                    yaw -= 360.0
                    
                rec = {fieldnames[0]: df.at[row, '# pano poses v1.0: ID'],
                           fieldnames[1]: df.at[row, ' filename'][1:],
                           fieldnames[2]: df.at[row, ' timestamp'],
                           fieldnames[3]: float(df.at[row, ' pano_pos_x'])*3.28084,
                           fieldnames[4]: float(df.at[row, ' pano_pos_y'])*3.28084,
                           fieldnames[5]: float(df.at[row, ' pano_pos_z'])*3.28084,
                           fieldnames[6]: roll,
                           fieldnames[7]: pitch,
                           fieldnames[8]: yaw}
                writer.writerow(rec)


            with open('converted.csv', 'rb') as f:    
                st.write('Conversion completed.')
                st.download_button("Download the converted files", f, file_name = "converted.csv")

if __name__ == "__main__":
    main()