Spaces:
Running
Running
Upload 2 files
Browse files- app.py +62 -19
- requirements.txt +6 -5
app.py
CHANGED
|
@@ -97,17 +97,27 @@ def detect_manipulation(image):
|
|
| 97 |
|
| 98 |
heatmap = segmentation_map.cpu().detach().numpy()
|
| 99 |
|
| 100 |
-
# Create visualization
|
| 101 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 102 |
ax.imshow(heatmap, cmap='jet')
|
| 103 |
ax.axis('off')
|
| 104 |
-
plt.tight_layout(pad=0)
|
| 105 |
|
| 106 |
# Convert to numpy array
|
| 107 |
buf = io.BytesIO()
|
| 108 |
-
plt.savefig(buf, format='png', bbox_inches='tight', pad_inches=0, dpi=
|
| 109 |
buf.seek(0)
|
| 110 |
result_image = Image.open(buf)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 111 |
result_array = np.array(result_image)
|
| 112 |
plt.close(fig)
|
| 113 |
|
|
@@ -126,25 +136,58 @@ custom_css = """
|
|
| 126 |
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
| 127 |
-webkit-background-clip: text;
|
| 128 |
-webkit-text-fill-color: transparent;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 129 |
}
|
| 130 |
"""
|
| 131 |
|
| 132 |
-
# Create interface using Gradio
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 140 |
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
)
|
| 148 |
|
| 149 |
# Launch
|
| 150 |
if __name__ == "__main__":
|
|
|
|
| 97 |
|
| 98 |
heatmap = segmentation_map.cpu().detach().numpy()
|
| 99 |
|
| 100 |
+
# Create visualization with exact size
|
| 101 |
+
# Calculate figure size to match image dimensions
|
| 102 |
+
dpi = 100
|
| 103 |
+
fig_height = original_size[1] / dpi
|
| 104 |
+
fig_width = original_size[0] / dpi
|
| 105 |
+
|
| 106 |
+
fig = plt.figure(figsize=(fig_width, fig_height), dpi=dpi)
|
| 107 |
+
ax = fig.add_axes([0, 0, 1, 1]) # No margins
|
| 108 |
ax.imshow(heatmap, cmap='jet')
|
| 109 |
ax.axis('off')
|
|
|
|
| 110 |
|
| 111 |
# Convert to numpy array
|
| 112 |
buf = io.BytesIO()
|
| 113 |
+
plt.savefig(buf, format='png', bbox_inches='tight', pad_inches=0, dpi=dpi)
|
| 114 |
buf.seek(0)
|
| 115 |
result_image = Image.open(buf)
|
| 116 |
+
|
| 117 |
+
# Ensure exact size match by resizing if needed
|
| 118 |
+
if result_image.size != original_size:
|
| 119 |
+
result_image = result_image.resize(original_size, Image.LANCZOS)
|
| 120 |
+
|
| 121 |
result_array = np.array(result_image)
|
| 122 |
plt.close(fig)
|
| 123 |
|
|
|
|
| 136 |
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
| 137 |
-webkit-background-clip: text;
|
| 138 |
-webkit-text-fill-color: transparent;
|
| 139 |
+
background-clip: text;
|
| 140 |
+
}
|
| 141 |
+
#subtitle {
|
| 142 |
+
text-align: center;
|
| 143 |
+
font-size: 1.2em;
|
| 144 |
+
color: #666;
|
| 145 |
+
margin-bottom: 1em;
|
| 146 |
+
}
|
| 147 |
+
#info {
|
| 148 |
+
background: #e8f4fd;
|
| 149 |
+
border-left: 4px solid #2196F3;
|
| 150 |
+
padding: 15px;
|
| 151 |
+
border-radius: 5px;
|
| 152 |
+
margin-bottom: 20px;
|
| 153 |
+
color: #1976D2;
|
| 154 |
}
|
| 155 |
"""
|
| 156 |
|
| 157 |
+
# Create interface using Gradio 4.x Blocks
|
| 158 |
+
with gr.Blocks(css=custom_css, title="RADAR - Image Manipulation Detection") as demo:
|
| 159 |
+
gr.HTML('<h1 id="title">🎯 RADAR</h1>')
|
| 160 |
+
gr.HTML('<p id="subtitle">ReliAble iDentification of inpainted AReas</p>')
|
| 161 |
+
|
| 162 |
+
gr.HTML('''
|
| 163 |
+
<div id="info">
|
| 164 |
+
<strong>ℹ️ About RADAR:</strong> Upload an image to detect and localize regions
|
| 165 |
+
that have been manipulated using diffusion-based inpainting models.
|
| 166 |
+
The output shows a heatmap where red areas indicate detected manipulations.
|
| 167 |
+
</div>
|
| 168 |
+
''')
|
| 169 |
+
|
| 170 |
+
with gr.Row():
|
| 171 |
+
with gr.Column():
|
| 172 |
+
input_image = gr.Image(label="Upload Image", type="numpy")
|
| 173 |
+
with gr.Column():
|
| 174 |
+
output_image = gr.Image(label="Manipulation Heatmap", type="numpy")
|
| 175 |
+
|
| 176 |
+
submit_btn = gr.Button("🔍 Detect Manipulations", variant="primary")
|
| 177 |
+
|
| 178 |
+
# Connect the button
|
| 179 |
+
submit_btn.click(
|
| 180 |
+
fn=detect_manipulation,
|
| 181 |
+
inputs=input_image,
|
| 182 |
+
outputs=output_image
|
| 183 |
+
)
|
| 184 |
|
| 185 |
+
# Also trigger on image upload
|
| 186 |
+
input_image.change(
|
| 187 |
+
fn=detect_manipulation,
|
| 188 |
+
inputs=input_image,
|
| 189 |
+
outputs=output_image
|
| 190 |
+
)
|
|
|
|
| 191 |
|
| 192 |
# Launch
|
| 193 |
if __name__ == "__main__":
|
requirements.txt
CHANGED
|
@@ -1,7 +1,8 @@
|
|
| 1 |
-
gradio==
|
| 2 |
-
|
| 3 |
-
numpy
|
| 4 |
-
Pillow
|
| 5 |
torch
|
| 6 |
torchvision
|
| 7 |
-
transformers
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio==4.20.0
|
| 2 |
+
gradio-client==0.10.1
|
|
|
|
|
|
|
| 3 |
torch
|
| 4 |
torchvision
|
| 5 |
+
transformers
|
| 6 |
+
pillow
|
| 7 |
+
matplotlib
|
| 8 |
+
numpy
|