dlib 支持 imglab 生成的数据格式;
imglab 用法见 我的博客 《标注工具》
目标检测 标注文件 格式如下
<?xml version='1.0' encoding='ISO-8859-1'?> <?xml-stylesheet type='text/xsl' href='image_metadata_stylesheet.xsl'?> <dataset> <name>imglab dataset</name> <comment>Created by imglab tool.</comment> <images> <image file='images\1.jpg' width='400' height='300'> <box top='59' left='14' width='377' height='229'/> </image> <image file='images\10.jpg' width='612' height='459'> <box top='38' left='67' width='488' height='361'/> </image> </images> </dataset>
图片大小无需一致
训练自己的数据
import dlib # 训练的参数,可以根据实际情况进行修改 options = dlib.simple_object_detector_training_options() options.add_left_right_image_flips = True options.C = 5 options.num_threads = 4 options.be_verbose = True current_path = r'D:\Pythoncode\dlib-master\tools\imglab\build\Release' train_folder = current_path + '/images/' # 图片地址 train_xml_path = current_path + '/data.xml' # 标注文件地址 print("start training:") # 最重要的一个函数,生成 detector.svm 模型 dlib.train_simple_object_detector(train_xml_path, 'detector.svm', options) # print("Training accuracy: {}".format( dlib.test_simple_object_detector(train_xml_path, "detector.svm")))
测试训练的模型
import dlib import cv2 import glob detector = dlib.simple_object_detector("detector.svm") # 训练好的模型 test_folder = 'img/test/' # 测试数据地址 for f in glob.glob(test_folder + '*.jpg'): print("Processing file: {}".format(f)) img = cv2.imread(f, cv2.IMREAD_COLOR) b, g, r = cv2.split(img) img2 = cv2.merge([r, g, b]) dets = detector(img2) print("Number of cars detected: {}".format(len(dets))) for index, car in enumerate(dets): print('car {}; left {}; top {}; right {}; bottom {}'.format(index, car.left(), car.top(), car.right(), car.bottom())) left = car.left() top = car.top() right = car.right() bottom = car.bottom() cv2.rectangle(img, (left, top), (right, bottom), (0, 255, 0), 3) cv2.namedWindow(f, cv2.WINDOW_AUTOSIZE) cv2.imshow(f, img) k = cv2.waitKey(0) cv2.destroyAllWindows()
参考资料:
https://blog.csdn.net/wyd1520/article/details/81585293 基于dlib的物体检测(转)
https://blog.csdn.net/djstavaV/article/details/86743600 基于dlib的物体检测(转)
https://www.jianshu.com/p/96eaa6a6ec22 人脸识别(dlib)——用imglab制作样本与测试