简体   繁体   English

如何通过 opencv python 中的网络摄像头从视频中捕获 500 张图像?

[英]How to capture a 500 images from a video through webcam in opencv python?

I'm using the below code to capture images from webcam.我正在使用以下代码从网络摄像头捕获图像。 But i need only some no.of images to be captured on click.但我只需要在点击时捕获一些图像。

 
# Opens the inbuilt camera of laptop to capture video.
cap = cv2.VideoCapture(0)
i = 0
 
while(cap.isOpened()):
    ret, frame = cap.read()
     
    # This condition prevents from infinite looping
    # incase video ends.
    if ret == False:
        break
     
    # Save Frame by Frame into disk using imwrite method
    cv2.imwrite('Frame'+str(i)+'.jpg', frame)
    i += 1
 
cap.release()
cv2.destroyAllWindows()```

Assuming you want 500 images add this:假设你想要 500 张图片添加:

...
    i+=1
    if (i+1)%500==0:
        break

that would be easy.那很容易。 you can use k = cv2.waitKey(1) and check what button was pressed.您可以使用k = cv2.waitKey(1)并检查按下了哪个按钮。 here is a simple example:这是一个简单的例子:

import cv2


def main():
    cap = cv2.VideoCapture(0)
    if not cap.isOpened():  # Check if the web cam is opened correctly
        print("failed to open cam")
        return -1
    else:
        print('webcam open')

    for i in range(10 ** 10):
        success, cv_frame = cap.read()
        if not success:
            print('failed to capture frame on iter {}'.format(i))
            break
        cv2.imshow('click t to save image and q to finish', cv_frame)
        k = cv2.waitKey(1)
        if k == ord('q'):
            print('q was pressed - finishing...')
            break
        elif k == ord('t'):
            print('t was pressed - saving image {}...'.format(i))
            image_path = 'Frame_{}.jpg'.format(i)  # i recommend a folder and not to save locally to avoid the mess
            cv2.imwrite(image_path, cv_frame)

    cap.release()
    cv2.destroyAllWindows()
    return


if __name__ == '__main__':
    main()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python,OpenCV:从网络摄像头捕获图像 - Python, OpenCV : Capture Images from WebCam 如何加快 opencv 中网络摄像头的视频捕获速度? - how to speed up video capture from webcam in opencv? Python:如何使用 OpenCV 在单击时从网络摄像头捕获图像 - Python: how to capture image from webcam on click using OpenCV 来自网络摄像头的Python / OpenCV捕获图像不起作用 - Python/OpenCV capture image from webcam not working 如何用 python 从相机(或网络摄像头)捕获视频(和音频) - How to capture a video (AND audio) in python, from a camera (or webcam) 我正在尝试使用 opencv_python 从我的网络摄像头捕获视频 - I am trying to capture video from my webcam using opencv_python 如何使用 OpenCV 在单击或按键盘上的任意键时从网络摄像头捕获视频并保存 - How to capture video and save from webcam on click or pressing any key from keyboard using OpenCV 如何在Python中使用OpenCV存储网络摄像头视频 - How to store webcam video with OpenCV in Python 如何在20秒后从python中的网络摄像头视频开始捕获视频帧 - How to start capture video frame after 20sec from webcam video in python OpenCV和Python:如果源不是来自网络摄像头,则视频输出为空 - OpenCV and Python: Video output is empty if the source is not coming from the webcam
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM