{"id":1130316,"date":"2025-01-08T20:38:37","date_gmt":"2025-01-08T12:38:37","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1130316.html"},"modified":"2025-01-08T20:38:40","modified_gmt":"2025-01-08T12:38:40","slug":"%e5%a6%82%e4%bd%95%e4%bb%8e%e7%bd%91%e7%bb%9c%e6%91%84%e5%83%8f%e5%a4%b4%e8%8e%b7%e5%8f%96%e8%a7%86%e9%a2%91%e6%b5%81python","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1130316.html","title":{"rendered":"\u5982\u4f55\u4ece\u7f51\u7edc\u6444\u50cf\u5934\u83b7\u53d6\u89c6\u9891\u6d41python"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25100420\/67759984-aed8-4979-a9c4-9ee3c6a08336.webp\" alt=\"\u5982\u4f55\u4ece\u7f51\u7edc\u6444\u50cf\u5934\u83b7\u53d6\u89c6\u9891\u6d41python\" \/><\/p>\n<p><p> <strong>\u5982\u4f55\u4ece\u7f51\u7edc\u6444\u50cf\u5934\u83b7\u53d6\u89c6\u9891\u6d41Python<\/strong><\/p>\n<\/p>\n<p><p><strong>\u5728Python\u4e2d\u83b7\u53d6\u7f51\u7edc\u6444\u50cf\u5934\u89c6\u9891\u6d41\u7684\u65b9\u6cd5\u6709\u5f88\u591a\uff0c\u5176\u4e2d\u5305\u62ec\u4f7f\u7528OpenCV\u3001PyCapture\u3001\u4ee5\u53ca\u5176\u4ed6\u5e93\u3002<\/strong> \u901a\u8fc7\u8fd9\u4e9b\u5e93\uff0c\u4f60\u53ef\u4ee5\u8f7b\u677e\u5730\u6355\u6349\u5e76\u5904\u7406\u89c6\u9891\u6d41\u3002\u4ee5\u4e0b\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u4f7f\u7528OpenCV\u6765\u83b7\u53d6\u548c\u5904\u7406\u7f51\u7edc\u6444\u50cf\u5934\u89c6\u9891\u6d41\u7684\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p>\u5176\u4e2d\uff0c<strong>\u4f7f\u7528OpenCV\u3001\u8bbe\u7f6e\u6444\u50cf\u5934\u53c2\u6570\u3001\u5904\u7406\u89c6\u9891\u6d41<\/strong> \u662f\u4e09\u79cd\u91cd\u8981\u7684\u65b9\u6cd5\u3002\u5177\u4f53\u6765\u8bf4\uff0c\u4f7f\u7528OpenCV\u6765\u83b7\u53d6\u89c6\u9891\u6d41\u662f\u6700\u5e38\u89c1\u548c\u6700\u7b80\u5355\u7684\u65b9\u6cd5\u3002OpenCV\u662f\u4e00\u4e2a\u5f00\u6e90\u7684\u8ba1\u7b97\u673a\u89c6\u89c9\u5e93\uff0c\u63d0\u4f9b\u4e86\u5927\u91cf\u7684\u529f\u80fd\uff0c\u7528\u4e8e\u56fe\u50cf\u548c\u89c6\u9891\u5904\u7406\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u8ba8\u8bba\u5982\u4f55\u4f7f\u7528OpenCV\u83b7\u53d6\u89c6\u9891\u6d41\uff0c\u5e76\u5bf9\u89c6\u9891\u6d41\u8fdb\u884c\u5904\u7406\u3002<\/p>\n<\/p>\n<h2><strong>\u4e00\u3001\u4f7f\u7528OpenCV\u83b7\u53d6\u89c6\u9891\u6d41<\/strong><\/h2>\n<p><p>OpenCV\uff08Open Source Computer Vision Library\uff09\u662f\u4e00\u4e2a\u5f00\u6e90\u7684\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\u3002\u5b83\u5305\u542b\u4e86\u591a\u79cd\u56fe\u50cf\u548c\u89c6\u9891\u5904\u7406\u529f\u80fd\u3002\u4f7f\u7528OpenCV\u83b7\u53d6\u7f51\u7edc\u6444\u50cf\u5934\u7684\u89c6\u9891\u6d41\u975e\u5e38\u65b9\u4fbf\u3002\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\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><h2>1\u3001\u521d\u59cb\u5316\u6444\u50cf\u5934<\/h2>\n<\/p>\n<p><p>\u8981\u4ece\u7f51\u7edc\u6444\u50cf\u5934\u83b7\u53d6\u89c6\u9891\u6d41\uff0c\u6211\u4eec\u9996\u5148\u9700\u8981\u521d\u59cb\u5316\u6444\u50cf\u5934\u3002OpenCV\u63d0\u4f9b\u4e86\u4e00\u4e2aVideoCapture\u7c7b\u6765\u5b9e\u73b0\u8fd9\u4e2a\u529f\u80fd\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u4ee3\u7801\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import cv2<\/p>\n<h2><strong>\u521d\u59cb\u5316\u6444\u50cf\u5934<\/strong><\/h2>\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>    exit()<\/p>\n<p>while True:<\/p>\n<p>    # \u4ece\u6444\u50cf\u5934\u8bfb\u53d6\u4e00\u5e27<\/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;frame&#39;, frame)<\/p>\n<p>    # \u6309\u4e0b &#39;q&#39; \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<h2><strong>\u91ca\u653e\u8d44\u6e90<\/strong><\/h2>\n<p>cap.release()<\/p>\n<p>cv2.destroyAllWindows()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>cv2.VideoCapture(0)<\/code>\u6765\u521d\u59cb\u5316\u9ed8\u8ba4\u6444\u50cf\u5934\u3002\u5982\u679c\u4f60\u6709\u591a\u4e2a\u6444\u50cf\u5934\uff0c\u53ef\u4ee5\u4f7f\u7528\u4e0d\u540c\u7684\u7d22\u5f15\uff080, 1, 2\u7b49\uff09\u6765\u9009\u62e9\u4e0d\u540c\u7684\u6444\u50cf\u5934\u3002<\/p>\n<\/p>\n<p><h2>2\u3001\u5904\u7406\u89c6\u9891\u6d41<\/h2>\n<\/p>\n<p><p>\u83b7\u53d6\u89c6\u9891\u6d41\u540e\uff0c\u6211\u4eec\u53ef\u4ee5\u5bf9\u6bcf\u4e00\u5e27\u8fdb\u884c\u5904\u7406\u3002OpenCV\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u56fe\u50cf\u5904\u7406\u529f\u80fd\u3002\u6bd4\u5982\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u6bcf\u4e00\u5e27\u8f6c\u6362\u4e3a\u7070\u5ea6\u56fe\u50cf\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import cv2<\/p>\n<h2><strong>\u521d\u59cb\u5316\u6444\u50cf\u5934<\/strong><\/h2>\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>    exit()<\/p>\n<p>while True:<\/p>\n<p>    # \u4ece\u6444\u50cf\u5934\u8bfb\u53d6\u4e00\u5e27<\/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>    # \u5c06\u5e27\u8f6c\u6362\u4e3a\u7070\u5ea6\u56fe\u50cf<\/p>\n<p>    gray_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)<\/p>\n<p>    # \u663e\u793a\u5e27<\/p>\n<p>    cv2.imshow(&#39;frame&#39;, gray_frame)<\/p>\n<p>    # \u6309\u4e0b &#39;q&#39; \u952e\u9000\u51fa<\/p>\n<p>    if cv2.waitKey(1) == ord(&#39;q&#39;):<\/p>\n<p>        break<\/p>\n<h2><strong>\u91ca\u653e\u8d44\u6e90<\/strong><\/h2>\n<p>cap.release()<\/p>\n<p>cv2.destroyAllWindows()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4f7f\u7528<code>cv2.cvtColor<\/code>\u51fd\u6570\uff0c\u6211\u4eec\u53ef\u4ee5\u8f7b\u677e\u5730\u5c06\u5f69\u8272\u56fe\u50cf\u8f6c\u6362\u4e3a\u7070\u5ea6\u56fe\u50cf\u3002\u7c7b\u4f3c\u5730\uff0c\u6211\u4eec\u53ef\u4ee5\u5bf9\u6bcf\u4e00\u5e27\u8fdb\u884c\u5404\u79cd\u56fe\u50cf\u5904\u7406\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<h2><strong>\u4e8c\u3001\u8bbe\u7f6e\u6444\u50cf\u5934\u53c2\u6570<\/strong><\/h2>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u4f60\u53ef\u80fd\u9700\u8981\u8bbe\u7f6e\u6444\u50cf\u5934\u7684\u53c2\u6570\uff0c\u4f8b\u5982\u5206\u8fa8\u7387\u3001\u5e27\u7387\u7b49\u3002OpenCV\u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u6cd5\u6765\u8bbe\u7f6e\u548c\u83b7\u53d6\u6444\u50cf\u5934\u53c2\u6570\u3002<\/p>\n<\/p>\n<p><h2>1\u3001\u8bbe\u7f6e\u5206\u8fa8\u7387<\/h2>\n<\/p>\n<p><p>\u4f60\u53ef\u4ee5\u901a\u8fc7<code>cv2.VideoCapture.set<\/code>\u65b9\u6cd5\u6765\u8bbe\u7f6e\u6444\u50cf\u5934\u7684\u5206\u8fa8\u7387\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import cv2<\/p>\n<h2><strong>\u521d\u59cb\u5316\u6444\u50cf\u5934<\/strong><\/h2>\n<p>cap = cv2.VideoCapture(0)<\/p>\n<h2><strong>\u8bbe\u7f6e\u5206\u8fa8\u7387<\/strong><\/h2>\n<p>cap.set(cv2.CAP_PROP_FRAME_WIDTH, 1280)<\/p>\n<p>cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 720)<\/p>\n<p>if not cap.isOpened():<\/p>\n<p>    print(&quot;\u65e0\u6cd5\u6253\u5f00\u6444\u50cf\u5934&quot;)<\/p>\n<p>    exit()<\/p>\n<p>while True:<\/p>\n<p>    # \u4ece\u6444\u50cf\u5934\u8bfb\u53d6\u4e00\u5e27<\/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;frame&#39;, frame)<\/p>\n<p>    # \u6309\u4e0b &#39;q&#39; \u952e\u9000\u51fa<\/p>\n<p>    if cv2.waitKey(1) == ord(&#39;q&#39;):<\/p>\n<p>        break<\/p>\n<h2><strong>\u91ca\u653e\u8d44\u6e90<\/strong><\/h2>\n<p>cap.release()<\/p>\n<p>cv2.destroyAllWindows()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u8bbe\u7f6e\u4e86\u6444\u50cf\u5934\u7684\u5206\u8fa8\u7387\u4e3a1280&#215;720\u3002\u4f60\u53ef\u4ee5\u6839\u636e\u9700\u8981\u8c03\u6574\u5206\u8fa8\u7387\u3002<\/p>\n<\/p>\n<p><h2>2\u3001\u83b7\u53d6\u6444\u50cf\u5934\u53c2\u6570<\/h2>\n<\/p>\n<p><p>\u4f60\u53ef\u4ee5\u901a\u8fc7<code>cv2.VideoCapture.get<\/code>\u65b9\u6cd5\u6765\u83b7\u53d6\u6444\u50cf\u5934\u7684\u53c2\u6570\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import cv2<\/p>\n<h2><strong>\u521d\u59cb\u5316\u6444\u50cf\u5934<\/strong><\/h2>\n<p>cap = cv2.VideoCapture(0)<\/p>\n<h2><strong>\u83b7\u53d6\u5206\u8fa8\u7387<\/strong><\/h2>\n<p>width = cap.get(cv2.CAP_PROP_FRAME_WIDTH)<\/p>\n<p>height = cap.get(cv2.CAP_PROP_FRAME_HEIGHT)<\/p>\n<p>print(f&quot;\u5206\u8fa8\u7387\uff1a{width}x{height}&quot;)<\/p>\n<p>if not cap.isOpened():<\/p>\n<p>    print(&quot;\u65e0\u6cd5\u6253\u5f00\u6444\u50cf\u5934&quot;)<\/p>\n<p>    exit()<\/p>\n<p>while True:<\/p>\n<p>    # \u4ece\u6444\u50cf\u5934\u8bfb\u53d6\u4e00\u5e27<\/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;frame&#39;, frame)<\/p>\n<p>    # \u6309\u4e0b &#39;q&#39; \u952e\u9000\u51fa<\/p>\n<p>    if cv2.waitKey(1) == ord(&#39;q&#39;):<\/p>\n<p>        break<\/p>\n<h2><strong>\u91ca\u653e\u8d44\u6e90<\/strong><\/h2>\n<p>cap.release()<\/p>\n<p>cv2.destroyAllWindows()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u83b7\u53d6\u5e76\u6253\u5370\u4e86\u6444\u50cf\u5934\u7684\u5206\u8fa8\u7387\u3002\u4f60\u53ef\u4ee5\u83b7\u53d6\u5176\u4ed6\u53c2\u6570\uff0c\u4f8b\u5982\u5e27\u7387\u3001\u4eae\u5ea6\u3001\u5bf9\u6bd4\u5ea6\u7b49\u3002<\/p>\n<\/p>\n<h2><strong>\u4e09\u3001\u5904\u7406\u89c6\u9891\u6d41<\/strong><\/h2>\n<p><p>\u5904\u7406\u89c6\u9891\u6d41\u662f\u8ba1\u7b97\u673a\u89c6\u89c9\u4e2d\u7684\u4e00\u4e2a\u91cd\u8981\u4efb\u52a1\u3002\u901a\u8fc7\u5904\u7406\u89c6\u9891\u6d41\uff0c\u6211\u4eec\u53ef\u4ee5\u5b9e\u73b0\u5404\u79cd\u5e94\u7528\uff0c\u4f8b\u5982\u4eba\u8138\u68c0\u6d4b\u3001\u8fd0\u52a8\u68c0\u6d4b\u7b49\u3002<\/p>\n<\/p>\n<p><h2>1\u3001\u4eba\u8138\u68c0\u6d4b<\/h2>\n<\/p>\n<p><p>OpenCV\u63d0\u4f9b\u4e86\u4eba\u8138\u68c0\u6d4b\u529f\u80fd\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u9884\u8bad\u7ec3\u7684Haar\u7ea7\u8054\u5206\u7c7b\u5668\u6765\u68c0\u6d4b\u4eba\u8138\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u4ee3\u7801\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import cv2<\/p>\n<h2><strong>\u52a0\u8f7d\u9884\u8bad\u7ec3\u7684Haar\u7ea7\u8054\u5206\u7c7b\u5668<\/strong><\/h2>\n<p>face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + &#39;haarcascade_frontalface_default.xml&#39;)<\/p>\n<h2><strong>\u521d\u59cb\u5316\u6444\u50cf\u5934<\/strong><\/h2>\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>    exit()<\/p>\n<p>while True:<\/p>\n<p>    # \u4ece\u6444\u50cf\u5934\u8bfb\u53d6\u4e00\u5e27<\/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>    # \u5c06\u5e27\u8f6c\u6362\u4e3a\u7070\u5ea6\u56fe\u50cf<\/p>\n<p>    gray_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)<\/p>\n<p>    # \u68c0\u6d4b\u4eba\u8138<\/p>\n<p>    faces = face_cascade.detectMultiScale(gray_frame, scaleFactor=1.1, minNeighbors=5, minSize=(30, 30))<\/p>\n<p>    # \u7ed8\u5236\u77e9\u5f62\u6846<\/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>    # \u663e\u793a\u5e27<\/p>\n<p>    cv2.imshow(&#39;frame&#39;, frame)<\/p>\n<p>    # \u6309\u4e0b &#39;q&#39; \u952e\u9000\u51fa<\/p>\n<p>    if cv2.waitKey(1) == ord(&#39;q&#39;):<\/p>\n<p>        break<\/p>\n<h2><strong>\u91ca\u653e\u8d44\u6e90<\/strong><\/h2>\n<p>cap.release()<\/p>\n<p>cv2.destroyAllWindows()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u9884\u8bad\u7ec3\u7684Haar\u7ea7\u8054\u5206\u7c7b\u5668\u6765\u68c0\u6d4b\u4eba\u8138\uff0c\u5e76\u5728\u68c0\u6d4b\u5230\u7684\u4eba\u8138\u5468\u56f4\u7ed8\u5236\u77e9\u5f62\u6846\u3002<\/p>\n<\/p>\n<p><h2>2\u3001\u8fd0\u52a8\u68c0\u6d4b<\/h2>\n<\/p>\n<p><p>\u8fd0\u52a8\u68c0\u6d4b\u662f\u53e6\u4e00\u4e2a\u5e38\u89c1\u7684\u5e94\u7528\u3002\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u8ba1\u7b97\u8fde\u7eed\u5e27\u4e4b\u95f4\u7684\u5dee\u5f02\u6765\u68c0\u6d4b\u8fd0\u52a8\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u4ee3\u7801\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import cv2<\/p>\n<h2><strong>\u521d\u59cb\u5316\u6444\u50cf\u5934<\/strong><\/h2>\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>    exit()<\/p>\n<h2><strong>\u83b7\u53d6\u7b2c\u4e00\u5e27<\/strong><\/h2>\n<p>ret, prev_frame = cap.read()<\/p>\n<p>if not ret:<\/p>\n<p>    print(&quot;\u65e0\u6cd5\u63a5\u6536\u5e27&quot;)<\/p>\n<p>    exit()<\/p>\n<h2><strong>\u5c06\u7b2c\u4e00\u5e27\u8f6c\u6362\u4e3a\u7070\u5ea6\u56fe\u50cf<\/strong><\/h2>\n<p>prev_gray = cv2.cvtColor(prev_frame, cv2.COLOR_BGR2GRAY)<\/p>\n<p>while True:<\/p>\n<p>    # \u4ece\u6444\u50cf\u5934\u8bfb\u53d6\u4e00\u5e27<\/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>    # \u5c06\u5e27\u8f6c\u6362\u4e3a\u7070\u5ea6\u56fe\u50cf<\/p>\n<p>    gray_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)<\/p>\n<p>    # \u8ba1\u7b97\u5e27\u4e4b\u95f4\u7684\u5dee\u5f02<\/p>\n<p>    diff_frame = cv2.absdiff(prev_gray, gray_frame)<\/p>\n<p>    # \u9608\u503c\u5316<\/p>\n<p>    _, thresh_frame = cv2.threshold(diff_frame, 25, 255, cv2.THRESH_BINARY)<\/p>\n<p>    # \u663e\u793a\u5e27<\/p>\n<p>    cv2.imshow(&#39;frame&#39;, thresh_frame)<\/p>\n<p>    # \u66f4\u65b0\u524d\u4e00\u5e27<\/p>\n<p>    prev_gray = gray_frame<\/p>\n<p>    # \u6309\u4e0b &#39;q&#39; \u952e\u9000\u51fa<\/p>\n<p>    if cv2.waitKey(1) == ord(&#39;q&#39;):<\/p>\n<p>        break<\/p>\n<h2><strong>\u91ca\u653e\u8d44\u6e90<\/strong><\/h2>\n<p>cap.release()<\/p>\n<p>cv2.destroyAllWindows()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u8ba1\u7b97\u4e86\u8fde\u7eed\u5e27\u4e4b\u95f4\u7684\u5dee\u5f02\uff0c\u5e76\u5bf9\u5dee\u5f02\u56fe\u50cf\u8fdb\u884c\u4e86\u9608\u503c\u5316\u5904\u7406\uff0c\u4ee5\u68c0\u6d4b\u8fd0\u52a8\u3002<\/p>\n<\/p>\n<h2><strong>\u56db\u3001\u4fdd\u5b58\u89c6\u9891\u6d41<\/strong><\/h2>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u4f60\u53ef\u80fd\u9700\u8981\u5c06\u89c6\u9891\u6d41\u4fdd\u5b58\u5230\u6587\u4ef6\u4e2d\u3002OpenCV\u63d0\u4f9b\u4e86VideoWriter\u7c7b\u6765\u5b9e\u73b0\u8fd9\u4e2a\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><h2>1\u3001\u521d\u59cb\u5316VideoWriter<\/h2>\n<\/p>\n<p><p>\u4f60\u53ef\u4ee5\u4f7f\u7528VideoWriter\u7c7b\u6765\u521d\u59cb\u5316\u89c6\u9891\u5199\u5165\u5668\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u4ee3\u7801\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import cv2<\/p>\n<h2><strong>\u521d\u59cb\u5316\u6444\u50cf\u5934<\/strong><\/h2>\n<p>cap = cv2.VideoCapture(0)<\/p>\n<h2><strong>\u83b7\u53d6\u89c6\u9891\u5e27\u7684\u5bbd\u5ea6\u548c\u9ad8\u5ea6<\/strong><\/h2>\n<p>frame_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))<\/p>\n<p>frame_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))<\/p>\n<h2><strong>\u521d\u59cb\u5316\u89c6\u9891\u5199\u5165\u5668<\/strong><\/h2>\n<p>out = cv2.VideoWriter(&#39;output.avi&#39;, cv2.VideoWriter_fourcc(*&#39;XVID&#39;), 20.0, (frame_width, frame_height))<\/p>\n<p>if not cap.isOpened():<\/p>\n<p>    print(&quot;\u65e0\u6cd5\u6253\u5f00\u6444\u50cf\u5934&quot;)<\/p>\n<p>    exit()<\/p>\n<p>while True:<\/p>\n<p>    # \u4ece\u6444\u50cf\u5934\u8bfb\u53d6\u4e00\u5e27<\/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>    # \u5199\u5165\u5e27<\/p>\n<p>    out.write(frame)<\/p>\n<p>    # \u663e\u793a\u5e27<\/p>\n<p>    cv2.imshow(&#39;frame&#39;, frame)<\/p>\n<p>    # \u6309\u4e0b &#39;q&#39; \u952e\u9000\u51fa<\/p>\n<p>    if cv2.waitKey(1) == ord(&#39;q&#39;):<\/p>\n<p>        break<\/p>\n<h2><strong>\u91ca\u653e\u8d44\u6e90<\/strong><\/h2>\n<p>cap.release()<\/p>\n<p>out.release()<\/p>\n<p>cv2.destroyAllWindows()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528VideoWriter\u7c7b\u5c06\u89c6\u9891\u6d41\u4fdd\u5b58\u5230\u6587\u4ef6\u4e2d\u3002\u4f60\u53ef\u4ee5\u6839\u636e\u9700\u8981\u8c03\u6574\u89c6\u9891\u7f16\u7801\u5668\u3001\u5e27\u7387\u548c\u5206\u8fa8\u7387\u3002<\/p>\n<\/p>\n<p><h2>2\u3001\u4fdd\u5b58\u5904\u7406\u540e\u7684\u89c6\u9891\u6d41<\/h2>\n<\/p>\n<p><p>\u4f60\u53ef\u4ee5\u5bf9\u89c6\u9891\u6d41\u8fdb\u884c\u5904\u7406\u540e\u518d\u4fdd\u5b58\u3002\u4f8b\u5982\uff0c\u5c06\u6bcf\u4e00\u5e27\u8f6c\u6362\u4e3a\u7070\u5ea6\u56fe\u50cf\u5e76\u4fdd\u5b58\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import cv2<\/p>\n<h2><strong>\u521d\u59cb\u5316\u6444\u50cf\u5934<\/strong><\/h2>\n<p>cap = cv2.VideoCapture(0)<\/p>\n<h2><strong>\u83b7\u53d6\u89c6\u9891\u5e27\u7684\u5bbd\u5ea6\u548c\u9ad8\u5ea6<\/strong><\/h2>\n<p>frame_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))<\/p>\n<p>frame_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))<\/p>\n<h2><strong>\u521d\u59cb\u5316\u89c6\u9891\u5199\u5165\u5668<\/strong><\/h2>\n<p>out = cv2.VideoWriter(&#39;output_gray.avi&#39;, cv2.VideoWriter_fourcc(*&#39;XVID&#39;), 20.0, (frame_width, frame_height), False)<\/p>\n<p>if not cap.isOpened():<\/p>\n<p>    print(&quot;\u65e0\u6cd5\u6253\u5f00\u6444\u50cf\u5934&quot;)<\/p>\n<p>    exit()<\/p>\n<p>while True:<\/p>\n<p>    # \u4ece\u6444\u50cf\u5934\u8bfb\u53d6\u4e00\u5e27<\/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>    # \u5c06\u5e27\u8f6c\u6362\u4e3a\u7070\u5ea6\u56fe\u50cf<\/p>\n<p>    gray_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)<\/p>\n<p>    # \u5199\u5165\u5e27<\/p>\n<p>    out.write(gray_frame)<\/p>\n<p>    # \u663e\u793a\u5e27<\/p>\n<p>    cv2.imshow(&#39;frame&#39;, gray_frame)<\/p>\n<p>    # \u6309\u4e0b &#39;q&#39; \u952e\u9000\u51fa<\/p>\n<p>    if cv2.waitKey(1) == ord(&#39;q&#39;):<\/p>\n<p>        break<\/p>\n<h2><strong>\u91ca\u653e\u8d44\u6e90<\/strong><\/h2>\n<p>cap.release()<\/p>\n<p>out.release()<\/p>\n<p>cv2.destroyAllWindows()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5c06\u6bcf\u4e00\u5e27\u8f6c\u6362\u4e3a\u7070\u5ea6\u56fe\u50cf\u540e\u518d\u4fdd\u5b58\u5230\u6587\u4ef6\u4e2d\u3002<\/p>\n<\/p>\n<h2><strong>\u4e94\u3001\u7f51\u7edc\u6444\u50cf\u5934\u7684\u8fdc\u7a0b\u8bbf\u95ee<\/strong><\/h2>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u4f60\u53ef\u80fd\u9700\u8981\u4ece\u8fdc\u7a0b\u8bbf\u95ee\u7f51\u7edc\u6444\u50cf\u5934\u7684\u89c6\u9891\u6d41\u3002\u8fd9\u53ef\u4ee5\u901a\u8fc7\u5c06\u89c6\u9891\u6d41\u4f20\u8f93\u5230\u7f51\u7edc\u4e0a\u6765\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<p><h2>1\u3001\u4f7f\u7528RTSP\u534f\u8bae<\/h2>\n<\/p>\n<p><p>RTSP\uff08\u5b9e\u65f6\u6d41\u534f\u8bae\uff09\u662f\u4e00\u79cd\u7528\u4e8e\u63a7\u5236\u6d41\u5a92\u4f53\u670d\u52a1\u5668\u7684\u7f51\u7edc\u63a7\u5236\u534f\u8bae\u3002\u8bb8\u591a\u7f51\u7edc\u6444\u50cf\u5934\u652f\u6301RTSP\u534f\u8bae\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528OpenCV\u6765\u4eceRTSP\u6d41\u83b7\u53d6\u89c6\u9891\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import cv2<\/p>\n<h2><strong>RTSP\u6d41\u5730\u5740<\/strong><\/h2>\n<p>rtsp_url = &#39;rtsp:\/\/username:password@ip_address:port\/path&#39;<\/p>\n<h2><strong>\u521d\u59cb\u5316\u6444\u50cf\u5934<\/strong><\/h2>\n<p>cap = cv2.VideoCapture(rtsp_url)<\/p>\n<p>if not cap.isOpened():<\/p>\n<p>    print(&quot;\u65e0\u6cd5\u6253\u5f00RTSP\u6d41&quot;)<\/p>\n<p>    exit()<\/p>\n<p>while True:<\/p>\n<p>    # \u4eceRTSP\u6d41\u8bfb\u53d6\u4e00\u5e27<\/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;frame&#39;, frame)<\/p>\n<p>    # \u6309\u4e0b &#39;q&#39; \u952e\u9000\u51fa<\/p>\n<p>    if cv2.waitKey(1) == ord(&#39;q&#39;):<\/p>\n<p>        break<\/p>\n<h2><strong>\u91ca\u653e\u8d44\u6e90<\/strong><\/h2>\n<p>cap.release()<\/p>\n<p>cv2.destroyAllWindows()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528RTSP\u6d41\u5730\u5740\u6765\u521d\u59cb\u5316VideoCapture\u5bf9\u8c61\uff0c\u4ece\u800c\u83b7\u53d6\u8fdc\u7a0b\u7f51\u7edc\u6444\u50cf\u5934\u7684\u89c6\u9891\u6d41\u3002<\/p>\n<\/p>\n<p><h2>2\u3001\u4f7f\u7528HTTP\u534f\u8bae<\/h2>\n<\/p>\n<p><p>\u6709\u4e9b\u7f51\u7edc\u6444\u50cf\u5934\u652f\u6301\u901a\u8fc7HTTP\u534f\u8bae\u8bbf\u95ee\u89c6\u9891\u6d41\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528OpenCV\u6765\u4eceHTTP\u6d41\u83b7\u53d6\u89c6\u9891\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import cv2<\/p>\n<h2><strong>HTTP\u6d41\u5730\u5740<\/strong><\/h2>\n<p>http_url = &#39;http:\/\/ip_address:port\/path&#39;<\/p>\n<h2><strong>\u521d\u59cb\u5316\u6444\u50cf\u5934<\/strong><\/h2>\n<p>cap = cv2.VideoCapture(http_url)<\/p>\n<p>if not cap.isOpened():<\/p>\n<p>    print(&quot;\u65e0\u6cd5\u6253\u5f00HTTP\u6d41&quot;)<\/p>\n<p>    exit()<\/p>\n<p>while True:<\/p>\n<p>    # \u4eceHTTP\u6d41\u8bfb\u53d6\u4e00\u5e27<\/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;frame&#39;, frame)<\/p>\n<p>    # \u6309\u4e0b &#39;q&#39; \u952e\u9000\u51fa<\/p>\n<p>    if cv2.waitKey(1) == ord(&#39;q&#39;):<\/p>\n<p>        break<\/p>\n<h2><strong>\u91ca\u653e\u8d44\u6e90<\/strong><\/h2>\n<p>cap.release()<\/p>\n<p>cv2.destroyAllWindows()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528HTTP\u6d41\u5730\u5740\u6765\u521d\u59cb\u5316VideoCapture\u5bf9\u8c61\uff0c\u4ece\u800c\u83b7\u53d6\u8fdc\u7a0b\u7f51\u7edc\u6444\u50cf\u5934\u7684\u89c6\u9891\u6d41\u3002<\/p>\n<\/p>\n<h2><strong>\u7ed3\u8bba<\/strong><\/h2>\n<p><p>\u901a\u8fc7\u4f7f\u7528OpenCV\uff0c\u4f60\u53ef\u4ee5\u8f7b\u677e\u5730\u4ece\u7f51\u7edc\u6444\u50cf\u5934\u83b7\u53d6\u89c6\u9891\u6d41\uff0c\u5e76\u5bf9\u89c6\u9891\u6d41\u8fdb\u884c\u5904\u7406\u3002\u6211\u4eec\u8ba8\u8bba\u4e86\u5982\u4f55\u521d\u59cb\u5316\u6444\u50cf\u5934\u3001\u8bbe\u7f6e\u6444\u50cf\u5934\u53c2\u6570\u3001\u5904\u7406\u89c6\u9891\u6d41\u3001\u4fdd\u5b58\u89c6\u9891\u6d41\u4ee5\u53ca\u4ece\u8fdc\u7a0b\u8bbf\u95ee\u7f51\u7edc\u6444\u50cf\u5934\u7684\u89c6\u9891\u6d41\u3002\u5e0c\u671b\u8fd9\u4e9b\u5185\u5bb9\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u8fde\u63a5\u7f51\u7edc\u6444\u50cf\u5934\uff1f<\/strong><br \/>\u8981\u8fde\u63a5\u7f51\u7edc\u6444\u50cf\u5934\uff0c\u60a8\u9700\u8981\u4f7f\u7528OpenCV\u5e93\u3002\u5b89\u88c5OpenCV\u540e\uff0c\u53ef\u4ee5\u901a\u8fc7\u6307\u5b9a\u6444\u50cf\u5934\u7684URL\u6216IP\u5730\u5740\u6765\u83b7\u53d6\u89c6\u9891\u6d41\u3002\u4f8b\u5982\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528<code>cv2.VideoCapture()<\/code>\u65b9\u6cd5\u5e76\u4f20\u5165\u6444\u50cf\u5934\u7684\u5730\u5740\uff0c\u968f\u540e\u8c03\u7528<code>read()<\/code>\u65b9\u6cd5\u6765\u83b7\u53d6\u89c6\u9891\u5e27\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5904\u7406\u7f51\u7edc\u6444\u50cf\u5934\u7684\u89c6\u9891\u6d41\u9700\u8981\u54ea\u4e9b\u5e93\uff1f<\/strong><br \/>\u5904\u7406\u7f51\u7edc\u6444\u50cf\u5934\u89c6\u9891\u6d41\u901a\u5e38\u9700\u8981OpenCV\u5e93\uff0c\u5b83\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u56fe\u50cf\u5904\u7406\u529f\u80fd\u3002\u6b64\u5916\uff0c\u60a8\u53ef\u80fd\u8fd8\u9700\u8981NumPy\u5e93\u6765\u5904\u7406\u56fe\u50cf\u6570\u7ec4\u548cMatplotlib\u7528\u4e8e\u53ef\u89c6\u5316\u89c6\u9891\u6d41\u3002\u786e\u4fdd\u5728\u5f00\u59cb\u4e4b\u524d\u5b89\u88c5\u8fd9\u4e9b\u5e93\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u4fdd\u5b58\u4ece\u7f51\u7edc\u6444\u50cf\u5934\u83b7\u53d6\u7684\u89c6\u9891\u6d41\uff1f<\/strong><br \/>\u8981\u4fdd\u5b58\u89c6\u9891\u6d41\uff0c\u53ef\u4ee5\u4f7f\u7528OpenCV\u7684<code>VideoWriter<\/code>\u7c7b\u3002\u901a\u8fc7\u6307\u5b9a\u8f93\u51fa\u6587\u4ef6\u7684\u540d\u79f0\u3001\u7f16\u7801\u683c\u5f0f\u548c\u5e27\u7387\uff0c\u60a8\u53ef\u4ee5\u5c06\u6bcf\u4e00\u5e27\u5199\u5165\u6587\u4ef6\u3002\u4f7f\u7528<code>VideoWriter.write()<\/code>\u65b9\u6cd5\u5c06\u83b7\u53d6\u7684\u89c6\u9891\u5e27\u5199\u5165\u6307\u5b9a\u6587\u4ef6\u4e2d\uff0c\u786e\u4fdd\u5728\u5b8c\u6210\u65f6\u6b63\u786e\u91ca\u653e\u8d44\u6e90\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5982\u4f55\u4ece\u7f51\u7edc\u6444\u50cf\u5934\u83b7\u53d6\u89c6\u9891\u6d41Python \u5728Python\u4e2d\u83b7\u53d6\u7f51\u7edc\u6444\u50cf\u5934\u89c6\u9891\u6d41\u7684\u65b9\u6cd5\u6709\u5f88\u591a\uff0c\u5176\u4e2d\u5305\u62ec\u4f7f\u7528Open [&hellip;]","protected":false},"author":3,"featured_media":1130323,"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\/1130316"}],"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=1130316"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1130316\/revisions"}],"predecessor-version":[{"id":1130325,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1130316\/revisions\/1130325"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1130323"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1130316"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1130316"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1130316"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}