dlib下训练自己的物体检测器
2018年12月30日
使用dlib创建属于自己的简单的物体识别器(这里以手的检测为例)
首先要准备手的图片数据:
或http://www.robots.ox.ac.uk/~vgg/data/hands/的相关数据集
也可以自己拍摄自己的手或别人的手图片做为数据
用于DLIB训练检测器的的数据为XML格式,可以用以下工具制作:
windows安装dlib的imglab工具
基于网页的图像标注工具imglab安装与使用
开始训练:
把“D:\dlib-19.15\python_examples”目录下的“train_object_detector.py”复制命名为“train_object_detector_hand.py”
并修改代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
import os import sys import glob import dlib ''' if len(sys.argv) != 2: print( "Give the path to the examples/faces directory as the argument to this " "program. For example, if you are in the python_examples folder then " "execute this program by running:\n" " ./train_object_detector.py ../examples/faces") exit() faces_folder = sys.argv[1] ''' faces_folder = "../tools/imglab/build/Release" #“handdetector.svm”为已训练好的,如果存在择不用再训练 if not os.path.exists("handdetector.svm"): 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 training_xml_path = os.path.join(faces_folder, "handdataset.xml")#训练数据集 testing_xml_path = os.path.join(faces_folder, "handdataset.xml")#测试数据集 dlib.train_simple_object_detector(training_xml_path, "handdetector.svm", options)#生成检测器“handdetector.svm” print("") # Print blank line to create gap from previous output print("Training accuracy: {}".format( dlib.test_simple_object_detector(training_xml_path, "handdetector.svm"))) print("Testing accuracy: {}".format( dlib.test_simple_object_detector(testing_xml_path, "handdetector.svm"))) #以下代码为测试代码 detector = dlib.simple_object_detector("handdetector.svm")#加载检测器 ''' # We can look at the HOG filter we learned. It should look like a face. Neat! win_det = dlib.image_window() win_det.set_image(detector) ''' # Now let's run the detector over the images in the faces folder and display the # results. print("Showing detections on the images in the faces folder...") win = dlib.image_window() #测试“testimages”目录下的所有jpg文件 for f in glob.glob(os.path.join(faces_folder, "testimages/*.jpg")): print("Processing file: {}".format(f)) img = dlib.load_rgb_image(f) dets = detector(img) print("Number of faces detected: {}".format(len(dets))) for k, d in enumerate(dets): print("Detection {}: Left: {} Top: {} Right: {} Bottom: {}".format( k, d.left(), d.top(), d.right(), d.bottom())) win.clear_overlay() win.set_image(img) win.add_overlay(dets) dlib.hit_enter_to_continue() ''' detector1 = dlib.fhog_object_detector("handdetector.svm") detector2 = dlib.fhog_object_detector("handdetector.svm") detectors = [detector1, detector2] image = dlib.load_rgb_image(faces_folder + '/2008_002506.jpg') [boxes, confidences, detector_idxs] = dlib.fhog_object_detector.run_multiple(detectors, image, upsample_num_times=1, adjust_threshold=0.0) for i in range(len(boxes)): print("detector {} found box {} with confidence {}.".format(detector_idxs[i], boxes[i], confidences[i])) images = [dlib.load_rgb_image(faces_folder + '/2008_002506.jpg'), dlib.load_rgb_image(faces_folder + '/2009_004587.jpg')] boxes_img1 = ([dlib.rectangle(left=329, top=78, right=437, bottom=186), dlib.rectangle(left=224, top=95, right=314, bottom=185), dlib.rectangle(left=125, top=65, right=214, bottom=155)]) boxes_img2 = ([dlib.rectangle(left=154, top=46, right=228, bottom=121), dlib.rectangle(left=266, top=280, right=328, bottom=342)]) boxes = [boxes_img1, boxes_img2] detector2 = dlib.train_simple_object_detector(images, boxes, options) # We could save this detector to disk by uncommenting the following. #detector2.save('detector2.svm') # Now let's look at its HOG filter! win_det.set_image(detector2) dlib.hit_enter_to_continue() print("\nTraining accuracy: {}".format( dlib.test_simple_object_detector(images, boxes, detector2))) ''' |
关于:options = dlib.simple_object_detector_training_options() 参数的设置可以查看:
“D:\dlib-19.15\dlib\image_processing\shape_predictor_trainer.h”头文件中的默认定义
命令行输入:
1 |
python train_object_detector_hand.py |
每回车一次查看一张图片检测效果如下: