1 Vott设置自定义自动标注模型(需要tfjs格式模型)
如果是tfjs格式比较简单,直接设置路径就行,否则要转换为tfjs格式模型
2 自己写代码自动标注,并修改Vott项目文件
需要修改xxx.vott项目信息文件和与图片文件关联的xxxx(这个是图片ID,在xxx.out文件中有相关信息)-asset.json
# 读打开文件
with open('/Users/gauss/Downloads/20220922_2101/fish.vott', encoding='utf-8') as a:
# 读取文件
result = json.load(a)
cnt = 0
for key, value in result.get('assets').items():
jj = '/Users/gauss/Downloads/20220922_2101/'+key+'-asset.json'
if os.path.exists(jj):
cnt += 1
result['assets'][key]['state']=2
#存在就直接跳过
continue
//这里执行图片的处理,获得标注框信息
trainer.predict(
images=get_test_images(FLAGS.infer_dir,value.get('path').replace('file:', '')),
draw_threshold=0.7,
output_dir='/Users/gauss/Downloads/20220922_2101/',
save_results=FLAGS.save_results,id=key,name=value.get('name'),path=value.get('path').replace('file:', ''))
#修改项目信息,重新保存到新文件
with open('/Users/gauss/Downloads/20220922_2101/fish_1.vott', 'w', encoding='utf8') as f2:
# ensure_ascii=False才能输入中文,否则是Unicode字符
# indent=2 JSON数据的缩进,美观
json.dump(result, f2, ensure_ascii=False, indent=2)
获取到标注框信息后,就可以生成与图片关联的json文件
参考代码如下:
regions =[]
for dt in np.array(bbox_res):
if im_id != dt['image_id']:
continue
catid, bbox, score = dt['category_id'], dt['bbox'], dt['score']
if score < 0.7:
continue
xmin, ymin, w, h = bbox
xmax = xmin + w
ymax = ymin + h
boundingBox = {'height':h,'width':w,'left':xmin,'top':ymin}
points = [
{
"x": xmin,
"y": ymin
},
{
"x": xmax,
"y": ymin
},
{
"x": xmax,
"y": ymax
},
{
"x": xmin,
"y": ymax
}
]
region = {
"id": self.generate_random_str(9),
"type": "RECTANGLE",
"tags": [
"h"
],
"boundingBox": boundingBox,
"points": points
}
regions.append(region)
j = {
"asset": {
"format": "jpg",
"id": id,
"name": name,
"path": "file:"+path,
"size": {
"width": 1728,
"height": 1728
},
"state": 2,
"type": 1
},
"regions":regions,
"version": "2.2.0"
}
with open(output_dir+id+'-asset.json', 'w', encoding='utf8') as f2:
# ensure_ascii=False才能输入中文,否则是Unicode字符
# indent=2 JSON数据的缩进,美观
json.dump(j, f2, ensure_ascii=False, indent=2)
2 对Vott项目的图片进行压缩
import json
import operator
import os
from PIL import Image
def get_size(file):
# 获取文件大小:KB
size = os.path.getsize(file)
return size / 1024
def compress_image(infile, outfile=None, mb=300, step=10, quality=80):
"""不改变图片尺寸压缩到指定大小
:param infile: 压缩源文件
:param outfile: 压缩文件保存地址
:param mb: 压缩目标,KB
:param step: 每次调整的压缩比率
:param quality: 初始压缩比率
:return: 压缩文件地址,压缩文件大小
"""
if outfile is None:
outfile = infile
o_size = get_size(infile)
if o_size <= mb:
im = Image.open(infile)
im.save(outfile)
while o_size > mb:
im = Image.open(infile)
im.save(outfile, quality=quality)
if quality - step < 0:
break
quality -= step
o_size = get_size(outfile)
#读打开文件
with open('/Users/gauss/Documents/20221004/fish202210.vott', encoding='utf-8') as a:
# 读取文件
result = json.load(a)
cnt = 0
for key, value in result.get('assets').items():
# if operator.contains(value.get('name'),"TL-IPC55A_8E-8D_0_20221006_12.01.39.586124.jpg") != True:
# #存在就直接跳过
# print("直接跳过图片 {}".format(
# value.get('name')))
# continue
print("压缩图片 {}".format(
value.get('name')))
os.rename(value.get('path').replace('file:', ''), value.get('path').replace('file:', '')+"_bak")
compress_image(value.get('path').replace('file:', '')+"_bak",
value.get('path').replace('file:', ''))
os.remove(value.get('path').replace('file:', '')+"_bak")