从零跑通:阿里云 ECS + 百炼 MaaS 大模型答题卡识别全流程

从零跑通:阿里云 ECS + 百炼 MaaS 大模型答题卡识别全流程
一、编写 server.py Flask 服务代码(云服端布置from flask import Flask, request, jsonify import base64 from openai import OpenAI app Flask(__name__) # 替换为你的正确API Key API_KEY sk-ws-H.EMXEPHH.Nzuj.MEYCIQCMTWx_h4kUIKbXNUTNKrCbpQOjx- BASE_URL https://llm-ab2cpkcjl1182sqz.cn-beijing.maas.aliyuncs.com/compatible-mode/v1 MODEL_ID qwen3.7-plus client OpenAI(api_keyAPI_KEY, base_urlBASE_URL) def img_to_base64(raw_bytes): return base64.b64encode(raw_bytes).decode(utf-8) app.route(/scan_card, methods[POST]) def scan(): try: img_file request.files[img] img_b64 img_to_base64(img_file.read()) prompt 这是30题标准单选题答题卡选项只有A/B/C/D仅判断被2B铅笔完整涂黑的唯一选项 严格按题号1~30顺序输出结果格式仅允许1:A,2:B,3:C 严禁编造答案、严禁额外文字、换行、注释、解释只输出逗号分隔结果 逐题精准定位方框位置以实际填涂色块为准 completion client.chat.completions.create( modelMODEL_ID, messages[ { role: user, content: [ {type: image_url, image_url: {url: fdata:image/jpeg;base64,{img_b64}}}, {type: text, text: prompt} ] } ], temperature0.0, top_p0.0, max_tokens200 ) result_text completion.choices[0].message.content.strip() return jsonify({code: 0, result: result_text}) except Exception as err: return jsonify({code: -1, msg: str(err)}) if __name__ __main__: app.run(host0.0.0.0, port8081, debugFalse)保存CtrlO→ 回车确认保存退出 nano 编辑器CtrlX步骤 2运行客户端脚本import requestsimport osSERVER_IP 9.1694.197.176SERVER_URL fhttp://{SERVER_IP}:8081/scan_carddef scan_image(img_path):try:with open(img_path, rb) as f:upload_file {img: f}res requests.post(SERVER_URL, filesupload_file, timeout60)data res.json()if data[code] 0:return f【{os.path.basename(img_path)}】识别结果{data[result]}else:return f【{os.path.basename(img_path)}】识别失败{data[msg]}except Exception as e:return f【{img_path}】网络请求错误{str(e)}if __name__ __main__:img_folder rD:\text-07output_txt rD:\text-07\识别结果.txtall_result []for name in os.listdir(img_folder):if name.lower().endswith((.jpg, .png, .jpeg)):full_path os.path.join(img_folder, name)print(f正在识别{name})res_text scan_image(full_path)all_result.append(res_text)print(res_text)with open(output_txt, w, encodingutf-8) as f:f.write(\n.join(all_result))print(f\n全部识别完成结果保存在{output_txt})三、Ubuntu 服务器环境配置 一步一步详细教程适配Ubuntu 22.04阿里云 ECSroot 用户全程复制命令执行前置已通过 final shell的SSH 登录服务器步骤 1更新系统软件包sudo apt update然后执行升级sudo apt upgrade -y步骤 2安装基础工具 Python3 环境2.1 安装常用工具 Python3 pippython3主程序python3-pipPython 包管理器nano文本编辑器curl/net-tools调试网络 / 端口步骤 3安装项目依赖包Flask、openai SDKpip install flask openai步骤 4配置防火墙放行端口22 SSH、8081 Flask 接口步骤 5编写 Flask 服务代码 server.py