Skip to content

Instantly share code, notes, and snippets.

@RodolfoFerro
Last active November 1, 2019 13:51
Show Gist options
  • Select an option

  • Save RodolfoFerro/7a8f3b3adb1bf30cfc14cd154415afb5 to your computer and use it in GitHub Desktop.

Select an option

Save RodolfoFerro/7a8f3b3adb1bf30cfc14cd154415afb5 to your computer and use it in GitHub Desktop.

Setup

You'll need (preferably) Python 3.6 or greater to run this script. The recommended way to install the requirements is using conda. To do so, you can run the following in your terminal:

# Create a new environment with conda:
conda create --name opt-flow

# Activate the environment
conda activate opt-flow

Install the requirements:

# Install the required packages using pip:
pip install -r requirements.txt

To run the script:

python OpticalFlow.py

To deactivate the environment:

conda deactivate
# Python 2/3 compatibility
from __future__ import print_function
import numpy as np
import imutils
import cv2
import sys
# Global vars:
WIDTH = 700
STEP = 16
QUIVER = (255, 100, 0)
def draw_flow(img, flow, step=STEP):
h, w = img.shape[:2]
y, x = np.mgrid[step/2:h:step, step/2:w:step].reshape(2, -1).astype(int)
fx, fy = flow[y, x].T
lines = np.vstack([x, y, x + fx, y + fy]).T.reshape(-1, 2, 2)
lines = np.int32(lines + 0.5)
vis = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)
cv2.polylines(vis, lines, 0, QUIVER)
for (x1, y1), (_x2, _y2) in lines:
cv2.circle(vis, (x1, y1), 1, (0, 255, 0), -1)
return vis
if __name__ == '__main__':
try:
fn = sys.argv[1]
except IndexError:
fn = 0
cam = cv2.VideoCapture(fn)
ret, prev = cam.read()
prev = imutils.resize(prev, width=WIDTH)
prevgray = cv2.cvtColor(prev, cv2.COLOR_BGR2GRAY)
while True:
ret, img = cam.read()
img = imutils.resize(img, width=WIDTH)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
flow = cv2.calcOpticalFlowFarneback(
prevgray, gray, None, 0.5, 3, 15, 3, 5, 1.2, 0)
prevgray = gray
cv2.imshow('flow', draw_flow(gray, flow))
ch = cv2.waitKey(5)
if ch == 27:
break
cv2.destroyAllWindows()
numpy
imutils
opencv-python
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment