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

发布于 2025-01-13 19:17:39 字数 491 浏览 1 评论 0原文

我正在使用下面的代码从网络摄像头捕获图像。但我只需要点击时捕获一些图像。

 
# 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()```

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()```

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

笔芯 2025-01-20 19:17:39

假设您想要 500 张图像,请添加以下内容:

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

Assuming you want 500 images add this:

...
    i+=1
    if (i+1)%500==0:
        break
不交电费瞎发啥光 2025-01-20 19:17:39

那很容易。您可以使用 k = cv2.waitKey(1) 并检查按下了哪个按钮。这是一个简单的例子:

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()

that would be easy. you can use k = cv2.waitKey(1) and check what button was pressed. 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()

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文