|
基于人工智能的通用图片识别平台
开发文档
Python

通用识别(JSON / Base64)

        
import base64
import json
import time
import requests

API_URL = "http://api.zhunkuai.cn"  # 支持HTTPS

def predict_base64(username, password, typeid, image_path, imageback_path=None, timeout=60, retries=3):
    """
    使用 JSON + Base64 提交识别请求。
    - 当类型需要两张图时(如:18 缺口识别、1029/2029 背景匹配旋转、1033 拖动拼图),传入 imageback_path。
    - 注意:HTTP 超时时间建议设置为 60 秒(见文档要求)。
    """
    with open(image_path, "rb") as f:
        b64_image = base64.b64encode(f.read()).decode()
    data = {"username": username, "password": password, "typeid": str(typeid), "image": b64_image}

    if imageback_path:
        with open(imageback_path, "rb") as fb:
            data["imageback"] = base64.b64encode(fb.read()).decode()

    for attempt in range(retries):
        resp = requests.post(f"{API_URL}/predict", json=data, timeout=timeout)
        result = resp.json()
        if result.get("success"):
            # 返回示例:{"result": "AXSZ", "id": "..."}
            return result["data"]
        msg = str(result.get("message", ""))
        # 人工不足/超时等情况重试,避免脚本卡死
        if any(x in msg for x in ["人工不足", "超时", "timeout", "请延长超时时间"]):
            time.sleep(2)
            continue
        return {"error": msg}
    return {"error": "重试仍失败"}


if __name__ == "__main__":
    # 1) 数英混合(默认):3
    r1 = predict_base64(username="你的账号", password="你的密码", typeid=3, image_path="C:/Users/Administrator/Desktop/file.jpg")
    print("结果1:", r1)

    # 2) 拖动拼图(两张图:上图 image,下图 imageback):1033
    r2 = predict_base64(username="你的账号", password="你的密码", typeid=1033,
                        image_path="C:/Users/Administrator/Desktop/top.jpg",
                        imageback_path="C:/Users/Administrator/Desktop/bottom.jpg")
    print("结果2:", r2)
        
    

通用识别(multipart/form-data)

        
import requests

API_URL = "http://api.zhunkuai.cn"

def predict_multipart(username, password, typeid, image_path, imageback_path=None, timeout=60):
    """
    使用表单提交(multipart/form-data)。image 是必须;部分类型需要 imageback。
    """
    data = {"username": username, "password": password, "typeid": str(typeid)}
    files = {"image": open(image_path, "rb")}
    try:
        if imageback_path:
            files["imageback"] = open(imageback_path, "rb")
        resp = requests.post(f"{API_URL}/predict", data=data, files=files, timeout=timeout)
        result = resp.json()
        return result.get("data") if result.get("success") else {"error": result.get("message")}
    finally:
        # 关闭文件句柄
        for f in files.values():
            try:
                f.close()
            except Exception:
                pass


if __name__ == "__main__":
    # 单缺口识别(返回 X 轴坐标,只需 1 张图):33 或 34
    r1 = predict_multipart(username="你的账号", password="你的密码", typeid=33, image_path="C:/Users/Administrator/Desktop/slide.jpg")
    print("结果1:", r1)

    # 缺口识别(需要 2 张图:目标 + 缺口):18
    r2 = predict_multipart(username="你的账号", password="你的密码", typeid=18,
                           image_path="C:/Users/Administrator/Desktop/target.jpg",
                           imageback_path="C:/Users/Administrator/Desktop/hole.jpg")
    print("结果2:", r2)
        
    

报错脚本

        
import json
import requests

API_URL = "http://api.zhunkuai.cn"

def report_error(error_id, timeout=30):
    data = {"id": error_id}
    resp = requests.post(f"{API_URL}/reporterror.json", json=data, timeout=timeout)
    result = resp.json()
    return "报错成功" if result.get("success") else result.get("message")


if __name__ == "__main__":
    print(report_error(error_id="成功返回的id"))
        
    

余额查询(GET)

        
import requests

API_URL = "http://api.zhunkuai.cn"

def query_account_info(username, password, timeout=30):
    params = {"username": username, "password": password}
    resp = requests.get(f"{API_URL}/queryAccountInfo.json", params=params, timeout=timeout)
    result = resp.json()
    return result.get("data") if result.get("success") else {"error": result.get("message")}


if __name__ == "__main__":
    print(query_account_info(username="你的账号", password="你的密码"))
        
    
 
您好,有什么需要帮助的吗?