Spaces:
Sleeping
Sleeping
Upload 5 files
Browse filesFiles changed (6)
- .gitattributes+1
- app.pyAdded+89
- captcha_model.hdf5Added+3
- captcha_solver_hf.pyAdded+62
- model_labels.datAdded
- requirements.txtAdded+5
32 unmodified lines33343532 unmodified lines*.zip filter=lfs diff=lfs merge=lfs -text*.zst filter=lfs diff=lfs merge=lfs -text*tfevents* filter=lfs diff=lfs merge=lfs -text32 unmodified lines3334353632 unmodified lines*.zip filter=lfs diff=lfs merge=lfs -text*.zst filter=lfs diff=lfs merge=lfs -text*tfevents* filter=lfs diff=lfs merge=lfs -textcaptcha_model.hdf5 filter=lfs diff=lfs merge=lfs -text
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889import gradio as gr
import cv2
import numpy as np
from captcha_solver_hf import solve_captcha
def predict_captcha(image):
if image is None:
return "Please upload an image."
img_array = np.array(image)
img_cv = cv2.cvtColor(img_array, cv2.COLOR_RGB2BGR)
result = solve_captcha(img_cv)
return result
# Example images (replace these with actual CAPTCHA images)
examples = [
"example_captcha1.png",
"example_captcha2.png",
"example_captcha3.png"
]
css = """
.gradio-container {
font-family: 'Arial', sans-serif;
}
.gr-button {
color: white;
border-color: black;
background: linear-gradient(45deg, #007bff, #00bcd4);
}
.gr-button:hover {
background: linear-gradient(45deg, #0056b3, #008ba3);
}
.gr-form {
flex-direction: column;
align-items: center;
}
.gr-image-input {
width: 100% !important;
max-width: 400px;
margin: auto;
}
.footer {
margin-top: 20px;
text-align: center;
color: #666;
}
h1, h3 {
text-align: center;
}
"""
with gr.Blocks(css=css) as iface:
gr.Markdown(
"""
# CAPTCHA Solver
### Unlock the Power of AI-Driven CAPTCHA Recognition
Upload your CAPTCHA image and let our advanced AI model decipher it instantly!
"""
)
with gr.Column(scale=1):
input_image = gr.Image(type="numpy", label="Upload CAPTCHA Image")
output_text = gr.Textbox(label="Predicted Text")
solve_button = gr.Button("Solve CAPTCHA", variant="primary")
solve_button.click(fn=predict_captcha, inputs=input_image, outputs=output_text)
gr.Examples(examples, inputs=input_image)
gr.Markdown(
"""
### How It Works
1. Upload a CAPTCHA image
2. Click "Solve CAPTCHA"
3. Get instant results!
Our state-of-the-art AI model processes your image and provides accurate text recognition.
"""
)
gr.Markdown(
"""
<div class="footer">
Developed with ❤️ by Arnav Agarwal | © 2024 All Rights Reserved
</div>
"""
)
if __name__ == "__main__":
iface.launch()123version https://git-lfs.github.com/spec/v1oid sha256:dbc5e6a8bd85d16acaaede3bc8050c9ff1f9f4ea8c2602f2bee16bc846d5e11asize 80454241234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162import numpy as np
import cv2
import pickle
from keras.models import load_model
from helpers import resize_to_fit
# Ensure these files are in the same directory as your script
MODEL_FILENAME = "captcha_model.hdf5"
MODEL_LABELS_FILENAME = "model_labels.dat"
# Load the model and labels
with open(MODEL_LABELS_FILENAME, "rb") as f:
lb = pickle.load(f)
model = load_model(MODEL_FILENAME)
def solve_captcha(image):
# Convert the image to grayscale
image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# Add some extra padding around the image
image = cv2.copyMakeBorder(image, 20, 20, 20, 20, cv2.BORDER_REPLICATE)
# Threshold the image
thresh = cv2.threshold(image, 0, 255, cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU)[1]
# Find contours
contours, _ = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
letter_image_regions = []
for contour in contours:
(x, y, w, h) = cv2.boundingRect(contour)
if w / h > 1.25:
half_width = int(w / 2)
letter_image_regions.append((x, y, half_width, h))
letter_image_regions.append((x + half_width, y, half_width, h))
else:
letter_image_regions.append((x, y, w, h))
if len(letter_image_regions) != 4:
return "Error: Could not identify 4 letters in the CAPTCHA"
letter_image_regions = sorted(letter_image_regions, key=lambda x: x[0])
predictions = []
for letter_bounding_box in letter_image_regions:
x, y, w, h = letter_bounding_box
letter_image = image[y - 2:y + h + 2, x - 2:x + w + 2]
letter_image = resize_to_fit(letter_image, 20, 20)
letter_image = np.expand_dims(letter_image, axis=2)
letter_image = np.expand_dims(letter_image, axis=0)
prediction = model.predict(letter_image)
letter = lb.inverse_transform(prediction)[0]
predictions.append(letter)
captcha_text = "".join(predictions)
return captcha_text
# We don't need a test function here anymore, as testing will be done through the Gradio interface12345numpyimutilssklearntensorflowkeras.gitattributes+1
app.pyAdded+89
captcha_model.hdf5Added+3
captcha_solver_hf.pyAdded+62
model_labels.datAdded
requirements.txtAdded+5