Click here to Skip to main content
15,881,715 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Python
  1  import cv2 as cv 
  2  import numpy as np
  3  
  4  # colors 
  5  # values =(blue, green, red) opencv accepts BGR values not RGB
  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          # y += 10 
 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):
 31      """
 32      Draws text with background, with  control transparency
 33      @param img:(mat) which you want to draw text
 34      @param text: (string) text you want draw
 35      @param font: fonts face, like FONT_HERSHEY_COMPLEX, FONT_HERSHEY_PLAIN etc.
 36      @param fontScale: (double) the size of text, how big it should be.
 37      @param textPos: tuple(x,y) position where you want to draw text
 38      @param textThickness:(int) fonts weight, how bold it should be
 39      @param textPos: tuple(x,y) position where you want to draw text
 40      @param textThickness:(int) fonts weight, how bold it should be.
 41      @param textColor: tuple(BGR), values -->0 to 255 each
 42      @param bgColor: tuple(BGR), values -->0 to 255 each
 43      @param pad_x: int(pixels)  padding of in x direction
 44      @param pad_y: int(pixels) 1 to 1.0 (), controls transparency of  text background 
 45      @return: img(mat) with draw with background
 46      """
 47      (t_w, t_h), _= cv.getTextSize(text, font, fontScale, textThickness) # getting the text size
 48      x, y = textPos
 49      overlay = img.copy() # coping the image
 50      cv.rectangle(overlay, (x-pad_x, y+ pad_y), (x+t_w+pad_x, y-t_h-pad_y), bgColor,-1) # draw rectangle 
 51      new_img = cv.addWeighted(overlay, bgOpacity, img, 1 - bgOpacity, 0) # overlaying the rectangle on the image.
 52      cv.putText(new_img,text, textPos,font, fontScale, textColor,textThickness ) # draw in text
 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):
 59      """    
 60      Draw text with background blured,  control the blur value, with kernal(odd, odd)
 61      @param img:(mat) which you want to draw text
 62      @param text: (string) text you want draw
 63      @param font: fonts face, like FONT_HERSHEY_COMPLEX, FONT_HERSHEY_PLAIN etc.
 64      @param fontScale: (double) the size of text, how big it should be.
 65      @param textPos: tuple(x,y) position where you want to draw text
 66      @param textThickness:(int) fonts weight, how bold it should be.
 67      @param textColor: tuple(BGR), values -->0 to 255 each
 68      @param kneral: tuple(3,3) int as odd number:  higher the value, more blurry background would be
 69      @param pad_x: int(pixels)  padding of in x direction
 70      @param pad_y: int(pixels)  padding of in y direction
 71      @return: img mat, with text drawn, with background blured
 72      
 73      call the function: 
 74       img =textBlurBackground(img, 'Blured Background Text', cv2.FONT_HERSHEY_COMPLEX, 0.9, (20, 60),2, (0,255, 0), (49,49), 13, 13 )
 75      """
 76      
 77      (t_w, t_h), _= cv.getTextSize(text, font, fontScale, textThickness) # getting the text size
 78      x, y = textPos
 79      blur_roi = img[y-pad_y-t_h: y+pad_y, x-pad_x:x+t_w+pad_x] # croping Text Background
 80      img[y-pad_y-t_h: y+pad_y, x-pad_x:x+t_w+pad_x]=cv.blur(blur_roi, kneral)  # merging the blured background to img
 81      cv.putText(img,text, textPos,font, fontScale, textColor,textThickness )          
 82      # cv.imshow('blur roi', blur_roi)
 83      # cv.imshow('blured', img)
 84  
 85      return img
 86  
 87  def fillPolyTrans(img, points, color, opacity):
 88      """
 89      @param img: (mat) input image, where shape is drawn.
 90      @param points: list [tuples(int, int) these are the points custom shape,FillPoly
 91      @param color: (tuples (int, int, int)
 92      @param opacity:  it is transparency of image.
 93      @return: img(mat) image with rectangle draw.
 94      """
 95      list_to_np_array = np.array(points, dtype=np.int32)
 96      overlay = img.copy()  # coping the image
 97      cv.fillPoly(overlay,[list_to_np_array], color )
 98      new_img = cv.addWeighted(overlay, opacity, img, 1 - opacity, 0)
 99      # print(points_list)
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):
105      """
106      @param img: (mat) input image, where shape is drawn.
107      @param pt1: tuple(int,int) it specifies the starting point(x,y) os rectangle
108      @param pt2: tuple(int,int)  it nothing but width and height of rectangle
109      @param color: (tuples (int, int, int), it tuples of BGR values
110      @param thickness: it thickness of board line rectangle, if (-1) passed then rectangle will be fulled with color.
111      @param opacity:  it is transparency of image.
112      @return:
113      """
114      overlay = img.copy()
115      cv.rectangle(overlay, pt1, pt2, color, thickness)
116      new_img = cv.addWeighted(overlay, opacity, img, 1 - opacity, 0) # overlaying the rectangle on the image.
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          # img = np.zeros((1000,1000, 3), dtype=np.uint8)
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          # cv.imwrite('color_image.png', img)
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'
Posted
Updated 25-Jul-22 22:16pm
v2

1 solution

The message is telling you that the img variable does not contain a valid reference, i.e. it is set to NoneType. At the beginning of your main function you have:
Python
cap = cv.VideoCapture('Girl.mp4')
counter =0
while True:
    success, img = cap.read()
    img=rectTrans(img, pt1=(30, 320), pt2=(160, 260), color=(0,255,255),thickness=-1, opacity=0.6)

So it is a reasonable guess that the cap.read call failed, but you did not check the return code in success to make sure. So now you need to find out why that call did not return a valid image object.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900