import glob
import cv2 as cv
import numpy as np
from matplotlib import pyplot as plt
from time import time
 
# load test image
def load_images():
	my_path = glob.glob("sign_photos/noc/hlavni_silnice/*.jpg")
	images = []
	for img in my_path:
		img_read = cv.imread(img)
		images.append(img_read)
	return(images)
	
# color detection
def color_detection(image):

	# convert to HSV
	image_HSV = cv.cvtColor(image, cv.COLOR_BGR2HSV)	
	
	light = ''		### Slunce/Zatazeno/noc/vse v jednom ('')
	
	for i in range(0,3):
		if light == 'Slunce':
		# Slunce ##################
			# red
			H_red = 175
			S_red = 210
			V_red = 125
			
			v1_red = 10
			v2_red = 70
			v3_red = 80
			
			#blue
			H_blue = 109
			S_blue = 205
			V_blue = 71
			
			v1_blue = 15
			v2_blue = 30
			v3_blue = 100
			
			#white
			H_white = 63
			S_white = 21
			V_white = 185
			
			v1_white = 40
			v2_white = 55
			v3_white = 15
		
		elif light == 'Zatazeno':
			# Zatazeno ################
			# red
			H_red = 177
			S_red = 137
			V_red = 84
			
			v1_red = 10
			v2_red = 30
			v3_red = 80
			
			#blue
			H_blue = 109
			S_blue = 188
			V_blue = 69
			
			v1_blue = 15
			v2_blue = 30
			v3_blue = 100
			
			#white
			H_white = 66
			S_white = 11
			V_white = 117
			
			v1_white = 40
			v2_white = 55
			v3_white = 20
		
		elif light == 'Noc':
			# Noc #####################
			# red
			H_red = 5
			S_red = 95
			V_red = 70
			
			v1_red = 10
			v2_red = 30
			v3_red = 80
			
			#blue
			H_blue = 112
			S_blue = 154
			V_blue = 48
			
			v1_blue = 15
			v2_blue = 30
			v3_blue = 100
			
			#white
			H_white = 30
			S_white = 5
			V_white = 98
			
			v1_white = 40
			v2_white = 55
			v3_white = 20
		
		else:
			# All in one #################
			# red
			H_red = 175
			S_red = 158
			V_red = 118
			
			v1_red = 20
			v2_red = 60
			v3_red = 120
			
			#blue
			H_blue = 110
			S_blue = 182
			V_blue = 63
			
			v1_blue = 15
			v2_blue = 60
			v3_blue = 120
			
			#white
			H_white = 40
			S_white = 14
			V_white = 143
			
			v1_white = 65
			v2_white = 60
			v3_white = 30
			
		# Threshold values
		lower_red = np.array([H_red-v1_red, S_red-v2_red, V_red-v3_red])
		upper_red = np.array([H_red+v1_red, S_red+v2_red, V_red+v3_red])
		
		lower_blue = np.array([H_blue-v1_blue, S_blue-v2_blue, V_blue-v3_blue])
		upper_blue = np.array([H_blue+v1_blue, S_blue+v2_blue, V_blue+v3_blue])
		
		lower_white = np.array([H_white-v1_white, S_white-v2_white, V_white-v3_white])
		upper_white = np.array([H_white+v1_white, S_white+v2_white, V_white+v3_white])	
		
		# create mask
		mask_red = cv.inRange(image_HSV, lower_red, upper_red)
		mask_blue = cv.inRange(image_HSV, lower_blue, upper_blue)
		mask_white = cv.inRange(image_HSV, lower_white, upper_white)
		
		mask = np.array([mask_red, mask_blue, mask_white])
		mask_color = mask[i]
		
		# find color contours
		_, contours, _= cv.findContours(mask_color, cv.RETR_TREE, cv.CHAIN_APPROX_SIMPLE)
		
		for contour in contours:
			# ignore too small and large area of contours
			h, w, _ = image.shape
			image_area = w*h
			contour_area = cv.contourArea(contour)
			
			if (contour_area < image_area * 0.0001) | (contour_area > image_area * 0.02):
				continue
				
			# find coordinates of detected contours
			x, y, w, h = cv.boundingRect(contour)
			
			# draw ractangle around detected object
			cv.rectangle(image, (x, y), (x+w, y+h), (0,255,255), 5)	# zluta
			
	return(mask_color, image)
	
# convert color to print
def convert_color(image):
	RGB_detected_signs = cv.cvtColor(image, cv.COLOR_BGR2RGB)
	return(RGB_detected_signs)

# print results
def print_images(mask, RGB_detected_signs):
	plt.figure('')
	plt.subplot(122),plt.imshow(mask, cmap='gray')
	plt.title('detekce bílé'), plt.xticks([]), plt.yticks([])
	plt.subplot(121),plt.imshow(RGB_detected_signs)
	plt.title('vzor'), plt.xticks([]), plt.yticks([])
	plt.show()

# main
images = load_images()
# run testing images
for image in images:
	start = time()
	# detect signs
	mask, image = color_detection(image)
	# print time of detection
	print(time()-start)
	#print image
	RGB_detected_signs = convert_color(image)
	print_images(mask, RGB_detected_signs)
	
		
	
	
	
	
	
	
	
	
	
	
	
	
	
	
