File size: 1,159 Bytes
05a88d6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# Define function to predict captcha
import cv2
import numpy as np
import string
import gradio as gr
import tensorflow as tf
import os

model = tf.keras.models.load_model("captcha2.h5")
symbols = string.ascii_lowercase + "0123456789"


def predict(img):
    # Resize image
    img = cv2.resize(img, (200, 50), interpolation=cv2.INTER_AREA)
    img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    # Normalize image
    img = img / 255.0

    # Pass image to model
    res = np.array(model.predict(img[np.newaxis, :, :, np.newaxis]))

    # Decode prediction
    ans = np.reshape(res, (5, 36))
    l_ind = []
    probs = []
    for a in ans:
        l_ind.append(np.argmax(a))

    capt = ''
    for l in l_ind:
        capt += symbols[l]
    return capt


def captcha(img):
    prediction = predict(img)

    return prediction


gr.Interface(fn=captcha, inputs="image", outputs="text",
             title="Enter Alphanumeric Captcha Image" , examples=[
        os.path.join(os.path.dirname(__file__), "./2cegf.png"),
        os.path.join(os.path.dirname(__file__), "./yf424.png"),
        os.path.join(os.path.dirname(__file__), "./yxd7m.png")
        ]).launch()