import glob
import os
import cv2 as cv
import numpy as np
from matplotlib import pyplot as plt

file_names = os.listdir('templates')
print(file_names)
for file_name in file_names:
	break
	tmp_read = cv.imread(file_name)
	w = 100
	h = 100
	template = cv.resize(tmp_read, (w, h))
	
	file_name = 'name.jpg'
	
	# copy template
	copy_template = template.copy()
	
	# create masks
	triangle_mask = np.zeros((h, w), np.uint8)
	square_mask = np.zeros((h, w), np.uint8)
	octagon_mask = np.zeros((h, w), np.uint8)
	circle_mask = np.zeros((h, w), np.uint8)
	
	# filter template
	filtered_template = cv.bilateralFilter(template, 5, 175, 175)
	# edge detection
	edge_detected_template = cv.Canny(filtered_template, 150, 200)
	# find edge contours
	_, contours, _ = cv.findContours(edge_detected_template, cv.RETR_LIST, cv.CHAIN_APPROX_SIMPLE)
			
	for contour in contours:
		# approx contours 
		approx = cv.approxPolyDP(contour, 0.01 * cv.arcLength(contour, True), True)
		
		# print geometry of detected signs
		if len(approx) == 3:
			print("triangle")
			cv.drawContours(triangle_mask,[contour],0,(255,255,255),-1)
		elif len(approx) == 4:
			print("square")
			cv.drawContours(square_mask,[contour],0,(255,255,255),-1)
		elif len(approx) == 8:
			print("octagon")
			cv.drawContours(octagon_mask,[contour],0,(255,255,255),-1)
		elif len(approx) > 15:
			print("circle")
			cv.drawContours(circle_mask,[contour],0,(255,255,255),-1)
		else:
			print('not recognized')
	
	_, triangle_contours, _ = cv.findContours(triangle_mask, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE)
	_, square_contours, _ = cv.findContours(square_mask, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE)
	_, octagon_contours, _ = cv.findContours(octagon_mask, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE)
	_, circle_contours, _ = cv.findContours(circle_mask, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE)
	
	if triangle_contours != []:
		for triangle_contour in triangle_contours:
			# find coordinates of detected contours
			xt, yt, wt, ht = cv.boundingRect(triangle_contour)
			
			# cut space in mask, where is the sign
			triangle_sign_mask = triangle_mask[yt:yt+ht, xt:xt+wt]
			triangle_sign_mask_inv = cv.bitwise_not(triangle_sign_mask)
			triangle_sign_mask_inv = cv.cvtColor(triangle_sign_mask_inv, cv.COLOR_GRAY2BGR)
			
			# cut space in image, where is the mask
			copy_template = copy_template[yt:yt+ht, xt:xt+wt]
			
			# split mask and sign
			triangle_sign = cv.add(copy_template, triangle_sign_mask_inv)
			
			# print sign
			cv.imshow(file_name, triangle_sign)
			cv.waitKey()
			
			# save sign
			cv.imwrite(file_name, triangle_sign)
			
	if square_contours != []:
		for square_contour in square_contours:
			# find coordinates of detected contours
			xs, ys, ws, hs = cv.boundingRect(square_contour)
			
			# cut space in mask, where is the sign
			square_sign_mask = square_mask[ys:ys+hs, xs:xs+ws]
			square_sign_mask_inv = cv.bitwise_not(square_sign_mask)
			square_sign_mask_inv = cv.cvtColor(square_sign_mask_inv, cv.COLOR_GRAY2BGR)
			
			# cut space in image, where is the mask
			copy_template = copy_template[ys:ys+hs, xs:xs+ws]
			
			# split mask and sign
			square_sign = cv.add(copy_template, square_sign_mask_inv)
			
			# print sign
			cv.imshow(file_name, square_sign)
			cv.waitKey()
			
			# save sign
			cv.imwrite(file_name, square_sign)
			
	if octagon_contours != []:
		for octagon_contour in octagon_contours:
			# find coordinates of detected contours
			xo, yo, wo, ho = cv.boundingRect(octagon_contour)
			
			# cut space in mask, where is the sign
			octagon_sign_mask = octagon_mask[yo:yo+ho, xo:xo+wo]
			octagon_sign_mask_inv = cv.bitwise_not(octagon_sign_mask)
			octagon_sign_mask_inv = cv.cvtColor(octagon_sign_mask_inv, cv.COLOR_GRAY2BGR)
			
			# cut space in image, where is the mask
			copy_template = copy_template[yt:yt+ht, xt:xt+wt]
			
			# split mask and sign
			octagon_sign = cv.add(copy_template, octagon_sign_mask_inv)
			
			# print sign
			cv.imshow(file_name, octagon_sign)
			cv.waitKey()
			
			# save signs
			cv.imwrite(file_name, octagon_sign)
			
	if circle_contours != []:
		for circle_contour in circle_contours:
			# find coordinates of detected contours
			xc, yc, wc, hc = cv.boundingRect(circle_contour)
			
			# cut space in mask, where is the sign
			circle_sign_mask = circle_mask[yc:yc+hc, xc:xc+wc]
			circle_sign_mask_inv = cv.bitwise_not(circle_sign_mask)
			circle_sign_mask_inv = cv.cvtColor(circle_sign_mask_inv, cv.COLOR_GRAY2BGR)
			
			# cut space in image, where is the mask
			copy_template = copy_template[yc:yc+hc, xc:xc+wc]
			
			# split mask and sign
			circle_sign = cv.add(copy_template, circle_sign_mask_inv)
			
			# print sign
			cv.imshow(file_name, circle_sign)
			cv.waitKey()
			
			# save signs
			cv.imwrite(file_name, circle_sign)
	
	cv.destroyAllWindows()



	









			
