{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "# 引入影象處理庫OpenCV\n", "import cv2" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "# 引入人臉識別庫OpenCV\n", "import dlib" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "# 分類名稱\n", "name = ['Obama','TZUYU']" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "# 載入訓練後的模型\n", "model = cv2.face.EigenFaceRecognizer_create()\n", "model.read('data/selfcascades/predict_face.xml')" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "# 選擇要辨識的圖片檔\n", "imagePath = \"data/image/who.jpg\"" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "# Read the image\n", "image = cv2.imread(imagePath)" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [], "source": [ "# 將影像轉換成灰度\n", "gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [], "source": [ "# 使用dlib庫提供的人臉提取器\n", "detector = dlib.get_frontal_face_detector()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# 返回臉的資訊\n", "faces = detector(gray) " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1. Label:0, Name:Obama, Confidence:16402.54\n", "2. Label:0, Name:Obama, Confidence:22148.39\n", "3. Label:0, Name:Obama, Confidence:12617.50\n", "4. Label:1, Name:TZUYU, Confidence:16025.01\n", "5. Label:0, Name:Obama, Confidence:12419.04\n", "6. Label:0, Name:Obama, Confidence:17780.87\n", "7. Label:0, Name:Obama, Confidence:17702.98\n", "8. Label:1, Name:TZUYU, Confidence:27281.46\n" ] } ], "source": [ "num = 1\n", "\n", "# 遍尋每張人臉\n", "for face in faces:\n", " \n", " x = face.left()\n", " y = face.top()\n", " w = face.width()\n", " h = face.height() \n", " \n", " # 繪製矩形框住人臉\n", " cv2.rectangle(image, (x, y), (x+w, y+h), (0, 255, 0), 2)\n", " \n", " # 擷取圖片中,人臉的部位圖片\n", " face = cv2.resize(gray[y:y+h, x:x+w], (400,400))\n", " \n", " # 使用模型去預測臉\n", " params = model.predict(face)\n", " \n", " print('%d. Label:%s, Name:%s, Confidence:%.2f'%(num, params[0], name[params[0]], params[1]))\n", " \n", " # params[0] : 表示對應的標籤 / params[1] : 表示閾值\n", " if(params[1] < 16500):\n", " who = name[params[0]]\n", " else:\n", " who = 'unknown'\n", " cv2.imshow(('%d.unknown face' % num), face)\n", " \n", " # 繪製文字於矩形上\n", " cv2.putText(image, who, (x, y-10), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2) \n", "\n", " num = num + 1\n", " \n", "# 顯示圖片\n", "cv2.imshow('Predict result', image)\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 }