0

我面临一个小问题,实际上我有一个函数可以从我提供给我的代码的视频中生成这么多帧,然后保存在特定文件夹中,然后我使用 RND(随机)命令从该文件夹中访问一些帧,现在我想要的是我的代码随机选择的那些随机帧也应该保存在其他文件夹中,例如,如果有 300 帧并且我的代码从这 300 帧中随机选择 15 帧,那么这 15 帧也必须保存在一些其他单独的文件夹。这是我的代码,

def video_frames(nameof_video,frame_savelocation,cropimages_save):
    vidcap = cv2.VideoCapture(nameof_video)
    success, image = vidcap.read()
    print(success)

    count = 1
    success = True
    while success:
        success, image = vidcap.read()
        cv2.imwrite(frame_savelocation+"/%d.jpg" % count, image)
        # save frame as JPEG file
        # if cv2.waitKey(10) == 27:                     # exit if Escape is hit
        #   break
        if count == 0:
            break
        count += 1
    print("frames saved in the desired location!!!!!")
        ##### crop faces from frame and save them----###
    for r in range(1, 15):
        random_imagecrop(frame_savelocation, cropimages_save)  #-----> function called
    return

def random_imagecrop(frame_savelocation,cropimages_save):
    #b=1
    crop_faces_path = frame_savelocation
    list_frames = os.listdir(crop_faces_path)  # dir is your directory path
    number_files = len(list_frames)

    rnd = random.randint(1, number_files)
    print("random:", rnd)
    image = face_recognition.load_image_file(frame_savelocation + "/" + str(rnd) + ".jpg")
    #pil_image.save("Datasets/randomimg" + "/" + str(b) + ".jpg")
    #b=b+1
    # Find all the faces in the image
    face_locations = face_recognition.face_locations(image)



    check = os.listdir(cropimages_save)  # dir is your directory path
    already_prsntimg = len(check)

    a = 1+already_prsntimg
    for face_location in face_locations:
    # Print the location of each face in this image
        top, right, bottom, left = face_location
        # print("A face is located at pixel location Top: {}, Left: {}, Bottom: {}, Right: {}".format(top, left, bottom, right))

         # You can access the actual face itself like this:
        face_image = image[top:bottom, left:right]
        pil_image = Image.fromarray(face_image)
         # pil_image.show()
        pil_image.save(cropimages_save + "/" + str(a) + ".jpg")
        a = a + 1
    print("--> Images saved successfully...!!!")
        # a=a+len(face_locations)
    return
4

1 回答 1

0

您可以使用os.chdir('output')将工作目录更改为输出目录,然后写入图像。然后回到原来的目录去挑选你随机使用的图片os.chdir('../')

于 2017-09-22T07:18:10.903 回答