From 5c1defc7fa0cd6e94aca5ad2ea1f4d277fb2b920 Mon Sep 17 00:00:00 2001 From: Diego Pino Garcia Date: Tue, 26 Jun 2018 20:03:56 +0200 Subject: [PATCH 1/3] Draw several shapes over video frames --- opencv/draw-shapes.py | 75 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100755 opencv/draw-shapes.py diff --git a/opencv/draw-shapes.py b/opencv/draw-shapes.py new file mode 100755 index 0000000..a23a74b --- /dev/null +++ b/opencv/draw-shapes.py @@ -0,0 +1,75 @@ +#!/usr/bin/env python + +import cv2 +import numpy as np +import os +import random +import tempfile + +from enum import Enum +from subprocess import call + +WIDTH = 640 +HEIGHT = 480 + +class Shape(Enum): + LINE = 1 + CIRCLE = 2 + ELLIPSE = 3 + +def shape(): + return random.choice(list(Shape)) + +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) + +def draw_shape(frame, kind): + if kind == Shape.LINE: + cv2.line(frame, (0,0), (WIDTH, HEIGHT), (255, 0, 0), 4) + elif kind == Shape.CIRCLE: + cv2.circle(frame, (WIDTH/2,HEIGHT/2), HEIGHT/2, (255, 0, 0), 4) + elif kind == Shape.ELLIPSE: + cv2.ellipse(frame, (WIDTH/2,HEIGHT/2), (WIDTH/2,HEIGHT/2), 0, 0, WIDTH, HEIGHT, 4) + +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) + +kind = shape() + +while (True): + # Capture frame-by-frame. + success, frame = cap.read() + + # Our operations on the frame come here. + frame = cv2.resize(frame, (WIDTH, HEIGHT)) + + # Draw shape (line, circle, ellipse). + draw_shape(frame, kind) + + # 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 4c0a911b86c589214dd1f4402b13dd98c3c0394b Mon Sep 17 00:00:00 2001 From: Diego Pino Garcia Date: Tue, 26 Jun 2018 21:03:40 +0200 Subject: [PATCH 2/3] Paint polygon --- opencv/draw-shapes.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/opencv/draw-shapes.py b/opencv/draw-shapes.py index a23a74b..76a75e3 100755 --- a/opencv/draw-shapes.py +++ b/opencv/draw-shapes.py @@ -16,6 +16,7 @@ class Shape(Enum): LINE = 1 CIRCLE = 2 ELLIPSE = 3 + POLYGON = 4 def shape(): return random.choice(list(Shape)) @@ -41,6 +42,11 @@ def draw_shape(frame, kind): cv2.circle(frame, (WIDTH/2,HEIGHT/2), HEIGHT/2, (255, 0, 0), 4) elif kind == Shape.ELLIPSE: cv2.ellipse(frame, (WIDTH/2,HEIGHT/2), (WIDTH/2,HEIGHT/2), 0, 0, WIDTH, HEIGHT, 4) + elif kind == Shape.POLYGON: + pts = np.array([[WIDTH/2, 0], [WIDTH, HEIGHT/3], [WIDTH-WIDTH/5, HEIGHT], [WIDTH/5, HEIGHT], [0, HEIGHT/3]], np.int32) + cv2.polylines(frame, [pts], True, (255, 0, 0), 4) + else: + raise Exception('Unknown shape: %s' % s) video = 'data/spain-vs-germany-2008.mp4' if not os.path.exists(video): From 844ec3d99df0d65b3fdd3bd2ea916894937d0d6a Mon Sep 17 00:00:00 2001 From: Diego Pino Garcia Date: Tue, 26 Jun 2018 21:04:42 +0200 Subject: [PATCH 3/3] Paint text --- opencv/draw-shapes.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/opencv/draw-shapes.py b/opencv/draw-shapes.py index 76a75e3..8dc77d7 100755 --- a/opencv/draw-shapes.py +++ b/opencv/draw-shapes.py @@ -48,6 +48,13 @@ def draw_shape(frame, kind): else: raise Exception('Unknown shape: %s' % s) +def put_text(frame, text): + font = cv2.FONT_HERSHEY_SIMPLEX + size = 4 + thick = 4 + color = (64, 192, 255) + cv2.putText(frame, "Fanequinha", (WIDTH/2-150,HEIGHT/2+200), 1, size, color, thick, cv2.LINE_AA) + video = 'data/spain-vs-germany-2008.mp4' if not os.path.exists(video): url = "https://www.youtube.com/watch?v=qRLbzpy1y8Y" @@ -66,6 +73,9 @@ def draw_shape(frame, kind): # Draw shape (line, circle, ellipse). draw_shape(frame, kind) + # Put text. + put_text(frame, 'OpenCV') + # Display the resulting frame. cv2.imshow('frame', frame)