Genetic Algorithm解决tmenu拼图验证码

tmenu验证码目前发现有3种拖动滑块、拼图、还有一个比较恶心的是根据英文点击文字字母形状的位置。这边只讲第二种拼图的情况,先来看下图片

刚开始想用OpenCV实现乱序碎片复原的,后来看到gaps项目感觉挺不错的。看了源码后感觉可以直接用。安装gaps后做了个web服务

import cv2 as cv
import numpy as np
import os
from sanic import Sanic
from sanic import response
from multiprocessing import Process
from gaps.genetic_algorithm import GeneticAlgorithm

def puzzle_image(str_image_file: str, size=90, generations=100, population=1000, debug=True) -> None:
    """Run puzzle solver.
    PUZZLE is the input puzzle image with square pieces.
    SOLUTION is the output image file for solved puzzle.
    Examples:
    $ gaps run puzzle.jpg solution.jpg --size=32 --generations=100 --population=1000
    """
    try:
        input_puzzle = cv.imread(str_image_file)
        # if size is None:
        #     detector = SizeDetector(input_puzzle)
        #     size = detector.detect()
        ga = GeneticAlgorithm(
            image=input_puzzle,
            piece_size=size,
            population_size=population,
            generations=generations,
        )
        result = ga.start_evolution(False)

        if debug:
            output_image = result.to_image()
            str_base_name = os.path.basename(str_image_file)
            str_name, str_ext = str_base_name.split('.')
            str_dir = os.path.dirname(str_image_file)
            str_solution_image = os.path.join(str_dir, "".join([str_name, '_solution.', str_ext]))
            print("str_solution_image=", str_solution_image)
            cv.imwrite(str_solution_image, output_image)
        return result._piece_mapping
    except Exception as e:
        print(f'puzzle_image str_image_file={str_image_file} error={str(e)}')
    return None


# 创建Flask服务
app = Sanic(__name__)

# 访问URL:http://127.0.0.1:8899
# 返回网页index.html
@app.route('/image/switch', methods=['POST'])
def image(request):
    # 获取POST请求中的json数据
    info_json = request.json
    str_image_file = info_json["image"]
    switch_info = puzzle_image(str_image_file)

    # 在这里处理您的信息处理逻辑
    ret_info = {} # 处理后的信息
    if switch_info is None:
        ret_info["status"] = -1
    else:
        ret_info["switch_info"] = switch_info
        ret_info["status"] = 200
    # 将处理后的信息返回给客户端
    return response.json(ret_info)


# 检测是否可以访问
@app.route('/check', methods=['GET'])
def check(request):
    # 在这里处理您的信息处理逻辑
    ret_info = {} # 处理后的信息
    ret_info["status"] = 200
    # 将处理后的信息返回给客户端
    return response.json(ret_info)

# 停止任务
@app.route("/stop")
def stop(request):
    app.add_task(app.stop())
    ret_info = {} # 处理后的信息
    ret_info["status"] = 200
    # 将处理后的信息返回给客户端
    return response.json(ret_info)

def run_svr(port):
    # 启动Flask服务,指定主机IP和端口
    app.run(host='127.0.0.1', port=port, debug=False)


# 子进程调用 
def call_sub_proc_svr(port=8899):
    sub_proc = Process(target=run_svr,
                        args=[port])
    sub_proc.start()

if __name__ == "__main__":
    run_svr(port=8899)

原图

重组后的图片

post数据返回 可以看出1和2要进行交换数据

 gaps项目地址:https://github.com/nemanja-m/gaps​​​​​​ 项目安装完成就可以直接调用 

  • 13
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值