1 import cv2 as cv
2 import numpy as np
3
4
5
6 BLACK = (0,0,0)
7 WHITE = (255,255,255)
8 BLUE = (255,0,0)
9 RED = (0,0,255)
10 CYAN = (255,255,0)
11 YELLOW =(0,255,255)
12 MAGENTA = (255,0,255)
13 GRAY = (128,128,128)
14 GREEN = (0,255,0)
15 PURPLE = (128,0,128)
16 ORANGE = (0,165,255)
17 PINK = (147,20,255)
18 points_list =[(200, 300), (150, 150), (400, 200)]
19 def drawColor(img, colors):
20 x, y = 0,10
21 w, h = 20, 30
22
23 for color in colors:
24 x += w+5
25
26 cv.rectangle(img, (x-6, y-5 ), (x+w+5, y+h+5), (10, 50, 10), -1)
27 cv.rectangle(img, (x, y ), (x+w, y+h), color, -1)
28
29
30 def textWithBackground(img, text, font, fontScale, textPos, textThickness=1,textColor=(0,255,0), bgColor=(0,0,0), pad_x=3, pad_y=3, bgOpacity=0.5)
47 (t_w, t_h), _= cv.getTextSize(text, font, fontScale, textThickness)
48 x, y = textPos
49 overlay = img.copy()
50 cv.rectangle(overlay, (x-pad_x, y+ pad_y), (x+t_w+pad_x, y-t_h-pad_y), bgColor,-1)
51 new_img = cv.addWeighted(overlay, bgOpacity, img, 1 - bgOpacity, 0)
52 cv.putText(new_img,text, textPos,font, fontScale, textColor,textThickness )
53 img = new_img
54
55 return img
56
57
58 def textBlurBackground(img, text, font, fontScale, textPos, textThickness=1,textColor=(0,255,0),kneral=(33,33) , pad_x=3, pad_y=3)
76
77 (t_w, t_h), _= cv.getTextSize(text, font, fontScale, textThickness)
78 x, y = textPos
79 blur_roi = img[y-pad_y-t_h: y+pad_y, x-pad_x:x+t_w+pad_x]
80 img[y-pad_y-t_h: y+pad_y, x-pad_x:x+t_w+pad_x]=cv.blur(blur_roi, kneral)
81 cv.putText(img,text, textPos,font, fontScale, textColor,textThickness )
82
83
84
85 return img
86
87 def fillPolyTrans(img, points, color, opacity)
95 list_to_np_array = np.array(points, dtype=np.int32)
96 overlay = img.copy()
97 cv.fillPoly(overlay,[list_to_np_array], color )
98 new_img = cv.addWeighted(overlay, opacity, img, 1 - opacity, 0)
99
100 img = new_img
101 cv.polylines(img, [list_to_np_array], True, color,1, cv.LINE_AA)
102 return img
103
104 def rectTrans(img, pt1, pt2, color, thickness, opacity)
114 overlay = img.copy()
115 cv.rectangle(overlay, pt1, pt2, color, thickness)
116 new_img = cv.addWeighted(overlay, opacity, img, 1 - opacity, 0)
117 img = new_img
118
119 return img
120
121 def main():
122 cap = cv.VideoCapture('Girl.mp4')
123 counter =0
124 while True:
125 success, img = cap.read()
126
127 img=rectTrans(img, pt1=(30, 320), pt2=(160, 260), color=(0,255,255),thickness=-1, opacity=0.6)
128 img =fillPolyTrans(img=img, points=points_list, color=(0,255,0), opacity=.5)
129 drawColor(img, [BLACK,WHITE ,BLUE,RED,CYAN,YELLOW,MAGENTA,GRAY ,GREEN,PURPLE,ORANGE,PINK])
130 textBlurBackground(img, 'Blured Background Text', cv.FONT_HERSHEY_COMPLEX, 0.8, (60, 140),2, YELLOW, (71,71), 13, 13)
131 img=textWithBackground(img, 'Colored Background Texts', cv.FONT_HERSHEY_SIMPLEX, 0.8, (60,80), textThickness=2, bgColor=GREEN, textColor=BLACK, bgOpacity=0.7, pad_x=6, pad_y=6)
132 imgGray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
133
134 counter +=1
135 cv.imshow('img', img)
136 cv.imwrite(f'image/image_{counter}.png', img)
137 if cv.waitKey(1) ==ord('q'):
138 break
139
140 if __name__ == "__main__":
141 main()
What I have tried:
I get an error, what should I do?
Traceback (most recent call last):
File "C:\Users\Admini\AppData\local-packages\Python310\site-packages\utils\utils.py", line 141, in <module>
main()
File "C:\Users\Admini\AppData\local-packages\Python310\site-packages\utils\utils.py", line 127, in main
img=rectTrans(img, pt1=(30, 320), pt2=(160, 260), color=(0,255,255),thickness=-1, opacity=0.6)
File "C:\Users\Admini\AppData\local-packages\Python310\site-packages\utils\utils.py", line 114, in rectTrans
overlay = img.copy()
AttributeError: 'NoneType' object has no attribute 'copy'