Created
June 3, 2022 16:33
-
-
Save harrylincoln/032787e8928afc3e9c1be91ba2603969 to your computer and use it in GitHub Desktop.
Predict what baby needs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import time | |
import audioop | |
import pyaudio | |
from pushbullet import Pushbullet | |
import wave | |
from tflite_support.task import core | |
from tflite_support.task import processor | |
from tflite_support.task import audio | |
from datetime import datetime | |
# Initialisation for PyAudio | |
CHUNK = 1024 | |
FORMAT = pyaudio.paInt16 | |
CHANNELS = 1 | |
RATE = 16000 | |
RECORD_SECONDS = 1 | |
threshold = 200 | |
reading = 0 | |
previousreading = 0 | |
wav_output_filename = 'bub-30-sec-clip.wav' | |
# PyAudio Object | |
ppAudio = pyaudio.PyAudio() | |
# Initialisation for Pushbullet | |
pb = Pushbullet('your-pushbullet-api-key') | |
# hangs until script is cancelled | |
while True: | |
stream = ppAudio.open(format=FORMAT, | |
channels=CHANNELS, | |
rate=RATE, | |
input=True, | |
frames_per_buffer=CHUNK) | |
frames = [] | |
for i in range(0, int(RATE/CHUNK*RECORD_SECONDS)): | |
data = stream.read(70, exception_on_overflow=False) | |
frames.append(data) | |
time.sleep(0.001) | |
reading = audioop.max(data, 2) | |
if reading - previousreading > threshold: | |
print(reading) | |
pb.push_note("Raspberry Pi", | |
f'Baby started crying at: {datetime.now().strftime("%H:%M:%S")}') | |
stream.stop_stream() | |
stream.close() | |
pb.push_note( | |
"Raspberry Pi", f'Camera started at http://192.168.0.84:8123') | |
thirtySecStream = ppAudio.open( | |
format=FORMAT, channels=CHANNELS, rate=RATE, input=True, frames_per_buffer=CHUNK) | |
thirtySecStreamFrames = [] | |
# loop through stream and append audio chunks to frame array | |
for ii in range(0, int((RATE/CHUNK)*30)): | |
thirtySecData = thirtySecStream.read( | |
CHUNK, exception_on_overflow=False) | |
thirtySecStreamFrames.append(thirtySecData) | |
print("finished recording") | |
pb.push_note( | |
"Raspberry Pi", f'Audio captured at {datetime.now().strftime("%H:%M:%S")}') | |
thirtySecStream.stop_stream() | |
thirtySecStream.close() | |
# save the audio frames as .wav file | |
wavefile = wave.open(wav_output_filename, 'wb') | |
wavefile.setnchannels(CHANNELS) | |
wavefile.setsampwidth(ppAudio.get_sample_size(FORMAT)) | |
wavefile.setframerate(RATE) | |
wavefile.writeframes(b''.join(thirtySecStreamFrames)) | |
wavefile.close() | |
# TF audio classifer | |
base_options = core.BaseOptions( | |
file_name=f'./cries_model.tflite') | |
classification_options = processor.ClassificationOptions( | |
max_results=2) | |
options = audio.AudioClassifierOptions( | |
base_options=base_options, classification_options=classification_options) | |
classifier = audio.AudioClassifier.create_from_options(options) | |
# Run inference | |
audio_file = audio.TensorAudio.create_from_wav_file( | |
f'./{wav_output_filename}', classifier.required_input_buffer_size) | |
audio_result = classifier.classify(audio_file) | |
print(f'Audio result: {audio_result}') | |
pb.push_note("Raspberry Pi", f'Predictions: {audio_result}') | |
previousreading = reading | |
stream.stop_stream() | |
stream.close() | |
# Clearing the resources | |
stream.stop_stream() | |
stream.close() | |
ppAudio.terminate() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment