|
import cv2 |
|
import sys |
|
from cv2 import CONTOURS_MATCH_I2 |
|
import numpy as np |
|
|
|
|
|
|
|
|
|
original =cv2.imread(r"Animate\images\flag (1).png") |
|
original2 =cv2.imread(r"Animate\images\brain2.png") |
|
|
|
|
|
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): |
|
|
|
blank = np.zeros(image1.shape, dtype='uint8') |
|
blank2 = np.zeros(image1.shape, dtype='uint8') |
|
|
|
|
|
|
|
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) |
|
|
|
|
|
|
|
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) |
|
|
|
|
|
|
|
positions=list_of_positions((len(contours))) |
|
frames=[] |
|
|
|
|
|
|
|
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) |
|
|
|
|
|
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() |
|
|
|
|
|
|
|
|
|
if text !=None: |
|
|
|
image = np.zeros(original.shape, dtype='uint8') |
|
|
|
|
|
font = cv2.FONT_HERSHEY_COMPLEX |
|
|
|
|
|
org = (10, 400) |
|
|
|
|
|
fontScale = 3 |
|
|
|
|
|
color = (186, 184, 108) |
|
|
|
|
|
thickness = 4 |
|
|
|
|
|
|
|
|
|
def text_frames(text,image,org): |
|
spacing=55 |
|
blink=image |
|
cv2.imwrite(f"blink.png", blink) |
|
for i in range(0, len(text)-1): |
|
|
|
text_vid.write(blink) |
|
|
|
|
|
image = cv2.putText(image, text[i], org, font, |
|
fontScale, color, thickness, cv2.LINE_AA) |
|
|
|
|
|
org=(org[0]+spacing,org[1]) |
|
if text[i].isupper(): |
|
org=(org[0] +spacing +1, org[1]) |
|
print(f"Upper {text[i]}") |
|
print(org) |
|
|
|
|
|
cv2.imwrite(f"text_im{i}.png", image) |
|
|
|
|
|
text_vid.write(image) |
|
text_vid.release() |
|
|
|
text_frames(text,image,org) |
|
|
|
|
|
contourfinder(original, original2, "spies gui Fly") |
|
|