Skip to content

Commit

Permalink
audio recording is not working well. The audio detected is too quite
Browse files Browse the repository at this point in the history
  • Loading branch information
QT138-ILab committed Feb 14, 2020
1 parent 497432d commit bf813eb
Show file tree
Hide file tree
Showing 9 changed files with 51 additions and 13 deletions.
Binary file modified cordial_dialogue/scripts/data/1.ogg
Binary file not shown.
Binary file modified cordial_dialogue/scripts/data/1.wav
Binary file not shown.
4 changes: 4 additions & 0 deletions cordial_dialogue/scripts/dialogue.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ def __init__(self):
# Declare subscribers and publishers
rospy.Subscriber('/cordial/microphone/audio', AudioData, self.handle_audio_data, queue_size=1)
self.text_publisher = rospy.Publisher('cordial/dialogue/script', String, queue_size=1)
self.dialogue_data_publisher = rospy.Publisher('cordial/dialogue/data', String, queue_size=1)


def handle_audio_data(self, data):
Expand All @@ -80,6 +81,9 @@ def handle_audio_data(self, data):
def handle_lex_response(self,lex_response):
if len(lex_response.text_response) > 0:
print("The lex response is: ", lex_response)
#Stored the user data:
#if lex_response.intent_name == "questionnairePhase":
print(lex_response.slots)
#When lex failed in understanding the user
if lex_response.dialog_state == 'Failed':
self.interaction_message = 'failed_understanding'
Expand Down
Binary file removed cordial_logger/scripts/data/OutPut.avi
Binary file not shown.
Binary file removed cordial_logger/scripts/data/test.wav
Binary file not shown.
47 changes: 36 additions & 11 deletions cordial_logger/scripts/recorded_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,16 @@
from std_msgs.msg import String, Bool
from sensor_msgs.msg import Image
from audio_common_msgs.msg import AudioData
from qt_robot_speaker.msg import PlayRequest
from datetime import datetime
import wave
import pyaudio
import base64

CHANNEL = 1
SAMPLERATE = 16000
FORMAT_SIZE = pyaudio.paInt16
CHUNK_SIZE = 1024

class RecordingManager():

Expand All @@ -27,7 +31,9 @@ def __init__(self):
self.is_audio_recording = True
self.cv_bridge = CvBridge()
rospy.Subscriber("/cordial/chest_camera/video", Image, self.handle_recorded_video, queue_size=1)
rospy.Subscriber("/cordial/recording/audio/data", AudioData, self.handle_recorded_audio, queue_size=1)
#rospy.Subscriber("/cordial/recording/audio/data", PlayRequest, self.handle_recorded_audio, queue_size=1)
rospy.Subscriber("/audio", AudioData, self.handle_recorded_audio_common, queue_size=1)
#rospy.Subscriber("/cordial/dialogue/data", String, self.handle_user_prompt, queue_size=1)
rospy.Subscriber("/cordial/recording/audio", Bool, self.handle_trigger_recorded_audio, queue_size=1)
rospy.Subscriber("/cordial/recording/video", Bool, self.handle_trigger_recorded_video, queue_size=1)

Expand All @@ -40,43 +46,62 @@ def handle_trigger_recorded_video(self, data):
self.is_video_recording = False
self.video_data.release()
cv2.destroyAllWindows()


#self.text_file.close()

def handle_recorded_video(self, data):
if self.is_video_recording:
if self.first_video_frame:
print("First frame")
# Create the writer for the video
fps = data.header.stamp.secs
#fourcc = cv2.VideoWriter_fourcc('M', 'J', 'P', 'G')
fourcc = cv2.VideoWriter_fourcc(*'MJPG')
path = "/home/qtrobot/catkin_ws/src/cordial-public/cordial_logger/scripts/data/OutPut.avi"
self.video_data = cv2.VideoWriter(path, fourcc , fps, (data.width, data.height), True)
file_name = datetime.now().strftime('%Y-%m-%d_%H-%M-%S')
path = "/home/qtrobot/catkin_ws/src/cordial-public/cordial_logger/scripts/data/video/"
self.video_data = cv2.VideoWriter(path + file_name + ".avi", fourcc , fps, (data.width, data.height), True)
frame = self.cv_bridge.imgmsg_to_cv2(data, "rgb8")
self.first_video_frame = False
# Create the txt file to store the data
text_file = open(path+file_name+'.txt','wb')
text_file.write("Start")
text_file.close()
self.text_file = open(path+file_name+'.txt','a')
else:
frame = self.cv_bridge.imgmsg_to_cv2(data, "rgb8")
try:
print("Writing")
self.recorded_video_frames.append(frame)
retval, buffer = cv2.imencode('.jpg', frame)
jpg_as_text = base64.b64encode(buffer)
#self.text_file.write('\n Frame:' + str(jpg_as_text))
except:
print("Not writing!!")


def handle_recorded_audio(self,data):
self.recorded_audio_frames.append(data.data)
self.sample_width = data.audio_frame


def handle_recorded_audio_common(self,data):
#print(data.data)
#P = 2
#S = data.data
#result = [x * P for x in S]
#print(result)
self.recorded_audio_frames.append(data.data)
#self.sample_width = data.audio_frame

def handle_trigger_recorded_audio(self, data):
if not data.data:
self.is_audio_recording = False
p = pyaudio.PyAudio()
recorded_ID = "test"
outdir = "/home/qtrobot/catkin_ws/src/cordial-public/cordial_logger/scripts/data"
wf = wave.open(outdir + "/"+ recorded_ID + ".wav", 'wb')
file_name = datetime.now().strftime('%Y-%m-%d_%H-%M-%S')
outdir = "/home/qtrobot/catkin_ws/src/cordial-public/cordial_logger/scripts/data/audio/"
wf = wave.open(outdir + "/"+ file_name + ".wav", 'wb')
wf.setnchannels(CHANNEL)
wf.setsampwidth(p.get_sample_size(FORMAT_SIZE))
wf.setframerate(SAMPLERATE)
wf.writeframes(b''.join(self.recorded_frames))
wf.setnframes(CHUNK_SIZE)
wf.writeframes(b''.join(self.recorded_audio_frames))
wf.close()


Expand Down
2 changes: 2 additions & 0 deletions cordial_manager/launch/modules.launch
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@
<!-- Launch the children -->
<include file="$(find cordial_face)/launch/face_client.launch"/>
<include file="$(find qt_robot_gestures)/launch/gestures.launch"/>
<include file="$(find qt_chest_camera)/launch/chest_camera.launch" />
<include file="$(find qt_tracker)/launch/head_track.launch" />
<include file="$(find qt_face_detector)/launch/face_detect.launch" />
<!-- Launch the controller parents -->
<include file="$(find cordial_logger)/launch/logger.launch" />
<include file="$(find cordial_dialogue)/launch/dialogue.launch" />
<include file="$(find cordial_behavior)/launch/behaviors.launch" />
<include file="$(find cordial_tts)/launch/tts.launch" />
Expand Down
7 changes: 7 additions & 0 deletions cordial_manager/scripts/data/test.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@
"action": "detecting",
"goal": "detecting_face"
},
{
"name": "start_recording",
"description": "Starting the recording from the camera chest",
"running_option": "parallel",
"action": "long_sensing",
"goal": "recording"
},
{
"name": "start_face_tracking",
"description": "Starting the face tracker",
Expand Down
4 changes: 2 additions & 2 deletions cordial_sensor/scripts/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,8 @@ def handle_listening_start(self, data):
self.microphone_publisher.publish(True)

def handle_recording_start(self, data):
#self.camera_record_publisher.publish(True)
#self.microphone_record_publisher.publish(True)
self.camera_record_publisher.publish(True)
self.microphone_record_publisher.publish(True)
return

def handle_listening_done(self,data):
Expand Down

0 comments on commit bf813eb

Please sign in to comment.