{"id":1069748,"date":"2025-01-08T10:51:50","date_gmt":"2025-01-08T02:51:50","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1069748.html"},"modified":"2025-01-08T10:51:52","modified_gmt":"2025-01-08T02:51:52","slug":"python%e5%a6%82%e4%bd%95%e5%b0%86%e8%a7%86%e9%a2%91%e5%a4%84%e7%90%86%e6%88%90%e5%9b%be%e7%89%87-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1069748.html","title":{"rendered":"Python\u5982\u4f55\u5c06\u89c6\u9891\u5904\u7406\u6210\u56fe\u7247"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25100703\/f47774f0-fbbe-47da-914f-7042b1aa5b82.webp\" alt=\"Python\u5982\u4f55\u5c06\u89c6\u9891\u5904\u7406\u6210\u56fe\u7247\" \/><\/p>\n<p><p> <strong>Python\u5c06\u89c6\u9891\u5904\u7406\u6210\u56fe\u7247\u7684\u6b65\u9aa4\u6709\uff1a\u4f7f\u7528OpenCV\u5e93\u3001\u8bfb\u53d6\u89c6\u9891\u5e27\u3001\u4fdd\u5b58\u5e27\u4e3a\u56fe\u50cf\u6587\u4ef6\u3001\u8c03\u6574\u5e27\u7387\u3002<\/strong> \u5176\u4e2d\uff0c<strong>\u4f7f\u7528OpenCV\u5e93<\/strong>\u662f\u5e38\u89c1\u4e14\u9ad8\u6548\u7684\u65b9\u6cd5\u3002OpenCV\u662f\u4e00\u4e2a\u5f00\u6e90\u7684\u8ba1\u7b97\u673a\u89c6\u89c9\u5e93\uff0c\u53ef\u4ee5\u8f7b\u677e\u5730\u8bfb\u53d6\u3001\u5904\u7406\u548c\u4fdd\u5b58\u89c6\u9891\u5e27\u3002\u4e0b\u9762\u6211\u4eec\u5c06\u8be6\u7ec6\u63cf\u8ff0\u8fd9\u4e00\u8fc7\u7a0b\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u5b89\u88c5\u5e76\u5bfc\u5165\u6240\u9700\u5e93<\/h3>\n<\/p>\n<p><p>\u5728\u5f00\u59cb\u5904\u7406\u89c6\u9891\u4e4b\u524d\uff0c\u6211\u4eec\u9700\u8981\u5b89\u88c5\u5e76\u5bfc\u5165\u5fc5\u8981\u7684Python\u5e93\u3002\u4e3b\u8981\u7684\u5e93\u5305\u62ecOpenCV\u548cos\u5e93\u3002OpenCV\u7528\u4e8e\u89c6\u9891\u5904\u7406\uff0c\u800cos\u5e93\u7528\u4e8e\u6587\u4ef6\u548c\u76ee\u5f55\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">pip install opencv-python<\/p>\n<p>pip install opencv-python-headless<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728Python\u811a\u672c\u4e2d\u5bfc\u5165\u8fd9\u4e9b\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import cv2<\/p>\n<p>import os<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u8bfb\u53d6\u89c6\u9891\u6587\u4ef6<\/h3>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u4f7f\u7528OpenCV\u7684<code>cv2.VideoCapture<\/code>\u65b9\u6cd5\u8bfb\u53d6\u89c6\u9891\u6587\u4ef6\u3002\u6211\u4eec\u9700\u8981\u63d0\u4f9b\u89c6\u9891\u6587\u4ef6\u7684\u8def\u5f84\u4f5c\u4e3a\u53c2\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">video_path = &#39;path_to_your_video.mp4&#39;<\/p>\n<p>cap = cv2.VideoCapture(video_path)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u521b\u5efa\u8f93\u51fa\u76ee\u5f55<\/h3>\n<\/p>\n<p><p>\u4e3a\u4e86\u5b58\u50a8\u4ece\u89c6\u9891\u4e2d\u63d0\u53d6\u7684\u5e27\uff0c\u6211\u4eec\u9700\u8981\u521b\u5efa\u4e00\u4e2a\u76ee\u5f55\u3002\u53ef\u4ee5\u4f7f\u7528os\u5e93\u6765\u5b8c\u6210\u8fd9\u4e2a\u4efb\u52a1\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">output_dir = &#39;output_images&#39;<\/p>\n<p>if not os.path.exists(output_dir):<\/p>\n<p>    os.makedirs(output_dir)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u63d0\u53d6\u548c\u4fdd\u5b58\u89c6\u9891\u5e27<\/h3>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u9700\u8981\u9010\u5e27\u8bfb\u53d6\u89c6\u9891\uff0c\u5e76\u5c06\u6bcf\u4e00\u5e27\u4fdd\u5b58\u4e3a\u56fe\u50cf\u6587\u4ef6\u3002\u53ef\u4ee5\u4f7f\u7528\u4e00\u4e2a\u5faa\u73af\u6765\u5b8c\u6210\u8fd9\u4e2a\u4efb\u52a1\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">frame_count = 0<\/p>\n<p>while cap.isOpened():<\/p>\n<p>    ret, frame = cap.read()<\/p>\n<p>    if not ret:<\/p>\n<p>        break<\/p>\n<p>    frame_file = os.path.join(output_dir, f&#39;frame_{frame_count:04d}.png&#39;)<\/p>\n<p>    cv2.imwrite(frame_file, frame)<\/p>\n<p>    frame_count += 1<\/p>\n<p>cap.release()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u8c03\u6574\u5e27\u7387<\/h3>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u53ef\u80fd\u4e0d\u9700\u8981\u6bcf\u4e00\u5e27\u90fd\u4fdd\u5b58\u4e3a\u56fe\u50cf\u6587\u4ef6\u3002\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u8c03\u6574\u5e27\u7387\u6765\u51cf\u5c11\u8f93\u51fa\u7684\u56fe\u50cf\u6570\u91cf\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff0c\u5c55\u793a\u4e86\u5982\u4f55\u6bcf\u9694\u4e00\u5b9a\u5e27\u6570\u4fdd\u5b58\u4e00\u5e27\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">frame_rate = 10<\/p>\n<p>frame_count = 0<\/p>\n<p>while cap.isOpened():<\/p>\n<p>    ret, frame = cap.read()<\/p>\n<p>    if not ret:<\/p>\n<p>        break<\/p>\n<p>    if frame_count % frame_rate == 0:<\/p>\n<p>        frame_file = os.path.join(output_dir, f&#39;frame_{frame_count:04d}.png&#39;)<\/p>\n<p>        cv2.imwrite(frame_file, frame)<\/p>\n<p>    frame_count += 1<\/p>\n<p>cap.release()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u5904\u7406\u89c6\u9891\u5e27<\/h3>\n<\/p>\n<p><p>\u6709\u65f6\uff0c\u6211\u4eec\u9700\u8981\u5bf9\u89c6\u9891\u5e27\u8fdb\u884c\u8fdb\u4e00\u6b65\u5904\u7406\uff0c\u4f8b\u5982\u88c1\u526a\u3001\u7f29\u653e\u6216\u5e94\u7528\u56fe\u50cf\u5904\u7406\u7b97\u6cd5\u3002\u53ef\u4ee5\u5728\u4fdd\u5b58\u5e27\u4e4b\u524d\u5bf9\u5176\u8fdb\u884c\u5904\u7406\u3002\u4f8b\u5982\uff0c\u88c1\u526a\u6bcf\u4e00\u5e27\u7684\u4e2d\u5fc3\u533a\u57df\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def crop_center(frame, width, height):<\/p>\n<p>    y, x, _ = frame.shape<\/p>\n<p>    startx = x \/\/ 2 - width \/\/ 2<\/p>\n<p>    starty = y \/\/ 2 - height \/\/ 2<\/p>\n<p>    return frame[starty:starty + height, startx:startx + width]<\/p>\n<p>frame_count = 0<\/p>\n<p>while cap.isOpened():<\/p>\n<p>    ret, frame = cap.read()<\/p>\n<p>    if not ret:<\/p>\n<p>        break<\/p>\n<p>    frame = crop_center(frame, 500, 500)<\/p>\n<p>    frame_file = os.path.join(output_dir, f&#39;frame_{frame_count:04d}.png&#39;)<\/p>\n<p>    cv2.imwrite(frame_file, frame)<\/p>\n<p>    frame_count += 1<\/p>\n<p>cap.release()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e03\u3001\u547d\u540d\u8f93\u51fa\u6587\u4ef6<\/h3>\n<\/p>\n<p><p>\u4e3a\u4e86\u66f4\u597d\u5730\u7ba1\u7406\u548c\u8bc6\u522b\u63d0\u53d6\u7684\u56fe\u50cf\uff0c\u53ef\u4ee5\u4f7f\u7528\u66f4\u5177\u63cf\u8ff0\u6027\u7684\u6587\u4ef6\u547d\u540d\u65b9\u6848\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u5305\u542b\u89c6\u9891\u540d\u79f0\u548c\u65f6\u95f4\u6233\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import datetime<\/p>\n<p>video_name = os.path.splitext(os.path.basename(video_path))[0]<\/p>\n<p>frame_count = 0<\/p>\n<p>fps = cap.get(cv2.CAP_PROP_FPS)<\/p>\n<p>while cap.isOpened():<\/p>\n<p>    ret, frame = cap.read()<\/p>\n<p>    if not ret:<\/p>\n<p>        break<\/p>\n<p>    timestamp = datetime.timedelta(seconds=frame_count \/ fps)<\/p>\n<p>    frame_file = os.path.join(output_dir, f&#39;{video_name}_frame_{frame_count:04d}_{timestamp}.png&#39;)<\/p>\n<p>    cv2.imwrite(frame_file, frame)<\/p>\n<p>    frame_count += 1<\/p>\n<p>cap.release()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516b\u3001\u5904\u7406\u591a\u4e2a\u89c6\u9891\u6587\u4ef6<\/h3>\n<\/p>\n<p><p>\u5982\u679c\u6709\u591a\u4e2a\u89c6\u9891\u6587\u4ef6\u9700\u8981\u5904\u7406\uff0c\u53ef\u4ee5\u4f7f\u7528\u4e00\u4e2a\u5faa\u73af\u6765\u904d\u5386\u6240\u6709\u89c6\u9891\u6587\u4ef6\uff0c\u5e76\u5bf9\u6bcf\u4e2a\u89c6\u9891\u6587\u4ef6\u6267\u884c\u4e0a\u8ff0\u6b65\u9aa4\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">video_files = [&#39;video1.mp4&#39;, &#39;video2.mp4&#39;, &#39;video3.mp4&#39;]<\/p>\n<p>for video_path in video_files:<\/p>\n<p>    cap = cv2.VideoCapture(video_path)<\/p>\n<p>    video_name = os.path.splitext(os.path.basename(video_path))[0]<\/p>\n<p>    output_dir = os.path.join(&#39;output_images&#39;, video_name)<\/p>\n<p>    if not os.path.exists(output_dir):<\/p>\n<p>        os.makedirs(output_dir)<\/p>\n<p>    frame_count = 0<\/p>\n<p>    while cap.isOpened():<\/p>\n<p>        ret, frame = cap.read()<\/p>\n<p>        if not ret:<\/p>\n<p>            break<\/p>\n<p>        frame_file = os.path.join(output_dir, f&#39;frame_{frame_count:04d}.png&#39;)<\/p>\n<p>        cv2.imwrite(frame_file, frame)<\/p>\n<p>        frame_count += 1<\/p>\n<p>    cap.release()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e5d\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u6b65\u9aa4\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528Python\u548cOpenCV\u5e93\u5c06\u89c6\u9891\u5904\u7406\u6210\u56fe\u7247\u3002\u4e3b\u8981\u6b65\u9aa4\u5305\u62ec\u5b89\u88c5\u548c\u5bfc\u5165\u5e93\u3001\u8bfb\u53d6\u89c6\u9891\u6587\u4ef6\u3001\u521b\u5efa\u8f93\u51fa\u76ee\u5f55\u3001\u63d0\u53d6\u548c\u4fdd\u5b58\u89c6\u9891\u5e27\u3001\u8c03\u6574\u5e27\u7387\u3001\u5904\u7406\u89c6\u9891\u5e27\u3001\u547d\u540d\u8f93\u51fa\u6587\u4ef6\uff0c\u4ee5\u53ca\u5904\u7406\u591a\u4e2a\u89c6\u9891\u6587\u4ef6\u3002\u901a\u8fc7\u4f7f\u7528\u8fd9\u4e9b\u6280\u672f\uff0c\u6211\u4eec\u53ef\u4ee5\u9ad8\u6548\u5730\u5c06\u89c6\u9891\u5185\u5bb9\u8f6c\u5316\u4e3a\u56fe\u50cf\u6587\u4ef6\uff0c\u4ece\u800c\u4fbf\u4e8e\u8fdb\u4e00\u6b65\u5206\u6790\u548c\u5904\u7406\u3002<\/p>\n<\/p>\n<p><p>\u603b\u4e4b\uff0c<strong>\u4f7f\u7528OpenCV\u5e93<\/strong>\u662f\u5c06\u89c6\u9891\u5904\u7406\u6210\u56fe\u7247\u7684\u9ad8\u6548\u65b9\u6cd5\uff0c\u5b83\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u529f\u80fd\u548c\u7075\u6d3b\u6027\uff0c\u9002\u7528\u4e8e\u5404\u79cd\u89c6\u9891\u5904\u7406\u4efb\u52a1\u3002\u5e0c\u671b\u8fd9\u7bc7\u6587\u7ae0\u80fd\u591f\u5e2e\u52a9\u60a8\u66f4\u597d\u5730\u7406\u89e3\u548c\u5e94\u7528\u8fd9\u4e00\u6280\u672f\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u63d0\u53d6\u89c6\u9891\u4e2d\u7684\u5e27\u4f5c\u4e3a\u56fe\u7247\uff1f<\/strong><br \/>\u8981\u63d0\u53d6\u89c6\u9891\u4e2d\u7684\u5e27\uff0c\u53ef\u4ee5\u4f7f\u7528OpenCV\u5e93\u3002\u9996\u5148\uff0c\u5b89\u88c5OpenCV\u5e93\uff0c\u4f7f\u7528\u547d\u4ee4<code>pip install opencv-python<\/code>\u3002\u63a5\u7740\uff0c\u52a0\u8f7d\u89c6\u9891\u6587\u4ef6\uff0c\u8bfb\u53d6\u6bcf\u4e00\u5e27\uff0c\u5e76\u5c06\u5176\u4fdd\u5b58\u4e3a\u56fe\u7247\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>cv2.VideoCapture()<\/code>\u6253\u5f00\u89c6\u9891\uff0c\u901a\u8fc7\u5faa\u73af\u8bfb\u53d6\u5e27\uff0c\u5e76\u4f7f\u7528<code>cv2.imwrite()<\/code>\u4fdd\u5b58\u6bcf\u4e00\u5e27\u4e3a\u56fe\u50cf\u6587\u4ef6\u3002<\/p>\n<p><strong>\u5904\u7406\u89c6\u9891\u65f6\u9700\u8981\u6ce8\u610f\u54ea\u4e9b\u53c2\u6570\u8bbe\u7f6e\uff1f<\/strong><br \/>\u5728\u5904\u7406\u89c6\u9891\u65f6\uff0c\u5e27\u7387\u548c\u5206\u8fa8\u7387\u662f\u4e24\u4e2a\u91cd\u8981\u53c2\u6570\u3002\u5e27\u7387\u51b3\u5b9a\u4e86\u63d0\u53d6\u7684\u56fe\u7247\u6570\u91cf\uff0c\u8fc7\u9ad8\u7684\u5e27\u7387\u53ef\u80fd\u4f1a\u5bfc\u81f4\u751f\u6210\u5927\u91cf\u56fe\u7247\uff0c\u5360\u7528\u8f83\u591a\u5b58\u50a8\u7a7a\u95f4\u3002\u5206\u8fa8\u7387\u5219\u5f71\u54cd\u56fe\u7247\u7684\u8d28\u91cf\uff0c\u5efa\u8bae\u6839\u636e\u5b9e\u9645\u9700\u6c42\u8c03\u6574\u8fd9\u4e24\u4e2a\u53c2\u6570\uff0c\u4ee5\u4fdd\u8bc1\u5904\u7406\u6548\u7387\u548c\u7ed3\u679c\u8d28\u91cf\u3002<\/p>\n<p><strong>\u63d0\u53d6\u89c6\u9891\u5e27\u7684\u4fdd\u5b58\u683c\u5f0f\u6709\u54ea\u4e9b\u9009\u62e9\uff1f<\/strong><br \/>\u5728\u4f7f\u7528Python\u63d0\u53d6\u89c6\u9891\u5e27\u65f6\uff0c\u53ef\u4ee5\u9009\u62e9\u591a\u79cd\u56fe\u7247\u683c\u5f0f\u6765\u4fdd\u5b58\uff0c\u5982JPEG\u3001PNG\u3001BMP\u7b49\u3002\u6bcf\u79cd\u683c\u5f0f\u6709\u5176\u7279\u5b9a\u7684\u7279\u70b9\uff0c\u4f8b\u5982\uff0cJPEG\u9002\u5408\u5b58\u50a8\u9ad8\u8d28\u91cf\u7684\u7167\u7247\uff0c\u800cPNG\u9002\u5408\u9700\u8981\u900f\u660e\u80cc\u666f\u7684\u56fe\u50cf\u3002\u6839\u636e\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u683c\u5f0f\uff0c\u4ee5\u5e73\u8861\u56fe\u50cf\u8d28\u91cf\u4e0e\u6587\u4ef6\u5927\u5c0f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u5c06\u89c6\u9891\u5904\u7406\u6210\u56fe\u7247\u7684\u6b65\u9aa4\u6709\uff1a\u4f7f\u7528OpenCV\u5e93\u3001\u8bfb\u53d6\u89c6\u9891\u5e27\u3001\u4fdd\u5b58\u5e27\u4e3a\u56fe\u50cf\u6587\u4ef6\u3001\u8c03\u6574\u5e27\u7387\u3002 \u5176\u4e2d\uff0c\u4f7f\u7528 [&hellip;]","protected":false},"author":3,"featured_media":1069758,"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\/1069748"}],"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=1069748"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1069748\/revisions"}],"predecessor-version":[{"id":1069761,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1069748\/revisions\/1069761"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1069758"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1069748"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1069748"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1069748"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}