Skip to content

Instantly share code, notes, and snippets.

@shyampurk
Created September 17, 2017 15:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shyampurk/e336c02d728f6b2f1682ad7855c418c8 to your computer and use it in GitHub Desktop.
Save shyampurk/e336c02d728f6b2f1682ad7855c418c8 to your computer and use it in GitHub Desktop.
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