Instantly share code, notes, and snippets.

Embed
What would you like to do?
import os
import sys
import glob
import dlib
from skimage import io
from skimage.draw import polygon_perimeter
import traceback
detector = dlib.simple_object_detector(detector_svm)
options.num_threads = 4
options.be_verbose = True
options = dlib.simple_object_detector_training_options()
options.add_left_right_image_flips = True
print("Showing detections on the images in the test folder...")
win = dlib.image_window()
for f in glob.glob(os.path.join(detector_folder, "test/*.jpg")):
print("Processing file: {}".format(f))
img = io.imread(f)
dets = detector(img)
print("Number of objects detected: {}".format(len(dets)))
bOverLays = False
for k, d in enumerate(dets):
print("Detection {}: Left: {} Top: {} Right: {} Bottom: {}".format(
k, d.left(), d.top(), d.right(), d.bottom()))
rr,cc = polygon_perimeter([d.top(), d.top(), d.bottom(), d.bottom()],
[d.right(), d.left(), d.left(), d.right()])
try:
img[rr, cc] = (255, 0, 0)
if bOverLays == False:
bOverLays = True
except:
traceback.print_exc()
# Save the image detections to a file for future review.
if bOverLays == True:
io.imsave(f.replace("test/","output/"), img)
win.clear_overlay()
win.set_image(img)
win.add_overlay(dets)
dlib.hit_enter_to_continue()
print("Testing accuracy: {}".format(
dlib.test_simple_object_detector(testing_xml_path, detector_svm)))
print("") # Print blank line to create gap from previous output
print("Training accuracy: {}".format(
dlib.test_simple_object_detector(training_xml_path, detector_svm)))
if not os.path.exists(detector_svm):
dlib.train_simple_object_detector(training_xml_path, detector_svm, options)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment