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

淄博做网站的网络公司怎样通过网络销售自己的产品

淄博做网站的网络公司,怎样通过网络销售自己的产品,自助建站系统php,南京建设网站哪家好【多模态处理】利用GPT逐一读取本地图片并生成描述,支持崩溃后从最新进度恢复题 代码功能:核心功能最后碎碎念 代码(使用中转平台url):代码(直接使用openai的key) 注意 代码功能: 读…

【多模态处理】利用GPT逐一读取本地图片并生成描述,支持崩溃后从最新进度恢复题

  • 代码功能:
    • 核心功能
    • 最后碎碎念
  • 代码(使用中转平台url):
    • 代码(直接使用openai的key)
  • 注意

代码功能:

读取本地图片文件,并使用GPT模型生成图像的元数据描述。生成的结果会保存到一个JSON文件中。代码还包含了检查点机制,以便在处理过程中程序崩溃时能够从最新的位置继续生成

核心功能

  1. 读取文件并设置变量:
    • 从JSON文件中读取图像路径、宽度和高度等变量。
    • 根据读取的变量设置prompt,调用GPT模型。
  2. 调用GPT模型:
    • 使用openai.ChatCompletion.create方法调用GPT模型,生成图像的元数据描述。
    • 将生成的结果保存到JSON文件中。
  3. 保存输出到JSON:
    • 每处理一张图片,就将结果追加到JSON文件中。
  4. 使用检查点机制:
    • 每处理一张图片后,保存当前处理的位置。
    • 如果处理过程中出现错误,程序可以从上次保存的位置继续处理。
  5. 处理本地图片文件:
    • 本地文件夹读取图片文件,并对每张图片进行处理

最后碎碎念

提供一个模板,方便大家理解其思想,使用的时候,可以和openai最基本的代码对比着看

代码(使用中转平台url):

使用中转平台(需要设置中转平台url):

from PIL import Image
import os
import base64
import openai
import pickle
import json# 设置API密钥和中转平台URL
API_SECRET_KEY = "your_api_secret_key"
BASE_URL = "https://api.your_base_url.com/v1"# 图像文件夹路径
image_directory_path = 'your_image_directory_path'# 设置要处理的图像数量
number_of_images_to_process = 50# 输出文件路径
output_file_path = "output_results.json"# 初始化计数器
image_counter = 0# 读取 JSON 数据文件
data_file = 'your_data_file.json'
with open(data_file, 'r') as f:data = json.load(f)def encode_image(image_path):with open(image_path, "rb") as image_file:return base64.b64encode(image_file.read()).decode('utf-8')def get_image_details(image_path):"""获取图像的详细信息,包括图像ID和尺寸。参数:image_path (str): 图像文件的路径。返回:tuple: 包含图像ID(文件名,不包括扩展名)和图像尺寸(宽度,高度)的元组。示例:get_image_details('path/to/image.jpg')  -> ('image', (800, 600))"""image_filename = os.path.basename(image_path)image_id = os.path.splitext(image_filename)[0]with Image.open(image_path) as img:image_size = img.sizereturn image_id, image_sizedef chat_completions(image_path, width, height):base64_image = encode_image(image_path)image_id, image_size = get_image_details(image_path)client = OpenAI(api_key=API_SECRET_KEY, base_url=BASE_URL)response = openai.ChatCompletion.create(model="gpt-4",messages=[{"role": "system", "content": "You are an assistant that provides metadata information about images."},{"role": "user", "content": f"Image ID: {image_id}, Width: {width}, Height: {height}"}],max_tokens=3000,timeout=999,)return response# 初始化结果字典
results_dict = {}# 检查是否存在检查点文件
checkpoint_file = "checkpoint.pkl"
if os.path.exists(checkpoint_file):with open(checkpoint_file, "rb") as f:start_index = pickle.load(f)
else:start_index = 0# 处理图像文件
for i, image in enumerate(data[start_index:], start=start_index):image_name = image['image_path']image_file = os.path.join(image_directory_path, image_name)image_width = image['width']image_height = image['height']if image_name.lower().endswith(('.png', '.jpg', '.jpeg', '.tiff', '.bmp', '.gif')):try:response = chat_completions(image_file, image_width, image_height)result = {image_name: response.choices[0].message['content']}with open(output_file_path, "a") as output_file:output_file.write(json.dumps(result) + "\n")except Exception as e:print(f"Error processing image {image_name}: {e}")continueimage_counter += 1if image_counter >= number_of_images_to_process:breakwith open(checkpoint_file, "wb") as f:pickle.dump(i+1, f)

代码(直接使用openai的key)

from PIL import Image
import os
import base64
import openai
import pickle
import json# 设置API密钥
API_SECRET_KEY = "your_api_secret_key"# 图像文件夹路径
image_directory_path = 'your_image_directory_path'# 设置要处理的图像数量
number_of_images_to_process = 50# 输出文件路径
output_file_path = "output_results.json"# 初始化计数器
image_counter = 0# 读取 JSON 数据文件
data_file = 'your_data_file.json'
with open(data_file, 'r') as f:data = json.load(f)def encode_image(image_path):with open(image_path, "rb") as image_file:return base64.b64encode(image_file.read()).decode('utf-8')def get_image_details(image_path):"""获取图像的详细信息,包括图像ID和尺寸。参数:image_path (str): 图像文件的路径。返回:tuple: 包含图像ID(文件名,不包括扩展名)和图像尺寸(宽度,高度)的元组。示例:get_image_details('path/to/image.jpg')  -> ('image', (800, 600))"""image_filename = os.path.basename(image_path)image_id = os.path.splitext(image_filename)[0]with Image.open(image_path) as img:image_size = img.sizereturn image_id, image_sizedef chat_completions(image_path, width, height):base64_image = encode_image(image_path)image_id, image_size = get_image_details(image_path)openai.api_key = API_SECRET_KEYresponse = openai.ChatCompletion.create(model="gpt-4",messages=[{"role": "system", "content": "You are an assistant that provides metadata information about images."},{"role": "user", "content": f"Image ID: {image_id}, Width: {width}, Height: {height}"}],max_tokens=3000,timeout=999,)return response# 初始化结果字典
results_dict = {}# 检查是否存在检查点文件
checkpoint_file = "checkpoint.pkl"
if os.path.exists(checkpoint_file):with open(checkpoint_file, "rb") as f:start_index = pickle.load(f)
else:start_index = 0# 处理图像文件
for i, image in enumerate(data[start_index:], start=start_index):image_name = image['image_path']image_file = os.path.join(image_directory_path, image_name)image_width = image['width']image_height = image['height']if image_name.lower().endswith(('.png', '.jpg', '.jpeg', '.tiff', '.bmp', '.gif')):try:response = chat_completions(image_file, image_width, image_height)result = {image_name: response.choices[0].message['content']}with open(output_file_path, "a") as output_file:output_file.write(json.dumps(result) + "\n")except Exception as e:print(f"Error processing image {image_name}: {e}")continueimage_counter += 1if image_counter >= number_of_images_to_process:breakwith open(checkpoint_file, "wb") as f:pickle.dump(i+1, f)

注意

上面的代码,最后四行:(先判断处理图像数量是否大于规定处理图像数量,再保存checkpoint)

if image_counter >= number_of_images_to_process:break
with open(checkpoint_file, "wb") as f:pickle.dump(i+1, f)

有时候要替换逻辑为这样(先保存checkpoint,再判断处理图像数量是否大于规定处理图像数量)

with open(checkpoint_file, "wb") as f:pickle.dump(i+1, f)
if image_counter >= number_of_images_to_process:break

然后,每次程序运行结束时,比如5.jpg处理完,第二次运行程序,不是再处理一遍5.jpg,而是从6.jpg开始。但有的时候不用替换,也仍然从6.jpg开始,不知道为什么。

但确实下方替换后更好一点,因为有的时候break完后直接跳出循环,导致最后一次的i+1没有更新

http://www.mmbaike.com/news/97361.html

相关文章:

  • php网站开发个人职责百度搜索历史记录
  • 用哪个软件做网站好河源新闻最新消息
  • 网站推广广告词指数基金怎么买
  • 做怎样的网站能赚钱关键词seo排名怎么做的
  • 网站一般用什么工具做银川网页设计公司
  • c 网站开发 视频教程软件公司
  • 中信建设有限责任公司 乔峰手机seo如何优化关键词排名
  • 北京赛车手机网站建设百度提交入口网址在哪
  • seo优化网站建设哪家好大冶seo网站优化排名推荐
  • 高中生沉迷哔哩哔哩怎么办电影站的seo
  • 天猫网站建设的意义seo的优化原理
  • 机器配件做外贸上什么网站有什么推广产品的渠道
  • 做动态网站的软件有哪些内容百度的代理商有哪些
  • 做美食网站的背景怎么推广自己的微信号
  • 武汉市二手房交易合同备案在那个网站上做呀网站首页布局设计模板
  • 帝国cms如何做电影网站电商运营培训机构哪家好
  • 重庆市住房城乡建设委员会官方网站网站优化排名技巧
  • 1.电子商务网站建设的核心是( )百度客户端
  • 怎么打击对手网站排名搜狗站长平台验证网站
  • wordpress Null天天seo站长工具
  • win服务器做网站网页设计模板html代码
  • 法院网站管理系统源码关键词搜索排名查询
  • wordpress目录图片不显示2023网站seo
  • 模型网站网络推广和网络销售的区别
  • 岗网站制作抖音推广渠道有哪些
  • 电子商务网站的建设流程是怎样的bing搜索引擎入口官网
  • 八年级信技做网站爱站网工具
  • 南京响应式网站设计适合30岁女人的培训班
  • 临汾网站建设系统优化工具
  • 西安企业网站seo快速排名优化方式