Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
import sys
import dlib
from PIL import Image
import numpy as np
from timeit import default_timer as timer
# (563, 511, 3) total time: 0.433351890999802
#file_name = "/home/ekmek/intership_project/hog_tests/face_example.jpg"
file_name = '/home/ekmek/intership_project/video_parser_v1/_videos_to_test/small_dataset/input/frames/s0216.jpg'
show = True
if show:
win = dlib.image_window()
# Create a HOG face detector using the built-in dlib class
face_detector = dlib.get_frontal_face_detector()
# Load the image into an array
#image = io.imread(file_name)
image = Image.open(file_name)
image = np.array(image)
print(image.shape)
# Run the HOG face detector on the image data.
# The result will be the bounding boxes of the faces in our image.
start = timer()
detected_faces = face_detector(image, 1)
if len(sys.argv) != 3:
print(
"execute this program by running: \n"
"python sleep_detection.py ./face_landmark_detection.py shape_predictor_68_face_landmarks.dat /path/to/image.jpg"
"You can download a trained facial shape predictor from:\n "
"http://dlib.net/files/shape_predictor_68_face_landmarks.dat.bz2")
exit()
predictor_path = sys.argv[1]
image_path = sys.argv[2]
detector = dlib.get_frontal_face_detector()
predictor = dlib.shape_predictor(predictor_path)
win = dlib.image_window()
img = io.imread(image_path)
win.clear_overlay()
win.set_image(img)
dets = detector(img, 1)
vec = np.empty([68, 2], dtype = int)
status="Not Sleeping"
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()))
# Get the landmarks/parts for the face in box d.
shape = predictor(img, d)
print("Part 0: {}, Part 1: {} ...".format(shape.part(0),
print(
"Give the path to the trained shape predictor model as the first "
"argument and then the directory containing the facial images.\n"
"For example, if you are in the python_examples folder then "
"execute this program by running:\n"
" ./face_landmark_detection.py shape_predictor_68_face_landmarks.dat ../examples/faces\n"
"You can download a trained facial shape predictor from:\n"
" http://sourceforge.net/projects/dclib/files/dlib/v18.10/shape_predictor_68_face_landmarks.dat.bz2")
exit()
predictor_path = sys.argv[1]
faces_folder_path = sys.argv[2]
detector = dlib.get_frontal_face_detector()
predictor = dlib.shape_predictor(predictor_path)
win = dlib.image_window()
for f in glob.glob(os.path.join(faces_folder_path, "*.jpg")):
print("Processing file: {}".format(f))
img = io.imread(f)
win.clear_overlay()
win.set_image(img)
# 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
# will make everything bigger and allow us to detect more faces.
dets = detector(img, 1)
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()))
def get_face(filename):
# Create a HOG face detector using the built-in dlib class
predictor_model = "shape_predictor_68_face_landmarks.dat"
face_detector = dlib.get_frontal_face_detector()
face_pose_predictor = dlib.shape_predictor(predictor_model)
face_aligner = openface.AlignDlib(predictor_model)
win = dlib.image_window()
# Load the image into an array
image = io.imread(filename)
# Run the HOG face detector on the image data.
# The result will be the bounding boxes of the faces in our image.
detected_faces = face_detector(image, 1)
# Open a window on the desktop showing the image
win.set_image(image)
# Loop through each face we found in the image
for i, face_rect in enumerate(detected_faces):
# Detected faces are returned as an object with the coordinates
# of the top, left, right and bottom edges
face1 = image[face_rect.top():face_rect.bottom(), face_rect.left():face_rect.right()]
import sys
import dlib
from skimage import io
import openface
# You can download the required pretrained face detection model here:
# http://dlib.net/files/shape_predictor_68_face_landmarks.dat.bz2
predictor_model = "shape_predictor_68_face_landmarks.dat"
# Create a HOG face detector using the built-in dlib class
face_detector = dlib.get_frontal_face_detector()
face_pose_predictor = dlib.shape_predictor(predictor_model)
face_aligner = openface.AlignDlib(predictor_model)
win = dlib.image_window()
# Load the image
#image = io.imread("images/Chandrakanth.jpg")
image = io.imread("test/2.jpg")
# Run the HOG face detector on the image data
detected_faces = face_detector(image, 1)
print("Found {} faces in the image file {}".format(len(detected_faces), "multipleimg/3.jpg"))
# Show the desktop window with the image
win.set_image(image)
# Loop through each face we found in the image
for i, face_rect in enumerate(detected_faces):
# On Ubuntu, this can be done easily by running the command:
# sudo apt-get install libboost-python-dev cmake
#
# Also note that this example requires scikit-image which can be installed
# via the command:
# pip install -U scikit-image
# Or downloaded from http://scikit-image.org/download.html.
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)
# 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.
dets = detector(img, 1)
print("Number of faces detected: {}".format(len(dets)))
for i, d in enumerate(dets):
print("Detection {}: Left: {} Top: {} Right: {} Bottom: {}".format(
i, d.left(), d.top(), d.right(), d.bottom()))
win.clear_overlay()
win.set_image(img)
win.add_overlay(dets)
def create_window(self):
self.window = dlib.image_window()
# face landmarking datasets you will obtain state-of-the-art results, as shown
# in the Kazemi paper.
testing_xml_path = os.path.join(faces_folder, "testing_with_face_landmarks.xml")
print("Testing accuracy: {}".format(
dlib.test_shape_predictor(testing_xml_path, "predictor.dat")))
# Now let's use it as you would in a normal application. First we will load it
# from disk. We also need to load a face detector to provide the initial
# estimate of the facial location.
predictor = dlib.shape_predictor("predictor.dat")
detector = dlib.get_frontal_face_detector()
# Now let's run the detector and shape_predictor over the images in the faces
# folder and display the results.
print("Showing detections and predictions on the images in the faces folder...")
win = dlib.image_window()
for f in glob.glob(os.path.join(faces_folder, "*.jpg")):
print("Processing file: {}".format(f))
img = io.imread(f)
win.clear_overlay()
win.set_image(img)
# 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
# will make everything bigger and allow us to detect more faces.
dets = detector(img, 1)
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()))
# Get the landmarks/parts for the face in box d.
# Github: https://github.com/coneypo/Dlib_examples
import dlib
from skimage import io
# 使用 Dlib 的正面人脸检测器 frontal_face_detector
detector = dlib.get_frontal_face_detector()
# Dlib 的 68 点模型
predictor = dlib.shape_predictor("../data/data_dlib/shape_predictor_68_face_landmarks.dat")
# 图片所在路径
img = io.imread("../data/data_faces/faces_2.jpeg")
# 生成 Dlib 的图像窗口
win = dlib.image_window()
win.set_image(img)
# 使用检测器来检测图像中的人脸
faces = detector(img, 1)
print("人脸数:", len(faces))
for i, d in enumerate(faces):
print("第", i+1, "个人脸的矩形框坐标:",
"left:", d.left(), '\t', "right:", d.right(), '\t', "top:", d.top(),'\t', "bottom:", d.bottom())
# 使用 predictor 来计算面部轮廓
shape = predictor(img, faces[i])
# 绘制面部轮廓, Blue
# full_object_detection
win.add_overlay(shape)