import numpy as np

RAWS = ".\RawPics"
STRIPS = ".\Strips"
PILLS = ".\Strips\Pills"

# this function was used to generate the corresponding darkness ratio of all the single pills
def createData():
	images = getRawImages()
	data = []
	print "Total Image: ", len(images)
	pc = 0
	for i in range(len(images)):
		strip = extractStrip(images[i])
		# cv2.imwrite(STRIPS+"\\P"+str(i)+".png", strip)
		pills = splitStripToPills(strip, 154, 154)
		for pill in pills:
			cv2.imshow("current", pill)
			cv2.waitKey(0)
			y = input("Enter desired Value: ")
			# cv2.imwrite(PILLS+"\\S"+str(pc)+".png", pill)
			pc += 1

# this is to load the raw images from the specified folder
def getRawImages():
	if os.path.isdir(RAWS):
		strips = os.listdir(RAWS)
		images = [cv2.imread(RAWS+"\\"+i,0) for i in strips]
		return images


# given an image filename this function will return the darkness ratio which
# is used for our test
def getTestData(filename):
	img = cv2.imread(filename,0)
	test_data = np.ndarray(shape=(1024,1), dtype=float, order='F')
	test_data = np.reshape(img, (1024,1))
	test_data = test_data*(-1)
	test_data = test_data + 255
	test_data = test_data/255.0
	return test_data

# this is function is overlooking the whole data generation and storage
def generateTestData():
	first, second = [], []
	ys = np.load("ys.npy")
	for i in range(0,290):
		pillpath = PILLS+"\\S"+str(i)+".png"
		print "[{0}]Processing ", pillpath,"".format(i)
		d = getTestData(pillpath)
		first.append(d)
		second.append(ys[i])
	saveTrainingData((first,second))

# given a image this function will return the darkness ratio for evaluation
def getTestDataForEvalutaion(img):
	test_data = np.ndarray(shape=(1024,1), dtype=float, order='F')
	test_data = np.reshape(img, (1024,1))
	test_data = test_data*(-1)
	test_data = test_data + 255
	test_data = test_data/255.0
	return test_data

# this function loads the weights generated by the learning algorithm
def loadEvaluationData():
	BIAS, WEIGHT = np.load("latest_update.npy")
	return BIAS, WEIGHT