Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add basic image io and remove python C-API refs from numpy_returns.cpp #1258

Merged
merged 9 commits into from
Apr 18, 2018
8 changes: 3 additions & 5 deletions python_examples/cnn_face_detector.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,12 @@
# command:
# sudo apt-get install cmake
#
# Also note that this example requires scikit-image which can be installed
# Also note that this example requires Numpy which can be installed
# via the command:
# pip install scikit-image
# Or downloaded from http://scikit-image.org/download.html.
# pip install numpy

import sys
import dlib
from skimage import io

if len(sys.argv) < 3:
print(
Expand All @@ -55,7 +53,7 @@

for f in sys.argv[2:]:
print("Processing file: {}".format(f))
img = io.imread(f)
img = dlib.load_rgb_image(f)
# The 1 in the second argument indicates that we should upsample the image
# 1 time. This will make everything bigger and allow us to detect more
# faces.
Expand Down
8 changes: 3 additions & 5 deletions python_examples/correlation_tracker.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,14 @@
# command:
# sudo apt-get install cmake
#
# Also note that this example requires scikit-image which can be installed
# Also note that this example requires Numpy which can be installed
# via the command:
# pip install scikit-image
# Or downloaded from http://scikit-image.org/download.html.
# pip install numpy

import os
import glob

import dlib
from skimage import io

# Path to the video frames
video_folder = os.path.join("..", "examples", "video_frames")
Expand All @@ -54,7 +52,7 @@
# We will track the frames as we load them off of disk
for k, f in enumerate(sorted(glob.glob(os.path.join(video_folder, "*.jpg")))):
print("Processing Frame {}".format(k))
img = io.imread(f)
img = dlib.load_rgb_image(f)

# We need to initialize the tracker on the first frame
if k == 0:
Expand Down
32 changes: 10 additions & 22 deletions python_examples/face_alignment.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,13 @@
# command:
# sudo apt-get install cmake
#
# Also note that this example requires OpenCV and Numpy which can be installed
# Also note that this example requires Numpy which can be installed
# via the command:
# pip install opencv-python numpy
# Or downloaded from http://opencv.org/releases.html
# pip install numpy

import sys

import dlib
import cv2
import numpy as np

if len(sys.argv) != 3:
print(
Expand All @@ -48,14 +45,8 @@
detector = dlib.get_frontal_face_detector()
sp = dlib.shape_predictor(predictor_path)

# Load the image using OpenCV
bgr_img = cv2.imread(face_file_path)
if bgr_img is None:
print("Sorry, we could not load '{}' as an image".format(face_file_path))
exit()

# Convert to RGB since dlib uses RGB images
img = cv2.cvtColor(bgr_img, cv2.COLOR_BGR2RGB)
# Load the image using Dlib
img = dlib.load_rgb_image(face_file_path)

# Ask the detector to find the bounding boxes of each face. The 1 in the
# second argument indicates that we should upsample the image 1 time. This
Expand All @@ -72,20 +63,17 @@
for detection in dets:
faces.append(sp(img, detection))

window = dlib.image_window()

# Get the aligned face images
# Optionally:
# images = dlib.get_face_chips(img, faces, size=160, padding=0.25)
images = dlib.get_face_chips(img, faces, size=320)
for image in images:
cv_bgr_img = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
cv2.imshow('image',cv_bgr_img)
cv2.waitKey(0)
window.set_image(image)
dlib.hit_enter_to_continue()

# It is also possible to get a single chip
image = dlib.get_face_chip(img, faces[0])
cv_bgr_img = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
cv2.imshow('image',cv_bgr_img)
cv2.waitKey(0)

cv2.destroyAllWindows()

window.set_image(image)
dlib.hit_enter_to_continue()
8 changes: 3 additions & 5 deletions python_examples/face_clustering.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,14 @@
# command:
# sudo apt-get install cmake
#
# Also note that this example requires scikit-image which can be installed
# Also note that this example requires Numpy which can be installed
# via the command:
# pip install scikit-image
# Or downloaded from http://scikit-image.org/download.html.
# pip install numpy

import sys
import os
import dlib
import glob
from skimage import io

if len(sys.argv) != 5:
print(
Expand Down Expand Up @@ -66,7 +64,7 @@
# Now find all the faces and compute 128D face descriptors for each face.
for f in glob.glob(os.path.join(faces_folder_path, "*.jpg")):
print("Processing file: {}".format(f))
img = io.imread(f)
img = dlib.load_rgb_image(f)

# Ask the detector to find the bounding boxes of each face. The 1 in the
# second argument indicates that we should upsample the image 1 time. This
Expand Down
11 changes: 4 additions & 7 deletions python_examples/face_detector.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,23 +37,20 @@
# command:
# sudo apt-get install cmake
#
# Also note that this example requires scikit-image which can be installed
# Also note that this example requires Numpy which can be installed
# via the command:
# pip install scikit-image
# Or downloaded from http://scikit-image.org/download.html.
# pip install numpy

import sys

import dlib
from skimage import io


detector = dlib.get_frontal_face_detector()
win = dlib.image_window()

for f in sys.argv[1:]:
print("Processing file: {}".format(f))
img = io.imread(f)
img = dlib.load_rgb_image(f)
# The 1 in the second argument indicates that we should upsample the image
# 1 time. This will make everything bigger and allow us to detect more
# faces.
Expand All @@ -76,7 +73,7 @@
# Also, the idx tells you which of the face sub-detectors matched. This can be
# used to broadly identify faces in different orientations.
if (len(sys.argv[1:]) > 0):
img = io.imread(sys.argv[1])
img = dlib.load_rgb_image(sys.argv[1])
dets, scores, idx = detector.run(img, 1, -1)
for i, d in enumerate(dets):
print("Detection {}, score: {}, face_type:{}".format(
Expand Down
34 changes: 12 additions & 22 deletions python_examples/face_jitter.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,26 +25,23 @@
# command:
# sudo apt-get install cmake
#
# Also note that this example requires OpenCV and Numpy which can be installed
# Also note that this example requires Numpy which can be installed
# via the command:
# pip install opencv-python numpy
# pip install numpy
#
# The image file used in this example is in the public domain:
# https://commons.wikimedia.org/wiki/File:Tom_Cruise_avp_2014_4.jpg
import sys

import dlib
import cv2
import numpy as np

def show_jittered_images(jittered_images):
def show_jittered_images(window, jittered_images):
'''
Shows the specified jittered images one by one
'''
for img in jittered_images:
cv_bgr_img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
cv2.imshow('image',cv_bgr_img)
cv2.waitKey(0)
window.set_image(img)
dlib.hit_enter_to_continue()

if len(sys.argv) != 2:
print(
Expand All @@ -62,14 +59,8 @@ def show_jittered_images(jittered_images):
detector = dlib.get_frontal_face_detector()
sp = dlib.shape_predictor(predictor_path)

# Load the image using OpenCV
bgr_img = cv2.imread(face_file_path)
if bgr_img is None:
print("Sorry, we could not load '{}' as an image".format(face_file_path))
exit()

# Convert to RGB since dlib uses RGB images
img = cv2.cvtColor(bgr_img, cv2.COLOR_BGR2RGB)
# Load the image using dlib
img = dlib.load_rgb_image(face_file_path)

# Ask the detector to find the bounding boxes of each face.
dets = detector(img)
Expand All @@ -83,15 +74,14 @@ def show_jittered_images(jittered_images):

# Get the aligned face image and show it
image = dlib.get_face_chip(img, faces[0], size=320)
cv_bgr_img = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
cv2.imshow('image',cv_bgr_img)
cv2.waitKey(0)
window = dlib.image_window()
window.set_image(image)
dlib.hit_enter_to_continue()

# Show 5 jittered images without data augmentation
jittered_images = dlib.jitter_image(image, num_jitters=5)
show_jittered_images(jittered_images)
show_jittered_images(window, jittered_images)

# Show 5 jittered images with data augmentation
jittered_images = dlib.jitter_image(image, num_jitters=5, disturb_colors=True)
show_jittered_images(jittered_images)
cv2.destroyAllWindows()
show_jittered_images(window, jittered_images)
8 changes: 3 additions & 5 deletions python_examples/face_landmark_detection.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,14 @@
# command:
# sudo apt-get install cmake
#
# Also note that this example requires scikit-image which can be installed
# Also note that this example requires Numpy which can be installed
# via the command:
# pip install scikit-image
# Or downloaded from http://scikit-image.org/download.html.
# pip install numpy

import sys
import os
import dlib
import glob
from skimage import io

if len(sys.argv) != 3:
print(
Expand All @@ -76,7 +74,7 @@

for f in glob.glob(os.path.join(faces_folder_path, "*.jpg")):
print("Processing file: {}".format(f))
img = io.imread(f)
img = dlib.load_rgb_image(f)

win.clear_overlay()
win.set_image(img)
Expand Down
8 changes: 3 additions & 5 deletions python_examples/face_recognition.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,14 @@
# command:
# sudo apt-get install cmake
#
# Also note that this example requires scikit-image which can be installed
# Also note that this example requires Numpy which can be installed
# via the command:
# pip install scikit-image
# Or downloaded from http://scikit-image.org/download.html.
# pip install numpy

import sys
import os
import dlib
import glob
from skimage import io

if len(sys.argv) != 4:
print(
Expand All @@ -76,7 +74,7 @@
# Now process all the images
for f in glob.glob(os.path.join(faces_folder_path, "*.jpg")):
print("Processing file: {}".format(f))
img = io.imread(f)
img = dlib.load_rgb_image(f)

win.clear_overlay()
win.set_image(img)
Expand Down
10 changes: 3 additions & 7 deletions python_examples/find_candidate_object_locations.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,14 @@
# command:
# sudo apt-get install cmake
#
# Also note that this example requires scikit-image which can be installed
# Also note that this example requires Numpy which can be installed
# via the command:
# pip install scikit-image
# Or downloaded from http://scikit-image.org/download.html.


# pip install numpy

import dlib
from skimage import io

image_file = '../examples/faces/2009_004587.jpg'
img = io.imread(image_file)
img = dlib.load_rgb_image(image_file)

# Locations of candidate objects will be saved into rects
rects = []
Expand Down
59 changes: 59 additions & 0 deletions python_examples/opencv_webcam_face_detection.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#!/usr/bin/python
# The contents of this file are in the public domain. See LICENSE_FOR_EXAMPLE_PROGRAMS.txt
#
# This example program shows how to find frontal human faces in a webcam stream using OpenCV.
# It is also meant to demonstrate that rgb images from Dlib can be used with opencv by just
# swapping the Red and Blue channels.
#
# You can run this program and see the detections from your webcam by executing the
# following command:
# ./opencv_face_detection.py
#
# This face detector is made using the now classic Histogram of Oriented
# Gradients (HOG) feature combined with a linear classifier, an image
# pyramid, and sliding window detection scheme. This type of object detector
# is fairly general and capable of detecting many types of semi-rigid objects
# in addition to human faces. Therefore, if you are interested in making
# your own object detectors then read the train_object_detector.py example
# program.
#
#
# COMPILING/INSTALLING THE DLIB PYTHON INTERFACE
# You can install dlib using the command:
# pip install dlib
#
# Alternatively, if you want to compile dlib yourself then go into the dlib
# root folder and run:
# python setup.py install
# or
# python setup.py install --yes USE_AVX_INSTRUCTIONS
# if you have a CPU that supports AVX instructions, since this makes some
# things run faster.
#
# Compiling dlib should work on any operating system so long as you have
# CMake installed. On Ubuntu, this can be done easily by running the
# command:
# sudo apt-get install cmake
#
# Also note that this example requires Numpy which can be installed
# via the command:
# pip install numpy

import sys
import dlib
import cv2

detector = dlib.get_frontal_face_detector()
cam = cv2.VideoCapture(0)
color_green = (0,255,0)
line_width = 3
while True:
ret_val, img = cam.read()
rgb_image = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
dets = detector(rgb_image)
for det in dets:
cv2.rectangle(img,(det.left(), det.top()), (det.right(), det.bottom()), color_green, line_width)
cv2.imshow('my webcam', img)
if cv2.waitKey(1) == 27:
break # esc to quit
cv2.destroyAllWindows()
1 change: 0 additions & 1 deletion python_examples/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
scikit-image>=0.9.3
opencv-python
numpy
Loading