批量图片识别(qwen-vl-max-latest)
基于Qwen-VL的批量图片识别与重命名script。使用阿里云的Qwen-VL大模型实现批量图片内容识别,并根据识别结果自动重命名图片文件。
功能概述
- 自动识别指定文件夹内的图片内容(支持JPG/JPEG/PNG格式)
- 调用Qwen-VL-max模型生成简洁中文描述
- 智能处理文件名冲突和非法字符
- 批量重命名图片为描述性名称
import os
import dashscope
from pathlib import Path
# 设置API密钥
os.environ["DASHSCOPE_API_KEY"]="此处替换你的通义千问API KEY"
dashscope.api_key = os.environ["DASHSCOPE_API_KEY"]
def get_image_description(image_path):
"""调用qwen-vl模型获取图片描述"""
try:
response = dashscope.MultiModalConversation.call(
model='qwen-vl-max',
messages=[
{
"role": "user",
"content": [
{"image": str(image_path.resolve())},
{"text": "请用最简洁的2-6个中文词语描述图片中的主要物体,直接输出名称不要解释"}
]
}
]
)
return response.output.choices[0].message.content[0]['text']
except Exception as e:
print(f"识别失败: {str(e)}")
return None
def sanitize_filename(name):
"""清理非法文件名字符"""
invalid_chars = '<>:"/\\|?*'
return ''.join(c if c not in invalid_chars else '_' for c in name)
def rename_images(folder_path):
"""批量重命名图片"""
image_exts = ('.jpg', '.jpeg','.png')
renamed_files = {}
for img_path in Path(folder_path).iterdir():
if img_path.suffix.lower() in image_exts:
# 获取图片描述
description = get_image_description(img_path)
if not description:
continue
# 清理并缩短名称
clean_name = sanitize_filename(description.strip().split()[0])[:6]
new_name = f"{clean_name}.jpg"
# 处理重复文件名
if new_name in renamed_files:
count = renamed_files[new_name] + 1
renamed_files[new_name] = count
new_name = f"{clean_name}-{count}.jpg"
else:
renamed_files[new_name] = 0
# 执行重命名
new_path = img_path.with_name(new_name)
img_path.rename(new_path)
print(f"重命名成功: {img_path.name} -> {new_name}")
if __name__ == "__main__":
# 设置包含图片的文件夹路径
image_folder = "./images" # 修改为你的图片文件夹路径
rename_images(image_folder)