Created
December 1, 2017 19:35
-
-
Save petrblahos/ceb78e2b009c2253ea1c0f4d093abe50 to your computer and use it in GitHub Desktop.
A python script that generates a video with a "ghost" effect, like this one: https://www.youtube.com/watch?v=Op3OuOc-9CY
This file contains 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
import os.path | |
import cv2 | |
import numpy as np | |
import pafy | |
class AddressableQueue(object): | |
""" | |
Creates a data structure that will hold some number of records. | |
A newly added record is always put on the position 0, the previusly | |
first record will become second, second will become third, and so | |
on. The structure only holds some defined number of records. | |
""" | |
def __init__(self, max_records=20): | |
self.max_records = max_records | |
self.data = [] | |
def add(self, i): | |
self.data.append(i) | |
if len(self.data) > 2*self.max_records: | |
del self.data[0:self.max_records] | |
def get_at_or_first(self, idx): | |
""" | |
Returns the idx-th record, or the first record if there is | |
nothing on that index. | |
""" | |
if idx >= len(self.data): | |
idx = 0 | |
return self.data[-idx-1] | |
def main(url): | |
video = pafy.new(url) | |
best = video.getbest() | |
fn = best.generate_filename() | |
if not os.path.exists(fn): | |
print("Downloading %s" % fn) | |
best.download(filepath=fn) | |
else: | |
print("Using %s" % fn) | |
camera = cv2.VideoCapture(fn) | |
prev = None | |
pic_queue = AddressableQueue(max_records=20) | |
(grabbed, frame) = camera.read() | |
prev = frame | |
out_buf = np.zeros(shape=(frame.shape[0]*2, frame.shape[1]*2, frame.shape[2]), | |
dtype=np.uint8) | |
BASE = 10 | |
FRAME_COUNT = 10 | |
while grabbed: | |
gray0 = cv2.cvtColor(prev, cv2.COLOR_BGR2GRAY) | |
gray1 = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) | |
diff = cv2.absdiff(gray1, gray0) | |
diff = cv2.threshold(diff, 35, 0, cv2.THRESH_TOZERO)[1] | |
pic_queue.add(diff) | |
out = frame | |
shadow = np.zeros_like(diff) | |
for i in range(0, FRAME_COUNT): | |
white = pic_queue.get_at_or_first(BASE + i) | |
shadow = cv2.addWeighted(shadow, 1.0, white, 1.0/(1 + i), 0) | |
white = cv2.cvtColor(white, cv2.COLOR_GRAY2RGB) | |
out = cv2.addWeighted(out, 1.0, white, 1.0/(1 + i), 0) | |
out_buf[0:frame.shape[0],0:frame.shape[1],:] = frame | |
out_buf[frame.shape[0]:2*frame.shape[0],0:frame.shape[1],:] = out | |
out_buf[0:frame.shape[0],frame.shape[1]:2*frame.shape[1], | |
:] = cv2.cvtColor(diff, cv2.COLOR_GRAY2RGB) | |
out_buf[frame.shape[0]:2*frame.shape[0],frame.shape[1]:2*frame.shape[1], | |
:] = cv2.cvtColor(shadow, cv2.COLOR_GRAY2RGB) | |
cv2.imshow("Frame", out_buf) | |
key = cv2.waitKey(1) & 0xFF | |
if key == ord("q"): # quit | |
break | |
if key == ord("p"): # pause | |
cv2.waitKey(0) | |
if key == ord("s"): # pause | |
cv2.imwrite("frame.jpg", diff) | |
prev = frame | |
(grabbed, frame) = camera.read() | |
camera.release() | |
if "__main__" == __name__: | |
main("https://www.youtube.com/watch?v=rBDiIYaFEF0") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment