| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102 |
- import io
- import numpy as np
- from openai import OpenAI
- import os
- import base64
- from PIL import Image
- def image_to_base64(image):
- # 将Image对象转换为BytesIO对象
- image_io = io.BytesIO()
- image.save(image_io, format='PNG')
- image_io.seek(0)
- # 使用base64编码
- image_base64 = base64.b64encode(image_io.read()).decode('utf-8')
- return f"data:image/png;base64,{image_base64}"
- def image_reader(image):
- """图片读取器,输出PIL.Image格式的图片"""
- if isinstance(image,str):
- if image.startswith("http"):
- return image
- else:
- image_path = image
- out_image = Image.open(image_path)
- elif isinstance(image,np.ndarray):
- out_image = Image.fromarray(image)
- else:
- out_image = image
- out_image=out_image.convert('RGB')
- base64_img=image_to_base64(out_image)
- return base64_img
- # 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("/data/data/Mia/product_env_project/gen_sellpoint/企业微信截图_17372766091671.png")
- img="/data/data/Mia/product_env_project/gen_sellpoint/扫描全能王 2025-03-12 15.12_01(1).jpg"
- client = OpenAI(
- # 若没有配置环境变量,请用百炼API Key将下行替换为:api_key="sk-xxx"
- api_key='sk-04b63960983445f980d85ff185a17876',
- base_url="https://dashscope.aliyuncs.com/compatible-mode/v1",
- )
- completion = client.chat.completions.create(
- 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": image_reader(img)},
- },
- {"type": "text", "text": "输入图片的字段信息,输出的时候有格式化一点"},
- ],
- }
- ],
- )
- print(completion.choices[0].message.content)
- # client = OpenAI(
- # # 此为默认路径,您可根据业务所在地域进行配置
- # base_url="https://ark.cn-beijing.volces.com/api/v3",
- # # 从环境变量中获取您的 API Key。此为默认方式,您可根据需要进行修改
- # api_key='817dff39-5586-4f9b-acba-55004167c0b1',
- # )
- # response = client.chat.completions.create(
- # # 指定您创建的方舟推理接入点 ID,此处已帮您修改为您的推理接入点 ID
- # model="doubao-1-5-vision-pro-32k-250115",
- # messages=[
- # {
- # "role": "user",
- # "content": [
- # {"type": "text", "text": "如果图片里有文字的话,请结合图片里的衣服和文本信息进行描述一下衣服,还要具体到衣服的风格"},
- # {
- # "type": "image_url",
- # "image_url": {
- # "url": image_reader(img)
- # },
- # },
- # ],
- # }
- # ],
- # )
- # print(response.choices[0])
|