niclabox_server_teleoperation.py
Go to the documentation of this file.
1 # BSD 3-Clause License
2 
3 # Copyright (c) 2023, Edoardo Del Bianco, Federico Rollo
4 
5 # Redistribution and use in source and binary forms, with or without
6 # modification, are permitted provided that the following conditions are met:
7 
8 # 1. Redistributions of source code must retain the above copyright notice, this
9 # list of conditions and the following disclaimer.
10 
11 # 2. Redistributions in binary form must reproduce the above copyright notice,
12 # this list of conditions and the following disclaimer in the documentation
13 # and/or other materials provided with the distribution.
14 
15 # 3. Neither the name of the copyright holder nor the names of its
16 # contributors may be used to endorse or promote products derived from
17 # this software without specific prior written permission.
18 
19 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20 # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
23 # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25 # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27 # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 
30 
31 # receiving distance and picture from Arduino Nicla Vision (client)
32 # and showing them using the terminal, numpy and OpenCV
33 # the picture fits into one UDP packet after compression
34 
35 import time
36 import socket
37 import numpy as np
38 import cv2
39 from pydub import AudioSegment
40 
41 IMAGE_TYPE = 0b00
42 AUDIO_TYPE = 0b01
43 DISTANCE_TYPE = 0b10
44 
45 FLAG = 1
46 accumulated_audio_data = []
47 
48 
49 # server address and port (the address of the machine running this code, any available port)
50 ip = "10.240.23.87"
51 port = 8002
52 
53 # set maximum packet size
54 packet_size = 65540
55 
56 # image window init
57 cv2.namedWindow("niclabox", cv2.WINDOW_NORMAL)
58 
59 # server socket init
60 server = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
61 server.bind((ip, port))
62 print("Waiting for niclabox to stream on", ip, ":", port)
63 
64 
66  global FLAG
67 
68  packet, client_address = server.recvfrom(packet_size)
69 
70  data_type = packet[0]
71  packet = packet[1:]
72 
73  # if len(packet) < 100: # a small packet is the distance
74  if data_type == DISTANCE_TYPE:
75  distance = packet
76  distance = int.from_bytes(distance, "big")
77 
78  # Print distance in terminal
79  print("Distance (mm): ", distance)
80 
81  elif data_type == IMAGE_TYPE:
82  picture = packet
83 
84  # Show image with numpy OpenCV
85  image = cv2.imdecode(np.frombuffer(picture, np.uint8), cv2.IMREAD_COLOR)
86  cv2.namedWindow("niclabox", cv2.WINDOW_NORMAL)
87  cv2.imshow("niclabox", image)
88  if cv2.waitKey(1) == ord("q"): # Press Q to exit
89  exit(0)
90 
91  # uncomment to output to a file without using numpy and OpenCV
92  # distance_file = open("distance.txt", "w")
93  # distance_file.write(str(distance))
94  # distance_file.close()
95  # picture_file = open("picture.jpg", "wb")
96  # picture_file.write(picture)
97  # picture_file.close()
98  elif data_type == AUDIO_TYPE:
99 
100  if FLAG == 1:
101  # Convert PCM data to numpy array
102  # pcm_data = np.array(packet, dtype=np.int16)
103  pcm_data = np.frombuffer(packet, dtype=np.int16)
104  accumulated_audio_data.append(pcm_data)
105 
106  if len(accumulated_audio_data) > 100:
107  FLAG = 2
108 
109  elif FLAG == 2:
110  pcm_data = np.concatenate(accumulated_audio_data)
111 
112  # Create an AudioSegment from the PCM data
113  audio_segment = AudioSegment(
114  pcm_data.tobytes(), frame_rate=16000, sample_width=2, channels=1
115  )
116 
117  # Export AudioSegment to an MP3 file
118  audio_segment.export("recording.mp3", format="mp3")
119 
120  FLAG = 0
121 
122 
123 while True:
124  try:
126 
127  except OSError as e:
128  print("Error: ", e)
129 
130  pass # try again
niclabox_server_teleoperation.receive_and_ros
def receive_and_ros()
Definition: niclabox_server_teleoperation.py:65


nicla_vision_ros
Author(s): Davide Torielli , Damiano Gasperini , Edoardo Del Bianco , Federico Rollo
autogenerated on Sat Nov 16 2024 03:38:18