ai图像识别平台搭建:79.人工智能基于ImageAI的实现目标检测数据集半自动标注
ai图像识别平台搭建:79.人工智能基于ImageAI的实现目标检测数据集半自动标注二、实现代码部分图像数据下载网址:https://github.com/OlafenwaMoses/ImageAI/releases/download/1.0/resnet50_coco_best_v2.0.1.h5该对象检测模型可以检测80种不同类型的对象,包括我们日常生活中大部分的物品。这里只是检测我们想要的目标。bicycleperson bicycle car motorcycle airplane bus train truck boat traffic light fire hydrant stop_sign parking meter bench bird cat dog horse sheep cow elephant bear zebra gira
在做人工智能目标检测任务时,需要对原始数据进行标注,俗称打标签,框选出图像中的目标位置和名称。对于个人来说,标注大一些的数据集,简直是灾难性的,因为时间成本太高了,从而也产生一些专业标注公司。
有没有办法可以实现自动标注或半自动标注,让机器协助我们标注数据集?
实现的思路方法:就是用训练好的大模型来识别要标注的目标。其实就是从大集合里取一个子集。
本文使用ImageAI的训练好的模型:RetinaNet(文件大小= 145 MB):
下载网址:https://github.com/OlafenwaMoses/ImageAI/releases/download/1.0/resnet50_coco_best_v2.0.1.h5
该对象检测模型可以检测80种不同类型的对象,包括我们日常生活中大部分的物品。这里只是检测我们想要的目标。bicycle
person bicycle car motorcycle airplane
bus train truck boat traffic light fire hydrant stop_sign
parking meter bench bird cat dog horse sheep cow elephant bear zebra
giraffe backpack umbrella handbag tie suitcase frisbee skis snowboard
sports ball kite baseball bat baseball glove skateboard surfboard tennis racket
bottle wine glass cup fork knife spoon bowl banana apple sandwich orange
broccoli carrot hot dog pizza donot cake chair couch potted plant bed
dining table toilet tv laptop mouse remote keyboard cell phone microwave
oven toaster sink refrigerator book clock vase scissors teddy bear hair dryer
toothbrush.
标注任务
共享单车数据集,共有136张图像。标注目标:自行车。这里我们生成VOC格式。每个图像对应生成一个xml标注文件。并存放在Annotations中
部分图像数据
二、实现代码
from imageai.Detection import ObjectDetection
import os cv2
import xml.etree.ElementTree as ET
basedir="bicycle"
imgdir="JPEGImages"
anndir="Annotations"
outdir="output"
detector = ObjectDetection()
detector.setModelTypeAsRetinaNet()
detector.setModelPath("model/resnet50_coco_best_v2.0.1.h5")
detector.loadModel(detection_speed="normal") #指定检测速度,会影响精度。
#检测指定目标对象:bicycle
custom_objects = detector.CustomObjects(bicycle=True)
#把检测结果按voc格式写入xml文件
def write_xml(folder filename width height depth objects):
root=ET.Element("annotation")
ET.SubElement(root "folder").text=folder
ET.SubElement(root "filename").text=filename
ET.SubElement(root "path").text=os.path.join(folder filename)
source=ET.SubElement(root "source")
ET.SubElement(source "database").text="Unknown"
size=ET.SubElement(root "size")
ET.SubElement(size "width").text=str(width)
ET.SubElement(size "height").text=str(height)
ET.SubElement(size "depth").text=str(depth)
ET.SubElement(root "segmented").text="0"
for obj in objects:
object=ET.SubElement(root "object")
ET.SubElement(object "name").text=obj["name"]
ET.SubElement(object "pose").text="Unspecified"
ET.SubElement(object "truncated").text="0"
ET.SubElement(object "difficult").text="0"
bndbox=ET.SubElement(object "bndbox")
ET.SubElement(bndbox "xmin").text=str(obj["xmin"])
ET.SubElement(bndbox "ymin").text=str(obj["ymin"])
ET.SubElement(bndbox "xmax").text=str(obj["xmax"])
ET.SubElement(bndbox "ymax").text=str(obj["ymax"])
tree=ET.ElementTree(root)
tree.write(os.path.join(folder anndir filename.replace(".jpg" ".xml")))
#遍历指定目录下的所有图片
objects=[] #存放检测结果
for i f in enumerate(os.listdir(os.path.join(basedir imgdir))):
imgpath=os.path.join(basedir imgdir f)
img=cv2.imread(imgpath)
h w c=img.shape
annpath=os.path.join(basedir anndir f.replace(".jpg" ".xml"))
outpath=os.path.join(basedir outdir f)
detections=detector.detectCustomObjectsFromImage(
custom_objects=custom_objects
input_image=img
input_type="array"
output_image_path=outpath)
#把结果存入objects
for obj in detections:
objects.append({
"name":obj["name"]
"xmin":obj["box_points"][0]
"ymin":obj["box_points"][1]
"xmax":obj["box_points"][2]
"ymax":obj["box_points"][3]
})
#print(objects)
#把结果写入xml文件
write_xml(basedir f w h c objects)
objects.clear()
print("{}/{}".format(i 1 len(os.listdir(os.path.join(basedir imgdir)))))
三、实现效果
1、检测结果保存在output目录中,方便我们可以直观清楚看到检测或标注质量。
结果结果:003.jpg
2、生成xml标注文件 存放在Annotations目录中
生成的xml标注文件
生成的003.JPG对应的003.xml标注文件