diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b9d28aa --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +opencv/data diff --git a/opencv/README.md b/opencv/README.md index 9369510..0be5b56 100644 --- a/opencv/README.md +++ b/opencv/README.md @@ -9,3 +9,8 @@ Sources: * [Getting Started with Videos][Getting Started with Videos]. [Getting Started with Videos]: http://opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/py_gui/py_video_display/py_video_display.html + +## Examples + +* capture-video-gray.py: Capture video and apply gray filter. +* play-video.py: Read video from file. diff --git a/opencv/play-video.py b/opencv/play-video.py new file mode 100755 index 0000000..75b26aa --- /dev/null +++ b/opencv/play-video.py @@ -0,0 +1,48 @@ +#!/usr/bin/env python + +import numpy as np +import cv2 +import os +import tempfile +from subprocess import call + +def screenshot(img): + dirname = tempfile._get_default_tempdir() + filename = next(tempfile._get_candidate_names()) + '.png' + filename = dirname + '/' + filename + print('Screenshot %s' % filename) + cv2.imwrite(filename, img) + +def download(url, path): + print("Downloading video: %s" % url) + if not os.path.exists(os.path.dirname(video)): + os.mkdir(os.path.dirname(video)) + call(["youtube-dl", url, "-o", video]) + print("Video saved: %s" % video) + +video = 'data/spain-vs-germany-2008.mp4' +if not os.path.exists(video): + url = "https://www.youtube.com/watch?v=qRLbzpy1y8Y" + download(url, video) +cap = cv2.VideoCapture(video) + +while (True): + # Capture frame-by-frame. + success, frame = cap.read() + + # Our operations on the frame come here. + frame = cv2.resize(frame, (640, 480)) + + # Display the resulting frame. + cv2.imshow('frame', frame) + + # Actions. + key = cv2.waitKey(1) & 0xFF + if key == ord('q'): + break + elif key == ord('s'): + screenshot(frame) + +# When everything done, release the capture. +cap.release() +cv2.destroyAllWindows()