Last active
August 15, 2021 14:44
-
-
Save pedrohenriquebr/de629f8cb52dce5031c1dcdfec745dae to your computer and use it in GitHub Desktop.
BLACK SCREEN WITH OPENCHV
This file contains hidden or 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 argparse | |
| import os | |
| import cv2 | |
| import numpy as np | |
| BUILD_DIR = './build' | |
| VERSION = '0.5' | |
| if __name__ == "__main__": | |
| parser = argparse.ArgumentParser() | |
| # add the argument 'length' optional with default value 1 minute | |
| parser.add_argument("-l", "--length", help="Length of the sequence", type=str, default='10s') | |
| # add the argument 'resolution' optional with default value 480 | |
| parser.add_argument("-r", "--resolution", help="Resolution of the sequence", type=int, default=480) | |
| # add the argument 'fps' optional with default value 30 | |
| parser.add_argument("-f", "--fps", help="Frames per second", type=int, default=30) | |
| # add the argument 'output' optional with default value output.mp4 | |
| parser.add_argument("-o", "--output", help="Output file", default="output.avi") | |
| # add the argument '--debug' optional with default value False | |
| parser.add_argument("-d", "--debug", help="Debug mode", action="store_true") | |
| # add the argument '--version' optional with default value False | |
| parser.add_argument("-v", "--version", help="Show version", action="store_true") | |
| # add the argument '--color' optional with default value 'black' | |
| parser.add_argument("-c", "--color", help="Color of the sequence", default='black', choices=['black', 'white', 'red', 'green', 'blue', 'yellow', 'random']) | |
| # get the arguments | |
| args = parser.parse_args() | |
| # show the version | |
| if args.version: | |
| print('version: ' + VERSION) | |
| exit(0) | |
| # show the debug mode | |
| if args.debug: | |
| print("Debug mode") | |
| # get all the arguments | |
| length = args.length | |
| resolution = args.resolution | |
| fps = args.fps | |
| output = args.output | |
| debug = args.debug | |
| color = args.color | |
| # the length of the sequence can be in seconds or minutes | |
| if length[-1] == 's': | |
| length = int(length[:-1]) | |
| elif length[-1] == 'm': | |
| length = int(length[:-1]) * 60 | |
| else: | |
| # if the length is not in seconds or minutes, exit the program | |
| print("The length must be in seconds or minutes") | |
| exit(1) | |
| # calculate the frameSize tuple with the resolution | |
| # if the resolution is 480p, the frameSize is 640x480 | |
| fourcc = cv2.VideoWriter_fourcc(*'DIVX') | |
| # if the resolution is 720p, the frameSize is 1280x720 | |
| if resolution == 480: | |
| frameSize = (640, 480) | |
| # HD | |
| elif resolution == 768: | |
| frameSize = (1360, 768) | |
| # FULL HD | |
| elif resolution == 1080: | |
| frameSize = (1920, 1080) | |
| # 4K | |
| elif resolution == 2160: | |
| frameSize = (3840, 2160) | |
| # check if the BUILD_DIR exists | |
| if not os.path.exists(BUILD_DIR): | |
| # if not, create it | |
| os.makedirs(BUILD_DIR) | |
| out = cv2.VideoWriter(os.path.abspath(os.path.join(BUILD_DIR, output)), fourcc, fps, frameSize) | |
| frame = np.zeros((frameSize[1], frameSize[0], 3), np.uint8) | |
| # I'm using opencv, so the color is BGR | |
| frame[:] = (0, 0, 0) | |
| if color == 'black': | |
| frame[:, :, 0] = 0 | |
| frame[:, :, 1] = 0 | |
| frame[:, :, 2] = 0 | |
| elif color == 'white': | |
| frame[:, :, 0] = 255 | |
| frame[:, :, 1] = 255 | |
| frame[:, :, 2] = 255 | |
| elif color == 'red': | |
| frame[:, :, 0] = 0 | |
| frame[:, :, 1] = 0 | |
| frame[:, :, 2] = 255 | |
| elif color == 'green': | |
| frame[:, :, 0] = 0 | |
| frame[:, :, 1] = 255 | |
| frame[:, :, 2] = 0 | |
| elif color == 'blue': | |
| frame[:, :, 0] = 255 | |
| frame[:, :, 1] = 0 | |
| frame[:, :, 2] = 0 | |
| elif color == 'yellow': | |
| frame[:, :, 0] = 0 | |
| frame[:, :, 1] = 255 | |
| frame[:, :, 2] = 255 | |
| # calculate the number of frames | |
| frames = int(fps * length) | |
| for i in range(frames): | |
| # if the color is 'random', generate a random color | |
| if color == 'random': | |
| frame = np.ones((frameSize[1], frameSize[0], 3), np.uint8) | |
| frame[:, :, 0] = np.random.randint(0, 256, (frameSize[1], frameSize[0])) | |
| frame[:, :, 1] = np.random.randint(0, 256, (frameSize[1], frameSize[0])) | |
| frame[:, :, 2] = np.random.randint(0, 256, (frameSize[1], frameSize[0])) | |
| if debug: | |
| print("Frame: ", i) | |
| print("Frame size: ", frameSize) | |
| print("Frame shape: ", frame.shape) | |
| print("Frame dtype: ", frame.dtype) | |
| out.write(frame) | |
| # close | |
| out.release() | |
| cv2.destroyAllWindows() | |
This file contains hidden or 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
| numpy==1.21.1 | |
| opencv-python==4.5.3.56 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment