{"id":1173795,"date":"2025-01-15T17:08:08","date_gmt":"2025-01-15T09:08:08","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1173795.html"},"modified":"2025-01-15T17:08:12","modified_gmt":"2025-01-15T09:08:12","slug":"python%e5%a6%82%e4%bd%95%e6%a3%80%e6%b5%8b%e6%91%84%e5%83%8f%e5%a4%b4","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1173795.html","title":{"rendered":"python\u5982\u4f55\u68c0\u6d4b\u6444\u50cf\u5934"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/26075455\/8de41397-9647-4483-8130-f249cd4f5f2f.webp\" alt=\"python\u5982\u4f55\u68c0\u6d4b\u6444\u50cf\u5934\" \/><\/p>\n<p><p> Python\u68c0\u6d4b\u6444\u50cf\u5934\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u51e0\u4e2a\u6b65\u9aa4\u6765\u5b9e\u73b0\uff1a<strong>\u4f7f\u7528OpenCV\u5e93\u3001\u4f7f\u7528\u6444\u50cf\u5934\u7d22\u5f15\u3001\u6355\u83b7\u5e27\u5e76\u663e\u793a\u56fe\u50cf<\/strong>\u3002\u5176\u4e2d\uff0c<strong>\u4f7f\u7528OpenCV\u5e93<\/strong>\u662f\u6700\u4e3a\u5173\u952e\u7684\u4e00\u6b65\uff0c\u56e0\u4e3aOpenCV\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u8ba1\u7b97\u673a\u89c6\u89c9\u5e93\uff0c\u80fd\u591f\u8f7b\u677e\u5730\u4e0e\u6444\u50cf\u5934\u8fdb\u884c\u4ea4\u4e92\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u8be6\u7ec6\u63cf\u8ff0\u4e00\u4e0b\u5982\u4f55\u4f7f\u7528OpenCV\u5e93\u6765\u68c0\u6d4b\u6444\u50cf\u5934\u3002<\/p>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u5b89\u88c5OpenCV\u5e93\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528pip\u547d\u4ee4\u6765\u5b89\u88c5OpenCV\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install opencv-python<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7136\u540e\uff0c\u6211\u4eec\u901a\u8fc7\u4e0b\u9762\u7684\u4ee3\u7801\u6765\u68c0\u6d4b\u6444\u50cf\u5934\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import cv2<\/p>\n<p>def detect_camera():<\/p>\n<p>    # \u6253\u5f00\u7b2c\u4e00\u4e2a\u6444\u50cf\u5934<\/p>\n<p>    cap = cv2.VideoCapture(0)<\/p>\n<p>    if not cap.isOpened():<\/p>\n<p>        print(&quot;\u65e0\u6cd5\u6253\u5f00\u6444\u50cf\u5934&quot;)<\/p>\n<p>        return False<\/p>\n<p>    while True:<\/p>\n<p>        ret, frame = cap.read()<\/p>\n<p>        if not ret:<\/p>\n<p>            print(&quot;\u65e0\u6cd5\u63a5\u6536\u5e27&quot;)<\/p>\n<p>            break<\/p>\n<p>        # \u663e\u793a\u5e27<\/p>\n<p>        cv2.imshow(&#39;\u6444\u50cf\u5934\u68c0\u6d4b&#39;, frame)<\/p>\n<p>        # \u6309\u4e0bq\u952e\u9000\u51fa<\/p>\n<p>        if cv2.w<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>tKey(1) == ord(&#39;q&#39;):<\/p>\n<p>            break<\/p>\n<p>    # \u91ca\u653e\u6444\u50cf\u5934<\/p>\n<p>    cap.release()<\/p>\n<p>    cv2.destroyAllWindows()<\/p>\n<p>    return True<\/p>\n<h2><strong>\u68c0\u6d4b\u6444\u50cf\u5934<\/strong><\/h2>\n<p>detect_camera()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e86OpenCV\u7684<code>cv2.VideoCapture(0)<\/code>\u6765\u6253\u5f00\u9ed8\u8ba4\u6444\u50cf\u5934\uff0c\u4f7f\u7528<code>cap.isOpened()<\/code>\u6765\u68c0\u67e5\u6444\u50cf\u5934\u662f\u5426\u6210\u529f\u6253\u5f00\uff0c\u5e76\u901a\u8fc7<code>cap.read()<\/code>\u6765\u6355\u83b7\u5e27\u3002\u6355\u83b7\u7684\u5e27\u4f7f\u7528<code>cv2.imshow()<\/code>\u6765\u663e\u793a\uff0c\u6309\u4e0b&#39;q&#39;\u952e\u53ef\u4ee5\u9000\u51fa\u5faa\u73af\uff0c\u6700\u540e\u91ca\u653e\u6444\u50cf\u5934\u8d44\u6e90\u5e76\u5173\u95ed\u6240\u6709\u7a97\u53e3\u3002<\/p>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecdPython\u68c0\u6d4b\u6444\u50cf\u5934\u7684\u5404\u4e2a\u65b9\u9762\uff0c\u5305\u62ec\u5982\u4f55\u9009\u62e9\u4e0d\u540c\u7684\u6444\u50cf\u5934\u3001\u5904\u7406\u6355\u83b7\u7684\u56fe\u50cf\u3001\u4ee5\u53ca\u5e38\u89c1\u7684\u6444\u50cf\u5934\u64cd\u4f5c\u548c\u4f18\u5316\u6280\u5de7\u3002<\/p>\n<\/p>\n<h2><strong>\u4e00\u3001\u5b89\u88c5\u548c\u914d\u7f6eOpenCV\u5e93<\/strong><\/h2>\n<p><h2>1\u3001\u5b89\u88c5OpenCV\u5e93<\/h2>\n<\/p>\n<p><p>\u5728\u5f00\u59cb\u4f7f\u7528\u6444\u50cf\u5934\u4e4b\u524d\uff0c\u6211\u4eec\u9700\u8981\u5b89\u88c5OpenCV\u5e93\u3002OpenCV\u5e93\u662f\u4e00\u4e2a\u5f00\u6e90\u8ba1\u7b97\u673a\u89c6\u89c9\u548c<a href=\"https:\/\/docs.pingcode.com\/ask\/59192.html\" target=\"_blank\">\u673a\u5668\u5b66\u4e60<\/a>\u8f6f\u4ef6\u5e93\uff0c\u5305\u542b\u4e86\u5927\u91cf\u7684\u51fd\u6570\u548c\u5de5\u5177\uff0c\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u8f7b\u677e\u5730\u5904\u7406\u56fe\u50cf\u548c\u89c6\u9891\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install opencv-python<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>2\u3001\u9a8c\u8bc1\u5b89\u88c5<\/h2>\n<\/p>\n<p><p>\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u5bfc\u5165OpenCV\u5e93\u5e76\u6253\u5370\u7248\u672c\u53f7\u6765\u9a8c\u8bc1\u5b89\u88c5\u662f\u5426\u6210\u529f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import cv2<\/p>\n<p>print(cv2.__version__)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5982\u679c\u80fd\u591f\u6210\u529f\u6253\u5370\u51fa\u7248\u672c\u53f7\uff0c\u8bf4\u660eOpenCV\u5e93\u5df2\u7ecf\u6b63\u786e\u5b89\u88c5\u3002<\/p>\n<\/p>\n<h2><strong>\u4e8c\u3001\u6253\u5f00\u548c\u68c0\u6d4b\u6444\u50cf\u5934<\/strong><\/h2>\n<p><h2>1\u3001\u6253\u5f00\u6444\u50cf\u5934<\/h2>\n<\/p>\n<p><p>\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528OpenCV\u7684<code>cv2.VideoCapture()<\/code>\u51fd\u6570\u6765\u6253\u5f00\u6444\u50cf\u5934\u3002\u53c2\u6570<code>0<\/code>\u8868\u793a\u6253\u5f00\u9ed8\u8ba4\u6444\u50cf\u5934\uff0c\u5982\u679c\u6709\u591a\u4e2a\u6444\u50cf\u5934\uff0c\u53ef\u4ee5\u4f20\u5165\u5176\u4ed6\u7d22\u5f15\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">cap = cv2.VideoCapture(0)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>2\u3001\u68c0\u6d4b\u6444\u50cf\u5934\u662f\u5426\u6210\u529f\u6253\u5f00<\/h2>\n<\/p>\n<p><p>\u4f7f\u7528<code>cap.isOpened()<\/code>\u51fd\u6570\u53ef\u4ee5\u68c0\u67e5\u6444\u50cf\u5934\u662f\u5426\u6210\u529f\u6253\u5f00\u3002\u5982\u679c\u8fd4\u56de<code>False<\/code>\uff0c\u8bf4\u660e\u6444\u50cf\u5934\u65e0\u6cd5\u6253\u5f00\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">if not cap.isOpened():<\/p>\n<p>    print(&quot;\u65e0\u6cd5\u6253\u5f00\u6444\u50cf\u5934&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>3\u3001\u6355\u83b7\u5e27\u5e76\u663e\u793a\u56fe\u50cf<\/h2>\n<\/p>\n<p><p>\u4f7f\u7528<code>cap.read()<\/code>\u51fd\u6570\u53ef\u4ee5\u6355\u83b7\u5e27\uff0c\u5e76\u4f7f\u7528<code>cv2.imshow()<\/code>\u51fd\u6570\u663e\u793a\u56fe\u50cf\u3002\u6309\u4e0b&#39;q&#39;\u952e\u53ef\u4ee5\u9000\u51fa\u5faa\u73af\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">while True:<\/p>\n<p>    ret, frame = cap.read()<\/p>\n<p>    if not ret:<\/p>\n<p>        print(&quot;\u65e0\u6cd5\u63a5\u6536\u5e27&quot;)<\/p>\n<p>        break<\/p>\n<p>    cv2.imshow(&#39;\u6444\u50cf\u5934\u68c0\u6d4b&#39;, frame)<\/p>\n<p>    if cv2.waitKey(1) == ord(&#39;q&#39;):<\/p>\n<p>        break<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>4\u3001\u91ca\u653e\u6444\u50cf\u5934\u8d44\u6e90<\/h2>\n<\/p>\n<p><p>\u5728\u7a0b\u5e8f\u7ed3\u675f\u65f6\uff0c\u6211\u4eec\u9700\u8981\u91ca\u653e\u6444\u50cf\u5934\u8d44\u6e90\u5e76\u5173\u95ed\u6240\u6709\u7a97\u53e3\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">cap.release()<\/p>\n<p>cv2.destroyAllWindows()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<h2><strong>\u4e09\u3001\u9009\u62e9\u4e0d\u540c\u7684\u6444\u50cf\u5934<\/strong><\/h2>\n<p><p>\u5982\u679c\u8ba1\u7b97\u673a\u4e0a\u6709\u591a\u4e2a\u6444\u50cf\u5934\uff0c\u53ef\u4ee5\u901a\u8fc7\u4f20\u5165\u4e0d\u540c\u7684\u7d22\u5f15\u6765\u9009\u62e9\u4e0d\u540c\u7684\u6444\u50cf\u5934\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>1<\/code>\u53ef\u4ee5\u9009\u62e9\u7b2c\u4e8c\u4e2a\u6444\u50cf\u5934\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">cap = cv2.VideoCapture(1)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u53ef\u4ee5\u901a\u8fc7\u904d\u5386\u7d22\u5f15\u6765\u67e5\u627e\u6240\u6709\u53ef\u7528\u7684\u6444\u50cf\u5934\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">for i in range(10):<\/p>\n<p>    cap = cv2.VideoCapture(i)<\/p>\n<p>    if cap.isOpened():<\/p>\n<p>        print(f&quot;\u6444\u50cf\u5934 {i} \u53ef\u7528&quot;)<\/p>\n<p>        cap.release()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<h2><strong>\u56db\u3001\u5904\u7406\u6355\u83b7\u7684\u56fe\u50cf<\/strong><\/h2>\n<p><h2>1\u3001\u7070\u5ea6\u5904\u7406<\/h2>\n<\/p>\n<p><p>\u5c06\u6355\u83b7\u7684\u56fe\u50cf\u8f6c\u6362\u4e3a\u7070\u5ea6\u56fe\u50cf\u53ef\u4ee5\u51cf\u5c11\u8ba1\u7b97\u91cf\uff0c\u5e76\u4e14\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\u53ef\u4ee5\u63d0\u9ad8\u5904\u7406\u6548\u7387\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)<\/p>\n<p>cv2.imshow(&#39;\u7070\u5ea6\u56fe\u50cf&#39;, gray)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>2\u3001\u8fb9\u7f18\u68c0\u6d4b<\/h2>\n<\/p>\n<p><p>\u4f7f\u7528Canny\u8fb9\u7f18\u68c0\u6d4b\u7b97\u6cd5\u53ef\u4ee5\u63d0\u53d6\u56fe\u50cf\u4e2d\u7684\u8fb9\u7f18\u4fe1\u606f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">edges = cv2.Canny(frame, 100, 200)<\/p>\n<p>cv2.imshow(&#39;\u8fb9\u7f18\u68c0\u6d4b&#39;, edges)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>3\u3001\u4eba\u8138\u68c0\u6d4b<\/h2>\n<\/p>\n<p><p>\u4f7f\u7528OpenCV\u7684\u9884\u8bad\u7ec3\u5206\u7c7b\u5668\u53ef\u4ee5\u8fdb\u884c\u4eba\u8138\u68c0\u6d4b\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + &#39;haarcascade_frontalface_default.xml&#39;)<\/p>\n<p>gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)<\/p>\n<p>faces = face_cascade.detectMultiScale(gray, 1.1, 4)<\/p>\n<p>for (x, y, w, h) in faces:<\/p>\n<p>    cv2.rectangle(frame, (x, y), (x+w, y+h), (255, 0, 0), 2)<\/p>\n<p>cv2.imshow(&#39;\u4eba\u8138\u68c0\u6d4b&#39;, frame)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<h2><strong>\u4e94\u3001\u5e38\u89c1\u7684\u6444\u50cf\u5934\u64cd\u4f5c<\/strong><\/h2>\n<p><h2>1\u3001\u8c03\u6574\u5206\u8fa8\u7387<\/h2>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528<code>cap.set()<\/code>\u51fd\u6570\u6765\u8c03\u6574\u6444\u50cf\u5934\u7684\u5206\u8fa8\u7387\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">cap.set(cv2.CAP_PROP_FRAME_WIDTH, 640)<\/p>\n<p>cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 480)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>2\u3001\u83b7\u53d6\u5e27\u7387<\/h2>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528<code>cap.get()<\/code>\u51fd\u6570\u6765\u83b7\u53d6\u6444\u50cf\u5934\u7684\u5e27\u7387\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">fps = cap.get(cv2.CAP_PROP_FPS)<\/p>\n<p>print(f&quot;\u5e27\u7387: {fps}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>3\u3001\u5f55\u5236\u89c6\u9891<\/h2>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528<code>cv2.VideoWriter()<\/code>\u51fd\u6570\u6765\u5f55\u5236\u89c6\u9891\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">fourcc = cv2.VideoWriter_fourcc(*&#39;XVID&#39;)<\/p>\n<p>out = cv2.VideoWriter(&#39;output.avi&#39;, fourcc, 20.0, (640, 480))<\/p>\n<p>while True:<\/p>\n<p>    ret, frame = cap.read()<\/p>\n<p>    if not ret:<\/p>\n<p>        break<\/p>\n<p>    out.write(frame)<\/p>\n<p>    cv2.imshow(&#39;\u5f55\u5236\u89c6\u9891&#39;, frame)<\/p>\n<p>    if cv2.waitKey(1) == ord(&#39;q&#39;):<\/p>\n<p>        break<\/p>\n<p>cap.release()<\/p>\n<p>out.release()<\/p>\n<p>cv2.destroyAllWindows()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<h2><strong>\u516d\u3001\u4f18\u5316\u6444\u50cf\u5934\u68c0\u6d4b\u6027\u80fd<\/strong><\/h2>\n<p><h2>1\u3001\u51cf\u5c11\u5e27\u7387<\/h2>\n<\/p>\n<p><p>\u51cf\u5c11\u5e27\u7387\u53ef\u4ee5\u964d\u4f4e\u5904\u7406\u8d1f\u62c5\uff0c\u4ece\u800c\u63d0\u9ad8\u6027\u80fd\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">cap.set(cv2.CAP_PROP_FPS, 15)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>2\u3001\u964d\u4f4e\u5206\u8fa8\u7387<\/h2>\n<\/p>\n<p><p>\u964d\u4f4e\u5206\u8fa8\u7387\u53ef\u4ee5\u51cf\u5c11\u56fe\u50cf\u6570\u636e\u91cf\uff0c\u4ece\u800c\u63d0\u9ad8\u5904\u7406\u901f\u5ea6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">cap.set(cv2.CAP_PROP_FRAME_WIDTH, 320)<\/p>\n<p>cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 240)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>3\u3001\u4f7f\u7528\u591a\u7ebf\u7a0b<\/h2>\n<\/p>\n<p><p>\u4f7f\u7528\u591a\u7ebf\u7a0b\u53ef\u4ee5\u63d0\u9ad8\u6444\u50cf\u5934\u68c0\u6d4b\u7684\u6548\u7387\u3002\u53ef\u4ee5\u4f7f\u7528Python\u7684<code>threading<\/code>\u6a21\u5757\u6765\u5b9e\u73b0\u591a\u7ebf\u7a0b\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import threading<\/p>\n<p>def capture_frames():<\/p>\n<p>    while True:<\/p>\n<p>        ret, frame = cap.read()<\/p>\n<p>        if not ret:<\/p>\n<p>            break<\/p>\n<p>        cv2.imshow(&#39;\u591a\u7ebf\u7a0b\u6444\u50cf\u5934\u68c0\u6d4b&#39;, frame)<\/p>\n<p>        if cv2.waitKey(1) == ord(&#39;q&#39;):<\/p>\n<p>            break<\/p>\n<p>cap = cv2.VideoCapture(0)<\/p>\n<p>t = threading.Thread(target=capture_frames)<\/p>\n<p>t.start()<\/p>\n<p>t.join()<\/p>\n<p>cap.release()<\/p>\n<p>cv2.destroyAllWindows()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<h2><strong>\u4e03\u3001\u5904\u7406\u591a\u6444\u50cf\u5934\u8f93\u5165<\/strong><\/h2>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u53ef\u80fd\u9700\u8981\u540c\u65f6\u5904\u7406\u591a\u4e2a\u6444\u50cf\u5934\u7684\u8f93\u5165\u3002\u53ef\u4ee5\u4f7f\u7528\u591a\u7ebf\u7a0b\u6765\u5b9e\u73b0\u8fd9\u4e00\u70b9\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import threading<\/p>\n<p>def capture_from_camera(index):<\/p>\n<p>    cap = cv2.VideoCapture(index)<\/p>\n<p>    while True:<\/p>\n<p>        ret, frame = cap.read()<\/p>\n<p>        if not ret:<\/p>\n<p>            break<\/p>\n<p>        cv2.imshow(f&#39;\u6444\u50cf\u5934 {index}&#39;, frame)<\/p>\n<p>        if cv2.waitKey(1) == ord(&#39;q&#39;):<\/p>\n<p>            break<\/p>\n<p>    cap.release()<\/p>\n<p>threads = []<\/p>\n<p>for i in range(2):<\/p>\n<p>    t = threading.Thread(target=capture_from_camera, args=(i,))<\/p>\n<p>    threads.append(t)<\/p>\n<p>    t.start()<\/p>\n<p>for t in threads:<\/p>\n<p>    t.join()<\/p>\n<p>cv2.destroyAllWindows()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<h2><strong>\u516b\u3001\u6444\u50cf\u5934\u68c0\u6d4b\u4e2d\u7684\u5e38\u89c1\u95ee\u9898<\/strong><\/h2>\n<p><h2>1\u3001\u6444\u50cf\u5934\u65e0\u6cd5\u6253\u5f00<\/h2>\n<\/p>\n<p><p>\u5982\u679c\u6444\u50cf\u5934\u65e0\u6cd5\u6253\u5f00\uff0c\u53ef\u80fd\u662f\u7531\u4e8e\u9a71\u52a8\u7a0b\u5e8f\u95ee\u9898\u3001\u6444\u50cf\u5934\u88ab\u5176\u4ed6\u7a0b\u5e8f\u5360\u7528\u7b49\u539f\u56e0\u3002\u53ef\u4ee5\u5c1d\u8bd5\u91cd\u542f\u8ba1\u7b97\u673a\u3001\u66f4\u65b0\u9a71\u52a8\u7a0b\u5e8f\u6216\u5173\u95ed\u5176\u4ed6\u5360\u7528\u6444\u50cf\u5934\u7684\u7a0b\u5e8f\u3002<\/p>\n<\/p>\n<p><h2>2\u3001\u6444\u50cf\u5934\u56fe\u50cf\u5361\u987f<\/h2>\n<\/p>\n<p><p>\u5982\u679c\u6444\u50cf\u5934\u56fe\u50cf\u5361\u987f\uff0c\u53ef\u80fd\u662f\u7531\u4e8e\u5904\u7406\u8d1f\u62c5\u8fc7\u91cd\u3001\u5e27\u7387\u8fc7\u9ad8\u7b49\u539f\u56e0\u3002\u53ef\u4ee5\u5c1d\u8bd5\u964d\u4f4e\u5206\u8fa8\u7387\u3001\u51cf\u5c11\u5e27\u7387\u6216\u4f7f\u7528\u591a\u7ebf\u7a0b\u6765\u63d0\u9ad8\u6027\u80fd\u3002<\/p>\n<\/p>\n<p><h2>3\u3001\u6444\u50cf\u5934\u68c0\u6d4b\u5931\u8d25<\/h2>\n<\/p>\n<p><p>\u5982\u679c\u6444\u50cf\u5934\u68c0\u6d4b\u5931\u8d25\uff0c\u53ef\u80fd\u662f\u7531\u4e8e\u6444\u50cf\u5934\u7d22\u5f15\u9519\u8bef\u3001\u6444\u50cf\u5934\u4e0d\u517c\u5bb9\u7b49\u539f\u56e0\u3002\u53ef\u4ee5\u5c1d\u8bd5\u4f7f\u7528\u4e0d\u540c\u7684\u6444\u50cf\u5934\u7d22\u5f15\u6216\u66f4\u6362\u6444\u50cf\u5934\u3002<\/p>\n<\/p>\n<h2><strong>\u4e5d\u3001\u8fdb\u9636\u6444\u50cf\u5934\u64cd\u4f5c<\/strong><\/h2>\n<p><h2>1\u3001\u80cc\u666f\u51cf\u9664<\/h2>\n<\/p>\n<p><p>\u80cc\u666f\u51cf\u9664\u53ef\u4ee5\u63d0\u53d6\u524d\u666f\u76ee\u6807\uff0c\u7528\u4e8e\u8fd0\u52a8\u68c0\u6d4b\u7b49\u5e94\u7528\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">fgbg = cv2.createBackgroundSubtractorMOG2()<\/p>\n<p>while True:<\/p>\n<p>    ret, frame = cap.read()<\/p>\n<p>    if not ret:<\/p>\n<p>        break<\/p>\n<p>    fgmask = fgbg.apply(frame)<\/p>\n<p>    cv2.imshow(&#39;\u80cc\u666f\u51cf\u9664&#39;, fgmask)<\/p>\n<p>    if cv2.waitKey(1) == ord(&#39;q&#39;):<\/p>\n<p>        break<\/p>\n<p>cap.release()<\/p>\n<p>cv2.destroyAllWindows()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>2\u3001\u76ee\u6807\u8ddf\u8e2a<\/h2>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528OpenCV\u7684\u76ee\u6807\u8ddf\u8e2a\u7b97\u6cd5\u6765\u8ddf\u8e2a\u76ee\u6807\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">tracker = cv2.TrackerKCF_create()<\/p>\n<p>ret, frame = cap.read()<\/p>\n<p>bbox = cv2.selectROI(frame, False)<\/p>\n<p>tracker.init(frame, bbox)<\/p>\n<p>while True:<\/p>\n<p>    ret, frame = cap.read()<\/p>\n<p>    if not ret:<\/p>\n<p>        break<\/p>\n<p>    ret, bbox = tracker.update(frame)<\/p>\n<p>    if ret:<\/p>\n<p>        p1 = (int(bbox[0]), int(bbox[1]))<\/p>\n<p>        p2 = (int(bbox[0] + bbox[2]), int(bbox[1] + bbox[3]))<\/p>\n<p>        cv2.rectangle(frame, p1, p2, (255, 0, 0), 2, 1)<\/p>\n<p>    cv2.imshow(&#39;\u76ee\u6807\u8ddf\u8e2a&#39;, frame)<\/p>\n<p>    if cv2.waitKey(1) == ord(&#39;q&#39;):<\/p>\n<p>        break<\/p>\n<p>cap.release()<\/p>\n<p>cv2.destroyAllWindows()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<h2><strong>\u5341\u3001\u603b\u7ed3<\/strong><\/h2>\n<p><p>\u901a\u8fc7\u4f7f\u7528OpenCV\u5e93\uff0c\u6211\u4eec\u53ef\u4ee5\u8f7b\u677e\u5730\u5728Python\u4e2d\u5b9e\u73b0\u6444\u50cf\u5934\u68c0\u6d4b\u3002<strong>\u4f7f\u7528OpenCV\u5e93\u3001\u4f7f\u7528\u6444\u50cf\u5934\u7d22\u5f15\u3001\u6355\u83b7\u5e27\u5e76\u663e\u793a\u56fe\u50cf<\/strong>\u662f\u5b9e\u73b0\u6444\u50cf\u5934\u68c0\u6d4b\u7684\u5173\u952e\u6b65\u9aa4\u3002\u6211\u4eec\u53ef\u4ee5\u6839\u636e\u5b9e\u9645\u9700\u6c42\u9009\u62e9\u4e0d\u540c\u7684\u6444\u50cf\u5934\u3001\u5904\u7406\u6355\u83b7\u7684\u56fe\u50cf\u3001\u8fdb\u884c\u5e38\u89c1\u7684\u6444\u50cf\u5934\u64cd\u4f5c\uff0c\u5e76\u901a\u8fc7\u4f18\u5316\u6280\u5de7\u63d0\u9ad8\u68c0\u6d4b\u6027\u80fd\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6211\u4eec\u8fd8\u53ef\u4ee5\u5904\u7406\u591a\u6444\u50cf\u5934\u8f93\u5165\u3001\u89e3\u51b3\u5e38\u89c1\u95ee\u9898\uff0c\u5e76\u8fdb\u884c\u8fdb\u9636\u7684\u6444\u50cf\u5934\u64cd\u4f5c\uff0c\u5982\u80cc\u666f\u51cf\u9664\u548c\u76ee\u6807\u8ddf\u8e2a\u3002\u5e0c\u671b\u672c\u6587\u80fd\u591f\u5e2e\u52a9\u4f60\u66f4\u597d\u5730\u7406\u89e3\u548c\u4f7f\u7528Python\u8fdb\u884c\u6444\u50cf\u5934\u68c0\u6d4b\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u8bc6\u522b\u8fde\u63a5\u7684\u6444\u50cf\u5934\u8bbe\u5907\uff1f<\/strong><br \/>\u4f7f\u7528OpenCV\u5e93\u53ef\u4ee5\u8f7b\u677e\u68c0\u6d4b\u8fde\u63a5\u7684\u6444\u50cf\u5934\u3002\u60a8\u53ef\u4ee5\u901a\u8fc7<code>cv2.VideoCapture()<\/code>\u51fd\u6570\u5c1d\u8bd5\u6253\u5f00\u4e0d\u540c\u7684\u6444\u50cf\u5934\u7d22\u5f15\uff08\u901a\u5e38\u662f0\u30011\u7b49\uff09\uff0c\u5e76\u68c0\u67e5\u8fd4\u56de\u503c\u662f\u5426\u6709\u6548\u3002\u6210\u529f\u6253\u5f00\u6444\u50cf\u5934\u540e\uff0c\u53ef\u4ee5\u83b7\u53d6\u5176\u5c5e\u6027\u548c\u7279\u6027\uff0c\u786e\u8ba4\u8bbe\u5907\u662f\u5426\u6b63\u5e38\u5de5\u4f5c\u3002<\/p>\n<p><strong>Python\u4e2d\u5982\u4f55\u5904\u7406\u6444\u50cf\u5934\u56fe\u50cf\u6d41\uff1f<\/strong><br \/>\u5728\u8bc6\u522b\u5230\u6444\u50cf\u5934\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528OpenCV\u7684<code>read()<\/code>\u65b9\u6cd5\u6765\u6355\u6349\u56fe\u50cf\u6d41\u3002\u901a\u8fc7\u4e0d\u65ad\u5faa\u73af\u8bfb\u53d6\u5e27\u6570\u636e\uff0c\u60a8\u53ef\u4ee5\u5b9e\u65f6\u5904\u7406\u56fe\u50cf\uff0c\u6bd4\u5982\u8fdb\u884c\u56fe\u50cf\u8bc6\u522b\u3001\u7279\u6548\u5e94\u7528\u6216\u89c6\u9891\u5f55\u5236\u3002\u6b64\u5916\uff0c\u53ef\u4ee5\u4f7f\u7528<code>imshow()<\/code>\u51fd\u6570\u5728\u7a97\u53e3\u4e2d\u663e\u793a\u6355\u83b7\u7684\u56fe\u50cf\u3002<\/p>\n<p><strong>\u5982\u679c\u6211\u7684\u6444\u50cf\u5934\u65e0\u6cd5\u88abPython\u68c0\u6d4b\u5230\uff0c\u6211\u8be5\u600e\u4e48\u529e\uff1f<\/strong><br \/>\u5982\u679c\u6444\u50cf\u5934\u672a\u88ab\u8bc6\u522b\uff0c\u9996\u5148\u68c0\u67e5\u8bbe\u5907\u662f\u5426\u6b63\u786e\u8fde\u63a5\uff0c\u5e76\u786e\u4fdd\u76f8\u5173\u9a71\u52a8\u7a0b\u5e8f\u5df2\u5b89\u88c5\u3002\u8fd8\u53ef\u4ee5\u5c1d\u8bd5\u91cd\u65b0\u542f\u52a8\u8ba1\u7b97\u673a\u6216\u66f4\u6362USB\u7aef\u53e3\u3002\u5982\u679c\u95ee\u9898\u4f9d\u65e7\u5b58\u5728\uff0c\u53ef\u4ee5\u901a\u8fc7\u6d4b\u8bd5\u5176\u4ed6\u8f6f\u4ef6\uff08\u5982\u76f8\u673a\u5e94\u7528\uff09\u786e\u8ba4\u6444\u50cf\u5934\u7684\u529f\u80fd\u662f\u5426\u6b63\u5e38\u3002\u786e\u4fdd\u60a8\u7684Python\u73af\u5883\u548c\u5e93\uff08\u5982OpenCV\uff09\u662f\u6700\u65b0\u7684\uff0c\u53ef\u80fd\u6709\u52a9\u4e8e\u89e3\u51b3\u517c\u5bb9\u6027\u95ee\u9898\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u68c0\u6d4b\u6444\u50cf\u5934\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u51e0\u4e2a\u6b65\u9aa4\u6765\u5b9e\u73b0\uff1a\u4f7f\u7528OpenCV\u5e93\u3001\u4f7f\u7528\u6444\u50cf\u5934\u7d22\u5f15\u3001\u6355\u83b7\u5e27\u5e76\u663e\u793a\u56fe\u50cf\u3002\u5176\u4e2d\uff0c [&hellip;]","protected":false},"author":3,"featured_media":1173804,"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\/1173795"}],"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=1173795"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1173795\/revisions"}],"predecessor-version":[{"id":1173806,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1173795\/revisions\/1173806"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1173804"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1173795"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1173795"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1173795"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}