Spaces:
Sleeping
Sleeping
Upload 2 files
Browse files- detect_app.py +117 -0
- method2(0.960).pt +3 -0
detect_app.py
ADDED
@@ -0,0 +1,117 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
import warnings
|
3 |
+
import gradio as gr
|
4 |
+
import cv2
|
5 |
+
import torchvision
|
6 |
+
from torch import nn
|
7 |
+
from torchvision.models import mobilenet_v3_small
|
8 |
+
import numpy as np
|
9 |
+
|
10 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
11 |
+
warnings.filterwarnings("ignore")
|
12 |
+
|
13 |
+
|
14 |
+
def flip_text(x):
|
15 |
+
return x[::-1]
|
16 |
+
|
17 |
+
|
18 |
+
def method1_prep(image):
|
19 |
+
transforms = torchvision.models.MobileNet_V3_Small_Weights.IMAGENET1K_V1.transforms()
|
20 |
+
image = torch.from_numpy(image).permute(2, 0, 1)
|
21 |
+
return transforms(image)
|
22 |
+
|
23 |
+
|
24 |
+
def method2_prep(image):
|
25 |
+
transforms = torchvision.transforms.Compose([
|
26 |
+
torchvision.transforms.Resize((256, 256)),
|
27 |
+
torchvision.transforms.CenterCrop((224, 224))
|
28 |
+
])
|
29 |
+
t_lower = 50
|
30 |
+
t_upper = 150
|
31 |
+
|
32 |
+
height, width = image.shape[:2]
|
33 |
+
|
34 |
+
x = (width - 1920) // 2
|
35 |
+
y = (height - 1080) // 2
|
36 |
+
|
37 |
+
image = image[y:y+1080, x:x+1920]
|
38 |
+
|
39 |
+
img = torch.from_numpy(cv2.Canny(image, t_lower, t_upper)[np.newaxis, ...])
|
40 |
+
img = torch.vstack((img, img, img))
|
41 |
+
|
42 |
+
return transforms(img.type(torch.float32))
|
43 |
+
|
44 |
+
|
45 |
+
def model1_inf(x):
|
46 |
+
print("Method 1")
|
47 |
+
|
48 |
+
image = method1_prep(x).unsqueeze(dim=0)
|
49 |
+
model = mobilenet_v3_small(weights='DEFAULT')
|
50 |
+
model.classifier[3] = nn.Linear(in_features=1024, out_features=2, bias=True)
|
51 |
+
|
52 |
+
model.load_state_dict(torch.load('./method1(0.668).pt'))
|
53 |
+
|
54 |
+
model.eval() # Set the model to evaluation mode
|
55 |
+
|
56 |
+
with torch.inference_mode():
|
57 |
+
model = model.to(device)
|
58 |
+
image = image.to(device)
|
59 |
+
output = torch.softmax(model(image), dim=1).detach().cpu()
|
60 |
+
prediction = torch.argmax(output, dim=1).item()
|
61 |
+
del model
|
62 |
+
torch.cuda.empty_cache()
|
63 |
+
if prediction == 0:
|
64 |
+
return "The image is not pixelated"
|
65 |
+
else:
|
66 |
+
return "The image is pixelated"
|
67 |
+
|
68 |
+
|
69 |
+
def model2_inf(x):
|
70 |
+
print("Method 2")
|
71 |
+
|
72 |
+
image = method2_prep(x).unsqueeze(dim=0)
|
73 |
+
model = mobilenet_v3_small(weights='DEFAULT')
|
74 |
+
model.classifier[3] = nn.Linear(in_features=1024, out_features=2, bias=True)
|
75 |
+
|
76 |
+
image_np = image[0].permute(1, 2, 0).cpu().numpy()
|
77 |
+
image_np = (image_np * 255).astype(np.uint8) # Ensure the image is of type uint8
|
78 |
+
|
79 |
+
model.load_state_dict(torch.load('./method2(0.960).pt'))
|
80 |
+
#print("\nModel weights loaded successfully")
|
81 |
+
|
82 |
+
model.eval() # Set the model to evaluation mode
|
83 |
+
|
84 |
+
with torch.inference_mode():
|
85 |
+
model = model.to(device)
|
86 |
+
image = image.to(device)
|
87 |
+
output = torch.softmax(model(image), dim=1).detach().cpu()
|
88 |
+
prediction = torch.argmax(output, dim=1).item()
|
89 |
+
del model
|
90 |
+
torch.cuda.empty_cache()
|
91 |
+
if prediction == 0:
|
92 |
+
return "The image is not pixelated"
|
93 |
+
else:
|
94 |
+
return "The image is pixelated"
|
95 |
+
|
96 |
+
|
97 |
+
with gr.Blocks() as app:
|
98 |
+
gr.Markdown("### Pixelation Detection App")
|
99 |
+
|
100 |
+
# Selecting method
|
101 |
+
method = gr.Radio(["Method 1", "Method 2"], label="Select Method")
|
102 |
+
|
103 |
+
with gr.Tab("Classification by image"):
|
104 |
+
image_input = gr.Image(type="numpy", label="Upload an image")
|
105 |
+
output_text = gr.Textbox(label="Output")
|
106 |
+
|
107 |
+
def classify_image(img, method):
|
108 |
+
if method == "Method 1":
|
109 |
+
return model1_inf(img)
|
110 |
+
else:
|
111 |
+
return model2_inf(img)
|
112 |
+
|
113 |
+
method.change(fn=classify_image, inputs=[image_input, method], outputs=output_text)
|
114 |
+
image_input.change(fn=classify_image, inputs=[image_input, method], outputs=output_text)
|
115 |
+
|
116 |
+
|
117 |
+
app.launch(share=False)
|
method2(0.960).pt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:af08d7011d16d43cdf5bacf03dcd12723f26464013571e597a6160dff2081c65
|
3 |
+
size 6214802
|