test.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. import io
  2. import numpy as np
  3. from openai import OpenAI
  4. import os
  5. import base64
  6. from PIL import Image
  7. def image_to_base64(image):
  8. # 将Image对象转换为BytesIO对象
  9. image_io = io.BytesIO()
  10. image.save(image_io, format='PNG')
  11. image_io.seek(0)
  12. # 使用base64编码
  13. image_base64 = base64.b64encode(image_io.read()).decode('utf-8')
  14. return f"data:image/png;base64,{image_base64}"
  15. def image_reader(image):
  16. """图片读取器,输出PIL.Image格式的图片"""
  17. if isinstance(image,str):
  18. if image.startswith("http"):
  19. return image
  20. else:
  21. image_path = image
  22. out_image = Image.open(image_path)
  23. elif isinstance(image,np.ndarray):
  24. out_image = Image.fromarray(image)
  25. else:
  26. out_image = image
  27. out_image=out_image.convert('RGB')
  28. base64_img=image_to_base64(out_image)
  29. return base64_img
  30. # base 64 编码格式
  31. # def encode_image(image_path):
  32. # with open(image_path, "rb") as image_file:
  33. # return base64.b64encode(image_file.read()).decode("utf-8")
  34. # 将xxxx/test.png替换为你本地图像的绝对路径
  35. # base64_image = encode_image("/data/data/Mia/product_env_project/gen_sellpoint/企业微信截图_17372766091671.png")
  36. img="/data/data/Mia/product_env_project/gen_sellpoint/扫描全能王 2025-03-12 15.12_01(1).jpg"
  37. client = OpenAI(
  38. # 若没有配置环境变量,请用百炼API Key将下行替换为:api_key="sk-xxx"
  39. api_key='sk-04b63960983445f980d85ff185a17876',
  40. base_url="https://dashscope.aliyuncs.com/compatible-mode/v1",
  41. )
  42. completion = client.chat.completions.create(
  43. model="qwen-vl-max-latest",#
  44. messages=[
  45. {
  46. "role": "system",
  47. "content": [{"type":"text","text": "You are a helpful assistant."}]},
  48. {
  49. "role": "user",
  50. "content": [
  51. {
  52. "type": "image_url",
  53. # 需要注意,传入Base64,图像格式(即image/{format})需要与支持的图片列表中的Content Type保持一致。"f"是字符串格式化的方法。
  54. # PNG图像: f"data:image/png;base64,{base64_image}"
  55. # JPEG图像: f"data:image/jpeg;base64,{base64_image}"
  56. # WEBP图像: f"data:image/webp;base64,{base64_image}"
  57. "image_url": {"url": image_reader(img)},
  58. },
  59. {"type": "text", "text": "输入图片的字段信息,输出的时候有格式化一点"},
  60. ],
  61. }
  62. ],
  63. )
  64. print(completion.choices[0].message.content)
  65. # client = OpenAI(
  66. # # 此为默认路径,您可根据业务所在地域进行配置
  67. # base_url="https://ark.cn-beijing.volces.com/api/v3",
  68. # # 从环境变量中获取您的 API Key。此为默认方式,您可根据需要进行修改
  69. # api_key='817dff39-5586-4f9b-acba-55004167c0b1',
  70. # )
  71. # response = client.chat.completions.create(
  72. # # 指定您创建的方舟推理接入点 ID,此处已帮您修改为您的推理接入点 ID
  73. # model="doubao-1-5-vision-pro-32k-250115",
  74. # messages=[
  75. # {
  76. # "role": "user",
  77. # "content": [
  78. # {"type": "text", "text": "如果图片里有文字的话,请结合图片里的衣服和文本信息进行描述一下衣服,还要具体到衣服的风格"},
  79. # {
  80. # "type": "image_url",
  81. # "image_url": {
  82. # "url": image_reader(img)
  83. # },
  84. # },
  85. # ],
  86. # }
  87. # ],
  88. # )
  89. # print(response.choices[0])