<jupyter_start><jupyter_text>Sound-spy easy demo program<jupyter_code>import ipywidgets as widgets
import numpy as np
import imageio
from IPython.core.display import HTML
import sound_spy
# this is a wrapper that take a filename and publish an html <audio> tag to listen to it

def wavPlayer(filepath):
    """ will display html 5 player for compatible browser

    Parameters :
    ------------
    filepath : relative filepath with respect to the notebook directory ( where the .ipynb are not cwd)
               of the file to play

    The browser need to know how to play wav through html5.

    there is no autoplay to prevent file playing when the browser opens
    """
    
    src = """
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>Simple Test</title>
    </head>
    
    <body>
    <audio controls="controls" style="width:600px" >
      <source src="files/%s" type="audio/wav" />
      Your browser does not support the audio element.
    </audio>
    </body>
    """%(filepath)
    display(HTML(src))<jupyter_output><empty_output><jupyter_text>Hiding sound in image vs. retrieving sound from image<jupyter_code>opt = widgets.RadioButtons(
    options=['Hide', 'Retrieve'],
    value='Hide',
    disabled=False,
)

opt<jupyter_output><empty_output><jupyter_text>Seed to use<jupyter_code>seed = widgets.Text(
    value='foo',
    placeholder='Enter seed here (e.g.: torus)',
    description='Seed:',
    disabled=False
)

seed<jupyter_output><empty_output><jupyter_text>Getting list of usable images from directoryOnly .png image files are accepted. Place new images inside "examples" folder.If hiding a sound, rerun the two cells bellow after the program executed to preview the new image with sound hidden<jupyter_code>%%capture img_cap --no-stderr
!find ./examples -name "*.png"
img_list = img_cap.stdout.replace("./examples/", "").split("\n")
del img_list[-1]

img_dropdown = widgets.Dropdown(
    options=img_list,
    value=img_list[0],
)<jupyter_output><empty_output><jupyter_text>Pick an image from the list<jupyter_code># After picking rerun this cell to preview the image chosen

file = open("examples/" + img_dropdown.value, "rb")
image = file.read()

img = widgets.Image(
    value=image,
    format='png',
    width=980,
)

widgets.VBox([img_dropdown, img])<jupyter_output><empty_output><jupyter_text>Getting list of usable sounds from directoryOnly PCM-16 .wav sound files are accepted. Place new sounds inside "sounds" folder.If retrieving a sound, rerun the two cells bellow after the program executed to preview the sound file recovered<jupyter_code>%%capture snd_cap --no-stderr
!find ./sounds -name "*.wav"
snd_list = snd_cap.stdout.replace("./sounds/", "").split("\n")
del snd_list[-1]

snd_dropdown = widgets.Dropdown(
    options=snd_list,
    value=snd_list[0],
)<jupyter_output><empty_output><jupyter_text>Pick a sound from the list (if hiding)<jupyter_code>snd_dropdown
# After picking rerun this cell to preview the sound chosen

wavPlayer("sounds/" + snd_dropdown.value)<jupyter_output><empty_output><jupyter_text>Pick the number of bitsPick the number of least significant bits to use (if hiding) or being used (if retrieving)<jupyter_code>bit_dropdown = widgets.Dropdown(
    options=[1, 2, 4, 8],
    value=2,
)

bit_dropdown<jupyter_output><empty_output><jupyter_text>Name the output fileIf hiding, provide a name for the output .png file. If retrieving, provide a name for the output .wav file.<jupyter_code>out_file = widgets.Text(
    value='foo.png',
    placeholder='Enter the output file name',
    description='Output:',
    disabled=False
)

out_file<jupyter_output><empty_output><jupyter_text>Executing the programIf hiding, output image will be in the examples folderIf retrieving, output sound will be in the sounds folder<jupyter_code>%%capture

img_in = "examples/" + img_dropdown.value
snd_in = "sounds/" + snd_dropdown.value

if (opt.value == "Hide"):
    out = "examples/" + out_file.value
    sound_spy.encode(seed.value, snd_in, img_in, bit_dropdown.value, out)
elif (opt.value == "Retrieve"):
    out = "sounds/" + out_file.value
    sound_spy.decode(seed.value, img_in, bit_dropdown.value, out)<jupyter_output><empty_output><jupyter_text>Calculating the RMSE error between input image and output image<jupyter_code>img1 = imageio.imread(img_in)
img2 = imageio.imread(out)

error = sound_spy.rmse_error(img1, img2)
print ("RMSE error: " + str(error))<jupyter_output>RMSE error: 3.11749126838
{'fraction_non_alphanumeric': 0.11222222222222222, 'fraction_numerical': 0.008, 'mean_word_length': 4.647427854454203, 'pattern_counts': {'":': 0, '<': 55, '<?xml version=': 0, '>': 55, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '11', 'dir': 'jupyter-structured-clean-dedup', 'id': '151753', 'license': 'MIT', 'n_tokens_mistral': 1507, 'n_tokens_neox': 1452, 'n_words': 423, 'path': 'Demo.ipynb', 'repo_name': 'NagisaFawkes/sound-spy', 'size': '4500'}