{"id":302,"date":"2026-05-29T23:05:38","date_gmt":"2026-05-29T15:05:38","guid":{"rendered":"https:\/\/www.lixiaodong.com\/?p=302"},"modified":"2026-05-29T23:05:38","modified_gmt":"2026-05-29T15:05:38","slug":"%e4%bd%bf%e7%94%a8opencv%e8%bf%9b%e8%a1%8c%e4%ba%ba%e8%84%b8%e6%a3%80%e6%b5%8b%e5%92%8c%e4%ba%ba%e8%84%b8%e5%85%b3%e9%94%ae%e7%82%b9%e6%a3%80%e6%b5%8b","status":"publish","type":"post","link":"https:\/\/www.lixiaodong.com\/?p=302","title":{"rendered":"\u4f7f\u7528OpenCV\u8fdb\u884c\u4eba\u8138\u68c0\u6d4b\u548c\u4eba\u8138\u5173\u952e\u70b9\u68c0\u6d4b"},"content":{"rendered":"\n<p>\u4f7f\u7528opencv haar cascades\u7ea7\u8054\u68c0\u6d4b\u5668\u8bc6\u522b\u4eba\u8138\uff0c\u4f7f\u7528opencv facemark LBF\u8bc6\u522b\u4eba\u8138\u5173\u952e\u70b9\u3002<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>\u5728\u672c\u5730\u7535\u8111\u65b0\u5efa\u4e00\u4e2a\u76ee\u5f55(\u5982face_recognition_demo)\uff0c\u7528\u6765\u5b58\u653e\u65b0\u7684python\u9879\u76ee\u3002\u521b\u5efa\u5b8c\u6210\u540e\u8fdb\u5165\u8be5\u76ee\u5f55\u3002<br>mkdir face_recognition_demo<br>cd face_recognition_demo<\/li>\n\n\n\n<li>\u4e3a\u65b0\u7684python\u9879\u76ee\u5efa\u7acb\u4e00\u4e2apython\u865a\u62df\u73af\u5883\uff08\u5728.venv\u5b50\u76ee\u5f55\u4e0b\uff09\uff0c\u5e76\u6fc0\u6d3b\u3002<br>python3 -m venv .venv<br>source .venv\/bin\/activate<br>\u6fc0\u6d3b\u865a\u62df\u73af\u5883\u540e\uff0c.venv\/bin\u8fd9\u4e2a\u76ee\u5f55\u4f1a\u52a0\u5165PATH\u73af\u5883\u53d8\u91cf\u7684\u5934\u90e8\uff0c\u8fd9\u4e2a\u76ee\u5f55\u4e0b\u5305\u542bpython, pip\u7b49\u547d\u4ee4\u3002\u6240\u4ee5\u540e\u9762\u6267\u884cpython\u6216pip\u547d\u4ee4\u65f6\uff0c\u5c31\u4f1a\u6267\u884c\u865a\u62df\u73af\u5883\u4e0bbin\u76ee\u5f55\u91cc\u5bf9\u5e94\u7684\u547d\u4ee4\u3002<br>\u5982\u679c\u60f3\u5173\u95ed\u865a\u62df\u73af\u5883\uff0c\u5728\u9879\u76ee\u6839\u76ee\u5f55\u6267\u884c\uff1adeactivate\u3002<\/li>\n\n\n\n<li>\u5b89\u88c5\u4f9d\u8d56\u5305\uff0c\u51c6\u5907facemark LBF\u6a21\u578b\u6587\u4ef6\uff0c\u51c6\u5907\u4eba\u8138\u7167\u7247\u3002<br>pip install opencv-contrib-python<br>\u6ce8\u610f\uff1aopencv-contrib-python\u5df2\u7ecf\u5305\u542b\u4e86opencv\u6838\u5fc3\uff0c\u6240\u4ee5\u4e0d\u9700\u8981\u518d\u5b89\u88c5opencv-python\u5305\u30022\u4e2a\u90fd\u5b89\u88c5\u4f1a\u9020\u6210\u91cd\u590d\u3002<br>\u65b0\u5efamodel\u5b50\u76ee\u5f55\uff0c\u5c06lbfmodel.yaml\u653e\u5165\u5176\u4e2d\u3002<br>\u5c06\u4eba\u8138\u7167\u7247\uff08\u8fd9\u91cc\u547d\u540d\u4e3aface9.jpeg\uff09\uff0c\u653e\u5230\u9879\u76ee\u6839\u76ee\u5f55\u3002<\/li>\n\n\n\n<li>\u542f\u52a8VScode\uff0c\u6253\u5f00\u4e0a\u9762\u7684python\u9879\u76ee\u7684\u6839\u76ee\u5f55\u3002\u65b0\u5efa1\u4e2apython\u6587\u4ef6\uff08\u8fd9\u91cc\u6587\u4ef6\u540d\u4e3ademo.py\uff09\u3002<br>\u4ee5\u4e0b\u5217\u51fa\u4ee3\u7801\u3002\u987a\u4fbf\u8bf4\u4e00\u4e0bobsidian\u91cc\u5173\u4e8e\u4ee3\u7801\u5757\u7684\u6ce8\u610f\u4e8b\u9879\uff1a<br>\uff081\uff09obsidian\u91cc\uff0c\u4ee3\u7801\u5757\u75283\u4e2a\u53cd\u5f15\u53f7\u5f00\u59cb\uff0c\u540e\u9762\u7d27\u8ddf\u7f16\u7a0b\u8bed\u8a00\u7684\u540d\u5b57\uff08\u5982python\uff09\uff0c\u75283\u4e2a\u53cd\u5f15\u53f7\u7ed3\u675f\u3002<br>\uff082\uff09\u7c98\u8d34\u4ee3\u7801\u5230\u4ee3\u7801\u5757\u65f6\uff0c\u5982\u679c\u60f3\u4fdd\u7559\u539f\u5148\u4ee3\u7801\u91cc\u7684\u7f29\u8fdb\uff0c\u7c98\u8d34\u65f6\u6309\u4f4f Command + Shift + V (MacOS\u7cfb\u7edf) \u6216 Control + Shift + V (windows\u7cfb\u7edf)\u3002<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>from pathlib import Path\n\nimport cv2\n\n\ndef resolve_haarcascade(base_dir: Path) -> str:\n    candidates = &#91;]\n\n    if hasattr(cv2, \"data\") and hasattr(cv2.data, \"haarcascades\"):\n        candidates.append(Path(cv2.data.haarcascades) \/ \"haarcascade_frontalface_alt2.xml\")\n\n    cv2_root = Path(cv2.__file__).resolve().parent\n    print(f\"cv2_root:{cv2_root}\")\n    \n    candidates.extend(\n        &#91;\n            base_dir \/ \"haarcascade_frontalface_alt2.xml\",\n            Path(\"\/opt\/homebrew\/Cellar\/opencv\/4.13.0_10\/share\/opencv4\/haarcascades\/haarcascade_frontalface_alt2.xml\"),\n            cv2_root \/ \"data\" \/ \"haarcascade_frontalface_alt2.xml\",\n            cv2_root.parent.parent.parent \/ \"share\" \/ \"opencv4\" \/ \"haarcascades\" \/ \"haarcascade_frontalface_alt2.xml\",\n        ]\n    )\n\n    for candidate in candidates:\n        if candidate.exists():\n            return str(candidate)\n\n    raise FileNotFoundError(\n        \"Cannot find haarcascade_frontalface_alt2.xml. \"\n        \"Download it from https:\/\/raw.githubusercontent.com\/opencv\/opencv\/4.x\/data\/haarcascades\/haarcascade_frontalface_alt2.xml \"\n        \"and place it next to demo.py.\"\n    )\n\n\ndef main() -> None:\n    base_dir = Path(__file__).resolve().parent\n    image_path = base_dir \/ \"face9.jpeg\"\n    model_path = base_dir \/ \"model\" \/ \"lbfmodel.yaml\"\n    output_path = base_dir \/ \"face9_landmarks.jpg\"\n\n    image = cv2.imread(str(image_path))\n    if image is None:\n        raise FileNotFoundError(f\"Cannot read image: {image_path}\")\n\n    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)\n\n    cascade_path = resolve_haarcascade(base_dir)\n    detector = cv2.CascadeClassifier(cascade_path)\n    if detector.empty():\n        raise RuntimeError(f\"Failed to load Haar cascade: {cascade_path}\")\n\n    faces = detector.detectMultiScale(\n        gray,\n        scaleFactor=1.1,\n        minNeighbors=5,\n        minSize=(80, 80),\n    )\n    if len(faces) == 0:\n        raise RuntimeError(\"No face detected in face4.jpeg\")\n\n    facemark = cv2.face.createFacemarkLBF()\n    facemark.loadModel(str(model_path))\n\n    ok, landmarks = facemark.fit(gray, faces)\n    if not ok:\n        raise RuntimeError(\"Facemark fitting failed\")\n\n    for (x, y, w, h), face_landmarks in zip(faces, landmarks):\n        cv2.rectangle(image, (x, y), (x + w, y + h), (0, 255, 0), 2)\n        # for px, py in face_landmarks&#91;0]:\n        #     cv2.circle(image, (int(px), int(py)), 2, (0, 0, 255), -1)\n        for i, (px, py) in enumerate(face_landmarks&#91;0]):\n            x, y = int(px), int(py)\n            cv2.circle(image, (x, y), 2, (0, 0, 255), -1)\n            cv2.putText(image, str(i), (x + 2, y - 2),\n                cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 0, 0), 1)\n\n    cv2.imwrite(str(output_path), image)\n\n    print(f\"Detected {len(faces)} face(s)\")\n    print(f\"Saved result to: {output_path}\")\n\n\nif __name__ == \"__main__\":\n    main()\n\n<\/code><\/pre>\n\n\n\n<ol start=\"5\" class=\"wp-block-list\">\n<li>\u8fd0\u884c\u7a0b\u5e8f\u3002<br>\u5728\u865a\u62df\u73af\u5883\u5df2\u7ecf\u6fc0\u6d3b\u7684\u60c5\u51b5\u4e0b\uff08\u5982\u672a\u6fc0\u6d3b\uff0c\u5219\u9700\u8981\u6267\u884c\u4e0a\u9762\u63d0\u53ca\u7684source\u547d\u4ee4\uff09\uff0c\u5728\u7ec8\u7aef\u6216\u547d\u4ee4\u884c\u4e2d\uff0c\u5728\u9879\u76ee\u7684\u6839\u76ee\u5f55\u4e0b\uff0c\u6267\u884c\uff1apython demo.py<br>\u76ee\u5f55\u4e0b\u4f1a\u751f\u6210\u4e00\u5f20\u6807\u8bb0\u4eba\u8138\u53ca\u4eba\u8138\u5173\u952e\u70b9\u7684\u56fe\u7247\u3002\u4eba\u8138\u7528\u7eff\u8272\u65b9\u6846\u6807\u51fa\uff0c\u4eba\u8138\u5173\u952e\u70b9\u7528\u7ea2\u8272\u5706\u70b9\u6807\u51fa\uff0c\u6bcf\u4e2a\u5173\u952e\u70b9\u4f1a\u6807\u51fa\u5e8f\u53f7\uff0c\u4ece0\uff5e67\u3002\u56e0\u4e3a\u6211\u4eec\u4f7f\u7528\u7684\u662f\u4eba\u813868\u70b9\u6807\u8bb0\u3002<\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>\u4f7f\u7528opencv haar cascades\u7ea7\u8054\u68c0\u6d4b\u5668\u8bc6\u522b\u4eba\u8138\uff0c\u4f7f\u7528opencv &hellip; <a href=\"https:\/\/www.lixiaodong.com\/?p=302\">\u7ee7\u7eed\u9605\u8bfb <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[58],"tags":[],"class_list":["post-302","post","type-post","status-publish","format-standard","hentry","category-opencv"],"_links":{"self":[{"href":"https:\/\/www.lixiaodong.com\/index.php?rest_route=\/wp\/v2\/posts\/302","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.lixiaodong.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.lixiaodong.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.lixiaodong.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.lixiaodong.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=302"}],"version-history":[{"count":1,"href":"https:\/\/www.lixiaodong.com\/index.php?rest_route=\/wp\/v2\/posts\/302\/revisions"}],"predecessor-version":[{"id":303,"href":"https:\/\/www.lixiaodong.com\/index.php?rest_route=\/wp\/v2\/posts\/302\/revisions\/303"}],"wp:attachment":[{"href":"https:\/\/www.lixiaodong.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=302"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.lixiaodong.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=302"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.lixiaodong.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=302"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}