Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,78 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from transformers import AutoImageProcessor, SiglipForImageClassification
|
3 |
+
from PIL import Image
|
4 |
+
import torch
|
5 |
+
import os
|
6 |
+
import zipfile
|
7 |
+
|
8 |
+
# Load model and processor
|
9 |
+
model_name = "prithivMLmods/Watermark-Detection-SigLIP2"
|
10 |
+
model = SiglipForImageClassification.from_pretrained(model_name)
|
11 |
+
processor = AutoImageProcessor.from_pretrained(model_name)
|
12 |
+
|
13 |
+
# Label mapping
|
14 |
+
id2label = {
|
15 |
+
"0": "No Watermark",
|
16 |
+
"1": "Watermark"
|
17 |
+
}
|
18 |
+
|
19 |
+
# Output folders
|
20 |
+
watermark_dir = "Watermarked"
|
21 |
+
no_watermark_dir = "No_Watermark"
|
22 |
+
zip_filename = "watermark_classified_images.zip"
|
23 |
+
os.makedirs(watermark_dir, exist_ok=True)
|
24 |
+
os.makedirs(no_watermark_dir, exist_ok=True)
|
25 |
+
|
26 |
+
def classify_and_save_watermarks(images):
|
27 |
+
results = {}
|
28 |
+
|
29 |
+
for image_file in images:
|
30 |
+
image_name = os.path.basename(image_file.name)
|
31 |
+
image = Image.open(image_file).convert("RGB")
|
32 |
+
inputs = processor(images=image, return_tensors="pt")
|
33 |
+
|
34 |
+
with torch.no_grad():
|
35 |
+
outputs = model(**inputs)
|
36 |
+
logits = outputs.logits
|
37 |
+
probs = torch.nn.functional.softmax(logits, dim=1).squeeze().tolist()
|
38 |
+
|
39 |
+
prediction = {id2label[str(i)]: round(probs[i], 3) for i in range(len(probs))}
|
40 |
+
max_label = max(prediction, key=prediction.get)
|
41 |
+
max_score = prediction[max_label]
|
42 |
+
|
43 |
+
# Save image to appropriate folder
|
44 |
+
filename = f"{max_label.replace(' ', '_')}_{max_score:.3f}.png"
|
45 |
+
save_dir = watermark_dir if "Watermark" in max_label else no_watermark_dir
|
46 |
+
save_path = os.path.join(save_dir, filename)
|
47 |
+
image.save(save_path)
|
48 |
+
|
49 |
+
results[image_name] = {
|
50 |
+
"predictions": prediction,
|
51 |
+
"saved_as": filename
|
52 |
+
}
|
53 |
+
|
54 |
+
# Create zip of both folders
|
55 |
+
with zipfile.ZipFile(zip_filename, "w") as zipf:
|
56 |
+
for folder in [watermark_dir, no_watermark_dir]:
|
57 |
+
for root, _, files in os.walk(folder):
|
58 |
+
for file in files:
|
59 |
+
file_path = os.path.join(root, file)
|
60 |
+
arcname = os.path.relpath(file_path, start=os.path.dirname(folder))
|
61 |
+
zipf.write(file_path, arcname)
|
62 |
+
|
63 |
+
return results, zip_filename
|
64 |
+
|
65 |
+
# Gradio interface
|
66 |
+
iface = gr.Interface(
|
67 |
+
fn=classify_and_save_watermarks,
|
68 |
+
inputs=gr.File(file_types=["image"], file_count="multiple", label="Upload Images"),
|
69 |
+
outputs=[
|
70 |
+
gr.JSON(label="Watermark Predictions"),
|
71 |
+
gr.File(label="Download Classified Images (ZIP)")
|
72 |
+
],
|
73 |
+
title="Watermark Detection and Classification",
|
74 |
+
description="Upload multiple images to detect watermarks. Images will be saved in 'Watermarked' or 'No Watermark' folders and available for download as a ZIP."
|
75 |
+
)
|
76 |
+
|
77 |
+
if __name__ == "__main__":
|
78 |
+
iface.launch(ssr_mode=True)
|