# Path: src/DroneVision/DroneVision_src/imgProcessing/frameTools/frameTools.py
# def GetShape(frame):
# 	'''
# 	 @brief Get shape of frame, regardless of depth dimension.
# 
# 	 @param frame 
# 	 
# 	 return (height, width)
# 	'''
# 	return frame.shape[:2]
# 
# def CheckColor(frame):
# 	'''
# 	 @brief Check wether the frame is of grayscale or color.
# 	 	converts the frame to color if necessary.
# 
# 	 @param frame
# 	 
# 	 @return color frame.
# 	'''
# 	shape = frame.shape
# 	if len(shape) == 2:
# 		cl_frame = ToColor(frame)
# 	else:
# 		cl_frame = frame
# 	return cl_frame
# 
# Path: src/bin/tools.py
# def CheckDir(directory):
# 	'''
# 	 @brief Create directory
# 
# 	 @param directory Name of directory.
# 	'''
# 	if not(os.path.isdir(directory)):
# 		try:
# 			os.makedirs(directory)
# 		except Exception, err:
# 			warnings.simplefilter('always')
# 			warnings.warn(str(err), Warning)
# 			warnings.simplefilter('default')
# 
# def RunThread(func, args=(), daemon=True, lock=None, wait_lock=False, wait_timeout=0.5):
# 	'''
# 	 @brief Run function in separate thread.
# 
# 	 @param func Function to run.
# 	 @param args Input arguments (single argument: (arg1,) - or multiple: (arg1, arg2))
# 	 @param daemon (True/false) - run as daemon or not.
# 	 @param lock Mutex lock - wait for it to be available before starting thread.
# 	 @param wait_lock - Wait for mutex to be available, or raise exception immideately.
# 	 @param wait_timeout - Time to wait in seconds (if wait_lock==True)
# 
# 	 return t Threading object
# 	'''
# 	t = threading.Thread(target=func, args=args)
# 	t.daemon = daemon
# 	if lock == None:
# 		t.start()
# 	else:
# 		if wait_lock:
# 			timeout = timeit.default_timer()
# 			while True:
# 				if not(lock.locked()):
# 					t.start()
# 					break
# 				elif (timeit.default_timer() - timeout) > wait_timeout:
# 					raise Exception('Runtime error - lock not availble')
# 		else:
# 			if not(lock.locked()):
# 				t.start()
# 			else:
# 				raise Exception('Runtime error - lock not availble')
# 	return t


import cv2, threading
import numpy as np
from src.DroneVision.DroneVision_src.imgProcessing.frameTools.frameTools import GetShape, CheckColor
from imageTools import WriteImage
from src.bin.tools import CheckDir, RunThread
		'''
		 @brief Initialize recording by adapting the video to the first frame input.

		 @param initFrame
		'''
		self.__height, self.__width = GetShape(initFrame)
		self.StartRecording()

	def StartRecording(self):
		'''
		 @brief Start video recording
		 	Creates a folder for the video, called by the same video filename,
		 	and creates a subfolder for storing each frame.
		'''
		# Create frames folder
		if self.__store_frames_as_images:
			self.__frames_folder = self.__folder + self.__video_output_filename+'_frames/'
			CheckDir(self.__frames_folder)

		if self.__store_frames_as_video:
			# Define the codec and create VideoWriter object
			fourcc = cv2.VideoWriter_fourcc(*'MJPG')
			self.video_out = cv2.VideoWriter(filename=self.__folder + self.__video_output_filename+'.avi', fourcc=fourcc, fps=self.__fps, frameSize=(self.__width, self.__height), isColor=True)
		
		self.__frame_i = 0

	def WriteFrameThread(self, frame):
		'''
		 @brief Write frame in thread for smoother program flow.
		'''

# Path: src/DroneVision/DroneVision_src/imgProcessing/frameTools/frameTools.py
# def GetShape(frame):
# 	'''
# 	 @brief Get shape of frame, regardless of depth dimension.
# 
# 	 @param frame 
# 	 
# 	 return (height, width)
# 	'''
# 	return frame.shape[:2]
# 
# def CheckColor(frame):
# 	'''
# 	 @brief Check wether the frame is of grayscale or color.
# 	 	converts the frame to color if necessary.
# 
# 	 @param frame
# 	 
# 	 @return color frame.
# 	'''
# 	shape = frame.shape
# 	if len(shape) == 2:
# 		cl_frame = ToColor(frame)
# 	else:
# 		cl_frame = frame
# 	return cl_frame
# 
# Path: src/bin/tools.py
# def CheckDir(directory):
# 	'''
# 	 @brief Create directory
# 
# 	 @param directory Name of directory.
# 	'''
# 	if not(os.path.isdir(directory)):
# 		try:
# 			os.makedirs(directory)
# 		except Exception, err:
# 			warnings.simplefilter('always')
# 			warnings.warn(str(err), Warning)
# 			warnings.simplefilter('default')
# 
# def RunThread(func, args=(), daemon=True, lock=None, wait_lock=False, wait_timeout=0.5):
# 	'''
# 	 @brief Run function in separate thread.
# 
# 	 @param func Function to run.
# 	 @param args Input arguments (single argument: (arg1,) - or multiple: (arg1, arg2))
# 	 @param daemon (True/false) - run as daemon or not.
# 	 @param lock Mutex lock - wait for it to be available before starting thread.
# 	 @param wait_lock - Wait for mutex to be available, or raise exception immideately.
# 	 @param wait_timeout - Time to wait in seconds (if wait_lock==True)
# 
# 	 return t Threading object
# 	'''
# 	t = threading.Thread(target=func, args=args)
# 	t.daemon = daemon
# 	if lock == None:
# 		t.start()
# 	else:
# 		if wait_lock:
# 			timeout = timeit.default_timer()
# 			while True:
# 				if not(lock.locked()):
# 					t.start()
# 					break
# 				elif (timeit.default_timer() - timeout) > wait_timeout:
# 					raise Exception('Runtime error - lock not availble')
# 		else:
# 			if not(lock.locked()):
# 				t.start()
# 			else:
# 				raise Exception('Runtime error - lock not availble')
# 	return t


# Path: src/DroneVision/DroneVision_src/hardware/RecordFrames.py
import cv2, threading
import numpy as np
from src.DroneVision.DroneVision_src.imgProcessing.frameTools.frameTools import GetShape, CheckColor
from imageTools import WriteImage
from src.bin.tools import CheckDir, RunThread

		'''
		 @brief Initialize recording by adapting the video to the first frame input.

		 @param initFrame
		'''
		self.__height, self.__width = GetShape(initFrame)
		self.StartRecording()

	def StartRecording(self):
		'''
		 @brief Start video recording
		 	Creates a folder for the video, called by the same video filename,
		 	and creates a subfolder for storing each frame.
		'''
		# Create frames folder
		if self.__store_frames_as_images:
			self.__frames_folder = self.__folder + self.__video_output_filename+'_frames/'
			CheckDir(self.__frames_folder)

		if self.__store_frames_as_video:
			# Define the codec and create VideoWriter object
			fourcc = cv2.VideoWriter_fourcc(*'MJPG')
			self.video_out = cv2.VideoWriter(filename=self.__folder + self.__video_output_filename+'.avi', fourcc=fourcc, fps=self.__fps, frameSize=(self.__width, self.__height), isColor=True)
		
		self.__frame_i = 0

	def WriteFrameThread(self, frame):
		'''
		 @brief Write frame in thread for smoother program flow.
		'''

		RunThread(self.WriteFrame, args=(frame,), lock=self.__write_lock, wait_lock=True)