jakro00
12/13/2019 - 9:51 PM

OpenCV video boilerplate

OpenCV video boilerplate

import numpy as np
import cv2

cap = cv2.VideoCapture(0)
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))

x = width // 2
y = height // 2
w = width // 4

while True:
    # Capture frame-by-frame
    _, frame = cap.read()
    # Our operations on the frame come here
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    cv2.rectangle(gray, (x, y), (x + w, y + w), color=(0, 0, 255), thickness=10)
    # Display the resulting frame
    cv2.imshow("frame", gray)
    if cv2.waitKey(1) & 0xFF == ord("q"):
        break

# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()