Update app.py
Browse files
app.py
CHANGED
|
@@ -1,75 +1,41 @@
|
|
| 1 |
-
import cv2
|
| 2 |
-
import numpy as np
|
| 3 |
import gradio as gr
|
| 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 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
return decrypted_image
|
| 41 |
-
|
| 42 |
-
# Create the Gradio interface
|
| 43 |
-
image_input = gr.inputs.Image(label="Upload Image")
|
| 44 |
-
password_input = gr.inputs.Textbox(label="Enter Password")
|
| 45 |
-
encrypt_button = gr.Button("Encrypt Image")
|
| 46 |
-
decrypt_button = gr.Button("Decrypt Image")
|
| 47 |
-
output_image = gr.outputs.Image(label="Output Image")
|
| 48 |
-
|
| 49 |
-
# Define the Gradio functions
|
| 50 |
-
def encrypt(image, password):
|
| 51 |
-
encrypted_data, key = encrypt_image(image, password)
|
| 52 |
-
return encrypted_data, key
|
| 53 |
-
|
| 54 |
-
def decrypt(encrypted_data, key, password):
|
| 55 |
-
decrypted_image = decrypt_image(encrypted_data, key, password)
|
| 56 |
-
return decrypted_image
|
| 57 |
-
|
| 58 |
-
# Create the Gradio app
|
| 59 |
-
app = gr.Interface(
|
| 60 |
-
fn=encrypt,
|
| 61 |
-
inputs=[image_input, password_input, encrypt_button],
|
| 62 |
-
outputs=[output_image, gr.Textbox(label="Encryption Key")],
|
| 63 |
-
examples=None,
|
| 64 |
-
)
|
| 65 |
-
|
| 66 |
-
# Add the decrypt functionality to the app
|
| 67 |
-
app.add(
|
| 68 |
-
fn=decrypt,
|
| 69 |
-
inputs=[gr.Image(label="Encrypted Image"), gr.Textbox(label="Encryption Key"), password_input, decrypt_button],
|
| 70 |
-
outputs=output_image,
|
| 71 |
-
examples=None,
|
| 72 |
)
|
| 73 |
|
| 74 |
# Launch the app
|
| 75 |
-
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import numpy as np
|
| 3 |
+
from PIL import Image
|
| 4 |
+
from skimage.util import random_noise
|
| 5 |
+
from skimage.restoration import denoise_nl_means, estimate_sigma
|
| 6 |
+
|
| 7 |
+
def add_noise(image, mean, var):
|
| 8 |
+
# Convert variance to noise standard deviation
|
| 9 |
+
sigma = var ** 0.5
|
| 10 |
+
# Add Gaussian noise to the image
|
| 11 |
+
noisy_image = random_noise(image, mode='gaussian', mean=mean, var=var)
|
| 12 |
+
noisy_image = np.array(255 * noisy_image, dtype=np.uint8) # Scale back to 0-255
|
| 13 |
+
return Image.fromarray(noisy_image)
|
| 14 |
+
|
| 15 |
+
def remove_noise(image):
|
| 16 |
+
# Convert image to grayscale for denoising (optional depending on need)
|
| 17 |
+
image = image.convert('L')
|
| 18 |
+
image_np = np.array(image)
|
| 19 |
+
# Estimate noise sigma from image
|
| 20 |
+
sigma_est = np.mean(estimate_sigma(image_np, multichannel=False))
|
| 21 |
+
# Apply Non-Local Means Denoising
|
| 22 |
+
denoised_image = denoise_nl_means(image_np, h=1.15 * sigma_est, fast_mode=True,
|
| 23 |
+
patch_size=5, patch_distance=6, multichannel=False)
|
| 24 |
+
denoised_image = np.array(255 * denoised_image, dtype=np.uint8) # Scale back to 0-255
|
| 25 |
+
return Image.fromarray(denoised_image)
|
| 26 |
+
|
| 27 |
+
# Define Gradio interface
|
| 28 |
+
interface = gr.Interface(
|
| 29 |
+
fn=[add_noise, remove_noise],
|
| 30 |
+
inputs=[
|
| 31 |
+
gr.Image(shape=None, label="Original Image"),
|
| 32 |
+
gr.Slider(0, 0.05, step=0.001, default=0, label="Mean of Gaussian Noise"),
|
| 33 |
+
gr.Slider(0, 0.01, step=0.0001, default=0.001, label="Variance of Gaussian Noise")
|
| 34 |
+
],
|
| 35 |
+
outputs=[gr.Image(label="Processed Image")],
|
| 36 |
+
title="Image Noise Addition and Removal",
|
| 37 |
+
description="Add Gaussian noise to an image and attempt to remove it. Adjust the mean and variance for different effects."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
)
|
| 39 |
|
| 40 |
# Launch the app
|
| 41 |
+
interface.launch()
|