HunzalaRasheed1 commited on
Commit
0ebcbdf
·
verified ·
1 Parent(s): 040ea86

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +144 -0
app.py ADDED
@@ -0,0 +1,144 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import io
3
+ import base64
4
+ from PIL import Image
5
+ import tempfile
6
+ import os
7
+ import json
8
+
9
+ # Import functions from your backend file
10
+ from backend import (
11
+ detect_clones,
12
+ error_level_analysis,
13
+ extract_exif_metadata,
14
+ noise_analysis,
15
+ manipulation_likelihood,
16
+ get_clone_explanation,
17
+ pil_to_base64
18
+ )
19
+
20
+ # Create temp directory
21
+ TEMP_DIR = tempfile.mkdtemp()
22
+
23
+ # Helper functions
24
+ def save_uploaded_image(uploaded_file):
25
+ """Save a Streamlit uploaded file and return the path"""
26
+ temp_path = os.path.join(TEMP_DIR, "temp_analyze.jpg")
27
+ with open(temp_path, "wb") as f:
28
+ f.write(uploaded_file.getbuffer())
29
+ return temp_path
30
+
31
+ # Streamlit UI
32
+ st.set_page_config(page_title="Image Forensic & Fraud Detection Tool", layout="wide")
33
+
34
+ st.title("Image Forensic & Fraud Detection Tool")
35
+ st.write("Upload an image to analyze it for potential manipulation using various forensic techniques.")
36
+
37
+ # Sidebar for uploading and basic controls
38
+ with st.sidebar:
39
+ uploaded_file = st.file_uploader("Upload Image", type=["jpg", "jpeg", "png"])
40
+
41
+ if uploaded_file is not None:
42
+ analyze_button = st.button("Analyze Image", type="primary")
43
+
44
+ # Display probability meter if analysis has been done
45
+ if "probability" in st.session_state:
46
+ st.markdown("### Manipulation Probability")
47
+ st.progress(st.session_state.probability)
48
+ st.write(f"{st.session_state.probability*100:.1f}%")
49
+
50
+ # Main content area with tabs
51
+ if uploaded_file is not None:
52
+ # Display the original image
53
+ image = Image.open(uploaded_file)
54
+
55
+ # Create tabs for different analyses
56
+ tab1, tab2, tab3, tab4, tab5, tab6 = st.tabs([
57
+ "Analysis Results",
58
+ "Original Image",
59
+ "Error Level Analysis",
60
+ "Noise Analysis",
61
+ "Clone Detection",
62
+ "AI Detection Heatmap"
63
+ ])
64
+
65
+ with tab2:
66
+ st.image(image, caption="Original Image")
67
+
68
+ # Run analysis when button is clicked
69
+ if analyze_button:
70
+ # Save the image
71
+ temp_path = save_uploaded_image(uploaded_file)
72
+
73
+ with st.spinner("Analyzing image..."):
74
+ try:
75
+ # Run all analyses
76
+ exif_result = extract_exif_metadata(temp_path)
77
+ manipulation_result = manipulation_likelihood(temp_path)
78
+ clone_result, clone_count = detect_clones(temp_path)
79
+ ela_image = error_level_analysis(temp_path)
80
+ noise_image = noise_analysis(temp_path)
81
+
82
+ # Store probability in session state
83
+ st.session_state.probability = manipulation_result["probability"]
84
+
85
+ # Display results in each tab
86
+ with tab1:
87
+ st.markdown(f"""
88
+ ## Manipulation Analysis Results
89
+
90
+ **Overall Assessment: {manipulation_result['probability']*100:.1f}% likelihood of manipulation**
91
+
92
+ {manipulation_result['explanation']}
93
+
94
+ ### Clone Detection Analysis:
95
+ Found {clone_count} potential cloned regions in the image.
96
+ {get_clone_explanation(clone_count)}
97
+
98
+ ### EXIF Metadata Analysis:
99
+ {exif_result['summary']}
100
+
101
+ Indicators found: {len(exif_result['indicators'])}
102
+ """)
103
+
104
+ if exif_result['indicators']:
105
+ st.markdown("### Detailed indicators:")
106
+ for indicator in exif_result['indicators']:
107
+ st.markdown(f"- {indicator}")
108
+
109
+ # Display EXIF data as expandable JSON
110
+ with st.expander("View EXIF Metadata"):
111
+ st.json(exif_result["metadata"])
112
+
113
+ with tab3:
114
+ st.markdown("""
115
+ Error Level Analysis reveals differences in compression levels. Areas with different compression levels
116
+ often indicate modifications. Brighter regions in the visualization suggest potential manipulations.
117
+ """)
118
+ st.image(ela_image, caption="Error Level Analysis Result")
119
+
120
+ with tab4:
121
+ st.markdown("""
122
+ Noise Analysis examines the noise patterns in the image. Inconsistent noise patterns often indicate
123
+ areas that have been manipulated or added from different sources.
124
+ """)
125
+ st.image(noise_image, caption="Noise Pattern Analysis")
126
+
127
+ with tab5:
128
+ st.markdown("""
129
+ Clone Detection identifies duplicated areas within the image. Red and blue rectangles highlight
130
+ matching regions that may indicate copy-paste manipulation.
131
+ """)
132
+ st.image(clone_result, caption="Clone Detection Result")
133
+
134
+ with tab6:
135
+ st.markdown("""
136
+ This heatmap highlights regions identified by our AI model as potentially manipulated.
137
+ Red areas indicate suspicious regions with a higher likelihood of manipulation.
138
+ """)
139
+ st.image(manipulation_result["heatmap_image"], caption="AI-Detected Suspicious Regions")
140
+
141
+ except Exception as e:
142
+ st.error(f"Error during analysis: {str(e)}")
143
+ else:
144
+ st.info("Please upload an image to begin analysis.")