import os
import numpy as np
from keras import backend as K
from scipy import ndimage 
from scipy.misc import imresize
import skvideo.io
import dlib

'''
class Videoaugmenter(object):
	@staticmethod
	def split_words(video,align):
	



















	@staticmethod
	def merge(video,aligns):














	@staticmethod
	def pick_subsentence(video,align,length):








	@staticmethod
	def pick_word(video,align():





	@staticmethod
	def horizontal_flip(video):

	




	@staticmethod
	def temporal_jitter(video,probability):






	@staticmethod
	def pad(video,length):





'''


class Video(object):


	def __init__(self,vtype='mouth',face_predictor_path=None):
		if vtype == 'face' and face_predictor_path is None:
			raise AttributeError('bhai face_predictor path compulsory hai mein hi kar detat hu initialize tere liye')
		self.face_predictor_path = face_predictor_path
		self.vtype=vtype

	

	def from_frames(self,path):
		frames_path = sorted([os.path.join(path,x) for x in os.listdir(path)])
		frames = [ndimage.imread(frame_path) for frame_path in frames_path]
		self.handle_type(frames)
		return self


	def from_video(self,path):
	
		frames = self.get_video_frames(path)
		self.handle_type(frames)
		return self

	
	def from_array(self,frames):
		self.handle_type(frames)
		return self


	def handle_type(self,frames):
		if self.vtype == "mouth":
			self.process_frames_mouth(mouth)

		elif self.vtype== 'face':
			self.process_frames_face(frames)
		else:
			raise Exception('Video type not found')



	def process_frames_face(self, frames):

		detector =dlib.get_frontal_face_detector()
		predictor= dlib.shape_predictor(self.face_predictor_path)
		mouth_frames = self.get_frames_mouth(detector,predictor,frames)
		self.face = np.array(frames)
		self.mouth = np.array(mouth_frames)
		self.set_data(mouth_frames)


	def process_frames_mouth(self, frames):
		self.face = np.array(frames)
		self.mouth = np.array(frames)
		self.set_data(mouth_frames)


	def get_frames_mouth(self,detector,predictor, frames):
		MOUTH_WIDTH = 100
		MOUTH_HEIGHT =  50
		HORIZONTAL_PAD = 0.19
		
		normalize_ratio = None
		mouth_frames = []
		for frame in frames:
			dets = detector(frame,1)
			shape = None
			for k,d in enumerate(dets):
				shape = predictor(frame,d)
				i=-1

			if shape is None: # if the detector does not detect a face shape change nai hoga 
				return frames
			mouth_points= []

			for part in shape.parts():
				i+=1
				if i<48: # to consider onlyy mouth region
					continue

				mouth_points.append((part.x,part.y))
			np_mouth_points = np.array(mouth_points)

			mouth_centroid = np.mean(np_mouth_points[:,-2:],axis=0)
		
		#	mouth_centroid = np.mean(np_mouth_points[:,-2],axis=0)

 
			if normalize_ratio is None:
				mouth_left =np.min(np_mouth_points[:,:-1])*(1.0 -  HORIZONTAL_PAD)
				mouth_right =np.max(np_mouth_points[:,:-1])*(1.0 + HORIZONTAL_PAD)
			
				normalize_ratio = MOUTH_WIDTH / float(mouth_right - mouth_left)
							

			new_img_shape = (int(frame.shape[0]* normalize_ratio), int (frame.shape[1]*normalize_ratio))

			resized_img = imresize(frame,new_img_shape)
			
			mouth_centroid_norm =mouth_centroid*normalize_ratio


			mouth_l = int(mouth_centroid_norm[0]-MOUTH_WIDTH/2 )
			mouth_r = int(mouth_centroid_norm[0]+MOUTH_WIDTH/2 )	
			mouth_t = int(mouth_centroid_norm[1]-MOUTH_HEIGHT/2 )
			mouth_b = int(mouth_centroid_norm[1]+MOUTH_HEIGHT/2 )

			mouth_crop_image = resized_img[mouth_t:mouth_b, mouth_l:mouth_r]

			mouth_frames.append(mouth_crop_image)

		return mouth_frames


	def get_video_frames(self, path):
		videogen = skvideo.io.vreader(path)
		frames = np.array([frame for frame in videogen])
		return frames



	
	def set_data(self, frames):
		data_frames = []
		for frame in frames:
			frame = frame.swapaxes(0,1) #swap width and height to form W xH xC
			if len(frame.shape) < 3:
				frame = np.array([frame]).swapaxes(0,2).swapaxes(0,1) #addgrayscale channel
			data_frames.append(frame)
		frames_n = len(data_frames)
		data_frames = np.array(data_frames) #T xW xH C
		
		if K.image_data_format() == 'channels_first':
			data_Frames = np.rollaxis(data_frames, 3) #C x T x W x H
		self.data = data_frames
		self.length = frames_n
	


	













