In the video lesson above we show how to add text, rectangles and circles to video frames using OpenCV. Below we include the code with we developed in this lesson. Enjoy!
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 |
import cv2 print(cv2.__version__) width=640 height=360 myRadius=30 myColor=(0,0,0) myThick=2 fontH=2 fontT=2 myText='Paul is Boss' myFont=cv2.FONT_HERSHEY_DUPLEX upperLeft=(250,140) lowerRight=(390,220) lineW=4 cam=cv2.VideoCapture(1,cv2.CAP_DSHOW) cam.set(cv2.CAP_PROP_FRAME_WIDTH, width) cam.set(cv2.CAP_PROP_FRAME_HEIGHT,height) cam.set(cv2.CAP_PROP_FPS, 30) cam.set(cv2.CAP_PROP_FOURCC,cv2.VideoWriter_fourcc(*'MJPG')) while True: ignore, frame = cam.read() frame[140:220,250:390]=(255,0,0) cv2.rectangle(frame,upperLeft,lowerRight,(0,255,0),lineW) cv2.circle(frame,(int(width/2),int(height/2)),myRadius,myColor,myThick) cv2.putText(frame,myText,(120,60),myFont,fontH,(0,0,255),fontT) cv2.imshow('my WEBcam', frame) cv2.moveWindow('my WEBcam',0,0) if cv2.waitKey(1) & 0xff ==ord('q'): break cam.release() |