File size: 3,184 Bytes
319dbb7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import streamlit as st
from PIL import Image
import numpy as np

def steganography_detection():
    def lsb_analysis(image):
        """

        Analyzes the Least Significant Bits (LSB) of the image pixels

        and checks for irregularities that might indicate hidden data.

        """
        img = Image.open(image)
        img = img.convert('RGB')  # Ensure RGB mode
        pixels = np.array(img)  # Convert image to numpy array

        if len(pixels.shape) != 3 or pixels.shape[2] != 3:
            raise ValueError("Unsupported image format. Please use RGB images.")

        height, width, _ = pixels.shape

        lsb_count = [0, 0]  # Count of 0s and 1s in the LSB
        lsb_distribution = []

        # Loop through each pixel and channel
        for y in range(height):
            for x in range(width):
                pixel = pixels[y, x]
                for channel in range(3):  # Process R, G, B channels
                    lsb = pixel[channel] & 1
                    lsb_count[lsb] += 1
                    lsb_distribution.append(lsb)

        total_lsb = sum(lsb_count)
        if total_lsb == 0:
            return None, None

        # Compute percentage distribution
        lsb_percentage = [count / total_lsb * 100 for count in lsb_count]
        return lsb_percentage, lsb_distribution

    st.title("Steganography Detection Tool")
    st.subheader("Analyze images for hidden messages based on LSB irregularities")

    uploaded_image = st.file_uploader("Upload an image to analyze", type=["png", "jpg", "jpeg"])
    analyze_button = st.button("Analyze Image")

    if analyze_button:
        if uploaded_image:
            try:
                st.image(uploaded_image, caption="Uploaded Image", use_column_width=True)
                lsb_percentage, lsb_distribution = lsb_analysis(uploaded_image)

                if lsb_percentage is None:
                    st.error("Unable to analyze the image.")
                else:
                    st.write("### LSB Distribution Analysis")
                    st.write(f"**Percentage of LSBs:**\n- 0s: {lsb_percentage[0]:.2f}%\n- 1s: {lsb_percentage[1]:.2f}%")

                    # Visualization
                    st.write("### Visualization of LSB Distribution")
                    st.bar_chart({
                        "LSB Values": ["0s", "1s"],
                        "Percentage": lsb_percentage
                    })

                    # Detection threshold
                    threshold = 50  # Typically, natural images have an approximately equal distribution of 0s and 1s
                    if abs(lsb_percentage[0] - lsb_percentage[1]) > 5:
                        st.warning("The image shows irregular LSB distribution, suggesting possible steganographic modification.")
                    else:
                        st.success("The LSB distribution appears normal, with no clear indication of hidden data.")

            except Exception as e:
                st.error(f"An error occurred while analyzing the image: {e}")
        else:
            st.error("Please upload an image for analysis.")