{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# 引入人臉識別庫dlib\n", "import dlib" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# 引入影象處理庫OpenCV、imutils\n", "import cv2\n", "from imutils import face_utils" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "imagePath = \"data/image/men.jpg\"" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# 使用dlib庫提供的人臉提取器\n", "detector = dlib.get_frontal_face_detector()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# 使用官方提供的模型構建特徵提取器\n", "predictor = dlib.shape_predictor(\"data/dlib/landmarks.dat\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "img = cv2.imread(imagePath)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# 轉成灰度圖\n", "gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# 返回臉的資訊\n", "faces = detector(gray)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# loop over the face detections\n", "for face in faces:\n", " \n", " # 標記人臉中的68個特徵點\n", " shape = predictor(gray, face)\n", " \n", " # shape轉換成68個坐標點矩陣\n", " shape = face_utils.shape_to_np(shape)\n", "\n", " # 繪製圖形標誌\n", " for (x, y) in shape:\n", " cv2.circle(img, (x, y), 1, (0, 0, 255), -1)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "cv2.imshow(\"Face landmark found\" ,img)\n", "cv2.waitKey(0)\n", "cv2.destroyAllWindows()" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.6.8" } }, "nbformat": 4, "nbformat_minor": 2 }