import cv2 import sys from cv2 import CONTOURS_MATCH_I2 import numpy as np #Takes two inputs as images #Original 1 and 2 are dummy data original =cv2.imread(r"Animate\images\flag (1).png") original2 =cv2.imread(r"Animate\images\brain2.png") # get key positions at which frame needs to be generated def list_of_positions(num_contours,num_frames=100): positions=[] for i in range(0, num_frames): positions.append(int(num_contours/num_frames*i)) return positions def contourfinder(image1, image2, text=None): #Create two blank pages to write into blank = np.zeros(image1.shape, dtype='uint8') blank2 = np.zeros(image1.shape, dtype='uint8') #Threshold and contours for image 1 and 2 threshold=cv2.Canny(image=image1, threshold1=100, threshold2=200) contours, hierarchies = cv2.findContours(threshold, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE) threshold2=cv2.Canny(image=original2, threshold1=100, threshold2=200) contours2, hierarchies2 = cv2.findContours(threshold2, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE) #Initialize three empty videos vid1 = cv2.VideoWriter('vid1.mp4',cv2.VideoWriter_fourcc(*'mp4v'), 24, threshold.shape) vid2 = cv2.VideoWriter('vid2.mp4',cv2.VideoWriter_fourcc(*'mp4v'), 24, threshold.shape) text_vid = cv2.VideoWriter('text_vid.mp4',cv2.VideoWriter_fourcc(*'mp4v'), 10, threshold.shape) #Get positions positions=list_of_positions((len(contours))) frames=[] #Loop over contours adding them to blank image then writing to video for i in range(0, len(contours)): cv2.drawContours(blank, contours=contours, contourIdx=i, color=(125, 200, 255), thickness=1) if i in positions: frames.append(blank) #Complile to video vid1.write(blank) vid1.release() positions=list_of_positions((len(contours2))) for i in range(0, len(contours2)): cv2.drawContours(blank2, contours=contours2, contourIdx=i, color=(125, 200, 255), thickness=1) if i in positions: frames.append(blank2) vid2.write(blank2) vid2.release() #Next is the text vid if text !=None: # Reading an image in default mode image = np.zeros(original.shape, dtype='uint8') # font font = cv2.FONT_HERSHEY_COMPLEX # org org = (10, 400) # fontScale fontScale = 3 # Blue color in BGR color = (186, 184, 108) # Line thickness of 2 px thickness = 4 def text_frames(text,image,org): spacing=55 #spacing between letters blink=image cv2.imwrite(f"blink.png", blink) for i in range(0, len(text)-1): text_vid.write(blink) # Using cv2.putText() method image = cv2.putText(image, text[i], org, font, fontScale, color, thickness, cv2.LINE_AA) #Take care of org spacing org=(org[0]+spacing,org[1]) if text[i].isupper(): org=(org[0] +spacing +1, org[1]) print(f"Upper {text[i]}") print(org) # Displaying the image cv2.imwrite(f"text_im{i}.png", image) #Complile to video text_vid.write(image) text_vid.release() text_frames(text,image,org) contourfinder(original, original2, "spies gui Fly")