# 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')
# 
# Path: src/DroneVision/DroneVision_src/hardware/RecordFrames.py
# class RecordFrames():
# 	def __init__(self, fps, folder, video_output_filename, store_frames_as_video=True, store_frames_as_images=True):
# 		'''CONSTRUCTOR'''
# 		self.__folder 					= folder
# 		self.__video_output_filename 	= video_output_filename
# 		self.__fps 						= int(np.ceil(fps))
# 		self.__frame_i 					= 0
# 		self.__store_frames_as_video 	= store_frames_as_video
# 		self.__store_frames_as_images 	= store_frames_as_images
# 		self.__write_lock 				= threading.Lock()
# 
# 	def GetNumberOfRecordedFrames(self):
# 		'''
# 		 @brief Get number of recorded frames
# 
# 		 @return number of recorded frames
# 		'''
# 		return self.__frame_i
# 
# 	def InitRecording(self, initFrame):
# 		'''
# 		 @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)
# 
# 	def WriteFrame(self, frame):
# 		'''
# 		 @brief Write new frame to output video
# 
# 		 @param frame Frame to write
# 		'''
# 		with self.__write_lock:
# 			if self.__frame_i == 0:
# 				self.InitRecording(frame)
# 			if not(frame.dtype == np.uint8 or frame.dtype == np.uint16):
# 				frame = frame.astype(np.uint16)
# 			if self.__store_frames_as_video:
# 				self.video_out.write(CheckColor(frame))
# 			if self.__store_frames_as_images:
# 				WriteImage(frame, self.__frames_folder + self.__video_output_filename + '_' + str(self.__frame_i))
# 			self.__frame_i += 1
# 
# 	def CloseRecording(self):
# 		'''
# 		 @Stop video recording
# 		'''
# 		if self.__frame_i > 0:
# 			if self.__write_lock.locked():
# 				pass
# 			if self.__store_frames_as_video:
# 				self.video_out.release()
# 
# 	def __del__(self):
# 		'''DESTRUCTOR'''
# 		self.CloseRecording()


import numpy as np
from src.bin.tools import CheckDir
from src.DroneVision.DroneVision_src.hardware.RecordFrames import RecordFrames
		for key in self.__process_frames:
			if not(key in self.__frame_recorders_dict):
				self.__frame_recorders_dict[key] = self.CreateFrameRecorder(self.__fps, self.__output_folder, key, self.__store_frames_as_video, self.__store_frames_as_images)
			self.RecordFrame(self.__frame_recorders_dict[key], self.__process_frames[key])
			
	def RecordFrame(self, frame_recorder, frame, use_threading=False):
		'''
		 @brief Record frame

		 @param frame_recorder
		 @param frame
		 @param use_threading (True/False (default=False))
		'''
		if isinstance(frame, np.ndarray):
			if use_threading:
				frame_recorder.WriteFrameThread(frame)
			else:
				frame_recorder.WriteFrame(frame)

	def CreateFrameRecorder(self, fps, output_folder, video_name, store_frames_as_video=True, store_frames_as_images=True):
		'''
		 @brief Create new frame recorder

		 @param fps (frames per second)
		 @param video_name (video filename)
		 @param store_frames_as_video (store all recorded frames as video (default=True))
		 @param store_frames_as_images (store all recorded frames as images in separate subfolder (default=True))

		 @return recordFrames (class instance of RecordFrames)
		'''

# 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')
# 
# Path: src/DroneVision/DroneVision_src/hardware/RecordFrames.py
# class RecordFrames():
# 	def __init__(self, fps, folder, video_output_filename, store_frames_as_video=True, store_frames_as_images=True):
# 		'''CONSTRUCTOR'''
# 		self.__folder 					= folder
# 		self.__video_output_filename 	= video_output_filename
# 		self.__fps 						= int(np.ceil(fps))
# 		self.__frame_i 					= 0
# 		self.__store_frames_as_video 	= store_frames_as_video
# 		self.__store_frames_as_images 	= store_frames_as_images
# 		self.__write_lock 				= threading.Lock()
# 
# 	def GetNumberOfRecordedFrames(self):
# 		'''
# 		 @brief Get number of recorded frames
# 
# 		 @return number of recorded frames
# 		'''
# 		return self.__frame_i
# 
# 	def InitRecording(self, initFrame):
# 		'''
# 		 @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)
# 
# 	def WriteFrame(self, frame):
# 		'''
# 		 @brief Write new frame to output video
# 
# 		 @param frame Frame to write
# 		'''
# 		with self.__write_lock:
# 			if self.__frame_i == 0:
# 				self.InitRecording(frame)
# 			if not(frame.dtype == np.uint8 or frame.dtype == np.uint16):
# 				frame = frame.astype(np.uint16)
# 			if self.__store_frames_as_video:
# 				self.video_out.write(CheckColor(frame))
# 			if self.__store_frames_as_images:
# 				WriteImage(frame, self.__frames_folder + self.__video_output_filename + '_' + str(self.__frame_i))
# 			self.__frame_i += 1
# 
# 	def CloseRecording(self):
# 		'''
# 		 @Stop video recording
# 		'''
# 		if self.__frame_i > 0:
# 			if self.__write_lock.locked():
# 				pass
# 			if self.__store_frames_as_video:
# 				self.video_out.release()
# 
# 	def __del__(self):
# 		'''DESTRUCTOR'''
# 		self.CloseRecording()


# Path: src/DataBase/FrameRecorder/FrameRecorder.py
import numpy as np
from src.bin.tools import CheckDir
from src.DroneVision.DroneVision_src.hardware.RecordFrames import RecordFrames

		for key in self.__process_frames:
			if not(key in self.__frame_recorders_dict):
				self.__frame_recorders_dict[key] = self.CreateFrameRecorder(self.__fps, self.__output_folder, key, self.__store_frames_as_video, self.__store_frames_as_images)
			self.RecordFrame(self.__frame_recorders_dict[key], self.__process_frames[key])
			
	def RecordFrame(self, frame_recorder, frame, use_threading=False):
		'''
		 @brief Record frame

		 @param frame_recorder
		 @param frame
		 @param use_threading (True/False (default=False))
		'''
		if isinstance(frame, np.ndarray):
			if use_threading:
				frame_recorder.WriteFrameThread(frame)
			else:
				frame_recorder.WriteFrame(frame)

	def CreateFrameRecorder(self, fps, output_folder, video_name, store_frames_as_video=True, store_frames_as_images=True):
		'''
		 @brief Create new frame recorder

		 @param fps (frames per second)
		 @param video_name (video filename)
		 @param store_frames_as_video (store all recorded frames as video (default=True))
		 @param store_frames_as_images (store all recorded frames as images in separate subfolder (default=True))

		 @return recordFrames (class instance of RecordFrames)
		'''

		recordFrames = RecordFrames(fps, output_folder, video_name, store_frames_as_video, store_frames_as_images)