# Path: pyKinectTools/algs/BackgroundSubtraction.py
# def constrain(img, mini=-1, maxi=-1): #500, 4000
# 	if mini == -1:
# 		min_ = np.min(img[np.nonzero(img)])
# 	else:
# 		min_ = mini
# 	if maxi == -1:
# 		max_ = img.max()
# 	else:
# 		max_ = maxi
# 
# 	img = np.clip(img, min_, max_)
# 	img -= min_
# 	if max_ == 0:
# 		max_ = 1
# 	img = np.array((img * (255.0 / (max_-min_))), dtype=np.uint8)
# 
# 	img[img==img.max()] = 0
# 
# 	return img


import os, time, sys
import numpy as np
import cv, cv2
import pyKinectTools.utils.SkeletonUtils as SR
from math import floor
from pyKinectTools.algs.BackgroundSubtraction import constrain


# import SkelPlay as SR
# from pyKinectTools.utils.DepthUtils import *

def getFolderTime(folderName):
	# Folder name is of format hr-min-sec
	return (int(folderName[0:2])*60*60 + int(folderName[3:5])*60 + int(folderName[6:8]))

def getFileTime(fileName, fileNames):
	if len(fileNames) < 4:
		return getFolderTime(fileName)
	# fileNames.sort
	start = int(fileNames[0][fileNames[0].find("_")+1:fileNames[0].find(".")])
	end   = int(fileNames[-1][fileNames[-1].find("_")+1:fileNames[-1].find(".")])
	current = int(fileName[fileName.find("_")+1:fileName.find(".")])
	diff = end-start

	return getFolderTime(fileName) + (current-start) / diff

def getFileListTimes(fileNames):
	if len(fileNames) < 4:
		currentTime = getFolderTime(fileNames[0])
		# print currentTime, fileNames
		return [[currentTime, fileNames]]
	start = float(fileNames[0][fileNames[0].find("_")+1:fileNames[0].find(".")])
	end   = float(fileNames[-1][fileNames[-1].find("_")+1:fileNames[-1].find(".")])
	diff = end-start
	times = []


# Path: pyKinectTools/algs/BackgroundSubtraction.py
# def constrain(img, mini=-1, maxi=-1): #500, 4000
# 	if mini == -1:
# 		min_ = np.min(img[np.nonzero(img)])
# 	else:
# 		min_ = mini
# 	if maxi == -1:
# 		max_ = img.max()
# 	else:
# 		max_ = maxi
# 
# 	img = np.clip(img, min_, max_)
# 	img -= min_
# 	if max_ == 0:
# 		max_ = 1
# 	img = np.array((img * (255.0 / (max_-min_))), dtype=np.uint8)
# 
# 	img[img==img.max()] = 0
# 
# 	return img


# Path: pyKinectTools/utils/DepthReader.py
import os, time, sys
import numpy as np
import cv, cv2
import pyKinectTools.utils.SkeletonUtils as SR
from math import floor
from pyKinectTools.algs.BackgroundSubtraction import constrain



# import SkelPlay as SR
# from pyKinectTools.utils.DepthUtils import *

def getFolderTime(folderName):
	# Folder name is of format hr-min-sec
	return (int(folderName[0:2])*60*60 + int(folderName[3:5])*60 + int(folderName[6:8]))

def getFileTime(fileName, fileNames):
	if len(fileNames) < 4:
		return getFolderTime(fileName)
	# fileNames.sort
	start = int(fileNames[0][fileNames[0].find("_")+1:fileNames[0].find(".")])
	end   = int(fileNames[-1][fileNames[-1].find("_")+1:fileNames[-1].find(".")])
	current = int(fileName[fileName.find("_")+1:fileName.find(".")])
	diff = end-start

	return getFolderTime(fileName) + (current-start) / diff

def getFileListTimes(fileNames):
	if len(fileNames) < 4:
		currentTime = getFolderTime(fileNames[0])
		# print currentTime, fileNames
		return [[currentTime, fileNames]]
	start = float(fileNames[0][fileNames[0].find("_")+1:fileNames[0].find(".")])
	end   = float(fileNames[-1][fileNames[-1].find("_")+1:fileNames[-1].find(".")])
	diff = end-start
	times = []


	for f in fileNames: