Spaces:
Runtime error
Runtime error
PushkarA07
commited on
Commit
·
3cf6ff9
1
Parent(s):
3b55e67
Upload app.py
Browse files
app.py
ADDED
@@ -0,0 +1,109 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import matplotlib.image as mpimg
|
2 |
+
import streamlit as st
|
3 |
+
import torch
|
4 |
+
import torchvision.transforms as transforms
|
5 |
+
from PIL import Image
|
6 |
+
from matplotlib import pyplot as plt
|
7 |
+
import pickle
|
8 |
+
from sklearn.preprocessing import LabelEncoder
|
9 |
+
|
10 |
+
DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
|
11 |
+
|
12 |
+
# Load the pre-trained model
|
13 |
+
model = torch.load('\model.pt')
|
14 |
+
model.eval()
|
15 |
+
|
16 |
+
# Define the image transformation to normalize the image
|
17 |
+
transform = transforms.Compose([
|
18 |
+
transforms.Resize((64, 64)),
|
19 |
+
transforms.ToTensor(),
|
20 |
+
transforms.Normalize((0.5,), (0.5,))
|
21 |
+
])
|
22 |
+
|
23 |
+
# Load the label encoder
|
24 |
+
with open('label_encoder.pkl', 'rb') as f:
|
25 |
+
lbl_enc = pickle.load(f)
|
26 |
+
|
27 |
+
def decode_predictions(preds, encoder):
|
28 |
+
preds = preds.permute(1, 0, 2)
|
29 |
+
preds = torch.softmax(preds, 2)
|
30 |
+
preds = torch.argmax(preds, 2)
|
31 |
+
preds = preds.detach().cpu().numpy()
|
32 |
+
cap_preds = []
|
33 |
+
for j in range(preds.shape[0]):
|
34 |
+
temp = []
|
35 |
+
for k in preds[j,:]:
|
36 |
+
k = k - 1
|
37 |
+
if k == -1:
|
38 |
+
temp.append("-")
|
39 |
+
else:
|
40 |
+
temp.append(encoder.inverse_transform([k])[0])
|
41 |
+
tp = "".join(temp)
|
42 |
+
cap_preds.append(tp)
|
43 |
+
return cap_preds
|
44 |
+
|
45 |
+
def predict_function(model, data):
|
46 |
+
model.eval()
|
47 |
+
fin_preds = []
|
48 |
+
with torch.no_grad():
|
49 |
+
# for data in data_loader:
|
50 |
+
for k, v in data.items():
|
51 |
+
data[k] = v.to(DEVICE)
|
52 |
+
batch_preds, _ = model(**data)
|
53 |
+
fin_preds.append(batch_preds)
|
54 |
+
return fin_preds
|
55 |
+
|
56 |
+
def clean_decoded_predictions(unclean_predictions):
|
57 |
+
cleaned_predictions = []
|
58 |
+
for i in unclean_predictions:
|
59 |
+
if i != "-":
|
60 |
+
cleaned_predictions.append(i)
|
61 |
+
cleaned_predictions = "".join(cleaned_predictions)
|
62 |
+
if len(cleaned_predictions) == 10:
|
63 |
+
return cleaned_predictions
|
64 |
+
else:
|
65 |
+
prev = "-"
|
66 |
+
new_cleaned_predictions = []
|
67 |
+
for char in cleaned_predictions:
|
68 |
+
if char == prev:
|
69 |
+
continue
|
70 |
+
new_cleaned_predictions.append(char)
|
71 |
+
prev = char
|
72 |
+
res = "".join(new_cleaned_predictions)
|
73 |
+
return res
|
74 |
+
|
75 |
+
def predict_captcha(model, image_path):
|
76 |
+
plt.figure(figsize=(15, 5))
|
77 |
+
image = mpimg.imread(image_path[0])
|
78 |
+
# target = image_path[0].split("/")[-1].split(".")[0]
|
79 |
+
plt.title(image_path[0].split("/")[-1])
|
80 |
+
plt.imshow(image)
|
81 |
+
|
82 |
+
valid_preds = predict_function(model, image)
|
83 |
+
current_preds = decode_predictions(valid_preds, lbl_enc)
|
84 |
+
preds = clean_decoded_predictions(current_preds[0])
|
85 |
+
# success = True if preds == target else False
|
86 |
+
return preds
|
87 |
+
|
88 |
+
# Define the Streamlit app
|
89 |
+
def app():
|
90 |
+
st.title("Captcha Breaker Project")
|
91 |
+
st.write("by - Pushkar Ambastha")
|
92 |
+
st.write("Upload an image of a captcha to recognize the text")
|
93 |
+
|
94 |
+
# Allow the user to upload an image
|
95 |
+
uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
|
96 |
+
|
97 |
+
if uploaded_file is not None:
|
98 |
+
# Load the image and transform it
|
99 |
+
img = Image.open(uploaded_file)
|
100 |
+
img = transform(img)
|
101 |
+
|
102 |
+
# Make a prediction with the model
|
103 |
+
with torch.no_grad():
|
104 |
+
prediction = predict_captcha(model, img.unsqueeze(0))
|
105 |
+
|
106 |
+
# Get the predicted text and display it
|
107 |
+
captcha_text = "".join([chr(int(x)) for x in prediction])
|
108 |
+
st.write(f"The captcha text is: {captcha_text}")
|
109 |
+
|