-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
162 lines (139 loc) · 4.99 KB
/
main.py
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import os
from flask import Flask, jsonify, request
from PIL import Image
import cv2
import numpy as np
import tensorflow as tf
import keras_ocr
import matplotlib.pyplot as plt
import pandas as pd
from werkzeug.utils import secure_filename
from src.firebase_module import postUsers
app = Flask(__name__)
app.config['MODEL_FILE'] = 'model_cnn_new.h5'
app.config['UPLOAD_FOLDER'] = 'static/uploads/'
app.config['ALLOWED_EXTENSIONS'] = set(['png', 'jpg', 'jpeg'])
model = tf.keras.models.load_model(app.config['MODEL_FILE'], compile=False)
def allowed_file(filename):
return '.' in filename and \
filename.rsplit('.', 1)[1] in app.config['ALLOWED_EXTENSIONS']
# @app.route('/predictnik', methods=["GET", "POST"])
def predict_nik(image):
# img = Image.open(image)
# bounding box
pipeline = keras_ocr.pipeline.Pipeline()
images = [keras_ocr.tools.read(image)]
results = pipeline.recognize(images)
keras_ocr.tools.drawAnnotations(images[0], results[0])
# titik koordinat yg membentuk bounding box pada NIK
df = pd.DataFrame(results[0], columns=['text', 'bbox'])
indices = len(df.index)
index = 0
for x in range(0, indices):
if df.loc[x, 'text'] != 'nik':
index += 1
if df.loc[x, 'text'] == 'nik':
break
index_no_nik = index + 1
bbox_coordinate = df.loc[index_no_nik, 'bbox']
x1 = int(bbox_coordinate[0][0])
y1 = int(bbox_coordinate[0][1])
x2 = int(bbox_coordinate[2][0])
y2 = int(bbox_coordinate[2][1])
# crop bagian NIK sebesar bounding box
img = cv2.imread(image)
roi = img[y1: y2, x1: x2]
# NIK image transformations
gray = cv2.cvtColor(roi, cv2.COLOR_BGR2GRAY)
blur = cv2.GaussianBlur(gray, (7, 7), 0)
thresh = cv2.threshold(blur, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]
# beri bounding box setiap angka pada NIK
contours, _ = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
contours = sorted(contours, key=lambda x: cv2.boundingRect(x)[0])
preprocessed_digits = []
i = 0
for c in contours:
x, y, w, h = cv2.boundingRect(c)
cv2.rectangle(roi, (x, y), (x+w, y+h), color=(255, 0, 0), thickness=1)
digit = thresh[y:y+h, x:x+w]
i += 1
resized_digit = cv2.resize(digit, (18, 18))
padded_digit = np.pad(resized_digit, ((5, 5), (5, 5)), 'constant', constant_values=0)
preprocessed_digits.append(padded_digit)
# prediksi
digit_nik = []
index = 0
for digit in preprocessed_digits:
prediction = model.predict(digit.reshape(1, 28, 28, 1))
index +=1
digit_nik.append(np.argmax(prediction))
# hasil prediksi
nik = ''
for digit in digit_nik:
nik += str(digit)
return nik
@app.route("/")
def hello_world():
resultTest = "<html> <p>Endpoint : /predict</p><p>Method : POST</p><p>Body/Payload :</p><pre>{image: image(file image), email: [email protected]}<pre></html>"
return resultTest
@app.route("/prediction", methods=["POST"])
def predict_route():
if request.method == "POST":
image = request.files["image"]
email_user = request.form.get('email')
if image and allowed_file(image.filename):
filename = secure_filename(image.filename)
image.save(os.path.join(app.config["UPLOAD_FOLDER"], filename))
image_path = os.path.join(app.config["UPLOAD_FOLDER"], filename)
nik = predict_nik(image_path)
# if len(nik) < 10:
# return jsonify({
# "status": {
# "code": 400,
# "message": "KTP invalid!"
# },
# "data": {
# "nik": nik,
# "email": email_user
# }
# }), 400
result_post = postUsers(email_user, nik)
if result_post:
return jsonify({
"status": {
"code": 200,
"message": "Success predicting"
},
"data": {
"nik": nik,
"email": email_user
}
}), 200
else :
return jsonify({
"status": {
"code": 500,
"message": "Something wrong"
},
"data": {
"msg": 'ok'
}
}), 500
else:
return jsonify({
"status": {
"code": 400,
"message": "Invalid file format. Please upload a JPG, JPEG, or PNG image."
},
"data": None,
}), 400
else:
return jsonify({
"status": {
"code": 405,
"message": "Method not allowed"
},
"data": None,
}), 405
if __name__ == "__main__":
app.run(debug=True)