WankioM commited on
Commit
0cfac42
·
unverified ·
1 Parent(s): 86434c8

Create contourfinder.py

Browse files
Files changed (1) hide show
  1. OpenCV/contourfinder.py +121 -0
OpenCV/contourfinder.py ADDED
@@ -0,0 +1,121 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import cv2
2
+ import sys
3
+ from cv2 import CONTOURS_MATCH_I2
4
+ import numpy as np
5
+
6
+ #Takes two inputs as images
7
+ #Original 1 and 2 are dummy data
8
+
9
+ original =cv2.imread(r"Animate\images\flag (1).png")
10
+ original2 =cv2.imread(r"Animate\images\brain2.png")
11
+
12
+ # get key positions at which frame needs to be generated
13
+ def list_of_positions(num_contours,num_frames=100):
14
+ positions=[]
15
+ for i in range(0, num_frames):
16
+ positions.append(int(num_contours/num_frames*i))
17
+ return positions
18
+
19
+
20
+
21
+ def contourfinder(image1, image2, text=None):
22
+ #Create two blank pages to write into
23
+ blank = np.zeros(image1.shape, dtype='uint8')
24
+ blank2 = np.zeros(image1.shape, dtype='uint8')
25
+
26
+
27
+ #Threshold and contours for image 1 and 2
28
+ threshold=cv2.Canny(image=image1, threshold1=100, threshold2=200)
29
+ contours, hierarchies = cv2.findContours(threshold, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
30
+
31
+ threshold2=cv2.Canny(image=original2, threshold1=100, threshold2=200)
32
+ contours2, hierarchies2 = cv2.findContours(threshold2, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
33
+
34
+
35
+ #Initialize three empty videos
36
+ vid1 = cv2.VideoWriter('vid1.mp4',cv2.VideoWriter_fourcc(*'mp4v'), 24, threshold.shape)
37
+ vid2 = cv2.VideoWriter('vid2.mp4',cv2.VideoWriter_fourcc(*'mp4v'), 24, threshold.shape)
38
+ text_vid = cv2.VideoWriter('text_vid.mp4',cv2.VideoWriter_fourcc(*'mp4v'), 10, threshold.shape)
39
+
40
+
41
+ #Get positions
42
+ positions=list_of_positions((len(contours)))
43
+ frames=[]
44
+
45
+
46
+ #Loop over contours adding them to blank image then writing to video
47
+ for i in range(0, len(contours)):
48
+ cv2.drawContours(blank, contours=contours, contourIdx=i, color=(125, 200, 255), thickness=1)
49
+
50
+ if i in positions:
51
+ frames.append(blank)
52
+
53
+ #Complile to video
54
+ vid1.write(blank)
55
+ vid1.release()
56
+
57
+ positions=list_of_positions((len(contours2)))
58
+
59
+ for i in range(0, len(contours2)):
60
+ cv2.drawContours(blank2, contours=contours2, contourIdx=i, color=(125, 200, 255), thickness=1)
61
+ if i in positions:
62
+ frames.append(blank2)
63
+
64
+ vid2.write(blank2)
65
+ vid2.release()
66
+
67
+
68
+ #Next is the text vid
69
+
70
+ if text !=None:
71
+ # Reading an image in default mode
72
+ image = np.zeros(original.shape, dtype='uint8')
73
+
74
+ # font
75
+ font = cv2.FONT_HERSHEY_COMPLEX
76
+
77
+ # org
78
+ org = (10, 400)
79
+
80
+ # fontScale
81
+ fontScale = 3
82
+
83
+ # Blue color in BGR
84
+ color = (186, 184, 108)
85
+
86
+ # Line thickness of 2 px
87
+ thickness = 4
88
+
89
+
90
+
91
+
92
+ def text_frames(text,image,org):
93
+ spacing=55 #spacing between letters
94
+ blink=image
95
+ cv2.imwrite(f"blink.png", blink)
96
+ for i in range(0, len(text)-1):
97
+
98
+ text_vid.write(blink)
99
+
100
+ # Using cv2.putText() method
101
+ image = cv2.putText(image, text[i], org, font,
102
+ fontScale, color, thickness, cv2.LINE_AA)
103
+
104
+ #Take care of org spacing
105
+ org=(org[0]+spacing,org[1])
106
+ if text[i].isupper():
107
+ org=(org[0] +spacing +1, org[1])
108
+ print(f"Upper {text[i]}")
109
+ print(org)
110
+
111
+ # Displaying the image
112
+ cv2.imwrite(f"text_im{i}.png", image)
113
+
114
+ #Complile to video
115
+ text_vid.write(image)
116
+ text_vid.release()
117
+
118
+ text_frames(text,image,org)
119
+
120
+
121
+ contourfinder(original, original2, "spies gui Fly")