{"id":1146115,"date":"2025-01-08T23:14:13","date_gmt":"2025-01-08T15:14:13","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1146115.html"},"modified":"2025-01-08T23:14:15","modified_gmt":"2025-01-08T15:14:15","slug":"python%e5%a6%82%e4%bd%95%e6%8a%8a%e6%91%84%e5%83%8f%e5%a4%b4%e4%bf%9d%e5%ad%98%e4%b8%ba%e8%a7%86%e9%a2%91%e6%96%87%e4%bb%b6%e5%a4%b9","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1146115.html","title":{"rendered":"python\u5982\u4f55\u628a\u6444\u50cf\u5934\u4fdd\u5b58\u4e3a\u89c6\u9891\u6587\u4ef6\u5939"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24182347\/f63ef50e-4181-4835-8493-53e2d31dd967.webp\" alt=\"python\u5982\u4f55\u628a\u6444\u50cf\u5934\u4fdd\u5b58\u4e3a\u89c6\u9891\u6587\u4ef6\u5939\" \/><\/p>\n<p><p> <strong>Python\u5982\u4f55\u628a\u6444\u50cf\u5934\u4fdd\u5b58\u4e3a\u89c6\u9891\u6587\u4ef6\u5939<\/strong><\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528OpenCV\u5e93\u5c06\u6444\u50cf\u5934\u6355\u83b7\u7684\u56fe\u50cf\u4fdd\u5b58\u4e3a\u89c6\u9891\u6587\u4ef6\u3002<strong>\u9996\u5148\uff0c\u901a\u8fc7OpenCV\u5e93\u5b9e\u73b0\u89c6\u9891\u6355\u83b7\u3001\u7136\u540e\u5c06\u6355\u83b7\u7684\u89c6\u9891\u5e27\u4fdd\u5b58\u4e3a\u89c6\u9891\u6587\u4ef6\u3001\u6700\u540e\u5c06\u89c6\u9891\u6587\u4ef6\u4fdd\u5b58\u5230\u6307\u5b9a\u7684\u6587\u4ef6\u5939<\/strong>\u3002\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e09\u4e2a\u6838\u5fc3\u6b65\u9aa4\uff0c\u5e76\u63d0\u4f9b\u5b8c\u6574\u7684\u4ee3\u7801\u793a\u4f8b\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u5f15\u5165\u6240\u9700\u5e93<\/h3>\n<\/p>\n<p><p>\u4e3a\u4e86\u5b9e\u73b0\u89c6\u9891\u6355\u83b7\u548c\u4fdd\u5b58\uff0c\u6211\u4eec\u9700\u8981\u5f15\u5165OpenCV\u5e93\u3002\u4f60\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u6765\u5b89\u88c5OpenCV\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install opencv-python<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u521d\u59cb\u5316\u6444\u50cf\u5934<\/h3>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u521d\u59cb\u5316\u6444\u50cf\u5934\u8bbe\u5907\u3002OpenCV\u63d0\u4f9b\u4e86<code>VideoCapture<\/code>\u7c7b\u6765\u5b9e\u73b0\u8fd9\u4e2a\u529f\u80fd\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import cv2<\/p>\n<h2><strong>\u521d\u59cb\u5316\u6444\u50cf\u5934\uff0c\u53c2\u65700\u8868\u793a\u7b2c\u4e00\u4e2a\u6444\u50cf\u5934<\/strong><\/h2>\n<p>cap = cv2.VideoCapture(0)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u8bbe\u7f6e\u89c6\u9891\u7f16\u7801\u683c\u5f0f\u548c\u8f93\u51fa\u6587\u4ef6<\/h3>\n<\/p>\n<p><p>\u5728\u6355\u83b7\u89c6\u9891\u5e27\u4e4b\u524d\uff0c\u6211\u4eec\u9700\u8981\u8bbe\u7f6e\u89c6\u9891\u7f16\u7801\u683c\u5f0f\u548c\u8f93\u51fa\u6587\u4ef6\u540d\u3002OpenCV\u63d0\u4f9b\u4e86<code>VideoWriter<\/code>\u7c7b\u6765\u5b9e\u73b0\u8fd9\u4e2a\u529f\u80fd\u3002\u4ee5\u4e0b\u662f\u76f8\u5173\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5b9a\u4e49\u89c6\u9891\u7f16\u7801\u683c\u5f0f\uff0c\u56db\u4e2a\u5b57\u7b26\u8868\u793a\u4e00\u79cd\u7f16\u7801\u683c\u5f0f<\/p>\n<p>fourcc = cv2.VideoWriter_fourcc(*&#39;XVID&#39;)<\/p>\n<h2><strong>\u521b\u5efaVideoWriter\u5bf9\u8c61\uff0c\u53c2\u6570\u4f9d\u6b21\u662f\uff1a\u8f93\u51fa\u6587\u4ef6\u540d\u3001\u7f16\u7801\u683c\u5f0f\u3001\u5e27\u7387\u3001\u5e27\u5927\u5c0f<\/strong><\/h2>\n<p>out = cv2.VideoWriter(&#39;output.avi&#39;, fourcc, 20.0, (640, 480))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u6355\u83b7\u89c6\u9891\u5e27\u5e76\u4fdd\u5b58<\/h3>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u9700\u8981\u6355\u83b7\u89c6\u9891\u5e27\u5e76\u5c06\u5176\u4fdd\u5b58\u5230\u8f93\u51fa\u6587\u4ef6\u4e2d\u3002\u6211\u4eec\u53ef\u4ee5\u5728\u4e00\u4e2a\u5faa\u73af\u4e2d\u4e0d\u65ad\u8bfb\u53d6\u6444\u50cf\u5934\u7684\u5e27\uff0c\u5e76\u5c06\u5176\u5199\u5165\u89c6\u9891\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">while(cap.isOpened()):<\/p>\n<p>    ret, frame = cap.read()<\/p>\n<p>    if ret == True:<\/p>\n<p>        # \u5199\u5165\u89c6\u9891\u6587\u4ef6<\/p>\n<p>        out.write(frame)<\/p>\n<p>        # \u663e\u793a\u5e27<\/p>\n<p>        cv2.imshow(&#39;frame&#39;, frame)<\/p>\n<p>        # \u6309q\u952e\u9000\u51fa\u5faa\u73af<\/p>\n<p>        if cv2.w<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>tKey(1) &amp; 0xFF == ord(&#39;q&#39;):<\/p>\n<p>            break<\/p>\n<p>    else:<\/p>\n<p>        break<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u91ca\u653e\u8d44\u6e90<\/h3>\n<\/p>\n<p><p>\u5f53\u6211\u4eec\u5b8c\u6210\u89c6\u9891\u6355\u83b7\u548c\u4fdd\u5b58\u4e4b\u540e\uff0c\u9700\u8981\u91ca\u653e\u76f8\u5173\u8d44\u6e90\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">cap.release()<\/p>\n<p>out.release()<\/p>\n<p>cv2.destroyAllWindows()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u5b8c\u6574\u4ee3\u7801\u793a\u4f8b<\/h3>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u5b8c\u6574\u7684\u4ee3\u7801\u793a\u4f8b\uff0c\u6f14\u793a\u5982\u4f55\u4f7f\u7528Python\u548cOpenCV\u5c06\u6444\u50cf\u5934\u6355\u83b7\u7684\u89c6\u9891\u4fdd\u5b58\u5230\u6587\u4ef6\u5939\u4e2d\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>\u5b9a\u4e49\u89c6\u9891\u7f16\u7801\u683c\u5f0f<\/strong><\/h2>\n<p>fourcc = cv2.VideoWriter_fourcc(*&#39;XVID&#39;)<\/p>\n<h2><strong>\u521b\u5efaVideoWriter\u5bf9\u8c61<\/strong><\/h2>\n<p>out = cv2.VideoWriter(&#39;output.avi&#39;, fourcc, 20.0, (640, 480))<\/p>\n<p>while(cap.isOpened()):<\/p>\n<p>    ret, frame = cap.read()<\/p>\n<p>    if ret == True:<\/p>\n<p>        # \u5199\u5165\u89c6\u9891\u6587\u4ef6<\/p>\n<p>        out.write(frame)<\/p>\n<p>        # \u663e\u793a\u5e27<\/p>\n<p>        cv2.imshow(&#39;frame&#39;, frame)<\/p>\n<p>        # \u6309q\u952e\u9000\u51fa\u5faa\u73af<\/p>\n<p>        if cv2.waitKey(1) &amp; 0xFF == ord(&#39;q&#39;):<\/p>\n<p>            break<\/p>\n<p>    else:<\/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><h3>\u4e03\u3001\u4f18\u5316\u548c\u6269\u5c55<\/h3>\n<\/p>\n<p><h4>1\u3001\u8c03\u6574\u89c6\u9891\u53c2\u6570<\/h4>\n<\/p>\n<p><p>\u6839\u636e\u5b9e\u9645\u9700\u6c42\uff0c\u6211\u4eec\u53ef\u4ee5\u8c03\u6574\u89c6\u9891\u53c2\u6570\uff0c\u4f8b\u5982\u5e27\u7387\u3001\u5206\u8fa8\u7387\u548c\u7f16\u7801\u683c\u5f0f\u7b49\u3002\u53ef\u4ee5\u901a\u8fc7\u4fee\u6539<code>VideoWriter<\/code>\u5bf9\u8c61\u7684\u53c2\u6570\u6765\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u4fdd\u5b58\u5230\u6307\u5b9a\u6587\u4ef6\u5939<\/h4>\n<\/p>\n<p><p>\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c\u89c6\u9891\u6587\u4ef6\u5c06\u4fdd\u5b58\u5230\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55\u3002\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u63d0\u4f9b\u5b8c\u6574\u7684\u6587\u4ef6\u8def\u5f84\uff0c\u5c06\u89c6\u9891\u6587\u4ef6\u4fdd\u5b58\u5230\u6307\u5b9a\u7684\u6587\u4ef6\u5939\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">output_path = &#39;videos\/output.avi&#39;<\/p>\n<p>out = cv2.VideoWriter(output_path, fourcc, 20.0, (640, 480))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001\u5904\u7406\u5e27\u6570\u636e<\/h4>\n<\/p>\n<p><p>\u5728\u4fdd\u5b58\u5e27\u4e4b\u524d\uff0c\u6211\u4eec\u53ef\u4ee5\u5bf9\u5e27\u6570\u636e\u8fdb\u884c\u5904\u7406\uff0c\u4f8b\u5982\u6dfb\u52a0\u6c34\u5370\u3001\u8c03\u6574\u4eae\u5ea6\u548c\u5bf9\u6bd4\u5ea6\u7b49\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff0c\u6f14\u793a\u5982\u4f55\u5728\u5e27\u4e0a\u6dfb\u52a0\u6587\u672c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">while(cap.isOpened()):<\/p>\n<p>    ret, frame = cap.read()<\/p>\n<p>    if ret == True:<\/p>\n<p>        # \u5728\u5e27\u4e0a\u6dfb\u52a0\u6587\u672c<\/p>\n<p>        cv2.putText(frame, &#39;OpenCV Video&#39;, (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 2, cv2.LINE_AA)<\/p>\n<p>        # \u5199\u5165\u89c6\u9891\u6587\u4ef6<\/p>\n<p>        out.write(frame)<\/p>\n<p>        # \u663e\u793a\u5e27<\/p>\n<p>        cv2.imshow(&#39;frame&#39;, frame)<\/p>\n<p>        # \u6309q\u952e\u9000\u51fa\u5faa\u73af<\/p>\n<p>        if cv2.waitKey(1) &amp; 0xFF == ord(&#39;q&#39;):<\/p>\n<p>            break<\/p>\n<p>    else:<\/p>\n<p>        break<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516b\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u6b65\u9aa4\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528Python\u548cOpenCV\u5c06\u6444\u50cf\u5934\u6355\u83b7\u7684\u89c6\u9891\u4fdd\u5b58\u4e3a\u89c6\u9891\u6587\u4ef6\uff0c\u5e76\u5c06\u5176\u4fdd\u5b58\u5230\u6307\u5b9a\u7684\u6587\u4ef6\u5939\u4e2d\u3002\u6211\u4eec\u8fd8\u53ef\u4ee5\u6839\u636e\u5b9e\u9645\u9700\u6c42\u8c03\u6574\u89c6\u9891\u53c2\u6570\u548c\u5bf9\u5e27\u6570\u636e\u8fdb\u884c\u5904\u7406\u3002<strong>OpenCV\u5e93\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u89c6\u9891\u5904\u7406\u529f\u80fd\uff0c\u4f7f\u6211\u4eec\u80fd\u591f\u8f7b\u677e\u5b9e\u73b0\u5404\u79cd\u89c6\u9891\u76f8\u5173\u7684\u4efb\u52a1<\/strong>\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u5c06\u6444\u50cf\u5934\u5f55\u5236\u7684\u89c6\u9891\u4fdd\u5b58\u5230\u6307\u5b9a\u6587\u4ef6\u5939\uff1f<\/strong><br \/>\u8981\u5c06\u6444\u50cf\u5934\u5f55\u5236\u7684\u89c6\u9891\u4fdd\u5b58\u5230\u7279\u5b9a\u6587\u4ef6\u5939\u4e2d\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528OpenCV\u5e93\u3002\u9996\u5148\uff0c\u786e\u4fdd\u60a8\u5df2\u5b89\u88c5\u8be5\u5e93\u3002\u901a\u8fc7\u4ee5\u4e0b\u4ee3\u7801\uff0c\u60a8\u53ef\u4ee5\u6253\u5f00\u6444\u50cf\u5934\u5e76\u5c06\u5f55\u5236\u7684\u89c6\u9891\u4fdd\u5b58\u5230\u60a8\u9009\u62e9\u7684\u6587\u4ef6\u5939\u4e2d\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">import cv2\n\ncap = cv2.VideoCapture(0)\nfourcc = cv2.VideoWriter_fourcc(*&#39;XVID&#39;)\nout = cv2.VideoWriter(&#39;path_to_folder\/output.avi&#39;, fourcc, 20.0, (640, 480))\n\nwhile cap.isOpened():\n    ret, frame = cap.read()\n    if ret:\n        out.write(frame)\n        cv2.imshow(&#39;Video&#39;, frame)\n        if cv2.waitKey(1) &amp; 0xFF == ord(&#39;q&#39;):\n            break\n    else:\n        break\n\ncap.release()\nout.release()\ncv2.destroyAllWindows()\n<\/code><\/pre>\n<p>\u5728\u4ee3\u7801\u4e2d\uff0c\u5c06<code>path_to_folder<\/code>\u66ff\u6362\u4e3a\u60a8\u5e0c\u671b\u4fdd\u5b58\u89c6\u9891\u7684\u6587\u4ef6\u5939\u8def\u5f84\u3002<\/p>\n<p><strong>\u9700\u8981\u5b89\u88c5\u54ea\u4e9b\u5e93\u624d\u80fd\u4f7f\u7528Python\u5f55\u5236\u89c6\u9891\uff1f<\/strong><br \/>\u4e3a\u4e86\u4f7f\u7528Python\u5f55\u5236\u89c6\u9891\uff0c\u60a8\u9700\u8981\u5b89\u88c5OpenCV\u5e93\u3002\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u901a\u8fc7pip\u5b89\u88c5\uff1a<\/p>\n<pre><code>pip install opencv-python\n<\/code><\/pre>\n<p>\u6b64\u5916\uff0c\u5982\u679c\u60a8\u5e0c\u671b\u5728\u5f55\u5236\u65f6\u5904\u7406\u89c6\u9891\u5e27\u6216\u6dfb\u52a0\u6548\u679c\uff0c\u53ef\u80fd\u9700\u8981\u5b89\u88c5NumPy\u5e93\uff1a<\/p>\n<pre><code>pip install numpy\n<\/code><\/pre>\n<p><strong>\u4fdd\u5b58\u7684\u89c6\u9891\u6587\u4ef6\u683c\u5f0f\u6709\u54ea\u4e9b\u63a8\u8350\u9009\u62e9\uff1f<\/strong><br \/>\u5728\u4f7f\u7528OpenCV\u4fdd\u5b58\u89c6\u9891\u65f6\uff0c\u63a8\u8350\u7684\u6587\u4ef6\u683c\u5f0f\u5305\u62ecAVI\u548cMP4\u3002\u60a8\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e\u4e0d\u540c\u7684\u7f16\u7801\u683c\u5f0f\u6765\u4fdd\u5b58\u8fd9\u4e9b\u6587\u4ef6\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>XVID<\/code>\u7f16\u7801\u53ef\u4ee5\u751f\u6210AVI\u6587\u4ef6\uff0c\u800c<code>mp4v<\/code>\u7f16\u7801\u9002\u7528\u4e8eMP4\u6587\u4ef6\u3002\u5728\u9009\u62e9\u6587\u4ef6\u683c\u5f0f\u65f6\uff0c\u8bf7\u8003\u8651\u64ad\u653e\u8bbe\u5907\u7684\u517c\u5bb9\u6027\u4ee5\u53ca\u6587\u4ef6\u5927\u5c0f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u5982\u4f55\u628a\u6444\u50cf\u5934\u4fdd\u5b58\u4e3a\u89c6\u9891\u6587\u4ef6\u5939 \u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528OpenCV\u5e93\u5c06\u6444\u50cf\u5934\u6355\u83b7\u7684\u56fe\u50cf\u4fdd\u5b58\u4e3a\u89c6\u9891 [&hellip;]","protected":false},"author":3,"featured_media":1146120,"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\/1146115"}],"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=1146115"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1146115\/revisions"}],"predecessor-version":[{"id":1146122,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1146115\/revisions\/1146122"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1146120"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1146115"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1146115"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1146115"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}