CoolFace
Apppublic

marclelarge/knn_encoder_decoder

sourceHugging Faceapache-2.0updated 4y agoView on Hugging Face
1likes
app.py71 linesDownload Raw Back to root
1import gradio as gr2from utils import encoder, decoder, decoder_noise3 4title = "**Playing with kNN for denoising**"5description = """The encoder takes any image (on the top left) and remove a fraction of the pixels producing a noisy image (on the top right).  6The decoder takes a noisy image (on the bottom left) and achieves denoising using kNN where you can choose the number of neighbors k.  7You can directly use the decoder on the encoded image (if you used the encoder first!).  8If you want to use the webcam (instead of upload), click on the webcam Tab.9"""10 11demo = gr.Blocks()12 13with demo:14    gr.Markdown(title)15    gr.Markdown(description)16    with gr.Tab("upload"):17        with gr.Row():18            with gr.Column():19                source_img = gr.Image(source="upload", type="filepath", label="init_img")20                21                noise = gr.Slider(label='noise', minimum = 0.5, maximum = 1, step = .005, value = .95)22                with gr.Row():23                    b1 = gr.Button("Encoder")24                    examples_encoder = gr.Examples([["images/joconde.png"], ["images/braque.png"], ["images/Voronoy.jpg"]], inputs=[source_img])25            with gr.Column():26                encoded_img = gr.Image()27                with gr.Row():28                    bt = gr.Button("Decoder noisy image above")29                    30                31 32                33        with gr.Row():34            with gr.Column():35                36                noise_img = gr.Image(source="upload", type="filepath", label="init_img")37                k = gr.Slider(label='k', minimum = 1, maximum = 10, step = 1, value = 1)38                with gr.Row():39                    b2 = gr.Button("Decoder")40                    examples_decoder = gr.Examples([["images/afghan_coded.png"], ["images/wave_coded.png"], ["images/spider_coded.png"]], inputs=[noise_img])41                    #bt = gr.Button("Decoder noisy image above")42            with gr.Column():43                    decoded_img = gr.Image()44 45    with gr.Tab("webcam"):46        with gr.Row():47            with gr.Column():48                source_img_w = gr.Image(source="webcam", type="filepath", label="init_img")49                50                noise_w = gr.Slider(label='noise', minimum = 0.5, maximum = 1, step = .005, value = .95)51                with gr.Row():52                    b1_w = gr.Button("Encoder")53                    examples_encoder_w = gr.Examples([["images/joconde.png"], ["images/Voronoy.jpg"]], inputs=[source_img_w])54            with gr.Column():55                encoded_img_w = gr.Image()56                k_w = gr.Slider(label='k', minimum = 1, maximum = 10, step = 1, value = 1)57                with gr.Row():58                    bt_w = gr.Button("Decoder")   59                    examples_decoder_w = gr.Examples([["images/afghan_coded.png"], ["images/spider_coded.png"]], inputs=[encoded_img_w])60                    #bt = gr.Button("Decoder noisy image above")61            with gr.Column():62                    decoded_img_w = gr.Image()63            64 65    b1.click(encoder, inputs=[source_img, noise], outputs=encoded_img)66    b2.click(decoder, inputs=[noise_img,k], outputs=decoded_img)67    bt.click(decoder_noise, inputs=[encoded_img,k], outputs=decoded_img)68    b1_w.click(encoder, inputs=[source_img_w, noise_w], outputs=encoded_img_w)69    bt_w.click(decoder_noise, inputs=[encoded_img_w,k_w], outputs=decoded_img_w)70    71demo.launch()