{"id":1069962,"date":"2025-01-08T10:53:54","date_gmt":"2025-01-08T02:53:54","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1069962.html"},"modified":"2025-01-08T10:53:56","modified_gmt":"2025-01-08T02:53:56","slug":"python%e8%87%aa%e5%8a%a8%e5%8c%96%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8%e4%ba%ba%e8%84%b8-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1069962.html","title":{"rendered":"python\u81ea\u52a8\u5316\u5982\u4f55\u4f7f\u7528\u4eba\u8138"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25100828\/1783054f-51fe-48db-b16a-5fe1a1212cc0.webp\" alt=\"python\u81ea\u52a8\u5316\u5982\u4f55\u4f7f\u7528\u4eba\u8138\" \/><\/p>\n<p><p> <strong>Python\u81ea\u52a8\u5316\u5982\u4f55\u4f7f\u7528\u4eba\u8138\u8bc6\u522b\uff1a\u5b89\u88c5\u5fc5\u8981\u7684\u5e93\u3001\u52a0\u8f7d\u548c\u5904\u7406\u56fe\u50cf\u3001\u68c0\u6d4b\u4eba\u8138\u3001\u5b9e\u73b0\u4eba\u8138\u8bc6\u522b\u3002<\/strong> <\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\u5b9e\u73b0\u4eba\u8138\u8bc6\u522b\u81ea\u52a8\u5316\u4e3b\u8981\u4f9d\u8d56\u4e8e\u51e0\u4e2a\u5f3a\u5927\u7684\u5e93\uff0c\u5982OpenCV\u548cdlib\u3002\u9996\u5148\uff0c\u9700\u8981\u5b89\u88c5\u8fd9\u4e9b\u5e93\u5e76\u786e\u4fdd\u73af\u5883\u914d\u7f6e\u6b63\u786e\u3002\u63a5\u4e0b\u6765\uff0c\u52a0\u8f7d\u548c\u5904\u7406\u56fe\u50cf\u662f\u975e\u5e38\u5173\u952e\u7684\u4e00\u6b65\uff0c\u56fe\u50cf\u5904\u7406\u5305\u62ec\u5c06\u56fe\u50cf\u8f6c\u6362\u4e3a\u7070\u5ea6\u56fe\u4ee5\u4fbf\u63d0\u9ad8\u8bc6\u522b\u7684\u51c6\u786e\u6027\u3002\u4e4b\u540e\uff0c\u901a\u8fc7\u68c0\u6d4b\u4eba\u8138\u6765\u8bc6\u522b\u56fe\u50cf\u4e2d\u6240\u6709\u7684\u4eba\u8138\u4f4d\u7f6e\uff0c\u5e76\u4f7f\u7528\u9884\u8bad\u7ec3\u7684\u6a21\u578b\u8fdb\u884c\u4eba\u8138\u8bc6\u522b\u3002<strong>\u5b9e\u73b0\u4eba\u8138\u8bc6\u522b<\/strong>\u8fd9\u4e00\u70b9\u6700\u4e3a\u91cd\u8981\uff0c\u6b63\u786e\u914d\u7f6e\u548c\u8c03\u7528\u6a21\u578b\uff0c\u4f7f\u4e4b\u80fd\u591f\u51c6\u786e\u8bc6\u522b\u548c\u533a\u5206\u4e0d\u540c\u7684\u4eba\u8138\uff0c\u786e\u4fdd\u6574\u4e2a\u6d41\u7a0b\u7684\u81ea\u52a8\u5316\u548c\u9ad8\u6548\u6027\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u5b89\u88c5\u5fc5\u8981\u7684\u5e93<\/h3>\n<\/p>\n<p><p>\u5728\u5f00\u59cb\u4f7f\u7528Python\u8fdb\u884c\u4eba\u8138\u8bc6\u522b\u4e4b\u524d\uff0c\u5fc5\u987b\u5b89\u88c5\u51e0\u4e2a\u5fc5\u8981\u7684\u5e93\u3002\u4e3b\u8981\u7684\u5e93\u5305\u62ecOpenCV\u3001dlib\u548cface_recognition\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u5b89\u88c5OpenCV<\/strong><\/p>\n<p>OpenCV\u662f\u4e00\u4e2a\u5f00\u6e90\u7684\u8ba1\u7b97\u673a\u89c6\u89c9\u5e93\uff0c\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u56fe\u50cf\u5904\u7406\u529f\u80fd\u3002\u53ef\u4ee5\u4f7f\u7528pip\u547d\u4ee4\u6765\u5b89\u88c5\uff1a<\/li>\n<\/p>\n<\/ol>\n<p><pre><code class=\"language-python\">pip install opencv-python<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u5b89\u88c5dlib<\/strong><\/p>\n<p>dlib\u662f\u4e00\u4e2a\u73b0\u4ee3\u7684C++\u5de5\u5177\u5305\uff0c\u5305\u542b<a href=\"https:\/\/docs.pingcode.com\/ask\/59192.html\" target=\"_blank\">\u673a\u5668\u5b66\u4e60<\/a>\u7b97\u6cd5\u548c\u5de5\u5177\uff0c\u5c24\u5176\u5728\u4eba\u8138\u68c0\u6d4b\u65b9\u9762\u8868\u73b0\u51fa\u8272\u3002<\/li>\n<\/p>\n<\/ol>\n<p><pre><code class=\"language-python\">pip install dlib<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"3\">\n<li><strong>\u5b89\u88c5face_recognition<\/strong><\/p>\n<p>face_recognition\u5e93\u662f\u57fa\u4e8edlib\u7684\u5c01\u88c5\uff0c\u63d0\u4f9b\u4e86\u66f4\u65b9\u4fbf\u7684\u63a5\u53e3\u6765\u5b9e\u73b0\u4eba\u8138\u8bc6\u522b\u529f\u80fd\u3002<\/li>\n<\/p>\n<\/ol>\n<p><pre><code class=\"language-python\">pip install face_recognition<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u52a0\u8f7d\u548c\u5904\u7406\u56fe\u50cf<\/h3>\n<\/p>\n<p><p>\u52a0\u8f7d\u548c\u5904\u7406\u56fe\u50cf\u662f\u4eba\u8138\u8bc6\u522b\u8fc7\u7a0b\u4e2d\u81f3\u5173\u91cd\u8981\u7684\u4e00\u6b65\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u52a0\u8f7d\u56fe\u50cf<\/strong><\/p>\n<p>\u4f7f\u7528OpenCV\u5e93\uff0c\u53ef\u4ee5\u8f7b\u677e\u52a0\u8f7d\u56fe\u50cf\uff1a<\/li>\n<\/p>\n<\/ol>\n<p><pre><code class=\"language-python\">import cv2<\/p>\n<p>image = cv2.imread(&#39;path_to_image.jpg&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u8f6c\u6362\u4e3a\u7070\u5ea6\u56fe<\/strong><\/p>\n<p>\u5c06\u56fe\u50cf\u8f6c\u6362\u4e3a\u7070\u5ea6\u56fe\u53ef\u4ee5\u63d0\u9ad8\u68c0\u6d4b\u7684\u51c6\u786e\u6027\u548c\u5904\u7406\u901f\u5ea6\uff1a<\/li>\n<\/p>\n<\/ol>\n<p><pre><code class=\"language-python\">gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"3\">\n<li><strong>\u7f29\u653e\u56fe\u50cf<\/strong><\/p>\n<p>\u6709\u65f6\u7f29\u5c0f\u56fe\u50cf\u53ef\u4ee5\u52a0\u5feb\u5904\u7406\u901f\u5ea6\uff0c\u5c24\u5176\u5728\u5904\u7406\u5927\u578b\u56fe\u50cf\u65f6\uff1a<\/li>\n<\/p>\n<\/ol>\n<p><pre><code class=\"language-python\">scaled_image = cv2.resize(gray_image, (0, 0), fx=0.5, fy=0.5)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u68c0\u6d4b\u4eba\u8138<\/h3>\n<\/p>\n<p><p>\u68c0\u6d4b\u4eba\u8138\u662f\u4eba\u8138\u8bc6\u522b\u7684\u57fa\u7840\uff0c\u51c6\u786e\u68c0\u6d4b\u51fa\u56fe\u50cf\u4e2d\u7684\u4eba\u8138\u4f4d\u7f6e\u975e\u5e38\u91cd\u8981\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u4f7f\u7528Haar\u7ea7\u8054\u5206\u7c7b\u5668<\/strong><\/p>\n<p>OpenCV\u63d0\u4f9b\u4e86\u9884\u8bad\u7ec3\u7684Haar\u7ea7\u8054\u5206\u7c7b\u5668\uff0c\u53ef\u4ee5\u7528\u6765\u68c0\u6d4b\u4eba\u8138\uff1a<\/li>\n<\/p>\n<\/ol>\n<p><pre><code class=\"language-python\">face_cascade = cv2.CascadeClassifier(&#39;haarcascade_frontalface_default.xml&#39;)<\/p>\n<p>faces = face_cascade.detectMultiScale(gray_image, scaleFactor=1.1, minNeighbors=5, minSize=(30, 30))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u4f7f\u7528dlib\u8fdb\u884c\u4eba\u8138\u68c0\u6d4b<\/strong><\/p>\n<p>dlib\u63d0\u4f9b\u4e86\u66f4\u5148\u8fdb\u7684HOG\u4eba\u8138\u68c0\u6d4b\u5668\uff1a<\/li>\n<\/p>\n<\/ol>\n<p><pre><code class=\"language-python\">import dlib<\/p>\n<p>detector = dlib.get_frontal_face_detector()<\/p>\n<p>faces = detector(gray_image, 1)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u5b9e\u73b0\u4eba\u8138\u8bc6\u522b<\/h3>\n<\/p>\n<p><p>\u5728\u68c0\u6d4b\u5230\u4eba\u8138\u4e4b\u540e\uff0c\u4e0b\u4e00\u6b65\u5c31\u662f\u8bc6\u522b\u8fd9\u4e9b\u4eba\u8138\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u52a0\u8f7d\u5df2\u77e5\u4eba\u8138\u7f16\u7801<\/strong><\/p>\n<p>\u9996\u5148\uff0c\u9700\u8981\u6709\u5df2\u77e5\u4eba\u8138\u7684\u7f16\u7801\uff0c\u8fd9\u4e9b\u7f16\u7801\u53ef\u4ee5\u63d0\u524d\u751f\u6210\u5e76\u5b58\u50a8\uff1a<\/li>\n<\/p>\n<\/ol>\n<p><pre><code class=\"language-python\">import face_recognition<\/p>\n<p>known_image = face_recognition.load_image_file(&quot;known_person.jpg&quot;)<\/p>\n<p>known_encoding = face_recognition.face_encodings(known_image)[0]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u8bc6\u522b\u4eba\u8138<\/strong><\/p>\n<p>\u901a\u8fc7\u6bd4\u8f83\u68c0\u6d4b\u5230\u7684\u4eba\u8138\u7f16\u7801\u548c\u5df2\u77e5\u4eba\u8138\u7f16\u7801\uff0c\u53ef\u4ee5\u8bc6\u522b\u51fa\u56fe\u50cf\u4e2d\u7684\u4eba\u8138\uff1a<\/li>\n<\/p>\n<\/ol>\n<p><pre><code class=\"language-python\">unknown_image = face_recognition.load_image_file(&quot;unknown_person.jpg&quot;)<\/p>\n<p>unknown_encodings = face_recognition.face_encodings(unknown_image)<\/p>\n<p>for unknown_encoding in unknown_encodings:<\/p>\n<p>    results = face_recognition.compare_faces([known_encoding], unknown_encoding)<\/p>\n<p>    if results[0]:<\/p>\n<p>        print(&quot;Known person detected!&quot;)<\/p>\n<p>    else:<\/p>\n<p>        print(&quot;Unknown person!&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u81ea\u52a8\u5316\u6d41\u7a0b<\/h3>\n<\/p>\n<p><p>\u4e3a\u4e86\u5b9e\u73b0\u4eba\u8138\u8bc6\u522b\u7684\u81ea\u52a8\u5316\uff0c\u53ef\u4ee5\u5c06\u4e0a\u8ff0\u6b65\u9aa4\u6574\u5408\u5230\u4e00\u4e2a\u811a\u672c\u4e2d\uff0c\u5e76\u8bbe\u7f6e\u4e3a\u5b9a\u65f6\u4efb\u52a1\u6216\u89e6\u53d1\u4efb\u52a1\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u521b\u5efa\u811a\u672c<\/strong><\/p>\n<p>\u5c06\u6240\u6709\u6b65\u9aa4\u6574\u5408\u5230\u4e00\u4e2aPython\u811a\u672c\u4e2d\uff1a<\/li>\n<\/p>\n<\/ol>\n<p><pre><code class=\"language-python\">import cv2<\/p>\n<p>import dlib<\/p>\n<p>import face_recognition<\/p>\n<p>def process_image(image_path):<\/p>\n<p>    image = cv2.imread(image_path)<\/p>\n<p>    gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)<\/p>\n<p>    detector = dlib.get_frontal_face_detector()<\/p>\n<p>    faces = detector(gray_image, 1)<\/p>\n<p>    known_image = face_recognition.load_image_file(&quot;known_person.jpg&quot;)<\/p>\n<p>    known_encoding = face_recognition.face_encodings(known_image)[0]<\/p>\n<p>    unknown_encodings = face_recognition.face_encodings(image)<\/p>\n<p>    for unknown_encoding in unknown_encodings:<\/p>\n<p>        results = face_recognition.compare_faces([known_encoding], unknown_encoding)<\/p>\n<p>        if results[0]:<\/p>\n<p>            print(&quot;Known person detected!&quot;)<\/p>\n<p>        else:<\/p>\n<p>            print(&quot;Unknown person!&quot;)<\/p>\n<p>process_image(&#39;path_to_image.jpg&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u81ea\u52a8\u5316\u4efb\u52a1<\/strong><\/p>\n<p>\u53ef\u4ee5\u4f7f\u7528\u4efb\u52a1\u8c03\u5ea6\u5de5\u5177\uff08\u5982cron\u6216Task Scheduler\uff09\u6765\u81ea\u52a8\u8fd0\u884c\u8be5\u811a\u672c\u3002\u4f8b\u5982\uff0c\u5728Linux\u7cfb\u7edf\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528cron\u6765\u8bbe\u7f6e\u5b9a\u65f6\u4efb\u52a1\uff1a<\/li>\n<\/p>\n<\/ol>\n<p><pre><code class=\"language-bash\">crontab -e<\/p>\n<h2><strong>Add the following line to run the script every day at 6 AM<\/strong><\/h2>\n<p>0 6 * * * \/usr\/bin\/python3 \/path_to_script\/face_recognition_script.py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u8fd9\u4e9b\u6b65\u9aa4\uff0c\u53ef\u4ee5\u5b9e\u73b0Python\u81ea\u52a8\u5316\u4eba\u8138\u8bc6\u522b\uff0c\u4ece\u800c\u5728\u5404\u79cd\u5e94\u7528\u573a\u666f\u4e2d\u5e7f\u6cdb\u4f7f\u7528\uff0c\u5982\u5b89\u5168\u76d1\u63a7\u3001\u51fa\u5165\u53e3\u63a7\u5236\u7b49\u3002<strong>\u786e\u4fdd\u4eba\u8138\u8bc6\u522b\u7684\u51c6\u786e\u6027\u548c\u81ea\u52a8\u5316\u6d41\u7a0b\u7684\u7a33\u5b9a\u6027<\/strong>\u662f\u6574\u4e2a\u8fc7\u7a0b\u7684\u6838\u5fc3\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u4eba\u8138\u8bc6\u522b\u5728Python\u81ea\u52a8\u5316\u4e2d\u80fd\u5b9e\u73b0\u54ea\u4e9b\u529f\u80fd\uff1f<\/strong><br \/>Python\u4e2d\u7684\u4eba\u8138\u8bc6\u522b\u53ef\u4ee5\u7528\u4e8e\u591a\u79cd\u81ea\u52a8\u5316\u4efb\u52a1\uff0c\u5982\u5b89\u5168\u76d1\u63a7\u3001\u8bbf\u5ba2\u7ba1\u7406\u3001\u81ea\u52a8\u5316\u767b\u5f55\u7cfb\u7edf\u3001\u60c5\u611f\u5206\u6790\u7b49\u3002\u901a\u8fc7\u4f7f\u7528\u5e93\u5982OpenCV\u548cFace Recognition\uff0c\u5f00\u53d1\u8005\u53ef\u4ee5\u8f7b\u677e\u5b9e\u73b0\u4eba\u8138\u68c0\u6d4b\u3001\u8bc6\u522b\u548c\u8ddf\u8e2a\u7b49\u529f\u80fd\uff0c\u4ece\u800c\u63d0\u9ad8\u5de5\u4f5c\u6548\u7387\u548c\u5b89\u5168\u6027\u3002<\/p>\n<p><strong>\u4f7f\u7528Python\u8fdb\u884c\u4eba\u8138\u8bc6\u522b\u9700\u8981\u54ea\u4e9b\u5e93\u548c\u5de5\u5177\uff1f<\/strong><br \/>\u5728\u8fdb\u884c\u4eba\u8138\u8bc6\u522b\u7684\u81ea\u52a8\u5316\u9879\u76ee\u65f6\uff0c\u5e38\u7528\u7684\u5e93\u5305\u62ecOpenCV\u3001dlib\u548cFace Recognition\u7b49\u3002\u8fd9\u4e9b\u5e93\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u56fe\u50cf\u5904\u7406\u548c\u673a\u5668\u5b66\u4e60\u529f\u80fd\uff0c\u80fd\u591f\u5e2e\u52a9\u5f00\u53d1\u8005\u5feb\u901f\u6784\u5efa\u4eba\u8138\u8bc6\u522b\u7cfb\u7edf\u3002\u6b64\u5916\uff0c\u8fd8\u53ef\u4ee5\u4f7f\u7528Jupyter Notebook\u8fdb\u884c\u53ef\u89c6\u5316\u5f00\u53d1\uff0c\u65b9\u4fbf\u8c03\u8bd5\u548c\u5c55\u793a\u7ed3\u679c\u3002<\/p>\n<p><strong>\u5982\u4f55\u63d0\u9ad8\u4eba\u8138\u8bc6\u522b\u7684\u51c6\u786e\u6027\uff1f<\/strong><br \/>\u63d0\u9ad8\u4eba\u8138\u8bc6\u522b\u51c6\u786e\u6027\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528\u9ad8\u8d28\u91cf\u7684\u56fe\u50cf\u3001\u589e\u52a0\u8bad\u7ec3\u6570\u636e\u96c6\u7684\u591a\u6837\u6027\u3001\u9009\u62e9\u5408\u9002\u7684\u6a21\u578b\u67b6\u6784\u4ee5\u53ca\u8fdb\u884c\u6a21\u578b\u7684\u53c2\u6570\u8c03\u6574\u548c\u4f18\u5316\u3002\u6b64\u5916\uff0c\u786e\u4fdd\u5728\u4e0d\u540c\u5149\u7167\u3001\u89d2\u5ea6\u548c\u8868\u60c5\u4e0b\u8fdb\u884c\u6d4b\u8bd5\uff0c\u53ef\u4ee5\u5e2e\u52a9\u63d0\u5347\u7cfb\u7edf\u7684\u9c81\u68d2\u6027\u548c\u51c6\u786e\u6027\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u81ea\u52a8\u5316\u5982\u4f55\u4f7f\u7528\u4eba\u8138\u8bc6\u522b\uff1a\u5b89\u88c5\u5fc5\u8981\u7684\u5e93\u3001\u52a0\u8f7d\u548c\u5904\u7406\u56fe\u50cf\u3001\u68c0\u6d4b\u4eba\u8138\u3001\u5b9e\u73b0\u4eba\u8138\u8bc6\u522b\u3002 \u5728Python\u4e2d\u5b9e [&hellip;]","protected":false},"author":3,"featured_media":1069968,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1069962"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=1069962"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1069962\/revisions"}],"predecessor-version":[{"id":1069969,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1069962\/revisions\/1069969"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1069968"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1069962"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1069962"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1069962"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}