-
Notifications
You must be signed in to change notification settings - Fork 0
/
sample.py
55 lines (49 loc) · 2.1 KB
/
sample.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import sys
import dlib
from PIL import Image
import PIL
import numpy as np
import cv2
if len(sys.argv) < 3:
print(
"Call this program like this:\n"
" ./cnn_face_detector.py mmod_human_face_detector.dat ../examples/faces/*.jpg\n"
"You can get the mmod_human_face_detector.dat file from:\n"
" http://dlib.net/files/mmod_human_face_detector.dat.bz2")
exit()
cnn_face_detector = dlib.cnn_face_detection_model_v1(sys.argv[1])
win = dlib.image_window()
for f in sys.argv[2:]:
print("Processing file: {}".format(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.
file = r"C:\New folder\image20.png"
image = PIL.Image.open(file)
print("Image is ", file)
# image=image.resize((800,600))
image = image.convert('RGB')
image.save("resizedImage.jpg")
image = PIL.Image.open("resizedImage.jpg")
image = np.float32(image)
dets = cnn_face_detector(image, 1)
'''
This detector returns a mmod_rectangles object. This object contains a list of mmod_rectangle objects.
These objects can be accessed by simply iterating over the mmod_rectangles object
The mmod_rectangle object has two member variables, a dlib.rectangle object, and a confidence score.
It is also possible to pass a list of images to the detector.
- like this: dets = cnn_face_detector([image list], upsample_num, batch_size = 128)
In this case it will return a mmod_rectangless object.
This object behaves just like a list of lists and can be iterated over.
'''
print("Number of faces detected: {}".format(len(dets)))
for i, d in enumerate(dets):
print("Detection {}: Left: {} Top: {} Right: {} Bottom: {} Confidence: {}".format(
i, d.rect.left(), d.rect.top(), d.rect.right(), d.rect.bottom(), d.confidence))
rects = dlib.rectangles()
rects.extend([d.rect for d in dets])
win.clear_overlay()
win.set_image(image)
win.add_overlay(rects)
dlib.hit_enter_to_continue()