Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
object detector with dlib
import dlib
import cv2
detector = dlib.simple_object_detector("detector.svm")
cap = cv2.VideoCapture(0)
while(True):
# Capture frame-by-frame
ret, frame = cap.read()
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
dets = detector(frame)
for d in dets:
cv2.rectangle(frame, (d.left(), d.top()), (d.right(), d.bottom()), (0, 0, 255), 2)
# Display the resulting frame
cv2.imshow("frame",frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()
import os
import sys
import dlib
img_dir = "."
options = dlib.simple_object_detector_training_options()
options.add_left_right_image_flips = False
options.C = 5
options.num_threads = 2
options.be_verbose = True
training_xml_path = os.path.join(img_dir, "training.xml")
testing_xml_path = os.path.join(img_dir, "testing.xml")
dlib.train_simple_object_detector(training_xml_path, "detector.svm", options)
print("")
print("Training accuracy: {}".format(
dlib.test_simple_object_detector(training_xml_path, "detector.svm")))
print("Testing accuracy: {}".format(
dlib.test_simple_object_detector(testing_xml_path, "detector.svm")))
@atotto
Copy link
Author

atotto commented Jul 17, 2016

setup

$ python --version
Python 3.5.2 :: Anaconda 4.1.1 (32-bit)
$ conda install -c menpo dlib
$ conda install -c menpo opencv3

face detection(dlib example):

$ git clone -b v19.0 --depth=1 https://github.com/davisking/dlib.git ~/dlib
$ cd ~/dlib/python_example
$ python train_object_detector.py ../examples/faces/
$ python ~/object_detect_with_dlib.py

object detection:

$ git clone -b v19.0 --depth=1 https://github.com/davisking/dlib.git ~/dlib
$ cd ~/dlib/tools/imglab/ && mkdir build && cd build && cmake .. && cmake --build . --config Release

$ cd ~/my_images
$ ~/dlib/tools/imglab/build/imglab -c training.xml training_*.jpg
$ ~/dlib/tools/imglab/build/imglab training.xml
$ ~/dlib/tools/imglab/build/imglab -c training.xml testing_*.jpg
$ ~/dlib/tools/imglab/build/imglab testing.xml

$ python ~/training_object_detector.py
$ python ~/object_detect_with_dlib.py

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment