From 605a1bd4e6ba77ae0231b52b6085699d506e749a Mon Sep 17 00:00:00 2001 From: Diego Pino Garcia Date: Tue, 19 Jun 2018 21:01:16 +0200 Subject: [PATCH 1/2] Play video from file tutorial --- .gitignore | 1 + opencv/README.md | 5 +++++ opencv/play-video.py | 38 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 44 insertions(+) create mode 100644 .gitignore create mode 100755 opencv/play-video.py 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..a8ad3f0 --- /dev/null +++ b/opencv/play-video.py @@ -0,0 +1,38 @@ +#!/usr/bin/env python + +import numpy as np +import cv2 +import os +import tempfile + +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) + +# URL: https://www.youtube.com/watch?v=qRLbzpy1y8Y +video = 'data/spain-vs-germany-2008.mp4' +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() From bf4f0ca949cc30af13d5e5395bf420d013a7516d Mon Sep 17 00:00:00 2001 From: Diego Pino Garcia Date: Tue, 26 Jun 2018 19:32:05 +0200 Subject: [PATCH 2/2] Download video if it doesn't exist --- opencv/play-video.py | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/opencv/play-video.py b/opencv/play-video.py index a8ad3f0..75b26aa 100755 --- a/opencv/play-video.py +++ b/opencv/play-video.py @@ -4,6 +4,7 @@ import cv2 import os import tempfile +from subprocess import call def screenshot(img): dirname = tempfile._get_default_tempdir() @@ -12,8 +13,17 @@ def screenshot(img): print('Screenshot %s' % filename) cv2.imwrite(filename, img) -# URL: https://www.youtube.com/watch?v=qRLbzpy1y8Y +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):