2017-09-22 20 views
0

私はちょっとした問題に直面していますが、 実際に私は自分のコードに提供したビデオからたくさんのフレームを生成して特定のフォルダに保存する機能を持っていますRND(ランダム)コマンドを使用してそのフォルダからフレームを取得すると、無作為に選択したランダムなフレームは、他のフォルダに保存する必要があります。たとえば、300フレームあり、15フレームランダムこれらの300フレームは、これらの15フレームも別の別のフォルダに保存する必要があります。 これは、あなたが出力ディレクトリに作業ディレクトリを変更するos.chdir('output')を使用して、イメージを書き込むことができます私のコード、ランダムな画像をPythonの別のフォルダに保存する

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 

答えて

0

です。その後、元のディレクトリに戻り、ランダムな画像を選択するには、os.chdir('../')を使用します。

+0

実際には動作するはずですが、私はそれを推奨しません。 Pythonには、この種の問題を正確に回避するために、パス管理 'os.path'のための巨大なライブラリが付属しています。 'os.chdir'は特定の問題のためには必要かもしれませんが、イメージの完全なパスを指定することはその一つではありません。 –

+0

実際には私はPythonに新しいとあなたがそれを実装する方法を言った理解することはできません、あなたは私のコードであなたが言ったとそれを実装する方法を追加してください –