Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,182 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# import gradio as gr
|
2 |
+
import random
|
3 |
+
import os
|
4 |
+
from typing import Tuple , Dict
|
5 |
+
import time
|
6 |
+
import torch
|
7 |
+
from PIL import Image
|
8 |
+
import numpy as np
|
9 |
+
from torchvision import transforms
|
10 |
+
import torch.nn as nn
|
11 |
+
from torch.nn.functional import relu
|
12 |
+
import requests
|
13 |
+
from io import BytesIO
|
14 |
+
|
15 |
+
|
16 |
+
class GlobalAttention(nn.Module):
|
17 |
+
def __init__(self, num_channels):
|
18 |
+
super(GlobalAttention, self).__init__()
|
19 |
+
self.attention = nn.Sequential(
|
20 |
+
nn.Conv2d(num_channels, 1, kernel_size=1),
|
21 |
+
nn.BatchNorm2d(1),
|
22 |
+
nn.Sigmoid()
|
23 |
+
)
|
24 |
+
|
25 |
+
def forward(self, x):
|
26 |
+
attention_weights = self.attention(x)
|
27 |
+
return x * attention_weights
|
28 |
+
|
29 |
+
|
30 |
+
class ModelWithAttention(nn.Module):
|
31 |
+
def __init__(self, num_characters):
|
32 |
+
super(ModelWithAttention, self).__init__()
|
33 |
+
self.conv1 = nn.Conv2d(1, 64, kernel_size=(3, 3), padding='same')
|
34 |
+
self.bn1 = nn.BatchNorm2d(64)
|
35 |
+
self.pool = nn.MaxPool2d(kernel_size=(2, 2))
|
36 |
+
|
37 |
+
self.conv2 = nn.Conv2d(64, 128, kernel_size=(3, 3), padding='same')
|
38 |
+
self.bn2 = nn.BatchNorm2d(128)
|
39 |
+
|
40 |
+
self.conv3 = nn.Conv2d(128, 256, kernel_size=(3, 3), padding='same')
|
41 |
+
self.bn3 = nn.BatchNorm2d(256)
|
42 |
+
|
43 |
+
self.conv4 = nn.Conv2d(256, 512, kernel_size=(3, 3), padding='same')
|
44 |
+
self.bn4 = nn.BatchNorm2d(512)
|
45 |
+
self.pool2 = nn.MaxPool2d(kernel_size=(1, 2))
|
46 |
+
|
47 |
+
self.attention = GlobalAttention(512)
|
48 |
+
|
49 |
+
self.flatten = nn.Flatten()
|
50 |
+
self.fc1 = nn.Linear(16384, 512)
|
51 |
+
self.bn5 = nn.BatchNorm1d(512)
|
52 |
+
self.dropout1 = nn.Dropout(0.5)
|
53 |
+
|
54 |
+
self.fc2 = nn.Linear(512, 512)
|
55 |
+
self.bn6 = nn.BatchNorm1d(512)
|
56 |
+
self.dropout2 = nn.Dropout(0.75)
|
57 |
+
|
58 |
+
self.output = nn.Linear(512, num_characters)
|
59 |
+
|
60 |
+
self.sm = nn.Softmax()
|
61 |
+
def forward(self, x):
|
62 |
+
x = self.pool(relu(self.bn1(self.conv1(x))))
|
63 |
+
x = self.pool(relu(self.bn2(self.conv2(x))))
|
64 |
+
x = self.pool(relu(self.bn3(self.conv3(x))))
|
65 |
+
x = self.pool2(relu(self.bn4(self.conv4(x))))
|
66 |
+
|
67 |
+
# x = self.attention(x)
|
68 |
+
|
69 |
+
x = self.flatten(x)
|
70 |
+
x = relu(self.bn5(self.fc1(x)))
|
71 |
+
x = self.dropout1(x)
|
72 |
+
|
73 |
+
x = relu(self.bn6(self.fc2(x)))
|
74 |
+
x = self.dropout2(x)
|
75 |
+
|
76 |
+
x = self.output(x)
|
77 |
+
x = self.sm(x)
|
78 |
+
return x
|
79 |
+
|
80 |
+
|
81 |
+
|
82 |
+
device = "cuda"
|
83 |
+
path = r"Captcha(Best).pt"
|
84 |
+
|
85 |
+
|
86 |
+
from torchvision import transforms
|
87 |
+
model = ModelWithAttention(10).to(device)
|
88 |
+
model.load_state_dict(torch.load(path))
|
89 |
+
|
90 |
+
|
91 |
+
transform = transforms.Compose([
|
92 |
+
transforms.ToTensor(),
|
93 |
+
transforms.Grayscale(),
|
94 |
+
transforms.Resize((64,64))
|
95 |
+
])
|
96 |
+
|
97 |
+
|
98 |
+
def predict(img= None , link:str = None) -> str:
|
99 |
+
|
100 |
+
sizes = [
|
101 |
+
[15,-5 , 15,27 ],
|
102 |
+
[15,-5 , 28,40 ],
|
103 |
+
[15,-5 , 41,53 ],
|
104 |
+
[15,-5 , 53,65 ],
|
105 |
+
[15,-5 , 66,78 ]]
|
106 |
+
|
107 |
+
answer = ""
|
108 |
+
|
109 |
+
if img != None:
|
110 |
+
imgss = np.array((img))
|
111 |
+
model.eval()
|
112 |
+
for size in (sizes):
|
113 |
+
img = imgss[size[0]:size[1], size[2]:size[3]]
|
114 |
+
img = Image.fromarray(img)
|
115 |
+
img = transform(img)
|
116 |
+
img = img.unsqueeze(0)
|
117 |
+
answer += str((torch.argmax(model(img.to(device)))).cpu().numpy())
|
118 |
+
|
119 |
+
return answer , imgss
|
120 |
+
|
121 |
+
if link != None:
|
122 |
+
|
123 |
+
response = requests.get(str(link))
|
124 |
+
|
125 |
+
if response.status_code == 200:
|
126 |
+
imgss = np.array(Image.open(BytesIO(response.content)))
|
127 |
+
# print("Image downloaded and converted to numpy array successfully!")
|
128 |
+
|
129 |
+
# print(imgss.shape)
|
130 |
+
|
131 |
+
model.eval()
|
132 |
+
for size in (sizes):
|
133 |
+
img = imgss[size[0]:size[1], size[2]:size[3]]
|
134 |
+
img = Image.fromarray(img)
|
135 |
+
img = transform(img)
|
136 |
+
img = img.unsqueeze(0)
|
137 |
+
answer += str((torch.argmax(model(img.to(device)))).cpu().numpy())
|
138 |
+
return answer , imgss
|
139 |
+
|
140 |
+
|
141 |
+
|
142 |
+
from pathlib import Path
|
143 |
+
|
144 |
+
path = r"example"
|
145 |
+
|
146 |
+
list_path = []
|
147 |
+
list_paths = os.listdir(path)
|
148 |
+
for i in list_paths:
|
149 |
+
list_path.append(os.path.join(path , i))
|
150 |
+
# print(list_path)
|
151 |
+
|
152 |
+
|
153 |
+
|
154 |
+
import gradio as gr
|
155 |
+
|
156 |
+
title = "Captcha Solver"
|
157 |
+
description = "This Model can solve persian numbers Captcha easly"
|
158 |
+
article = "Created By A.M.Parviz <3"
|
159 |
+
|
160 |
+
# Create the Gradio demo
|
161 |
+
demo = gr.Interface(
|
162 |
+
fn=predict,
|
163 |
+
inputs=[gr.Image(type="pil"),
|
164 |
+
gr.Text()],
|
165 |
+
outputs=[
|
166 |
+
gr.Label(num_top_classes=10, label="Predictions"),
|
167 |
+
gr.Image()
|
168 |
+
],
|
169 |
+
examples = [[img_path, ""] for img_path in list_path],
|
170 |
+
title=title,
|
171 |
+
description=description,
|
172 |
+
article=article,
|
173 |
+
)
|
174 |
+
demo.launch()
|
175 |
+
# share=True)
|
176 |
+
|
177 |
+
|
178 |
+
|
179 |
+
|
180 |
+
|
181 |
+
|
182 |
+
|