Update app.py
Browse files
app.py
CHANGED
|
@@ -1,10 +1,6 @@
|
|
| 1 |
-
import
|
| 2 |
import hashlib
|
| 3 |
-
import qrcode
|
| 4 |
-
from PIL import Image, ImageDraw
|
| 5 |
-
import os
|
| 6 |
from PIL import Image
|
| 7 |
-
import numpy as np
|
| 8 |
import io
|
| 9 |
|
| 10 |
# Function to hash image data
|
|
@@ -13,27 +9,6 @@ def hash_image(image):
|
|
| 13 |
image_hash = hashlib.sha256(image_data).hexdigest()
|
| 14 |
return image_hash
|
| 15 |
|
| 16 |
-
# Function to generate QR code
|
| 17 |
-
def generate_qr_code(data):
|
| 18 |
-
qr = qrcode.QRCode(
|
| 19 |
-
version=1,
|
| 20 |
-
error_correction=qrcode.constants.ERROR_CORRECT_L,
|
| 21 |
-
box_size=10,
|
| 22 |
-
border=4,
|
| 23 |
-
)
|
| 24 |
-
qr.add_data(data)
|
| 25 |
-
qr.make(fit=True)
|
| 26 |
-
qr_img = qr.make_image(fill='black', back_color='white')
|
| 27 |
-
return qr_img
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
def embed_qr_code(image, qr_img):
|
| 31 |
-
if isinstance(image, np.ndarray):
|
| 32 |
-
image = Image.fromarray(image.astype('uint8')) # Convert NumPy array to PIL Image
|
| 33 |
-
image.paste(qr_img, (10, 10)) # Adjust position as needed
|
| 34 |
-
return image
|
| 35 |
-
|
| 36 |
-
|
| 37 |
# Function to save hash to file
|
| 38 |
def save_hash(hash_code, description=""):
|
| 39 |
with open("hash.txt", "a") as file:
|
|
@@ -52,7 +27,6 @@ def check_authenticity(image):
|
|
| 52 |
if saved_hash == hash_code:
|
| 53 |
return f"Image is authentic. Description: {description}"
|
| 54 |
else:
|
| 55 |
-
# Handle the case where there is no ':' or description
|
| 56 |
saved_hash = parts[0]
|
| 57 |
if saved_hash == hash_code:
|
| 58 |
return "Image is authentic, but no description provided."
|
|
@@ -60,56 +34,26 @@ def check_authenticity(image):
|
|
| 60 |
return "Hash file not found. Please process an image first."
|
| 61 |
return "Image is new or modified."
|
| 62 |
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
def process_image(image, description):
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
return image_with_qr, "Image processed and hashes stored."
|
| 90 |
-
|
| 91 |
-
# Adjust the Gradio block as needed
|
| 92 |
-
with gr.Blocks() as app:
|
| 93 |
-
with gr.Tab("Upload and Process Image"):
|
| 94 |
-
with gr.Row():
|
| 95 |
-
image_input = gr.Image(label="Upload Image", type="pil")
|
| 96 |
-
description_input = gr.Textbox(label="Description")
|
| 97 |
-
submit_button = gr.Button("Process Image")
|
| 98 |
-
image_output = gr.Image(label="Processed Image", type="pil")
|
| 99 |
-
message_output = gr.Textbox(label="Status Message")
|
| 100 |
-
submit_button.click(
|
| 101 |
-
process_image,
|
| 102 |
-
inputs=[image_input, description_input],
|
| 103 |
-
outputs=[image_output, message_output]
|
| 104 |
-
)
|
| 105 |
-
|
| 106 |
-
with gr.Tab("Check Image Authenticity"):
|
| 107 |
-
with gr.Row():
|
| 108 |
-
image_check_input = gr.Image(label="Upload Image to Verify", type="pil")
|
| 109 |
-
check_button = gr.Button("Check Authenticity")
|
| 110 |
-
authenticity_output = gr.Textbox(label="Result")
|
| 111 |
-
check_button.click(check_authenticity, inputs=[image_check_input], outputs=authenticity_output)
|
| 112 |
-
|
| 113 |
-
# Launch the application
|
| 114 |
-
app.launch()
|
| 115 |
-
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
import hashlib
|
|
|
|
|
|
|
|
|
|
| 3 |
from PIL import Image
|
|
|
|
| 4 |
import io
|
| 5 |
|
| 6 |
# Function to hash image data
|
|
|
|
| 9 |
image_hash = hashlib.sha256(image_data).hexdigest()
|
| 10 |
return image_hash
|
| 11 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
# Function to save hash to file
|
| 13 |
def save_hash(hash_code, description=""):
|
| 14 |
with open("hash.txt", "a") as file:
|
|
|
|
| 27 |
if saved_hash == hash_code:
|
| 28 |
return f"Image is authentic. Description: {description}"
|
| 29 |
else:
|
|
|
|
| 30 |
saved_hash = parts[0]
|
| 31 |
if saved_hash == hash_code:
|
| 32 |
return "Image is authentic, but no description provided."
|
|
|
|
| 34 |
return "Hash file not found. Please process an image first."
|
| 35 |
return "Image is new or modified."
|
| 36 |
|
|
|
|
|
|
|
|
|
|
| 37 |
def process_image(image, description):
|
| 38 |
+
image_format = image.format if hasattr(image, 'format') else 'PNG' # Default to PNG if format cannot be detected
|
| 39 |
+
hash_code = hash_image(image)
|
| 40 |
+
save_hash(hash_code, description)
|
| 41 |
+
return "Image hash computed and stored."
|
| 42 |
+
|
| 43 |
+
st.title('Image Authenticity Checker')
|
| 44 |
+
|
| 45 |
+
uploaded_file = st.file_uploader("Choose an image...", type=['png', 'jpg', 'jpeg'])
|
| 46 |
+
description = st.text_input("Enter a description for the image:")
|
| 47 |
+
|
| 48 |
+
if uploaded_file is not None:
|
| 49 |
+
image = Image.open(uploaded_file)
|
| 50 |
+
if st.button('Process Image'):
|
| 51 |
+
message = process_image(image, description)
|
| 52 |
+
st.success(message)
|
| 53 |
+
|
| 54 |
+
if st.button('Check Authenticity'):
|
| 55 |
+
if uploaded_file is not None:
|
| 56 |
+
result = check_authenticity(image)
|
| 57 |
+
st.info(result)
|
| 58 |
+
else:
|
| 59 |
+
st.error("Please upload an image first.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|