import dlib
import cv2
import imutils

# 讀取照片圖檔
# sources :https://blog.gtwang.org/programming/python-opencv-dlib-face-detection-implementation-tutorial/
img = cv2.imread('/Users/Ivan.lam/Projects/DjangoReact-Boilerplate/project/project/human-20180303-01-1024x683.jpg')

# 縮小圖片
img = imutils.resize(img, width=1280)

# Dlib 的人臉偵測器
detector = dlib.get_frontal_face_detector()

# 偵測人臉
#face_rects = detector(img, 0)
# with score
face_rects, scores, idx = detector.run(img, 0, -1)

# 取出所有偵測的結果
for i, d in enumerate(face_rects):
  x1 = d.left()
  y1 = d.top()
  x2 = d.right()
  y2 = d.bottom()
  text = "%2.2f(%d)" % ( scores[i], idx[i])

  # 以方框標示偵測的人臉
  # rectangle(img, pt1, pt2, color, thickness=None, lineType=None, shift=None)
  cv2.rectangle(img, (x1, y1), (x2, y2), (0, 255, 0), 4, cv2.LINE_AA)

  # 標示分數
  # putText(img, text, org, fontFace, fontScale, color, thickness=None, lineType=None, bottomLeftOrigin=None)
  cv2.putText(img, text, (x1, y1), cv2.FONT_HERSHEY_DUPLEX,
              0.7, (255, 255, 255), 1, cv2.LINE_AA)

# 顯示結果
cv2.imshow("Face Detection", img)

cv2.waitKey(0)
cv2.destroyAllWindows()