【问题标题】:Saving images in different folders using opencv使用opencv将图像保存在不同的文件夹中
【发布时间】:2021-08-14 08:13:20
【问题描述】:

我一直在尝试将网络摄像头中的图像保存在不同的文件夹中,但它不起作用。我试图实现的是我试图从我的网络摄像头中为每个文件夹保存 5 张图像,但它不起作用。有人可以帮忙吗?谢谢 PS这是我的代码

def createFolder(directory):
    try:
        if not os.path.exists(directory):
            os.makedirs(directory)
    except OSError:
        print ('Error: Creating directory. ' +  directory)

import os 
import cv2 
import time 

video_capture = cv2.VideoCapture(0)
counter = 0
while(video_capture.isOpened()):
    location = f'D:/DATA_SCIENCE/anand_fabrics/{counter}'
    ret, frame = video_capture.read()
    cv2.imwrite(os.path.join(location , f"frame{counter}.jpg"), frame)
    if counter % 5 == 0:
        createFolder(f'D:/DATA_SCIENCE/anand_fabrics/{counter}')
    counter = counter + 1 

【问题讨论】:

    标签: image opencv computer-vision webcam


    【解决方案1】:

    您是说每 5 张图片切换一次文件夹,但您更改了每张图片的路径:

    while(video_capture.isOpened()):
        location = f'D:/DATA_SCIENCE/anand_fabrics/{counter}'
        ...
        counter = counter + 1 
    

    所以基本上你正在尝试写入一个不存在的路径。

    试试这个:

    video_capture = cv2.VideoCapture(0)
    counter = 0
    while(video_capture.isOpened()):
        if counter % 5 == 0:
            location = f'D:/DATA_SCIENCE/anand_fabrics/{counter}'
            createFolder(loaction)
        ret, frame = video_capture.read()
        cv2.imwrite(os.path.join(location , f"frame{counter}.jpg"), frame)
        counter = counter + 1 
    

    【讨论】:

    • 太棒了!查看更新。如果你喜欢它,请分享,喜欢和订阅:P
    猜你喜欢
    • 1970-01-01
    • 2022-06-27
    • 2017-05-25
    • 1970-01-01
    • 2018-05-26
    • 2021-07-15
    • 1970-01-01
    • 2013-06-23
    • 2015-12-21
    相关资源
    最近更新 更多