当前位置: 首页 > news >正文

使用阿里AI的API接口实现图片内容提取功能

参考链接地址:如何使用Qwen-VL模型_大模型服务平台百炼(Model Studio)-阿里云帮助中心

在windows下,使用python语言测试,版本:Python 3.8.9

一. 使用QVQ模型解决图片数学难题

import os
import base64
import requests#  base 64 编码格式
def encode_image(image_path):with open(image_path, "rb") as image_file:return base64.b64encode(image_file.read()).decode("utf-8")# 将xxxx/test.png替换为你本地图像的绝对路径
base64_image = encode_image("D:\\AI\\Ali_AI\\test1.png")
# 若没有配置环境变量,请用百炼API Key将下行替换为:api_key="sk-xxx"
#api_key = os.getenv("DASHSCOPE_API_KEY")
#qwen-vl-max-latest
api_key="sk-cb1a12c560fa441eabefdb2938c6cdff"
headers = {"Content-Type": "application/json", "Authorization": f"Bearer {api_key}"}
payload = {"model": "qwen-vl-max-latest","messages": [{"role": "system","content": [{"type":"text","text": "You are a helpful assistant."}]},{"role": "user","content": [{"type": "image_url",# 需要注意,传入Base64,图像格式(即image/{format})需要与支持的图片列表中的Content Type保持一致。"f"是字符串格式化的方法。# PNG图像:  f"data:image/png;base64,{base64_image}"# JPEG图像: f"data:image/jpeg;base64,{base64_image}"# WEBP图像: f"data:image/webp;base64,{base64_image}""image_url": {"url": f"data:image/png;base64,{base64_image}"},},{"type": "text", "text": "识别图片中的内容并输出"},],}],
}
response = requests.post("https://dashscope.aliyuncs.com/compatible-mode/v1/chat/completions",headers=headers,json=payload,
)content1=response.json()["choices"][0]["message"]["content"]
print(content1)
payload = {"model": "qwen2-math-72b-instruct","messages": [{"role": "system","content": "You are a helpful assistant."},{"role": "user", "content": f"{content1},给出正确的答案,不需要过程分析"}],
}
response = requests.post("https://dashscope.aliyuncs.com/compatible-mode/v1/chat/completions",headers=headers,json=payload,
)
print(response.json()["choices"][0]["message"]["content"]) 

1.先获取图片内容

使用模型:qwen-vl-max-latest

2.对数学难题进行解答

使用模型:qwen2-math-72b-instruct

二. 调用API实现图片内容提取

测试源码如下:

import os
import base64
import requests#  base 64 编码格式
def encode_image(image_path):with open(image_path, "rb") as image_file:return base64.b64encode(image_file.read()).decode("utf-8")# 将xxxx/test.png替换为你本地图像的绝对路径
base64_image = encode_image("D:\\AI\\Ali_AI\\test.jpg")
# 若没有配置环境变量,请用百炼API Key将下行替换为:api_key="sk-xxx"
#api_key = os.getenv("DASHSCOPE_API_KEY")
api_key="sk-d9b2178c495e4ea7bd3a11e0fc71984a"
headers = {"Content-Type": "application/json", "Authorization": f"Bearer {api_key}"}
payload = {"model": "qwen-vl-max-latest","messages": [{"role": "system","content": [{"type":"text","text": "You are a helpful assistant."}]},{"role": "user","content": [{"type": "image_url",# 需要注意,传入Base64,图像格式(即image/{format})需要与支持的图片列表中的Content Type保持一致。"f"是字符串格式化的方法。# PNG图像:  f"data:image/png;base64,{base64_image}"# JPEG图像: f"data:image/jpeg;base64,{base64_image}"# WEBP图像: f"data:image/webp;base64,{base64_image}""image_url": {"url": f"data:image/png;base64,{base64_image}"},},{"type": "text", "text": "请扫描图片中的文字并输出?"},],}],
}
response = requests.post("https://dashscope.aliyuncs.com/compatible-mode/v1/chat/completions",headers=headers,json=payload,
)
print(response.json()["choices"][0]["message"]["content"])

文件放置路径:

运行结果如下:

http://www.dtcms.com/a/175417.html

相关文章:

  • three.js通过GEO数据生成3D地图
  • 2025年5月HCIP题库(带解析)
  • 基于计算机视觉的试卷答题区表格识别与提取技术
  • js var a=如果ForRemove=true,是“normal“,否则为“bold“
  • 网页版部署MySQL + Qwen3-0.5B + Flask + Dify 工作流部署指南
  • 自定义SpringBoot Starter-笔记
  • 当K8S容器没有bash时高阶排查手段
  • Github上如何准确地搜索开源项目
  • (二)毛子整洁架构(CQRS/Dapper/DomianEvent Handler)
  • 8.软考高项(信息系统项目管理师)-沟通管理
  • 作为主动唤醒的节点,ECU上电如何请求通讯
  • String、StringBuilder、StringBuffer的区别
  • 翻转二叉树(简单)
  • 使用原生javascript手动实现一个可选链运算符
  • 牛客——暴力、技巧、字符与数组的使用(强强联合、字符数量)
  • 【工具】解析URL获取实际图片地址下载原始FFHQ图像
  • C++:实现线程池
  • VMware中虚拟机和主机的SSH远程连接
  • langchain使用推理模型如DeepSeek,删除回答中的推理过程<think></think>
  • 数据库实验10 函数存储
  • vitepress 复杂环境引入 mermaid
  • Python技巧:TX串口输入十六进制字符串,并获取输出,RX获取输出;循环1000次,通过分析RX输出,计算丢包率。
  • 使用docker配置Mysql
  • 深度学习:图神经网络GNN、GCN及其在推荐系统的应用
  • 人工智能 计算智能领域中分布估计算法的核心思想
  • 影刀RPA中使用AI模型
  • 【PhysUnits】1 SI Prefixes 实现解析(prefix.rs)
  • 常识补充(NVIDIA NVLink技术:打破GPU通信瓶颈的革命性互联技术)
  • 前端基础之《Vue(14)—组件通信》
  • 【LLM】什么是 MCPACPACA