# Path: util/data_preprocessing.py
# WAV_FILE_MIN_LEN = 2.00 # in seconds
# WAV_FILE_MAX_LEN = 5.00 # in seconds
# def convert_mp4(video_dir, audio_dir):
# def read_json_file(file_path):
# def find_text_and_time_limits(alignment_data):
# def split(split_file_path, main_file_path, transcript_path, split_info):
# def split_aligned_audio(audio_dir, json_dir	, output_dir_train, output_dir_dev,
# 						output_dir_test, train_split, dev_split, test_split):
# def create_csv(data_dir):
# 
# Path: util/data_preprocessing_video.py
# FACE_DETECTOR_MODEL = None
# LANDMARKS_PREDICTOR = None
# IMAGE_WIDTH = 500 # Every frame will be resized to this width before any processing
# VIDEO_DIR = "./data/RHL_mp4/"
# AUDIO_DIR = "./data/RHL_wav/"
# JSON_DIR  = "./data/RHL_json/"
# AE_MODEL_DIR = "./data/AE_and_RBM_model_saves/"
# AE_LAYER_NAMES = [['gbrbm_1_w', 'gbrbm_1_h'], # Layer names will help us in restoring trained Autoencoder
# 				  ['bbrbm_1_w', 'bbrbm_1_h'],
# 				  ['bbrbm_2_w', 'bbrbm_2_h'], 
# 				  ['bgrbm_1_w', 'bgrbm_1_h']] 
# AUTO_ENCODER = None
# FIXED_VIDEO_FPS = 30.0 # Videos with FPS == 30 will only be used.
# 	FACE_DETECTOR_MODEL = dlib.get_frontal_face_detector()
# 	LANDMARKS_PREDICTOR = dlib.shape_predictor("data/dlib_data/shape_predictor_68_face_landmarks.dat")
# 	AUTO_ENCODER = AutoEncoder(
# 					32*32, 
# 					encoding_layer_sizes=[2000, 1000, 500, 50], 
# 					layer_names=AE_LAYER_NAMES)
# 				MSD = ((mouth_roi_1 - mouth_roi_2)**2).mean()
# 	FPS = stream.stream.get(cv2.CAP_PROP_FPS)
# 	FPS = stream.stream.get(cv2.CAP_PROP_FPS)
# def load_trained_models():
# def load_AE():
# def crop_suitable_face(rects, frame, prev_frame_faces=None):
# def validate_frames(all_frames):
# def run_video_and_refine(video_file_path, split_info):
# def encode_and_store(batch_x, output_dir, file_name):
# def preprocess_videos(output_dir_train, output_dir_dev, output_dir_test, 
# 					  train_split, dev_split, test_split):
# def extract_and_store_visual_features(video_file_path, json_dir, json_name):


import os
import argparse
import sys
from util import data_preprocessing
from util import data_preprocessing_video
audio_dir = "data/RHL_wav/"

# Percentages/ratios should add up to 1.0 
if args.train_split + args.dev_split + args.test_split != 1.0:
	print "Make sure that train, test, and dev split ratios add upto 1.0"
	exit()

# Convert any mp4 found in data/RHL_mp4/ and store it in data/RHL_wav/
data_preprocessing.convert_mp4(video_dir, audio_dir)
print "[INFO]: Main video files converted to .wav files."

# Generates data for audio-visual speech recognition
# Visual encodings are stored as JSON files and audio data as .wav files
status = data_preprocessing_video.preprocess_videos(
	args.output_dir_train,
	args.output_dir_dev,
	args.output_dir_test,
	args.train_split,
	args.dev_split,
	args.test_split)

if not status:
	print "[ERROR] Make sure that you have atleast one video file with correct FPS" \
		  + " / or which is trainable for AVSR."
	exit()

print "[INFO]: Train/Dev/Test files generated. Now creating CSVs."

# Create CSVs out of all split files.
data_preprocessing.create_csv(args.output_dir_train)

# Path: util/data_preprocessing.py
# WAV_FILE_MIN_LEN = 2.00 # in seconds
# WAV_FILE_MAX_LEN = 5.00 # in seconds
# def convert_mp4(video_dir, audio_dir):
# def read_json_file(file_path):
# def find_text_and_time_limits(alignment_data):
# def split(split_file_path, main_file_path, transcript_path, split_info):
# def split_aligned_audio(audio_dir, json_dir	, output_dir_train, output_dir_dev,
# 						output_dir_test, train_split, dev_split, test_split):
# def create_csv(data_dir):
# 
# Path: util/data_preprocessing_video.py
# FACE_DETECTOR_MODEL = None
# LANDMARKS_PREDICTOR = None
# IMAGE_WIDTH = 500 # Every frame will be resized to this width before any processing
# VIDEO_DIR = "./data/RHL_mp4/"
# AUDIO_DIR = "./data/RHL_wav/"
# JSON_DIR  = "./data/RHL_json/"
# AE_MODEL_DIR = "./data/AE_and_RBM_model_saves/"
# AE_LAYER_NAMES = [['gbrbm_1_w', 'gbrbm_1_h'], # Layer names will help us in restoring trained Autoencoder
# 				  ['bbrbm_1_w', 'bbrbm_1_h'],
# 				  ['bbrbm_2_w', 'bbrbm_2_h'], 
# 				  ['bgrbm_1_w', 'bgrbm_1_h']] 
# AUTO_ENCODER = None
# FIXED_VIDEO_FPS = 30.0 # Videos with FPS == 30 will only be used.
# 	FACE_DETECTOR_MODEL = dlib.get_frontal_face_detector()
# 	LANDMARKS_PREDICTOR = dlib.shape_predictor("data/dlib_data/shape_predictor_68_face_landmarks.dat")
# 	AUTO_ENCODER = AutoEncoder(
# 					32*32, 
# 					encoding_layer_sizes=[2000, 1000, 500, 50], 
# 					layer_names=AE_LAYER_NAMES)
# 				MSD = ((mouth_roi_1 - mouth_roi_2)**2).mean()
# 	FPS = stream.stream.get(cv2.CAP_PROP_FPS)
# 	FPS = stream.stream.get(cv2.CAP_PROP_FPS)
# def load_trained_models():
# def load_AE():
# def crop_suitable_face(rects, frame, prev_frame_faces=None):
# def validate_frames(all_frames):
# def run_video_and_refine(video_file_path, split_info):
# def encode_and_store(batch_x, output_dir, file_name):
# def preprocess_videos(output_dir_train, output_dir_dev, output_dir_test, 
# 					  train_split, dev_split, test_split):
# def extract_and_store_visual_features(video_file_path, json_dir, json_name):


# Path: bin/preprocess_audio_video.py
import os
import argparse
import sys
from util import data_preprocessing
from util import data_preprocessing_video

audio_dir = "data/RHL_wav/"

# Percentages/ratios should add up to 1.0 
if args.train_split + args.dev_split + args.test_split != 1.0:
	print "Make sure that train, test, and dev split ratios add upto 1.0"
	exit()

# Convert any mp4 found in data/RHL_mp4/ and store it in data/RHL_wav/
data_preprocessing.convert_mp4(video_dir, audio_dir)
print "[INFO]: Main video files converted to .wav files."

# Generates data for audio-visual speech recognition
# Visual encodings are stored as JSON files and audio data as .wav files
status = data_preprocessing_video.preprocess_videos(
	args.output_dir_train,
	args.output_dir_dev,
	args.output_dir_test,
	args.train_split,
	args.dev_split,
	args.test_split)

if not status:
	print "[ERROR] Make sure that you have atleast one video file with correct FPS" \
		  + " / or which is trainable for AVSR."
	exit()

print "[INFO]: Train/Dev/Test files generated. Now creating CSVs."

# Create CSVs out of all split files.
data_preprocessing.create_csv(args.output_dir_train)

data_preprocessing.create_csv(args.output_dir_dev)