import MTM, cv2 import numpy as np
letter_a = cv2.imread('letter_a.png', 0) letter_b = cv2.imread('letter_b.png', 0) letter_c = cv2.imread('letter_c.png', 0) letter_d = cv2.imread('letter_d.png', 0)
letter_a.astype(np.uint8) letter_b.astype(np.uint8) letter_c.astype(np.uint8) letter_d.astype(np.uint8)
listTemplates = [('A', letter_a), ('B', letter_b), ('C', letter_c), ('D', letter_d)]
webcam = cv2.VideoCapture(2) webcam.set(cv2.CAP_PROP_FRAME_WIDTH, 1024) webcam.set(cv2.CAP_PROP_FRAME_HEIGHT, 768)
def drawBoxesOnRGB2(image, tableHit, boxThickness=2, boxColor=(255, 255, 00), showLabel=False, labelColor=(255, 255, 0), labelScale=0.5 ): # Convert Grayscale to RGB to be able to see the color bboxes if image.ndim == 2: outImage = cv2.cvtColor(image, cv2.COLOR_GRAY2RGB) # convert to RGB to be able to show detections as color box on grayscale image else: outImage = image.copy()
for _, row in tableHit.iterrows(): x,y,w,h = row['BBox'] score = row['Score'] cv2.rectangle(outImage, (x, y), (x+w, y+h), color=boxColor, thickness=boxThickness) if showLabel: cv2.putText(outImage, text=row['TemplateName'] + "@" + str(score * 100), org=(x, y), fontFace=cv2.FONT_HERSHEY_SIMPLEX, fontScale=labelScale, color=labelColor, lineType=cv2.LINE_AA)
return outImage
if not webcam.isOpened(): print("Could not open webcam") exit()
while webcam.isOpened(): status, image = webcam.read() image.astype(np.uint8) image_gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
tableHit = MTM.matchTemplates(listTemplates, image_gray, score_threshold=0.8, method=cv2.TM_CCOEFF_NORMED, maxOverlap=0) print("Found {} letters".format(len(tableHit))) print(tableHit)
Overlay = drawBoxesOnRGB2(image, tableHit, showLabel=True)
if status: cv2.imshow("test", Overlay)
if cv2.waitKey(1) & 0xFF == ord('q'): break
webcam.release() cv2.destroyAllWindows() |