-
Notifications
You must be signed in to change notification settings - Fork 0
/
flaskapp.py
42 lines (32 loc) · 1002 Bytes
/
flaskapp.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
from captcha_solver import CaptchaSolver
from flask import Flask, request, jsonify
import cv2
import numpy
from tempfile import NamedTemporaryFile
from flask_cors import CORS
app = Flask(__name__)
cors = CORS(app)
num = 1
solver = CaptchaSolver()
@app.route("/")
def success():
return jsonify("success")
@app.route("/solve", methods=["POST"])
def solve():
file = request.files["file"]
if file:
npimg = numpy.fromfile(file, numpy.uint8)
image = cv2.imdecode(npimg, cv2.IMREAD_COLOR)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
prediction = solver.predict_captcha(image)
result = 0
if prediction == "star":
result = solver.solve_star(gray)
else:
result = solver.solve_number(gray)
if result > 8 or result < 0:
result = 0
return jsonify({"predict": prediction, "data": str(result)})
return jsonify({"data": "image not found"})
if __name__ == "__main__":
app.run()