本文默认使用以下代码前已准备好Qwen-VL的运行环境,且已下载了Qwen-VL-Chat模型文件。
具体环境按照方式参考官网:
https://github.com/QwenLM/Qwen-VL/blob/master/README_CN.md
本工具参考:https://github.com/QwenLM/Qwen-VL/blob/master/TUTORIAL.md
# 借助Qwen-VL-Chat大模型生成xml标注的工具, 但生成的标签名称不固定,因为是大模型随机分析的目标名,可根据需求自行修改
import os
import os.path as osp
import re
from tqdm import tqdm
import torch
torch.manual_seed(1234)
# from transformers.generation import GenerationConfig
from transformers import AutoModelForCausalLM, AutoTokenizer
from xml.dom.minidom import Document
import cv2
def write_xml(objects:list, img_name:str, xml_pth:str):
xml_dir = osp.join(xml_pth, img_name.replace(".jpg", ".xml"))
doc = Document()
annotation = doc.createElement("annotation")
doc.appendChild(annotation)
folder = doc.createElement("folder")
folder_text = doc.createTextNode("JPEGImages")
folder.appendChild(folder_text)
annotation.appendChild(folder)
filename = doc.createElement("filename")
filename_text = doc.createTextNode(img_name)
filename.appendChild(filename_text)
annotation.appendChild(filename)
path = doc.createElement("path")
path_text = doc.createTextNode(objects.get("imagePath"))
path.appendChild(path_text)
annotation.appendChild(path)
source = doc.createElement("source")
database = doc.createElement("database")
database_text = doc.createTextNode("Unknown")
database.appendChild(database_text)
source.appendChild(database)
annotation.appendChild(source)
size = doc.createElement("size")
width = doc.createElement("width")