Video-Summary/generate test footage/gen.py

55 lines
1.4 KiB
Python
Raw Normal View History

2022-01-09 19:25:44 +00:00
import math
from PIL import Image, ImageDraw
import random
2020-09-19 13:21:07 +00:00
import imageio
import glob
import os
2020-09-19 21:23:01 +00:00
import numpy as np
fps = 30
xmax = 1920
ymax = 1080
2020-12-05 19:57:24 +00:00
length = 1 # in minutes
numberOfEvents = 4
2020-09-19 21:13:44 +00:00
2020-12-05 19:57:24 +00:00
dirname = os.path.dirname(__file__)
2022-01-09 19:25:44 +00:00
outputPath = os.path.join(dirname, "out.mp4")
2020-12-05 19:57:24 +00:00
def getRandomColorString():
2022-01-09 19:25:44 +00:00
return "#{:06x}".format(random.randint(0, 256 ** 3))
2020-12-05 19:57:24 +00:00
def genVideo():
2020-09-19 21:23:01 +00:00
writer = imageio.get_writer(outputPath, fps=fps)
2020-09-22 18:25:06 +00:00
writer.append_data(np.zeros(shape=[1080, 1920, 3], dtype=np.uint8))
writer.append_data(np.zeros(shape=[1080, 1920, 3], dtype=np.uint8))
2020-09-19 13:21:07 +00:00
2020-12-05 19:57:24 +00:00
for i in range(numberOfEvents):
2022-01-09 19:25:44 +00:00
objectWidth = (5 + random.randint(0, 5)) * xmax / 100
2020-09-19 13:21:07 +00:00
objectHeight = (10 + random.randint(-5, 5)) * ymax / 100
objectX = random.randint(0, xmax)
objectY = random.randint(0, ymax)
2022-01-09 19:25:44 +00:00
objectSpeedX = random.randint(1, 5)
objectSpeedY = random.randint(1, 5)
2020-09-19 13:21:07 +00:00
color = getRandomColorString()
2022-01-09 19:25:44 +00:00
for j in range(int(fps * length * 60 / numberOfEvents)):
2020-09-19 13:21:07 +00:00
objectX -= objectSpeedX
objectY -= objectSpeedY
2022-01-09 19:25:44 +00:00
objectShape = [(objectX, objectY), (objectX + objectWidth, objectY + objectHeight)]
img = Image.new("RGB", (xmax, ymax))
img1 = ImageDraw.Draw(img)
img1.rectangle(objectShape, fill=color)
2020-09-19 21:23:01 +00:00
writer.append_data(np.array(img))
2020-09-20 20:01:54 +00:00
2020-09-19 21:23:01 +00:00
writer.close()
2020-09-19 13:21:07 +00:00
2022-01-09 19:25:44 +00:00
genVideo()