{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import cv2" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "# Image path\n", "imagePath = \"data/image/smile.jpg\"" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "# Cascade path\n", "cascPath = \"data/haarcascades/haarcascade_frontalface_default.xml\"\n", "smilePath = \"data/haarcascades/haarcascade_smile.xml\"" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "# Create the haar cascade\n", "faceCascade = cv2.CascadeClassifier(cascPath)\n", "smileCascade = cv2.CascadeClassifier(smilePath)" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "# Read the image\n", "image = cv2.imread(imagePath)\n", "gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "# Detect faces in the image\n", "faces = faceCascade.detectMultiScale(\n", " gray,\n", " scaleFactor=1.1, #表示在前後兩次相繼的掃描中,搜索窗口的比例係數。默認為1.1即每次搜索窗口依次擴大10%\n", " minNeighbors=5, #表示構成檢測目標的相鄰矩形的最小個數(默認為3個)\n", " minSize=(30, 30),#限制得到的目標區域的最小範圍\n", ")" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Found 4 faces!\n" ] } ], "source": [ "print(\"Found {0} faces!\".format(len(faces)))" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [], "source": [ "# Draw a rectangle around the faces\n", "for (x, y, w, h) in faces:\n", " cv2.rectangle(image, (x, y), (x+w, y+h), (0, 255, 0), 2)\n", "\n", " roi_gray = gray[y:y+h, x:x+w]\n", " roi_color = image[y:y+h, x:x+w]\n", "\n", " smile = smileCascade.detectMultiScale(\n", " roi_gray,\n", " scaleFactor= 1.16,\n", " minNeighbors=35,\n", " minSize=(25, 25)\n", " )\n", "\n", " # Draw a rectangle around the smile faces and tag it\n", " for (x2, y2, w2, h2) in smile:\n", " cv2.rectangle(roi_color, (x2, y2), (x2+w2, y2+h2), (255, 0, 0), 2)\n", " cv2.putText(image,'Smile',(x,y-7), 3, 1.2, (0, 255, 0), 2, cv2.LINE_AA)" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [], "source": [ "cv2.imshow(\"Who is smiling ?\" ,image)" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "-1" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "cv2.waitKey(0)" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [], "source": [ "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 }