wangrong
2025-01-23 02819b5c047bb354b0ef2374e7c6a6ac4bbd5bba
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
import socket
import re
import pandas as pd
 
'''
receive embedding data AND properties of vehicles from socket and save to csv using pandas periodically.
'''
udp_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
bind_ip = '192.168.77.68'
bind_port = 8989
udp_socket.bind((bind_ip, bind_port))
 
embedding_properties_df = pd.DataFrame(columns=["path", "color", "type", "plate", "embedding"])
count = 0
while True:
    data, addr = udp_socket.recvfrom(1024 * 64)
    s = data.decode()
    print('############################################')
    print(f'Received data: \n{s}')
    print(f'From address: {addr}')
 
    li = re.findall("\<\-\-.*?\-\-\>", s, re.DOTALL)
    print(f"total {len(li)} records parsed! going to append to data frame...")
    for l in li:
        items = str(l).split('\n')
        ''' 
        items[0] <--
        items[1] path
        items[2] properties (color, type)
        items[3] plate (empty is possible)
        items[4] embedding
        items[5] -->
        '''
        assert(len(items) == 6)
        color = items[2].split(',')[0]
        type = items[2].split(',')[1]
        # append path, color, type, plate, embedding to dataframe
        embedding_properties_df.loc[len(embedding_properties_df)] = [items[1], color, type, items[3], items[4]]
        count += 1
    print(embedding_properties_df)
    print(f"now total {len(embedding_properties_df)} records in data frame.")
    # save to file every 100 records
    if count > 100:
        count = 0
        embedding_properties_df.to_csv('embeddings_properties.csv', sep='|', header=True, index=True)
        print("new snapshot generated, saved to csv!")
    print('############################################')
 
udp_socket.close()