import os import base64 import cv2 import dashscope # 各地域配置不同,请根据实际地域修改 dashscope.base_http_api_url = "https://dashscope.aliyuncs.com/api/v1" camera_id = 10 # 摄像头ID def capture_image_from_camera(): cap = cv2.VideoCapture(camera_id) if not cap.isOpened(): print(f"无法打开摄像头ID: {camera_id}") return None ret, frame = cap.read() cap.release() if not ret: print(f"无法捕获图像,camera_id: {camera_id}") return None _, buffer = cv2.imencode('.jpg', frame) image_base64 = base64.b64encode(buffer).decode('utf-8') return image_base64 image_base64 = capture_image_from_camera() if image_base64 is None: print(f"摄像头捕获失败,camera_id: {camera_id}") exit(1) messages = [ { "role": "user", "content": [ {"image": f"data:image/jpeg;base64,{image_base64}"}, {"text": "图中描绘的是什么景象?"}] }] response = dashscope.MultiModalConversation.call( # 若没有配置环境变量, 请用百炼API Key将下行替换为: api_key ="sk-xxx" # 各地域的API Key不同。获取API Key:https://help.aliyun.com/zh/model-studio/get-api-key api_key = os.getenv('DASHSCOPE_API_KEY'), model = 'qwen3.5-plus', # 此处以qwen3.5-plus为例,可按需更换模型名称。模型列表:https://help.aliyun.com/zh/model-studio/models messages = messages ) print(f'模型回复: {response.output.choices[0].message.content[0]["text"]}')