File size: 1,040 Bytes
e98cc83 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
import gradio as gr
import numpy as np
import matplotlib.pyplot as plt
# Function to convert an image to an audio signal
def image_to_audio(image):
# Convert the image to grayscale
gray_image = np.dot(image[..., :3], [0.2989, 0.5870, 0.1140])
# Normalize the grayscale image to the range [0, 1]
normalized_image = gray_image / 255.0
# Convert the normalized image to a 1D array
audio_signal = normalized_image.flatten()
# Generate a sample rate
sample_rate = 44100
# Return the audio signal and sample rate
return (sample_rate, audio_signal)
# Create a Gradio interface
with gr.Blocks() as demo:
# Input component for the image
input_image = gr.Image(label="Input Image")
# Output component for the audio
output_audio = gr.Audio(label="Output Audio", streaming=True, autoplay=True)
# Set up the event listener to convert the image to audio
input_image.change(image_to_audio, inputs=input_image, outputs=output_audio)
# Launch the interface
demo.launch(show_error=True) |