{"id":412,"date":"2020-06-27T14:42:30","date_gmt":"2020-06-27T06:42:30","guid":{"rendered":"http:\/\/123.57.142.208\/?p=412"},"modified":"2021-06-28T00:32:31","modified_gmt":"2021-06-27T16:32:31","slug":"%e5%88%a9%e7%94%a8dlib%e5%ae%9e%e7%8e%b0%e4%ba%ba%e8%84%b8%e8%af%86%e5%88%ab","status":"publish","type":"post","link":"http:\/\/43.142.23.155\/?p=412","title":{"rendered":"\u5229\u7528dlib\u5b9e\u73b0\u4eba\u8138\u8bc6\u522b"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">\u6700\u8fd1\u4e5f\u662f\u4e34\u5371\u53d7\u547d\uff0c\u8981\u628a3\u6708\u505a\u7684\u4eba\u8138\u8bc6\u522b\u5d4c\u5165\u5230\u9879\u76ee\u7684\u603b\u4f53\u7cfb\u7edf\u4e2d\uff0c\u4e3a\u4e86\u6ee1\u8db3\u201c\u5ba2\u6237\u8981\u6c42\u201d\uff0c\u6211\u5bf9\u5f53\u65f6\u7684\u4ee3\u7801\u7ed3\u6784\u8fdb\u884c\u4e86\u4e00\u4e9b\u6539\u52a8\u548c\u589e\u5220\uff0c\u7528\u65f6\u4e24\u5929&#8230;\u6211\u592a\u83dc\u4e86<\/p>\n\n\n\n<p class=\"has-large-font-size wp-block-paragraph\">\u4e00\u3001\u4eba\u8138\u8bc6\u522b\u7684\u539f\u7406<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">dlib\u662fpython\u7684\u4e00\u4e2a\u5e93\uff0c\u5176\u914d\u5408\u5df2\u8bad\u7ec3\u597d\u7684dat\u6a21\u578b\uff08\u68c0\u6d4b\u5668\uff09\u53ef\u4ee5\u7b80\u5355\u5730\u5b9e\u73b0\u4eba\u8138\u5f55\u5165\u4e0e\u68c0\u6d4b\u8bc6\u522b<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u8868\u5c42\u539f\u7406\u5f88\u7b80\u5355\uff0c\u8c03\u7528\u68c0\u6d4b\u5668\u68c0\u6d4b\u4eba\u8138\u5e76\u5c06\u4eba\u8138\u7684\u56fe\u50cf\u7279\u5f81\u8f6c\u5316\u6210128D\u7684\u7279\u5f81\u5411\u91cf\u5e76\u4fdd\u5b58\u5728csv\u6587\u4ef6\u4e2d<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u7ed9\u51e0\u4e2a\u56fe\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/img0.baidu.com\/it\/u=521715670,1279568492&amp;fm=26&amp;fmt=auto&amp;gp=0.jpg\" alt=\"\"\/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" src=\"http:\/\/123.57.142.208\/wp-content\/uploads\/2020\/06\/image-6.png\" alt=\"\" class=\"wp-image-423\"\/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\u6f02\u4eae\u59d0\u59d0i\u4e86i\u4e86<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u53e6\u5916\u5173\u4e8e\u4eba\u8138\u6846\u7684\u5b9a\u4f4d\uff1a\u5982\u56fe<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" src=\"http:\/\/39.107.80.187\/wp-content\/uploads\/2020\/06\/image-7-1.png\" alt=\"\" class=\"wp-image-424\"\/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e3b\u4f53\u91c7\u7528Resnet\u751f\u6210\u4e00\u4e2a128D\u7684\u7279\u5f81\u5411\u91cf\uff0cresnet\u662f\u91c7\u752834\u5c42\u7684\uff08resnet34\uff09\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4f46\u7531\u4e8eresnet34\u6700\u540e\u4e00\u5c42\u795e\u7ecf\u7f51\u7edc\u91cc\u5b9e\u9645\u4e0a\u67091000\u4e2a\u795e\u7ecf\u5143\uff0c\u6240\u4ee5dlib\u5728\u540e\u9762\u52a0\u4e86Dense\uff08128\uff09\uff0c\u6240\u4ee5\u751f\u6210\u7684\u662f128\u7ef4\u5ea6\u7684\u5411\u91cf<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" src=\"http:\/\/39.107.80.187\/wp-content\/uploads\/2020\/06\/image-8-1.png\" alt=\"\" class=\"wp-image-426\"\/><\/figure>\n\n\n\n<p class=\"has-large-font-size wp-block-paragraph\">\u4e8c\u3001python\u5b9e\u73b0<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4eba\u8138\u5f55\u5165\u6e90\u7801\u5982\u4e0b\uff1a<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd9\u91cc\u6211\u4eec\u91c7\u7528opencv\u8c03\u7528\u7535\u8111\u6444\u50cf\u5934\u8fdb\u884c\u4eba\u8138\u5f55\u5165<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u652f\u6301\u529f\u80fd\uff1a\u8303\u56f4\u63d0\u9192\u3001\u4fdd\u5b58\u56fe\u50cf\u3001\u547d\u540d\u4eba\u540d\uff08\u5230\u6587\u4ef6\u5939\uff09\u7b49\u7b49\u5427\uff08\u5065\u5fd8\uff09<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># \u8fdb\u884c\u4eba\u8138\u5f55\u5165 \/ face register\n# \u5f55\u5165\u591a\u5f20\u4eba\u8138 \/ support multi-faces\n# EotStxTaB\n\n#!\/usr\/bin\/python3\n\nimport dlib         # \u4eba\u8138\u5904\u7406\u7684\u5e93 Dlib\nimport numpy as np  # \u6570\u636e\u5904\u7406\u7684\u5e93 Numpy\nimport cv2          # \u56fe\u50cf\u5904\u7406\u7684\u5e93 OpenCv\nimport os           # \u8bfb\u5199\u6587\u4ef6\nimport shutil       # \u8bfb\u5199\u6587\u4ef6\n\n# Dlib \u6b63\u5411\u4eba\u8138\u68c0\u6d4b\u5668 \/ frontal face detector\ndetector = dlib.get_frontal_face_detector()\n\n# OpenCv \u8c03\u7528\u6444\u50cf\u5934 \/ Use camera\ncap = cv2.VideoCapture(0)\n\n# \u4eba\u8138\u622a\u56fe\u7684\u8ba1\u6570\u5668 \/ The counter for screen shoot\ncnt_ss = 0\n\n# \u5b58\u50a8\u4eba\u8138\u7684\u6587\u4ef6\u5939 \/ The folder to save face images\ncurrent_face_dir = \"\"\n\n# \u4fdd\u5b58 faces images \u7684\u8def\u5f84 \/ The directory to save images of faces\npath_photos_from_camera = \"C:\/Users\/EotStxTaB\/Documents\/face\/data\/data_faces_from_camera\/\"\n\n\n# 1. \u65b0\u5efa\u4fdd\u5b58\u4eba\u8138\u56fe\u50cf\u6587\u4ef6\u548c\u6570\u636eCSV\u6587\u4ef6\u5939\n# 1. Mkdir for saving photos and csv\ndef pre_work_mkdir():\n\n    # \u65b0\u5efa\u6587\u4ef6\u5939  make folders to save faces images and csv\n    if os.path.isdir(path_photos_from_camera):\n        pass\n    else:\n        os.mkdir(path_photos_from_camera)\n\n\npre_work_mkdir()\n\n'''\n##### optional\/\u53ef\u9009, \u9ed8\u8ba4\u5173\u95ed #####\n# 2. \u5220\u9664\u4e4b\u524d\u5b58\u7684\u4eba\u8138\u6570\u636e\u6587\u4ef6\u5939\n# 2. Delete the old data of faces\ndef pre_work_del_old_face_folders():\n    # \u5220\u9664\u4e4b\u524d\u5b58\u7684\u4eba\u8138\u6570\u636e\u6587\u4ef6\u5939\n    # \u5220\u9664 \"\/data_faces_from_camera\/person_x\/\"...\n    folders_rd = os.listdir(path_photos_from_camera)\n    for i in range(len(folders_rd)):\n        shutil.rmtree(path_photos_from_camera+folders_rd&#91;i])\n\n    if os.path.isfile(\"C:\/Users\/EotStxTaB\/Documents\/face\/data\/features_all.csv\"):\n        os.remove(\"C:\/Users\/EotStxTaB\/Documents\/face\/data\/features_all.csv\")\n\n# \u8fd9\u91cc\u5728\u6bcf\u6b21\u7a0b\u5e8f\u5f55\u5165\u4e4b\u524d, \u5220\u6389\u4e4b\u524d\u5b58\u7684\u4eba\u8138\u6570\u636e\n# \u5982\u679c\u8fd9\u91cc\u6253\u5f00\uff0c\u6bcf\u6b21\u8fdb\u884c\u4eba\u8138\u5f55\u5165\u7684\u65f6\u5019\u90fd\u4f1a\u5220\u6389\u4e4b\u524d\u7684\u4eba\u8138\u56fe\u50cf\u6587\u4ef6\u5939 person_1\/,person_2\/,person_3\/...\n# If enable this function, it will delete all the old data in dir person_1\/,person_2\/,\/person_3\/...\n# pre_work_del_old_face_folders()\n##################################\n'''\n'''\n# 3. Check people order: person_cnt\n# \u5982\u679c\u6709\u4e4b\u524d\u5f55\u5165\u7684\u4eba\u8138 \/ If the old folders existsq\n# \u5728\u4e4b\u524d person_x \u7684\u5e8f\u53f7\u6309\u7167 person_x+1 \u5f00\u59cb\u5f55\u5165 \/ Start from person_x+1\nif os.listdir(\"C:\/Users\/EotStxTaB\/Documents\/face\/data\/data_faces_from_camera\/\"):\n    # \u83b7\u53d6\u5df2\u5f55\u5165\u7684\u6700\u540e\u4e00\u4e2a\u4eba\u8138\u5e8f\u53f7 \/ Get the num of latest person\n    person_list = os.listdir(\"C:\/Users\/EotStxTaB\/Documents\/face\/data\/data_faces_from_camera\/\")\n    person_num_list = &#91;]\n    for person in person_list:\n        person_num_list.append(int(person.split('_')&#91;-1]))\n    person_cnt = max(person_num_list)\n\n# \u5982\u679c\u7b2c\u4e00\u6b21\u5b58\u50a8\u6216\u8005\u6ca1\u6709\u4e4b\u524d\u5f55\u5165\u7684\u4eba\u8138, \u6309\u7167 person_1 \u5f00\u59cb\u5f55\u5165\n# Start from person_1\nelse:\n    '''\nperson_cnt = 0\n\n# \u4e4b\u540e\u7528\u6765\u63a7\u5236\u662f\u5426\u4fdd\u5b58\u56fe\u50cf\u7684 flag \/ The flag to control if save\nsave_flag = 1\n\n# \u4e4b\u540e\u7528\u6765\u68c0\u67e5\u662f\u5426\u5148\u6309 'n' \u518d\u6309 's' \/ The flag to check if press 'n' before 's'\npress_n_flag = 0\n\n#name = &#91;]\nk = 0\n\nwhile cap.isOpened():\n    flag, img_rd = cap.read()\n    # print(img_rd.shape)\n    # It should be 480 height * 640 width in Windows and Ubuntu by default\n    # Maybe 1280x720 in macOS\n\n    kk = cv2.waitKey(1)\n\n    img_gray = cv2.cvtColor(img_rd, cv2.COLOR_RGB2GRAY)\n    \n    # \u4eba\u8138 \/ Faces\n    faces = detector(img_gray, 0)\n\n    # \u5f85\u4f1a\u8981\u5199\u7684\u5b57\u4f53 \/ Font to write\n    font = cv2.FONT_ITALIC\n\n    # 4. \u6309\u4e0b 'n' \u65b0\u5efa\u5b58\u50a8\u4eba\u8138\u7684\u6587\u4ef6\u5939 \/ press 'n' to create the folders for saving faces\n    if kk == ord('n'):\n        #person_cnt += 1\n        name = input(\"\u8bf7\u8f93\u5165\uff1a\")\n        current_face_dir = path_photos_from_camera + name\n        os.makedirs(current_face_dir)\n        print('\\n')\n        print(\"\u65b0\u5efa\u7684\u4eba\u8138\u6587\u4ef6\u5939 \/ Create folders: \", current_face_dir)\n\n        cnt_ss = 0              # \u5c06\u4eba\u8138\u8ba1\u6570\u5668\u6e05\u96f6 \/ clear the cnt of faces\n        press_n_flag = 1        # \u5df2\u7ecf\u6309\u4e0b 'n' \/ have pressed 'n'\n\n    # \u68c0\u6d4b\u5230\u4eba\u8138 \/ Face detected\n    if len(faces) != 0:\n        # \u77e9\u5f62\u6846 \/ Show the rectangle box of face\n        for k, d in enumerate(faces):\n            # \u8ba1\u7b97\u77e9\u5f62\u5927\u5c0f\n            # Compute the width and height of the box\n            # (x,y), (\u5bbd\u5ea6width, \u9ad8\u5ea6height)\n            pos_start = tuple(&#91;d.left(), d.top()])\n            pos_end = tuple(&#91;d.right(), d.bottom()])\n\n            # \u8ba1\u7b97\u77e9\u5f62\u6846\u5927\u5c0f \/ compute the size of rectangle box\n            height = (d.bottom() - d.top())\n            width = (d.right() - d.left())\n\n            hh = int(height\/2)\n            ww = int(width\/2)\n\n            # \u8bbe\u7f6e\u989c\u8272 \/ the color of rectangle of faces detected\n            color_rectangle = (255, 255, 255)\n\n            # \u5224\u65ad\u4eba\u8138\u77e9\u5f62\u6846\u662f\u5426\u8d85\u51fa 480x640\n            if (d.right()+ww) &gt; 640 or (d.bottom()+hh &gt; 480) or (d.left()-ww &lt; 0) or (d.top()-hh &lt; 0):\n                cv2.putText(img_rd, \"OUT OF RANGE\", (20, 300), font, 0.8, (0, 0, 255), 1, cv2.LINE_AA)\n                color_rectangle = (0, 0, 255)\n                save_flag = 0\n                if kk == ord('s'):\n                    print(\"\u8bf7\u8c03\u6574\u4f4d\u7f6e \/ Please adjust your position\")\n            else:\n                color_rectangle = (255, 255, 255)\n                save_flag = 1\n\n            cv2.rectangle(img_rd,\n                          tuple(&#91;d.left() - ww, d.top() - hh]),\n                          tuple(&#91;d.right() + ww, d.bottom() + hh]),\n                          color_rectangle, 2)\n\n            # \u6839\u636e\u4eba\u8138\u5927\u5c0f\u751f\u6210\u7a7a\u7684\u56fe\u50cf \/ Create blank image according to the shape of face detected\n            im_blank = np.zeros((int(height*2), width*2, 3), np.uint8)\n\n            if save_flag:\n                # 5. \u6309\u4e0b 's' \u4fdd\u5b58\u6444\u50cf\u5934\u4e2d\u7684\u4eba\u8138\u5230\u672c\u5730 \/ Press 's' to save faces into local images\n                if kk == ord('s'):\n                    # \u68c0\u67e5\u6709\u6ca1\u6709\u5148\u6309'n'\u65b0\u5efa\u6587\u4ef6\u5939 \/ check if you have pressed 'n'\n                    if press_n_flag:\n                        cnt_ss += 1\n                        for ii in range(height*2):\n                            for jj in range(width*2):\n                                im_blank&#91;ii]&#91;jj] = img_rd&#91;d.top()-hh + ii]&#91;d.left()-ww + jj]\n                        cv2.imwrite(current_face_dir + \"\/img_face_\" + str(cnt_ss) + \".jpg\", im_blank)\n                        print(\"\u5199\u5165\u672c\u5730 \/ Save into\uff1a\", str(current_face_dir) + \"\/img_face_\" + str(cnt_ss) + \".jpg\")\n                    else:\n                        print(\"\u8bf7\u5728\u6309 'S' \u4e4b\u524d\u5148\u6309 'N' \u6765\u5efa\u6587\u4ef6\u5939 \/ Please press 'N' before 'S'\")\n\n    # \u663e\u793a\u4eba\u8138\u6570 \/ Show the numbers of faces detected\n    cv2.putText(img_rd, \"Faces: \" + str(len(faces)), (20, 100), font, 0.8, (0, 255, 0), 1, cv2.LINE_AA)\n\n    # \u6dfb\u52a0\u8bf4\u660e \/ Add some statements\n    cv2.putText(img_rd, \"Face Register\", (20, 40), font, 1, (0, 0, 0), 1, cv2.LINE_AA)\n    cv2.putText(img_rd, \"N: Create face folder\", (20, 350), font, 0.8, (0, 0, 0), 1, cv2.LINE_AA)\n    cv2.putText(img_rd, \"S: Save current face\", (20, 400), font, 0.8, (0, 0, 0), 1, cv2.LINE_AA)\n    cv2.putText(img_rd, \"Q: Quit\", (20, 450), font, 0.8, (0, 0, 0), 1, cv2.LINE_AA)\n\n    # 6. \u6309\u4e0b 'q' \u952e\u9000\u51fa \/ Press 'q' to exit\n    if kk == ord('q'):\n        break\n\n    # \u5982\u679c\u9700\u8981\u6444\u50cf\u5934\u7a97\u53e3\u5927\u5c0f\u53ef\u8c03 \/ Uncomment this line if you want the camera window is resizeable\n    # cv2.namedWindow(\"camera\", 0)\n\n    cv2.imshow(\"camera\", img_rd)\n\n# \u91ca\u653e\u6444\u50cf\u5934 \/ Release camera and destroy all windows\ncap.release()\ncv2.destroyAllWindows()<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u63a5\u4e0b\u6765\u662f\u8f6c\u5316\u56fe\u50cf\u4e3a128\u5411\u91cf<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u6839\u636e\u8981\u6c42\uff0c\u6211\u628acsv\u6bcf\u4e2a\u90fd\u62c6\u5206\u5f00\u6765\u5b58\u5728\u5404\u81ea\u6587\u4ef6\u5939<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># \u4ece\u4eba\u8138\u56fe\u50cf\u6587\u4ef6\u4e2d\u63d0\u53d6\u4eba\u8138\u7279\u5f81\u5b58\u5165 CSV\n# Features extraction from images and save into features_all.csv\n# EotStxTaB\n\nimport cv2\nimport os\nimport dlib\nfrom skimage import io\nimport csv\nimport numpy as np\n\n# \u8981\u8bfb\u53d6\u4eba\u8138\u56fe\u50cf\u6587\u4ef6\u7684\u8def\u5f84\npath_images_from_camera = \"C:\/Users\/EotStxTaB\/Documents\/face\/data\/data_faces_from_camera\/\"\n\n# Dlib \u6b63\u5411\u4eba\u8138\u68c0\u6d4b\u5668\ndetector = dlib.get_frontal_face_detector()\n\n# Dlib \u4eba\u8138\u9884\u6d4b\u5668\npredictor = dlib.shape_predictor(\"C:\/Users\/EotStxTaB\/Documents\/face\/data\/data_dlib\/shape_predictor_68_face_landmarks.dat\")\n\n# Dlib \u4eba\u8138\u8bc6\u522b\u6a21\u578b\n# Face recognition model, the object maps human faces into 128D vectors\nface_rec = dlib.face_recognition_model_v1(\"C:\/Users\/EotStxTaB\/Documents\/face\/data\/data_dlib\/dlib_face_recognition_resnet_model_v1.dat\")\n\n\n# \u8fd4\u56de\u5355\u5f20\u56fe\u50cf\u7684 128D \u7279\u5f81\ndef return_128d_features(path_img):\n    img_rd = io.imread(path_img)\n    img_gray = cv2.cvtColor(img_rd, cv2.COLOR_BGR2RGB)\n    faces = detector(img_gray, 1)\n\n    print(\"%-40s %-20s\" % (\"\u68c0\u6d4b\u5230\u4eba\u8138\u7684\u56fe\u50cf \/ image with faces detected:\", path_img), '\\n')\n\n    # \u56e0\u4e3a\u6709\u53ef\u80fd\u622a\u4e0b\u6765\u7684\u4eba\u8138\u518d\u53bb\u68c0\u6d4b\uff0c\u68c0\u6d4b\u4e0d\u51fa\u6765\u4eba\u8138\u4e86\n    # \u6240\u4ee5\u8981\u786e\u4fdd\u662f \u68c0\u6d4b\u5230\u4eba\u8138\u7684\u4eba\u8138\u56fe\u50cf \u62ff\u53bb\u7b97\u7279\u5f81\n    if len(faces) != 0:\n        shape = predictor(img_gray, faces&#91;0])\n        face_descriptor = face_rec.compute_face_descriptor(img_gray, shape)\n    else:\n        face_descriptor = 0\n        print(\"no face\")\n\n    return face_descriptor\n\n\n# \u5c06\u6587\u4ef6\u5939\u4e2d\u7167\u7247\u7279\u5f81\u63d0\u53d6\u51fa\u6765, \u5199\u5165 CSV\ndef return_features_mean_personX(path_faces_personX):\n    features_list_personX = &#91;]\n    photos_list = os.listdir(path_faces_personX)\n    if photos_list:\n        #for i in range(len(photos_list)):\n            # \u8c03\u7528return_128d_features()\u5f97\u5230128d\u7279\u5f81\n            print(\"%-40s %-20s\" % (\"\u6b63\u5728\u8bfb\u7684\u4eba\u8138\u56fe\u50cf \/ image to read:\",path_faces_personX + \"\/img_face_1.jpg\"))\n            features_128d = return_128d_features(path_faces_personX + \"\/img_face_1.jpg\")  \n            #  print(features_128d)\n            # \u9047\u5230\u6ca1\u6709\u68c0\u6d4b\u51fa\u4eba\u8138\u7684\u56fe\u7247\u8df3\u8fc7\n            if features_128d == 0:\n                i += 1\n            else:\n                features_list_personX.append(features_128d)\n    else:\n        print(\"\u6587\u4ef6\u5939\u5185\u56fe\u50cf\u6587\u4ef6\u4e3a\u7a7a \/ Warning: No images in \" + path_faces_personX + '\/', '\\n')\n\n    # \u8ba1\u7b97 128D \u7279\u5f81\u7684\u5747\u503c\n    # personX \u7684 N \u5f20\u56fe\u50cf x 128D -&gt; 1 x 128D\n    if features_list_personX:\n        features_mean_personX = np.array(features_list_personX).mean(axis=0)\n    else:\n        features_mean_personX = '0'\n\n    return features_mean_personX\n\n\ndirct = 'C:\/Users\/EotStxTaB\/Documents\/face\/data\/data_faces_from_camera'\ndirList=&#91;]\nn = 0\nfiles=os.listdir(dirct)  #\u6587\u4ef6\u5939\u4e0b\u6240\u6709\u76ee\u5f55\u7684\u5217\u8868\n#print('files:',files)\nfor f in files:\n    if os.path.isdir(dirct + '\/'+f):   #\u8fd9\u91cc\u662f\u7edd\u5bf9\u8def\u5f84\uff0c\u8be5\u53e5\u5224\u65ad\u76ee\u5f55\u662f\u5426\u662f\u6587\u4ef6\u5939\n        dirList.append(f)\n        n=n+1\n    elif os.path.isfile(dirct + '\/'+f):#\u8fd9\u91cc\u662f\u7edd\u5bf9\u8def\u5f84\uff0c\u8be5\u53e5\u5224\u65ad\u76ee\u5f55\u662f\u5426\u662f\u6587\u4ef6\n        continue\n\n\nfor i in range(0,n):\n    with open (\"C:\/Users\/EotStxTaB\/Documents\/face\/data\/data_faces_from_camera\/\"+dirList&#91;i]+\"\/features.csv\", \"w\", newline='') as csvfile:\n        writer = csv.writer(csvfile)\n        features_mean_personX = return_features_mean_personX(path_images_from_camera + dirList&#91;i])\n        writer.writerow(features_mean_personX)\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u63a5\u4e0b\u6765\u662f\u5927\u6539\u7684\u5730\u65b9\uff0c\u7531\u4e8e\u83ab\u540d\u7684\u8ba1\u7b97\u5ef6\u65f6\u5f88\u9ad8\uff0c\u6211\u628a\u8bc6\u522b\u591a\u8138\u6539\u6210\u4e86\u56fe\u50cf\u91cc\u6700\u5927\u7684\u8138\uff08hhhh\uff09<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u540c\u65f6\u6539\u6210\u4e86\u8f93\u51fa\u8f93\u5165\u662f\u5355\u5f20\u56fe\u7247\uff0c\u4e3a\u4e86\u517c\u5bb9\u9879\u76ee\u7cfb\u7edf\u4e2d\u7684\u53e6\u5916\u4e00\u4e2a\u6444\u50cf\u5934<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># EotStxTaB\n\nimport dlib          # \u4eba\u8138\u5904\u7406\u7684\u5e93 Dlib\nfrom dlib import dlib  #vscode\u7684\u4e13\u5c5e\u9519\u8bef\u89e3\u51b3\u65b9\u5f0f\uff0ccv2\u4e0e\u6b64\u76f8\u540c\nimport numpy as np   # \u6570\u636e\u5904\u7406\u7684\u5e93 numpy\nimport cv2           # \u56fe\u50cf\u5904\u7406\u7684\u5e93 OpenCv\nfrom cv2 import cv2\nimport pandas as pd  # \u6570\u636e\u5904\u7406\u7684\u5e93 Pandas\nimport matplotlib.pyplot as plt\nimport os\nimport csv\n\n# \u4eba\u8138\u8bc6\u522b\u6a21\u578b\uff0c\u63d0\u53d6128D\u7684\u7279\u5f81\u77e2\u91cf\nfacerec = dlib.face_recognition_model_v1(\".\/data\/data_dlib\/dlib_face_recognition_resnet_model_v1.dat\")\n\n\n# \u8ba1\u7b97\u4e24\u4e2a128D\u5411\u91cf\u95f4\u7684\u6b27\u5f0f\u8ddd\u79bb\ndef return_euclidean_distance(feature_1, feature_2):\n    feature_1 = np.array(feature_1)\n    feature_2 = np.array(feature_2)\n    dist = np.sqrt(np.sum(np.square(feature_1 - feature_2)))\n    return dist\n\ndetector = dlib.get_frontal_face_detector()\npredictor = dlib.shape_predictor('.\/data\/data_dlib\/shape_predictor_68_face_landmarks.dat')\n\nimg_rd = cv2.imread('.\/data\/input.jpg',cv2.IMREAD_UNCHANGED)\nimg_gray = cv2.cvtColor(img_rd,cv2.COLOR_RGB2GRAY)\nfaces = detector(img_gray, 0)\n\nface=max(faces, key=lambda rect: rect.width() * rect.height())\n#&#91;x1,x2,y1,y2]=&#91;face.left(),face.right(),face.top(),face.bottom()]\n\nn=0\ndirct = '.\/data\/data_faces_from_camera'\ndirList=&#91;]\nfiles=os.listdir(dirct)  #\u6587\u4ef6\u5939\u4e0b\u6240\u6709\u76ee\u5f55\u7684\u5217\u8868\nfor f in files:\n    if os.path.isdir(dirct + '\/'+f):   #\u8fd9\u91cc\u662f\u7edd\u5bf9\u8def\u5f84\uff0c\u8be5\u53e5\u5224\u65ad\u76ee\u5f55\u662f\u5426\u662f\u6587\u4ef6\u5939\n        dirList.append(f)\n        n=n+1\n    elif os.path.isfile(dirct + '\/'+f): #\u8fd9\u91cc\u662f\u7edd\u5bf9\u8def\u5f84\uff0c\u8be5\u53e5\u5224\u65ad\u76ee\u5f55\u662f\u5426\u662f\u6587\u4ef6\n        continue\n\n# \u5f85\u4f1a\u8981\u5199\u7684\u5b57\u4f53 font to write later\nfont = cv2.FONT_ITALIC\n#font = cv2.FONT_HERSHEY_SCRIPT_SIMPLEX#\u624b\u5199\u82b1\u4f53\n\nshape = predictor(img_rd, face)\nfeatures_cap_arr=(facerec.compute_face_descriptor(img_rd, shape))\n#print(features_cap_arr)\n    \nfor m in range(0,n):\n    cs_rd = &#91;]\n    path_features_known_csv = \".\/data\/data_faces_from_camera\/\"+dirList&#91;m]+\"\/features.csv\"\n    df = pd.read_csv(path_features_known_csv,engine='python')#\u636e\u8bf4\u4e5f\u662fvscode\u4e13\u5c5e\u9519\u8bef\n    file=open(path_features_known_csv,'r')  #\u6253\u5f00\u6587\u4ef6\n    df=csv.reader(file)         #\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\n    for stu in df:             #\u4e00\u884c\u662f\u4e00\u4e2a\u6570\u7ec4\n        for i in range (0,128):\n            cs_rd.append(float(stu&#91;i]))            #\u53d6\u6bcf\u4e2a\u6570\u7ec4\u7684\u7b2c\u4e00\u4e2a\u5143\u7d20\n    #print(cs_rd)\n\n    # \u8ba9\u4eba\u540d\u8ddf\u968f\u5728\u77e9\u5f62\u6846\u7684\u4e0b\u65b9\n    # \u786e\u5b9a\u4eba\u540d\u7684\u4f4d\u7f6e\u5750\u6807\n    # \u5148\u9ed8\u8ba4\u6240\u6709\u4eba\u4e0d\u8ba4\u8bc6\uff0c\u662f unknown\n    name = \"unknown\"\n\n    # \u6bcf\u4e2a\u6355\u83b7\u4eba\u8138\u7684\u540d\u5b57\u5750\u6807 the positions of faces captured\n    pos_name = (tuple(&#91;face.left(), int(face.bottom() + (face.bottom() - face.top())\/4)]))\n\n    # \u5bf9\u4e8e\u67d0\u5f20\u4eba\u8138\uff0c\u904d\u5386\u6240\u6709\u5b58\u50a8\u7684\u4eba\u8138\u7279\u5f81\n    # For every faces detected, compare the faces in the database\n    # \u5982\u679c person_X \u6570\u636e\u4e0d\u4e3a\u7a7a\n    if str(cs_rd) != '0.0':\n        e_distance_tmp = return_euclidean_distance(features_cap_arr, cs_rd)\n        print(e_distance_tmp)\n    else:\n        # \u7a7a\u6570\u636e person_X\n        e_distance_tmp = 999999999\n                            \n    if e_distance_tmp &lt; 0.4:\n        name = dirList&#91;m]\n        print(\"May be person \")\n        break\n            \n    else:\n        print(\"Unknown person\")\n        continue\n\n    # \u77e9\u5f62\u6846\n    # draw rectangle\n    #\u4e0b\u4e00\u884c\u6ce8\u91ca\u6389\u5c31\u53ef\u4ee5\u6ca1\u6709\u77e9\u5f62\u6846\ncv2.rectangle(img_rd, tuple(&#91;face.left(), face.top()]), tuple(&#91;face.right(), face.bottom()]), (0, 255, 255), 2)\n#print('\\n')\n\n    # \u5199\u540d\u5b57\n    #\u4e0b\u9762\uff080\uff0c255\uff0c255\uff09\u5de6\u8fb9\u662f\u5b57\u5927\u5c0f\uff0c\u53f3\u8fb9\u662f\u7c97\u7ec6\nimg = cv2.putText(img_rd, name, pos_name, font, 5, (0, 255, 255), 4, cv2.LINE_AA)\nplt.imshow(img)\ncv2.imwrite('.\/data\/output.jpg',img)\n\n   \n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u6700\u5f00\u59cb\u7684\u662f\u5168\u7a0b\u8c03\u7528opencv\u6444\u50cf\u5934\uff0c\u6e90\u7801\u8fd9\u5e94\u8be5\u8fd8\u6709\uff0c\u60f3\u8981\u7684\u8bdd\u5431\u4e00\u58f0\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6700\u8fd1\u4e5f\u662f\u4e34\u5371\u53d7\u547d\uff0c\u8981\u628a3\u6708\u505a\u7684\u4eba\u8138\u8bc6\u522b\u5d4c\u5165\u5230\u9879\u76ee\u7684\u603b\u4f53\u7cfb\u7edf\u4e2d\uff0c\u4e3a\u4e86\u6ee1\u8db3\u201c\u5ba2\u6237\u8981\u6c42\u201d\uff0c\u6211\u5bf9\u5f53\u65f6\u7684\u4ee3\u7801\u7ed3\u6784\u8fdb\u884c\u4e86\u4e00\u4e9b\u6539\u52a8\u548c\u589e\u5220\uff0c\u7528\u65f6\u4e24\u5929&#8230;\u6211\u592a\u83dc\u4e86 \u4e00\u3001\u4eba\u8138\u8bc6\u522b\u7684\u539f\u7406 dlib\u662fpython\u7684&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-412","post","type-post","status-publish","format-standard","hentry","category-2"],"_links":{"self":[{"href":"http:\/\/43.142.23.155\/index.php?rest_route=\/wp\/v2\/posts\/412","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/43.142.23.155\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/43.142.23.155\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/43.142.23.155\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/43.142.23.155\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=412"}],"version-history":[{"count":2,"href":"http:\/\/43.142.23.155\/index.php?rest_route=\/wp\/v2\/posts\/412\/revisions"}],"predecessor-version":[{"id":595,"href":"http:\/\/43.142.23.155\/index.php?rest_route=\/wp\/v2\/posts\/412\/revisions\/595"}],"wp:attachment":[{"href":"http:\/\/43.142.23.155\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=412"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/43.142.23.155\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=412"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/43.142.23.155\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=412"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}