lyimo commited on
Commit
664f2dc
·
verified ·
1 Parent(s): c60a4c5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -71
app.py CHANGED
@@ -1,75 +1,41 @@
1
- import cv2
2
- import numpy as np
3
  import gradio as gr
4
- from cryptography.fernet import Fernet
5
-
6
- def encrypt_image(image, password):
7
- # Convert the image to a NumPy array
8
- image_array = np.array(image)
9
-
10
- # Generate a key from the password
11
- key = Fernet.generate_key()
12
- fernet = Fernet(key)
13
-
14
- # Encrypt the image data using the key
15
- encrypted_image_data = fernet.encrypt(image_array.tobytes())
16
-
17
- # Add Gaussian noise to the encrypted image data
18
- noise = np.random.normal(0, 30, encrypted_image_data.shape).astype('uint8')
19
- encrypted_image_data += noise
20
-
21
- return encrypted_image_data, key
22
-
23
- def decrypt_image(encrypted_image_data, key, password):
24
- # Check if the password is correct
25
- try:
26
- fernet = Fernet(key)
27
- except Exception:
28
- return "Incorrect password"
29
-
30
- # Remove the Gaussian noise from the encrypted image data
31
- encrypted_image_data = encrypted_image_data.astype('int16') - 30
32
- encrypted_image_data = encrypted_image_data.clip(0, 255).astype('uint8')
33
-
34
- # Decrypt the image data using the key
35
- decrypted_image_data = fernet.decrypt(encrypted_image_data.tobytes())
36
-
37
- # Convert the decrypted data back to an image
38
- decrypted_image = cv2.imdecode(np.frombuffer(decrypted_image_data, np.uint8), cv2.IMREAD_COLOR)
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
- app.launch()
 
 
 
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()