Update app.py
Browse files
app.py
CHANGED
@@ -1,65 +1,76 @@
|
|
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 |
import hashlib
|
7 |
-
|
|
|
|
|
8 |
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
return
|
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 |
-
|
41 |
-
|
42 |
-
denoised_image = (255 * denoised_image).astype(np.uint8) # Scale back to 0-255
|
43 |
-
return Image.fromarray(denoised_image)
|
44 |
-
|
45 |
-
# Define Gradio interface with tabs for adding and removing noise
|
46 |
-
with gr.Blocks() as interface:
|
47 |
-
gr.Markdown("### Image Noise Encryption and Decryption App")
|
48 |
-
with gr.Tab("Encrypt"):
|
49 |
with gr.Row():
|
50 |
-
image_input = gr.Image(label="
|
51 |
-
|
52 |
-
|
53 |
-
image_output = gr.Image(label="
|
54 |
-
|
55 |
|
56 |
-
with gr.Tab("
|
57 |
with gr.Row():
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
decrypt_button.click(remove_noise, inputs=[image_input_decrypt, password_input_decrypt], outputs=image_output_decrypt)
|
63 |
|
64 |
-
# Launch the
|
65 |
-
|
|
|
1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
2 |
import hashlib
|
3 |
+
import qrcode
|
4 |
+
from PIL import Image, ImageDraw
|
5 |
+
import os
|
6 |
|
7 |
+
# Function to hash image data
|
8 |
+
def hash_image(image):
|
9 |
+
image_data = image.tobytes()
|
10 |
+
image_hash = hashlib.sha256(image_data).hexdigest()
|
11 |
+
return image_hash
|
12 |
|
13 |
+
# Function to generate QR code
|
14 |
+
def generate_qr_code(data):
|
15 |
+
qr = qrcode.QRCode(
|
16 |
+
version=1,
|
17 |
+
error_correction=qrcode.constants.ERROR_CORRECT_L,
|
18 |
+
box_size=10,
|
19 |
+
border=4,
|
20 |
+
)
|
21 |
+
qr.add_data(data)
|
22 |
+
qr.make(fit=True)
|
23 |
+
qr_img = qr.make_image(fill='black', back_color='white')
|
24 |
+
return qr_img
|
25 |
|
26 |
+
# Function to embed QR code into image
|
27 |
+
def embed_qr_code(image, qr_img):
|
28 |
+
image.paste(qr_img, (10, 10)) # Adjust position as needed
|
29 |
+
return image
|
30 |
|
31 |
+
# Function to save hash to file
|
32 |
+
def save_hash(hash_code, description=""):
|
33 |
+
with open("hash.txt", "a") as file:
|
34 |
+
file.write(f"{hash_code}: {description}\n")
|
35 |
|
36 |
+
# Function to check image authenticity
|
37 |
+
def check_authenticity(image):
|
38 |
+
hash_code = hash_image(image)
|
39 |
+
with open("hash.txt", "r") as file:
|
40 |
+
hashes = file.readlines()
|
41 |
+
for line in hashes:
|
42 |
+
saved_hash, description = line.strip().split(': ', 1)
|
43 |
+
if saved_hash == hash_code:
|
44 |
+
return f"Image is authentic. Description: {description}"
|
45 |
+
return "Image is new or modified."
|
46 |
|
47 |
+
# Main processing function
|
48 |
+
def process_image(image, description):
|
49 |
+
hash_code1 = hash_image(image)
|
50 |
+
qr_img = generate_qr_code(hash_code1)
|
51 |
+
qr_img = qr_img.resize((100, 100)) # Resize QR code as needed
|
52 |
+
image_with_qr = embed_qr_code(image, qr_img)
|
53 |
+
save_hash(hash_code1, description)
|
54 |
+
hash_code2 = hash_image(image_with_qr)
|
55 |
+
save_hash(hash_code2)
|
56 |
+
return image_with_qr, "Image processed and hashes stored."
|
57 |
|
58 |
+
# Gradio interface setup
|
59 |
+
with gr.Blocks() as app:
|
60 |
+
with gr.Tab("Upload and Process Image"):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
61 |
with gr.Row():
|
62 |
+
image_input = gr.Image(label="Upload Image")
|
63 |
+
description_input = gr.Textbox(label="Description")
|
64 |
+
submit_button = gr.Button("Process Image")
|
65 |
+
image_output = gr.Image(label="Processed Image")
|
66 |
+
submit_button.click(process_image, inputs=[image_input, description_input], outputs=image_output)
|
67 |
|
68 |
+
with gr.Tab("Check Image Authenticity"):
|
69 |
with gr.Row():
|
70 |
+
image_check_input = gr.Image(label="Upload Image to Verify")
|
71 |
+
check_button = gr.Button("Check Authenticity")
|
72 |
+
authenticity_output = gr.Textbox(label="Result")
|
73 |
+
check_button.click(check_authenticity, inputs=[image_check_input], outputs=authenticity_output)
|
|
|
74 |
|
75 |
+
# Launch the application
|
76 |
+
app.launch()
|