Last active
November 7, 2024 02:36
-
-
Save villares/838749efc63ca0c8590b03783970a92a to your computer and use it in GitHub Desktop.
OpenCV Optical Flow + py5
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# https://docs.opencv.org/4.x/d4/dee/tutorial_optical_flow.html | |
import py5 | |
import numpy as np | |
import cv2 # opencv-python | |
cap = None | |
py5_img = None | |
prvs_frame = None | |
reduced_size = 600, 400 | |
def setup(): | |
py5.size(600, 400) | |
py5.launch_thread(open_capture) | |
def open_capture(): | |
global cap, hsv | |
cap = cv2.VideoCapture(0) | |
print('Capture started') | |
def draw(): | |
global py5_img, prvs_frame, flow | |
py5.background(255, 255, 0) | |
ret = False | |
if cap: | |
ret, frame = cap.read() # frame is a numpy array | |
if ret: | |
frame = cv2.resize(frame, reduced_size) | |
next_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) | |
if prvs_frame is not None: | |
flow = cv2.calcOpticalFlowFarneback(prvs_frame, next_frame, None, 0.5, 3, 15, 3, 5, 1.2, 0) | |
vmag, vang = cv2.cartToPolar(flow[..., 0], flow[..., 1]) | |
hsv = np.zeros_like(frame) | |
hsv[..., 1] = 255 | |
hsv[..., 0] = vang * 180 / np.pi / 2 # 0 a 360 / 0 1 / 0 255 | |
hsv[..., 2] = cv2.normalize(vmag, None, 0, 255, cv2.NORM_MINMAX) | |
bgr = cv2.cvtColor(hsv, cv2.COLOR_HSV2BGR) | |
py5_img = py5.create_image_from_numpy(bgr, bands='BGR', dst=py5_img) | |
py5.image(py5_img, 0, 0) | |
draw_direction(flow, 100) | |
if vmag.mean() > 1: | |
print('moved!') | |
else: | |
print('') | |
prvs_frame = next_frame | |
def exiting(): # py5 will call this for clean up on exit | |
cap.release() | |
def draw_direction(f, s): | |
py5.stroke_weight(5) | |
py5.stroke(255) | |
hw, hh = py5.width / 2, py5.height / 2 | |
py5.line(hw, hh, | |
hw + f[... , 0].mean() * s, | |
hh + f[... , 1].mean() * s) | |
py5.run_sketch(block=False) |
Author
villares
commented
Nov 5, 2024
•
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment