Python Forum
extract frame from video error
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
extract frame from video error
#1
Sad 
Hi and greetings , I am new to this forum. Who here really needs help from a python expert. I am a student and am currently doing a final year project. so my project is to detect deepfake or real video. So for this project I use google colab to run my python code. I want to extract frame from video and run and no error comes out but the coding is often repeated in the "ret part, frame = cap.read()" and "face_rects, scores, idx = detector.run(frame, 0)". waited for hours but no output came out. can you help me ? what am I doing wrong here?. Below is attachment from coding in google colab that I try.

train_frame_folder = '/content/drive/MyDrive/train_sample_videos'
header_list = ["file","label"]
labels = pd.read_csv('/content/drive/MyDrive/Gobal_metadata.csv',names=header_list)

print(shape(labels))

list_of_train_data = [f for f in os.listdir(train_frame_folder) if f.endswith('.mp4')]
detector = dlib.get_frontal_face_detector()

for vid in list_of_train_data:
    #print(labels.iloc[(labels.loc[labels["file"] == vid].index.values[0]),1])
    count = 0
    cap = cv2.VideoCapture(os.path.join(train_frame_folder, vid))
    frameRate = cap.get(5)     
    while cap.isOpened():
        frameId = cap.get(1)  
        ret, frame = cap.read()
        if ret != True:
            break
        if frameId % ((int(frameRate)+1)*1) == 0:
            face_rects, scores, idx = detector.run(frame, 0)
            for i, d in enumerate(face_rects):
                x1 = d.left()
                y1 = d.top()
                x2 = d.right()
                y2 = d.bottom()
                crop_img = frame[y1:y2, x1:x2]
label = labels.iloc[(labels.loc[labels["file"] == vid].index.values[0]),1]
if(label == 'FAKE'):
                  #print(count)
                  os.chdir('/content/drive/MyDrive/train_sample_videos/fake')
                  cv2.imwrite(vid.split('.')[0]+'_'+str(count)+'.png', cv2.resize(crop_img, (224, 224)))
if(label == 'REAL'):
                  os.chdir('/content/drive/MyDrive/train_sample_videos/real')
                  cv2.imwrite(vid.split('.')[0]+'_'+str(count)+'.png', cv2.resize(crop_img, (224, 224)))
count+=1

Attached Files

Thumbnail(s)
       
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020