Skip to content

Instantly share code, notes, and snippets.

@J0onYEong
Created May 19, 2024 14:53
Show Gist options
  • Save J0onYEong/803f382e2be32cd08b32a69201c184d7 to your computer and use it in GitHub Desktop.
Save J0onYEong/803f382e2be32cd08b32a69201c184d7 to your computer and use it in GitHub Desktop.
Untitled0.ipynb
Display the source blob
Display the rendered blob
Raw
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"provenance": [],
"authorship_tag": "ABX9TyNH3bCM/SJ3ggfIGhxhm29D",
"include_colab_link": true
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
},
"language_info": {
"name": "python"
}
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
"<a href=\"https://colab.research.google.com/gist/J0onYEong/803f382e2be32cd08b32a69201c184d7/untitled0.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"cell_type": "code",
"source": [
"import dlib\n",
"import cv2\n",
"import matplotlib.pyplot as plt\n",
"\n",
"# 얼굴 검출기와 랜드마크 검출기 초기화\n",
"detector = dlib.get_frontal_face_detector()\n",
"predictor = dlib.shape_predictor(\"shape_predictor_68_face_landmarks.dat\")\n",
"\n",
"# 이미지 로드\n",
"image = cv2.imread(\"face_image.jpg\")\n",
"gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)\n",
"\n",
"# 얼굴 검출\n",
"faces = detector(gray_image)\n",
"\n",
"for face in faces:\n",
" x1, y1, x2, y2 = face.left(), face.top(), face.right(), face.bottom()\n",
" cv2.rectangle(image, (x1, y1), (x2, y2), (0, 255, 0), 3)\n",
"\n",
" # 랜드마크 검출\n",
" landmarks = predictor(gray, face)\n",
"\n",
" # 코 교량의 양쪽 끝점\n",
" nose_start = landmarks.part(28)\n",
" nose_end = landmarks.part(31)\n",
"\n",
"\n",
"\n",
"\n",
"\n",
" for n in range(0, 68):\n",
" x = landmarks.part(n).x\n",
" y = landmarks.part(n).y\n",
" cv2.putText(image, str(n+1), (x, y), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 0, 0), 1, cv2.LINE_AA)\n",
"\n",
"\n",
"# BGR 이미지를 RGB로 변환\n",
"image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)\n",
"\n",
"# matplotlib를 사용해 이미지 표시\n",
"plt.figure(figsize=(10, 10))\n",
"plt.imshow(image_rgb)\n",
"plt.axis(\"off\") # 축을 표시하지 않음\n",
"plt.show()"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 297
},
"id": "iSqk3DY_-rHD",
"outputId": "d53beb25-f87c-48b5-a8c2-926dca4329dd"
},
"execution_count": null,
"outputs": [
{
"output_type": "error",
"ename": "RuntimeError",
"evalue": "Error deserializing object of type short\n while deserializing a floating point number.\n while deserializing a dlib::matrix\n while deserializing object of type std::vector\n while deserializing object of type std::vector\n while deserializing object of type std::vector",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mRuntimeError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-4-bedd7c6031a7>\u001b[0m in \u001b[0;36m<cell line: 7>\u001b[0;34m()\u001b[0m\n\u001b[1;32m 5\u001b[0m \u001b[0;31m# 얼굴 검출기와 랜드마크 검출기 초기화\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 6\u001b[0m \u001b[0mdetector\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mdlib\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mget_frontal_face_detector\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 7\u001b[0;31m \u001b[0mpredictor\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mdlib\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mshape_predictor\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"shape_predictor_68_face_landmarks.dat\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 8\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 9\u001b[0m \u001b[0;31m# 이미지 로드\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;31mRuntimeError\u001b[0m: Error deserializing object of type short\n while deserializing a floating point number.\n while deserializing a dlib::matrix\n while deserializing object of type std::vector\n while deserializing object of type std::vector\n while deserializing object of type std::vector"
]
}
]
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment