Jetson nano を使用してカメラ・音声データ取得とjpeg・音声データ送信:ソースコード:publisher_member_function.py

CHUNK_SIZEが、SAMPLE_RATE/fpsより大きくすること。これより小さいと、小さいと映像と比べて音声データだけが遅れて流れるという事が発生すす。ようは、データを取り込み切れずに後ろ倒しになるため。

fpsと合わせて音声データの生データを表示しているが、これがゼロの時上手くマイクデバイスを認識できてない可能性がある。USBを抜き差しで解決することがある。

# Copyright 2016 Open Source Robotics Foundation, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


# Copyright 2022 robot-creation-diary.com
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# 整数とテキストを送信するチュートリアルの内容から、ステレオカメラデータを取得し
# 変換・送信するように変更してある。

import rclpy
from rclpy.node import Node

from cpp_pubsub.msg import VideoMsg
#from cpp_pubsub.msg import VideoMsg
from hmdcontroller_sub.CustomMjpeg import CustomMjpeg
import cv2
import time
import pyaudio
import numpy as np
# ステレオサウンドの設定
CHANNELS = 2
SAMPLE_RATE = 44100
SAMPLE_WIDTH = 2
CHUNK_SIZE = 8192  # バッファーサイズ(2の累乗)SAMPLE_RATE/fps より大きくすること

class MinimalPublisher(Node):

    def __init__(self):
        super().__init__('minimal_publisher')
        
        print("init start")
        self.publisher = self.create_publisher(VideoMsg, 'topic', 10)
        timer_period = 0.016  # seconds
        print("init 0")
        self.st = time.time()
        # カメラ関連
        # 0はカメラを指定する番号。状況に応じて変更のこと
        self.video_capture_l = CustomMjpeg(1,1280,720,30,640,360)
        # 1はカメラを指定する番号。状況に応じて変更のこと
        self.video_capture_r = CustomMjpeg(0,1280,720,30,640,360)

        p = pyaudio.PyAudio()
        # 入力ストリームを開く  
        self.audio_input_stream = p.open(format=p.get_format_from_width(SAMPLE_WIDTH),
                              channels=CHANNELS,
                              rate=SAMPLE_RATE,
                              input=True,
                              frames_per_buffer=CHUNK_SIZE)
        self.timer = self.create_timer(timer_period, self.timer_callback)

    def __del__(self):
        pass

    def timer_callback(self):

        # カメラ関連
        frame_data_l = self.video_capture_l.get_jpeg_data()
        frame_data_r = self.video_capture_r.get_jpeg_data()

       # バイナリデータを読み取る
        input_data = self.audio_input_stream.read(CHUNK_SIZE)
        # バイト配列に変換する
            
        if None is not frame_data_l and None is not frame_data_r:
            # データ送信関連
            msg = VideoMsg()
            # 送信用データの代入単純に=で代入しようとすると型が違うと怒られる
            msg.img_data_l.frombytes(frame_data_l)
            msg.img_data_r.frombytes(frame_data_r)
           
            msg.audio_data.frombytes(input_data)
            self.publisher.publish(msg)
            # コンソールに何も表示されないと不安なのでデータの内容を表示

            print("FPS", 1/(time.time() - self.st ), input_data[0],input_data[1])
            
            self.st = time.time()
            pass
        else:
            if None is not frame_data_l:
                print("l", len(frame_data_l))
        
            if None is not frame_data_r:
                print("r", len(frame_data_r))
            pass
            # print("error")

def main(args=None):
    rclpy.init(args=args)

    minimal_publisher = MinimalPublisher()

    rclpy.spin(minimal_publisher)

    # Destroy the node explicitly
    # (optional - otherwise it will be done automatically
    # when the garbage collector destroys the node object)
    minimal_publisher.destroy_node()
    rclpy.shutdown()


if __name__ == '__main__':
    main()