The goal of this lesson is for you to become more comfortable with the openCV video window frame. To do this, we will create a solid rectangular box which will move and bounce within the video window. For your convenience, this is the code we developed in the video.
|
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 |
import cv2 import time from picamera2 import Picamera2 piCam = Picamera2() W=1280 H=720 tStart = time.time() fps = 0 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() textLowerLeft = (int(W*.01),int(H*.05)) fontFace = cv2.FONT_HERSHEY_SIMPLEX fontThickness = int(W/425) fontScale = H*.0015 fontColor = (0,0,255) x1 = int(.1*(W-1)) y1 = int(.25*(H-1)) upperLeft = (x1,y1) bW = int(W*.15) bH = int(H*.15) x2 = x1 + bW y2 = y1 + bH lowerRight = (x2, y2) xDelta = 1 yDelta = 1 boxColor = (0,255,255) boxThickness = -1 while True: deltaT = time.time() - tStart tStart=time.time() fps = fps*.95 + (1/deltaT)*.05 frame= piCam.capture_array() frame=cv2.flip(frame,-1) myText = "FPS: "+str(round(fps,1)) cv2.putText(frame,myText,textLowerLeft,fontFace,fontScale,fontColor,fontThickness) cv2.rectangle(frame,upperLeft,lowerRight,boxColor,boxThickness) x1=x1+xDelta y1=y1+yDelta x2 = x1 +bW y2 = y1 +bH upperLeft = (x1,y1) lowerRight = (x2,y2) if x1<=0 or x2>=(W-1): xDelta=xDelta*(-1) if y1<=0 or y2>=(H-1): yDelta=yDelta*(-1) cv2.imshow("Camera", frame) cv2.moveWindow("Camera",0,60) if cv2.waitKey(1)==ord('q'): break cv2.destroyAllWindows() print('Program Terminated') |