为什么我不能使用cv2.imwrite保存图像

时间:2020-05-16 13:59:50

标签: opencv

我用了大约2个月的代码将视频数据集转换为图像,并且现在我正在尝试使用它,但是它运行正常,但是没有错误并打印输出和图像路径,但是当我打开此路径时,什么都没发现

在这段代码中,我将每个视频转换为7张图像:

import os
import cv2

def frame_capture2(filepath,subdir):

    # Opens the Video file
    cap= cv2.VideoCapture(filepath)

    # Make a directory to save in the frames in each folder you enter
    path = subdir + "/data"

    try:
        if not os.path.exists('path'):
            os.mkdir(path)
    except OSError:
            print ("Creation of the directory %s failed" % path)
    else:
            print ("Successfully created the directory %s " % path)

    # define the name of the directory to be created



    #Print the No. of frames In the video submited
    video_length = int(cap.get(cv2.CAP_PROP_FRAME_COUNT)) - 1
    print ("Number of frames: ", video_length)

    sec = 0
   # cv2.CAP_PROP_FPS = 2
   # print("fps",cv2.CAP_PROP_FPS)
   # cv2.CAP_PROP_FPS = 2
    #FPS = 5
   # cap.set(cv2.CAP_PROP_FPS,2)

    #loop through the videos
    i=0
    frameRate = 0.5

    while(cap.isOpened()):

        cap.set(cv2.CAP_PROP_POS_MSEC,sec*1000)

        ret, frame = cap.read()
        print(ret)
       #print ('Read a new frame: ', ret)
        if ret == False:
            break
        #if sec == 3:
          #  break

   # Saves image of the current frame in jpg format (in this path (path +'/'+ Path(filepath).resolve().stem))
        name = path +'/'+ Path(filepath).resolve().stem +" _ "+ str(i) + " "+'.jpg'
        i+=1
        print ('Creating...' + name)
        cv2.imwrite(name, frame)
        if sec == 3 or sec > 3:
            break
        sec = sec + frameRate

    cap.release()
    cv2.destroyAllWindows()

在这里我调用我的函数:

import os
from pathlib import Path

for subdir, dirs, files in os.walk(r"C:\Users\win10\Desktop\data_sets\hh"):
    for filename in files:
        filepaath = subdir + os.sep + filename
        if filepaath.endswith(".mp4"):
            print(filepaath)
            frame_capture2(filepaath,subdir)

这是我得到的输出:

C:\Users\win10\Desktop\data_sets\hh\data\try\V_5.mp4
Successfully created the directory C:\Users\win10\Desktop\data_sets\hh\data\try/data 
Number of frames:  113
True
Creating...C:\Users\win10\Desktop\data_sets\hh\data\try/data/V_5 _ 0 .jpg
True
Creating...C:\Users\win10\Desktop\data_sets\hh\data\try/data/V_5 _ 1 .jpg
True
Creating...C:\Users\win10\Desktop\data_sets\hh\data\try/data/V_5 _ 2 .jpg
True
Creating...C:\Users\win10\Desktop\data_sets\hh\data\try/data/V_5 _ 3 .jpg
True
Creating...C:\Users\win10\Desktop\data_sets\hh\data\try/data/V_5 _ 4 .jpg
True
Creating...C:\Users\win10\Desktop\data_sets\hh\data\try/data/V_5 _ 5 .jpg
True
Creating...C:\Users\win10\Desktop\data_sets\hh\data\try/data/V_5 _ 6 .jpg

在此路径中没有任何内容

0 个答案:

没有答案