<jupyter_start><jupyter_text>Python Packages<jupyter_code>import cv2
import sys, os
import glob
import matplotlib.pyplot as plt
import imageio
import numpy as np
# Ref: https://www.geeksforgeeks.org/python-os-chdir-method/
# Ref: https://stackoverflow.com/questions/9234560/find-all-csv-files-in-a-directory-using-python/12280052 
#os.chdir(r"C:\Users\Gfg\Desktop\geeks")
#print("Directory changed")

# This is the path where you want to search
#path = '/content/drive/MyDrive/American_University/2021_Fall/DATA-793-001_Data Science Practicum/Datasets/manipulated_sequences/Deepfakes/raw/videos'

# this is the extension you want to detect
extension = '.mp4'

def get_list_filenames(cwd, path, extension):
    try:
        os.chdir(path)
        print("Successfully change the directory!")
        filenames = glob.glob('*.{}'.format(extension))
        print("Number of files: ", len(filenames))

    # Caching the exception
    except: 
        print("Something wrong with specified directory. Exception- ", sys.exc_info()) 

    # handling with finally: restore the path which is the current directory before changing directory           
    finally: 
        print("Restoring the path") 
        os.chdir(cwd) # Change it back to cwd (original directory)
        print("Current directory is-", os.getcwd()) 
        
    return filenames


# Ref: https://realpython.com/python-keras-text-classification/

# Get a list of dataframe after reading all csv files given a path
def get_all_image_files_read(path, filenames):
    list_img = []
    for file in filenames:
        img = imageio.imread(path+file)
        list_img.append(img)
    return list_img
%cd ./drive/MyDrive/American_University/2021_Fall/DATA-793-001_Data Science Practicum/Datasets/manipulated_sequences/Deepfakes/raw/videos
!pwd
# initial directory 
cwd = os.getcwd()
path_org = './' # the directory where all the output result are located.
extension = 'mp4'

list_Deepfake_filenames = get_list_filenames(cwd, path_org, extension)
print(list_Deepfake_filenames[0])
print(len(list_Deepfake_filenames))<jupyter_output>0<jupyter_text>Extract up to 7 frames from 1000 DeepFake videos<jupyter_code># Ref: https://www.geeksforgeeks.org/extract-images-from-video-in-python/
for i in range(len(list_Deepfake_filenames)):
  cam = cv2.VideoCapture(list_Deepfake_filenames[i])
  try:   
      # creating a folder named images
      if not os.path.exists('data'):
          os.makedirs('data')
  # if not created then raise error
  except OSError:
      print ('Error: Creating directory of images data')

  count = 0  # count the number of pictures
  frame_interval = 30  # video frame count interval frequency, the paper mentioned 7 frames per video
  frame_interval_count = 0
  currentframe = 1 # start with frame 1

  while(True):
    # reading from frame
    ret,frame = cam.read()
    if ret:
      if frame_interval_count % frame_interval == 0 and currentframe <= 7:
      # if video is still left continue creating images, extract up to 7 imgs /per video
        name = './data/Deepfake_video' + str(i) + "_" + str(currentframe) + '.jpg'
        print ('Creating...' + name)
        frame_interval_count += 1
      # writing the extracted images
        cv2.imwrite(name, frame)
      # increasing counter so that it will show how many frames are created
        currentframe += 1
      else:
        frame_interval_count += 1
    else:
      break
  
# Release all space and windows once done
cam.release()
cv2.destroyAllWindows()<jupyter_output>[1;30;43mStreaming output truncated to the last 5000 lines.[0m
Creating..../data/Deepfake_video285_6.jpg
Creating..../data/Deepfake_video285_7.jpg
Creating..../data/Deepfake_video286_1.jpg
Creating..../data/Deepfake_video286_2.jpg
Creating..../data/Deepfake_video286_3.jpg
Creating..../data/Deepfake_video286_4.jpg
Creating..../data/Deepfake_video286_5.jpg
Creating..../data/Deepfake_video286_6.jpg
Creating..../data/Deepfake_video286_7.jpg
Creating..../data/Deepfake_video287_1.jpg
Creating..../data/Deepfake_video287_2.jpg
Creating..../data/Deepfake_video287_3.jpg
Creating..../data/Deepfake_video287_4.jpg
Creating..../data/Deepfake_video287_5.jpg
Creating..../data/Deepfake_video287_6.jpg
Creating..../data/Deepfake_video287_7.jpg
Creating..../data/Deepfake_video288_1.jpg
Creating..../data/Deepfake_video288_2.jpg
Creating..../data/Deepfake_video288_3.jpg
Creating..../data/Deepfake_video288_4.jpg
Creating..../data/Deepfake_video288_5.jpg
Creating..../data/Deepfake_video288_6.jpg
Creating...[...]<jupyter_text>Extract up to 7 frames from 1000 REAL videos<jupyter_code>%cd ./drive/MyDrive/American_University/2021_Fall/DATA-793-001_Data Science Practicum/Datasets/original_sequences/youtube/raw/videos
!pwd
# initial directory 
cwd = os.getcwd()
path_org = './' # the directory where all the output result are located.
extension = 'mp4'

list_real_filenames = get_list_filenames(cwd, path_org, extension)
print(list_real_filenames[0])
# Ref: https://www.geeksforgeeks.org/extract-images-from-video-in-python/
for i in range(len(list_real_filenames)):
  cam = cv2.VideoCapture(list_real_filenames[i])
  try:   
      # creating a folder named images
      if not os.path.exists('data'):
          os.makedirs('data')
  # if not created then raise error
  except OSError:
      print ('Error: Creating directory of images data')

  count = 0  # count the number of pictures
  frame_interval = 30  # video frame count interval frequency, the paper mentioned 7 frames per video
  frame_interval_count = 0
  currentframe = 1 # start with frame 1

  while(True):
    # reading from frame
    ret,frame = cam.read()
    if ret:
      if frame_interval_count % frame_interval == 0 and currentframe <= 7:
      # if video is still left continue creating images, extract up to 7 imgs /per video
        name = './data/real_video' + str(i) + "_" + str(currentframe) + '.jpg'
        print ('Creating...' + name)
        frame_interval_count += 1
      # writing the extracted images
        cv2.imwrite(name, frame)
      # increasing counter so that it will show how many frames are created
        currentframe += 1
      else:
        frame_interval_count += 1
    else:
      break
  
# Release all space and windows once done
cam.release()
cv2.destroyAllWindows()<jupyter_output><empty_output><jupyter_text>Count the number of DeepFake and REAL imagesNow, we capture 7000 images from 1000 Deepfake videos, and capture 7000 images from 1000 original videos.<jupyter_code># Deepfake
img_folder_path = '/content/drive/MyDrive/American_University/2021_Fall/DATA-793-001_Data Science Practicum/Datasets/manipulated_sequences/Deepfakes/raw/videos/data'
dirListing = os.listdir(img_folder_path)
print(len(dirListing))
# REAL
img_folder_path = '/content/drive/MyDrive/American_University/2021_Fall/DATA-793-001_Data Science Practicum/Datasets/original_sequences/youtube/raw/videos/data'
dirListing = os.listdir(img_folder_path)
print(len(dirListing))<jupyter_output>7000<jupyter_text>Testing Zone<jupyter_code>"""
# Ref: https://www.geeksforgeeks.org/extract-images-from-video-in-python/
for i in range(len(list_filenames)):
  cam = cv2.VideoCapture(list_filenames[i])
  try:   
      # creating a folder named images
      if not os.path.exists('data'):
          os.makedirs('data')
  # if not created then raise error
  except OSError:
      print ('Error: Creating directory of images data')

  count = 0  # count the number of pictures
  frame_interval = 50  # video frame count interval frequency, the paper mentioned 7 frames per video
  frame_interval_count = 0
  currentframe = 1 # start with frame 1

  while(True):
    # reading from frame
    ret,frame = cam.read()
    if ret:
      if frame_interval_count % frame_interval == 0:
      # if video is still left continue creating images
        name = './data/frame' + str(i) + "_" + str(currentframe) + '.jpg'
        print ('Creating...' + name)
        frame_interval_count += 1
      # writing the extracted images
        cv2.imwrite(name, frame)
      # increasing counter so that it will show how many frames are created
        currentframe += 1
      else:
        frame_interval_count += 1
    else:
      break
  
# Release all space and windows once done
cam.release()
cv2.destroyAllWindows()
"""
"""
# Ref: https://www.geeksforgeeks.org/extract-images-from-video-in-python/
for i in range(len(list_real_filenames)):
  cam = cv2.VideoCapture(list_real_filenames[i])
  try:   
      # creating a folder named images
      if not os.path.exists('data'):
          os.makedirs('data')
  # if not created then raise error
  except OSError:
      print ('Error: Creating directory of images data')

  count = 0  # count the number of pictures
  frame_interval = 50  # video frame count interval frequency, the paper mentioned 7 frames per video
  frame_interval_count = 0
  currentframe = 1 # start with frame 1

  while(True):
    # reading from frame
    ret,frame = cam.read()
    if ret:
      if frame_interval_count % frame_interval == 0:
      # if video is still left continue creating images
        name = './data/frame' + str(i) + "_" + str(currentframe) + '.jpg'
        print ('Creating...' + name)
        frame_interval_count += 1
      # writing the extracted images
        cv2.imwrite(name, frame)
      # increasing counter so that it will show how many frames are created
        currentframe += 1
      else:
        frame_interval_count += 1
    else:
      break
  
# Release all space and windows once done
cam.release()
cv2.destroyAllWindows()
"""
"""
# Ref: https://www.geeksforgeeks.org/extract-images-from-video-in-python/
cam = cv2.VideoCapture(list_filenames[0])
try:   
    # creating a folder named images
    if not os.path.exists('data'):
        os.makedirs('data')
# if not created then raise error
except OSError:
    print ('Error: Creating directory of images data')
# start with frame 1
currentframe = 1
while(True):
  # reading from frame
  ret,frame = cam.read()
  if ret:
  # if video is still left continue creating images
    name = './data/frame' + str(currentframe) + '.jpg'
    print ('Creating...' + name)

  # writing the extracted images
    cv2.imwrite(name, frame)
  # increasing counter so that it will show how many frames are created
    currentframe += 1
  else:
    break
  
# Release all space and windows once done
cam.release()
cv2.destroyAllWindows()
"""
# print(list_filenames[999])<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10523294747919258, 'fraction_numerical': 0.025829905290347267, 'mean_word_length': 3.4982788296041307, 'pattern_counts': {'":': 0, '<': 20, '<?xml version=': 0, '>': 18, 'https://': 8, 'lorem ipsum': 0, 'www.': 6, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '171924', 'license': 'MIT', 'n_tokens_mistral': 3482, 'n_tokens_neox': 3227, 'n_words': 887, 'path': 'code/01.capture_videos_to_images.ipynb', 'repo_name': 'twyunting/Deepfake_Video_Classifier', 'size': '10453'}