Upload 4 files
Browse files- char_bidv.txt +1 -0
- model_bidv.h5 +3 -0
- note.txt +1 -0
- run.py +102 -0
char_bidv.txt
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
['2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'j', 'k', 'm', 'n', 'p', 'q', 'r', 's', 't', 'u', 'v', 'x', 'y', 'z']
|
model_bidv.h5
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:48a3c58bdbd504220aee613bf268059a2d46b1e21ac56f5b9a960aab3a05f5be
|
| 3 |
+
size 5304984
|
note.txt
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
[C?m on qu� kh�ch d� mua h�ng t?i DichVuDark.Vn]
|
run.py
ADDED
|
@@ -0,0 +1,102 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from flask import Flask
|
| 2 |
+
from flask_cors import CORS, cross_origin
|
| 3 |
+
from flask import request
|
| 4 |
+
from flask import jsonify
|
| 5 |
+
|
| 6 |
+
import os
|
| 7 |
+
import numpy as np
|
| 8 |
+
import matplotlib.pyplot as plt
|
| 9 |
+
|
| 10 |
+
from pathlib import Path
|
| 11 |
+
from collections import Counter
|
| 12 |
+
|
| 13 |
+
import tensorflow as tf
|
| 14 |
+
from tensorflow import keras
|
| 15 |
+
from tensorflow.keras import layers
|
| 16 |
+
import time
|
| 17 |
+
# Khởi tạo Flask Server Backend
|
| 18 |
+
app = Flask(__name__)
|
| 19 |
+
# Apply Flask CORS
|
| 20 |
+
CORS(app)
|
| 21 |
+
app.config['CORS_HEADERS'] = 'Content-Type'
|
| 22 |
+
|
| 23 |
+
characters = ['2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'j', 'k', 'm', 'n', 'p', 'q', 'r', 's', 't', 'u', 'v', 'x', 'y', 'z']
|
| 24 |
+
|
| 25 |
+
batch_size = 16
|
| 26 |
+
img_width = 145
|
| 27 |
+
img_height = 50
|
| 28 |
+
downsample_factor = 4
|
| 29 |
+
|
| 30 |
+
max_length = 15
|
| 31 |
+
char_to_num = layers.StringLookup(vocabulary=list(characters), mask_token=None)
|
| 32 |
+
num_to_char = layers.StringLookup(
|
| 33 |
+
vocabulary=char_to_num.get_vocabulary(), mask_token=None, invert=True
|
| 34 |
+
)
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
class CTCLayer(layers.Layer):
|
| 43 |
+
def __init__(self, name=None):
|
| 44 |
+
super().__init__(name=name)
|
| 45 |
+
self.loss_fn = keras.backend.ctc_batch_cost
|
| 46 |
+
|
| 47 |
+
|
| 48 |
+
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
model = keras.models.load_model("model_bidv.h5", custom_objects={"CTCLayer": CTCLayer})
|
| 52 |
+
# Get the prediction model by extracting layers till the output layer
|
| 53 |
+
prediction_model = keras.models.Model(
|
| 54 |
+
model.get_layer(name="image").input, model.get_layer(name="dense2").output
|
| 55 |
+
)
|
| 56 |
+
|
| 57 |
+
# Đọc ảnh base64 và mã hóa
|
| 58 |
+
def encode_base64x(base64):
|
| 59 |
+
img = tf.io.decode_base64(base64)
|
| 60 |
+
img = tf.io.decode_png(img, channels=1)
|
| 61 |
+
img = tf.image.convert_image_dtype(img, tf.float32)
|
| 62 |
+
img = tf.image.resize(img, [img_height, img_width])
|
| 63 |
+
img = tf.transpose(img, perm=[1, 0, 2])
|
| 64 |
+
return {"image": img}
|
| 65 |
+
# A utility function to decode the output of the network
|
| 66 |
+
def decode_batch_predictions(pred):
|
| 67 |
+
input_len = np.ones(pred.shape[0]) * pred.shape[1]
|
| 68 |
+
# Use greedy search. For complex tasks, you can use beam search
|
| 69 |
+
results = keras.backend.ctc_decode(pred, input_length=input_len, greedy=True)[0][0][
|
| 70 |
+
:, :max_length
|
| 71 |
+
]
|
| 72 |
+
# Iterate over the results and get back the text
|
| 73 |
+
output_text = []
|
| 74 |
+
for res in results:
|
| 75 |
+
res = tf.strings.reduce_join(num_to_char(res)).numpy().decode("utf-8")
|
| 76 |
+
output_text.append(res)
|
| 77 |
+
return output_text
|
| 78 |
+
|
| 79 |
+
|
| 80 |
+
|
| 81 |
+
|
| 82 |
+
|
| 83 |
+
# hàm để truy cập: 127.0.0.1/run -> 127.0.0.1 là ip server
|
| 84 |
+
@app.route("/api/captcha/bidv", methods=["POST"])
|
| 85 |
+
@cross_origin(origin='*')
|
| 86 |
+
def mb():
|
| 87 |
+
content = request.json
|
| 88 |
+
start_time = time.time()
|
| 89 |
+
imgstring = content['base64']
|
| 90 |
+
image_encode = encode_base64x(imgstring.replace("+", "-").replace("/", "_"))["image"]
|
| 91 |
+
listImage = np.array([image_encode])
|
| 92 |
+
preds = prediction_model.predict(listImage)
|
| 93 |
+
pred_texts = decode_batch_predictions(preds)
|
| 94 |
+
captcha = pred_texts[0].replace('[UNK]', '').replace('-', '')
|
| 95 |
+
response = jsonify(status = "success",captcha = captcha)
|
| 96 |
+
|
| 97 |
+
return response
|
| 98 |
+
|
| 99 |
+
|
| 100 |
+
# Chạy server
|
| 101 |
+
if __name__ == '__main__':
|
| 102 |
+
app.run(host='0.0.0.0', port='2222') # -> chú ý port, không để bị trùng với port chạy cái khác
|