Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update prediction_simulation.py #29

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,13 @@ Script `train_set.py` saves the trainset in folder _dataset_ and, script `train_

This step is to be executed on Raspberry Pi. Please refer to [baby_cry_rpi](https://github.com/giulbia/baby_cry_rpi.git)

##### PREDICTION ON PC

this added step to test the same functionality on the pc using s shell, use sh baby_cry-sh.sh,
it uses the sox utility for macOS to use the microphone
brew install sox


##### SIMULATION

This step allows you to test the model on your computer. It uses scripts from `rpi_methods` folder.
Expand All @@ -94,4 +101,4 @@ Log files are created for each step, they are saved in folder `baby_cry_detectio


>Part of the data used for training comes from
[ESC-50: Dataset for environmental sound classification](https://github.com/karoldvl/ESC-50)
[ESC-50: Dataset for environmental sound classification](https://github.com/karoldvl/ESC-50)
70 changes: 70 additions & 0 deletions baby-cry-sh.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#!/usr/bin/env bash

function recording() {
echo "Start Recording..."
sox -t coreaudio default "$filename" trim 0 9 rate 44100 channels 1
}

filename="/Users/MT/Desktop/stg.wav"
PREDICTION_SCRIPT="/Users/MT/Desktop/baby_cry_detection/baby_cry_detection/prediction_simulation/prediction_simulation.py"
lullaby_file="/Users/MT/Desktop/baby_cry_detection/baby_cry_detection/prediction_simulation/strange-lullaby-28691.mp3"
log_file="/Users/MT/Desktop/output/prediction/prediction.txt"
PREDICTION=1
PLAYING=0
CPT=0
result=0

function predict() {
echo "Predicting..."
PREDICTION=$(python3 "$PREDICTION_SCRIPT" --file_name "$filename")
if grep -q "1" "$log_file"; then
result=1
elif grep -q "0" "$log_file"; then
result=0
fi
}

function start_playing() {
if [[ $PLAYING == 0 ]]; then
echo "Start playing"
play -q "$lullaby_file"
PLAYING=1
fi
}

function stop_playing(){
if [[ $PLAYING == 1 ]]; then
echo "Stop playing"
PLAYING=0
fi
}

function clean_up {
# Perform program exit housekeeping
echo ""
echo "Thank you for using parenting 2.0"
echo "Good Bye."
stop_playing
exit
}

trap clean_up SIGHUP SIGINT SIGTERM

echo "Welcome to Parenting 2.0"
echo ""

recording
predict

if [[ $result == 0 ]]; then
stop_playing
echo "Your Baby is relaxing"
else
CPT=$(expr $CPT + 1)
start_playing
fi

echo "Majority Vote Prediction: $PREDICTION"
echo "State of the Process PREDICTION=$PREDICTION, PLAYING=$PLAYING, COMPTEUR=$CPT"

clean_up
70 changes: 70 additions & 0 deletions baby_cry_detection/pc_main/baby-cry-sh.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#!/usr/bin/env bash

function recording() {
echo "Start Recording..."
sox -t coreaudio default "$filename" trim 0 9 rate 44100 channels 1
}

filename="/Users/MT/Desktop/stg.wav"
PREDICTION_SCRIPT="/Users/MT/Desktop/baby_cry_detection/baby_cry_detection/prediction_simulation/prediction_simulation.py"
lullaby_file="/Users/MT/Desktop/baby_cry_detection/baby_cry_detection/prediction_simulation/strange-lullaby-28691.mp3"
log_file="/Users/MT/Desktop/output/prediction/prediction.txt"
PREDICTION=1
PLAYING=0
CPT=0
result=0

function predict() {
echo "Predicting..."
PREDICTION=$(python3 "$PREDICTION_SCRIPT" --file_name "$filename")
if grep -q "1" "$log_file"; then
result=1
elif grep -q "0" "$log_file"; then
result=0
fi
}

function start_playing() {
if [[ $PLAYING == 0 ]]; then
echo "Start playing"
play -q "$lullaby_file"
PLAYING=1
fi
}

function stop_playing(){
if [[ $PLAYING == 1 ]]; then
echo "Stop playing"
PLAYING=0
fi
}

function clean_up {
# Perform program exit housekeeping
echo ""
echo "Thank you for using parenting 2.0"
echo "Good Bye."
stop_playing
exit
}

trap clean_up SIGHUP SIGINT SIGTERM

echo "Welcome to Parenting 2.0"
echo ""

recording
predict

if [[ $result == 0 ]]; then
stop_playing
echo "Your Baby is relaxing"
else
CPT=$(expr $CPT + 1)
start_playing
fi

echo "Majority Vote Prediction: $PREDICTION"
echo "State of the Process PREDICTION=$PREDICTION, PLAYING=$PLAYING, COMPTEUR=$CPT"

clean_up
4 changes: 2 additions & 2 deletions baby_cry_detection/pc_main/train_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def main():
logging.basicConfig(format='%(asctime)s - %(levelname)s - %(message)s',
datefmt='%Y-%m-%d %I:%M:%S %p',
filename=os.path.join(log_path, 'logs_pc_methods_model.log'),
filemode='w',
filemode='a',
level=logging.DEBUG)

# TRAIN MODEL
Expand All @@ -56,7 +56,7 @@ def main():
json.dump(parameters, fp)

# Save model
with open(os.path.join(save_path, 'model.pkl'), 'wb') as fp:
with open(os.path.join(save_path, 'model_1.pkl'), 'wb') as fp:
pickle.dump(best_estimator, fp)

if __name__ == '__main__':
Expand Down
2 changes: 2 additions & 0 deletions baby_cry_detection/pc_main/train_set.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ def main():
logging.info('Time taken for reading files and feature engineering: {0}'.format(stop - start))

# Save to numpy binary format
os.makedirs(save_path, exist_ok=True)

logging.info('Saving training set...')
np.save(os.path.join(save_path, 'dataset.npy'), X)
np.save(os.path.join(save_path, 'labels.npy'), y)
Expand Down
Binary file not shown.
Binary file not shown.
24 changes: 18 additions & 6 deletions baby_cry_detection/prediction_simulation/prediction_simulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import timeit
import warnings


from baby_cry_detection.rpi_methods import Reader
from baby_cry_detection.rpi_methods.feature_engineer import FeatureEngineer
from baby_cry_detection.rpi_methods.majority_voter import MajorityVoter
Expand All @@ -22,7 +23,7 @@ def main():
default='{}/../../../output/model/'.format(os.path.dirname(os.path.abspath(__file__))))
parser.add_argument('--save_path',
default='{}/../../../output/prediction/'.format(os.path.dirname(os.path.abspath(__file__))))
parser.add_argument('--file_name', default='V_2017-04-01+08_04_36=0_13.mp3')
parser.add_argument('--file_name', default='V_2017-04-01+08_04_36=0_13.mp3') # is mp3 sound whil will generate No Backend error
parser.add_argument('--log_path',
default='{}/../../'.format(os.path.dirname(os.path.abspath(__file__))))

Expand All @@ -38,7 +39,7 @@ def main():
logging.basicConfig(format='%(asctime)s - %(levelname)s - %(message)s',
datefmt='%Y-%m-%d %I:%M:%S %p',
filename=os.path.join(log_path, 'logs_prediction_test_test_model.log'),
filemode='w',
filemode='a',
level=logging.INFO)

# READ RAW SIGNAL
Expand All @@ -59,7 +60,7 @@ def main():
logging.info('Starting feature engineering')
start = timeit.default_timer()

# Feature extraction
# Feature extraction
engineer = FeatureEngineer()

play_list_processed = list()
Expand Down Expand Up @@ -97,18 +98,29 @@ def main():
majority_vote = majority_voter.vote()

stop = timeit.default_timer()
logging.info('Time taken for prediction: {0}. Is it a baby cry?? {1}'.format(stop - start, majority_vote))
logging.info('Time taken for prediction: {0}. Is it a baby cry??'.format(stop - start))

if majority_vote == 1:
logging.info('Baby crying')
result = 1
else:
logging.info('Baby is relaxing')
result = 0





# SAVE

logging.info('Saving prediction...')

# Save prediction result
with open(os.path.join(save_path, 'prediction.txt'), 'wb') as text_file:
with open(os.path.join (save_path, 'prediction.txt'), 'w') as text_file:
text_file.write("{}".format(majority_vote))

logging.info('Saved! {}'.format(os.path.join(save_path, 'prediction.txt')))

return result

if __name__ == '__main__':
main()
Binary file not shown.
Binary file not shown.