123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107 |
- # -*- coding: UTF-8 -*-
- """
- 通过服务接口方式识别验证码
- 构建flask接口服务
- 接收 files={'image_file': ('captcha.jpg', BytesIO(bytes), 'application')} 参数识别验证码
- 需要配置参数:
- image_height = 40
- image_width = 80
- max_captcha = 4
- """
- import json
- from io import BytesIO
- import os
- from cnnlib.recognition_object import Recognizer
- import time
- from flask import Flask, request, jsonify, Response
- from PIL import Image
- from log_ware import LogWare
- logger = LogWare().get_logger()
- # 默认使用CPU
- os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID"
- os.environ["CUDA_VISIBLE_DEVICES"] = "-1"
- with open("conf/sample_config.json", "r") as f:
- sample_conf = json.load(f)
- # 配置参数
- env = sample_conf["env"] # 环境
- image_height = sample_conf["image_height"]
- image_width = sample_conf["image_width"]
- max_captcha = sample_conf["max_captcha"]
- api_image_dir = sample_conf["api_image_dir"]
- model_save_dir = sample_conf["model_save_dir"]
- image_suffix = sample_conf["image_suffix"] # 文件后缀
- use_labels_json_file = sample_conf['use_labels_json_file']
- webserver_recognize_url = sample_conf['webserver_recognize_url'] # 识别服务器IP
- webserver_recognize_port = sample_conf['webserver_recognize_port'] # 识别服务器端口
- if use_labels_json_file:
- with open("tools/labels.json", "r") as f:
- char_set = f.read().strip()
- else:
- char_set = sample_conf["char_set"]
- # Flask对象
- app = Flask(__name__)
- basedir = os.path.abspath(os.path.dirname(__file__))
- # 生成识别对象,需要配置参数
- recognizer = Recognizer(image_height, image_width, max_captcha, char_set, model_save_dir)
- # 如果你需要使用多个模型,可以参照原有的例子配置路由和编写逻辑
- # Q = Recognizer(image_height, image_width, max_captcha, char_set, model_save_dir)
- def response_headers(content):
- resp = Response(content)
- resp.headers['Access-Control-Allow-Origin'] = '*'
- return resp
- @app.route('/b', methods=['POST'])
- def up_image():
- if request.method == 'POST' and request.files.get('image_file'):
- timec = str(time.time()).replace(".", "")
- file = request.files.get('image_file')
- img = file.read()
- img = BytesIO(img)
- img = Image.open(img, mode="r")
- # username = request.form.get("name")
- logger.debug("接收图片尺寸: %s", img.size)
- s = time.time()
- value = recognizer.rec_image(img)
- e = time.time()
- logger.debug("识别结果: %s", value)
- if env.lower() == 'dev':
- # 保存图片
- logger.debug("保存图片: %s%s_%s.%s", api_image_dir, value, timec, image_suffix)
- file_name = "{}_{}.{}".format(value, timec, image_suffix)
- file_path = os.path.join(api_image_dir + file_name)
- img.save(file_path)
- result = {
- 'time': timec, # 时间戳
- 'value': value, # 预测的结果
- 'speed_time(ms)': int((e - s) * 1000) # 识别耗费的时间
- }
- img.close()
- return jsonify(result)
- else:
- content = json.dumps({"error_code": "1001", "error_msg": "验证码识别出错"})
- resp = response_headers(content)
- return resp
- if __name__ == '__main__':
- app.run(
- host=webserver_recognize_url,
- port=webserver_recognize_port,
- debug=True
- )
|