In this video lesson we show you how to decorate and annotate video frames in openCV. We are using the Raspberry Pi camera on the Raspberry Pi 5. Our overall goal in vision AI is to use our setup to grab a frame, analyze the frame, and then decorate the frame. For example, if analysis finds a banana, we can then add a box and a label around the banana found. So, learning how to add text and figures to the video frame is an important skill moving forward. In today’s lesson this is the code that we developed, demonstrating the different things we can add to video frames.
|
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 |
import cv2 from picamera2 import Picamera2 piCam = Picamera2() W=1280 H=720 # W= 640 # H= 360 W=320 H=180 RES = (W,H) piCam.preview_configuration.main.size = RES piCam.preview_configuration.main.format = "RGB888" piCam.preview_configuration.controls.FrameRate=60 piCam.preview_configuration.align() piCam.configure("preview") piCam.start() while True: frame= piCam.capture_array() frame=cv2.flip(frame,-1) upperLeft = (int((W-1)*.1),int((H-1)*.1)) lowerRight = (int((W-1)*.9),int((H-1)*.9)) myColor = (255,0,0) myThick = int(W/250) cv2.rectangle(frame,upperLeft,lowerRight,myColor,myThick) upperLeft = (int((W-1)*.3),int((H-1)*.3)) lowerRight = (int((W-1)*.7),int((H-1)*.7)) myColor = (0,0,255) myThick = int(W/250) myThick = -1 cv2.rectangle(frame,upperLeft,lowerRight,myColor,myThick) startingPoint = (int((W-1)*.1),int((H-1)*.1)) endingPoint = (int((W-1)*.9),int((H-1)*.9)) myColor = (255,0,0) myThick = int(W/250) cv2.line(frame,startingPoint,endingPoint,myColor,myThick) startingPoint = (int((W-1)*.7),int((H-1)*.3)) endingPoint = (int((W-1)*.3),int((H-1)*.7)) myColor = (0,255,0) myThick = int(W/250) cv2.arrowedLine(frame,startingPoint,endingPoint,myColor,myThick) myCenter = (int((W-1)/2),int((H-1)/2)) myRadius = int(.1*(H-1)) myThick = int(W/250) myColor = (0,255,0) cv2.circle(frame,myCenter,myRadius,myColor,myThick) myCenter1 = (int((W-1)*.3),int((H-1)*.3)) myCenter2 = (int((W-1)*.7),int((H-1)*.7)) myCenter3 = (int((W-1)*.7),int((H-1)*.3)) myCenter4 = (int((W-1)*.3),int((H-1)*.7)) myRadius = int((W-1)*.05) myColor = (255,0,0) myThick= -1 cv2.circle(frame,myCenter1,myRadius,myColor,myThick) cv2.circle(frame,myCenter2,myRadius,myColor,myThick) cv2.circle(frame,myCenter3,myRadius,myColor,myThick) cv2.circle(frame,myCenter4,myRadius,myColor,myThick) myText = "My Excellent Label" textLowerLeft= (int(W*.01),int(H*.05)) fontFace = cv2.FONT_HERSHEY_SIMPLEX fontThickness = int(W/450) fontScale = H*.0015 fontColor = (255,0,255) cv2.putText(frame,myText,textLowerLeft,fontFace,fontScale, fontColor, fontThickness) cv2.imshow("Camera", frame) cv2.moveWindow("Camera",0,60) if cv2.waitKey(1)==ord('q'): break cv2.destroyAllWindows() print('Program Terminated') |
