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

# load test images
my_path = glob.glob("sign_photos/slunce/test/dej_prednost_v_jizde_test/*.jpg")
images = []
for img in my_path:
	img_read = cv.imread(img)
	h, w, _ = img_read.shape
	# reducing image size
	w = w//1
	h = h//1
	img_resize = cv.resize(img_read, (w, h), interpolation = cv.INTER_NEAREST)
	images.append(img_resize)
	
# load template
template = cv.imread('dej_prednost_v_jizde.jpg')
templates = []
temp_w = temp_h = 150
template_resize = cv.resize(template, (temp_w, temp_h), interpolation = cv.INTER_NEAREST)
templates.append(template_resize)


orb = cv.ORB_create()
bf = cv.BFMatcher(cv.NORM_HAMMING, crossCheck = True)

for img in images:
	pos1, des1 = orb.detectAndCompute(img, None)

	shody_max = [0]
	for template in templates:
		pos2, des2 = orb.detectAndCompute(template, None)
		shody = bf.match(des1, des2)
		shody = sorted(shody, key = lambda x:x.distance)
		
		img_det = cv.drawMatches(img, pos1, template, pos2, shody[:15], None)
	
	# convert color to print with pyplot as plt matched_sign
	RGB_template = cv.cvtColor(template, cv.COLOR_BGR2RGB)
	RGB_matched_sign = cv.cvtColor(img_det, cv.COLOR_BGR2RGB)
			
	# print results
	plt.subplot(121),plt.imshow(RGB_template)
	plt.title('Template'), plt.xticks([]), plt.yticks([])
	plt.subplot(122),plt.imshow(RGB_matched_sign)
	plt.title('Matched sign'), plt.xticks([]), plt.yticks([])
	plt.show()
		
		
		
		
