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 项目安装完成就可以直接调用