File size: 3,674 Bytes
0cfac42
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e651493
0cfac42
 
 
 
 
 
 
 
 
 
 
e651493
0cfac42
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e651493
 
0cfac42
 
 
 
 
 
 
 
 
e651493
 
0cfac42
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e651493
0cfac42
 
e651493
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
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, num_frames=100):
    #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)
    last_image1=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)
    last_image2=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)
    return last_image1,last_image2
        

#contourfinder(original, original2, "spies gui Fly")