Commit 3f20fffa authored by Gihan Jayatilaka's avatar Gihan Jayatilaka

a

parent 10009d29
import sys
import numpy as np
import cv2
def makeVideo(READ_VIDEO_FILE,msg,WRITE_VIDEO_FILE,DELTA):
GRID_Y=msg.shape[1]
GRID_X=msg.shape[2]
inputVideo = cv2.VideoCapture(READ_VIDEO_FILE)
NO_FRAMES_MAX=min(msg.shape[0],inputVideo.get(cv2.CAP_PROP_FRAME_COUNT))-1
ret,frame=inputVideo.read()
WIDTH=frame.shape[1]
HEIGHT=frame.shape[0]
inputVideo.release()
inputVideo = cv2.VideoCapture(READ_VIDEO_FILE)
video = cv2.VideoWriter(WRITE_VIDEO_FILE,cv2.VideoWriter_fourcc('M','J','P','G'),20,(WIDTH,HEIGHT))
one = np.ones((HEIGHT, WIDTH, 1), np.uint8)*255
zero = np.zeros((HEIGHT, WIDTH, 1), np.uint8)
white = cv2.merge([one*0.6, one*0.6, one*0.6]).astype(np.uint8)
red = cv2.merge([zero, zero, one])
green = cv2.merge([zero, one, zero])
blue = cv2.merge([one, zero, zero])
for col in [red,green,blue,red,green,blue,white,white,white,white]:
video.write(col)
frameIndex=0
while inputVideo.isOpened():
if frameIndex==NO_FRAMES_MAX:
break
ret, frame = inputVideo.read()
if ret:
mask = msg[frameIndex].astype(np.float32)
mask = np.repeat(mask, HEIGHT/GRID_Y, axis=0)
mask = np.repeat(mask, WIDTH/GRID_X, axis=1)
raw = cv2.cvtColor(cv2.resize(frame, (WIDTH, HEIGHT)), cv2.COLOR_BGR2LAB)
l,a,b = cv2.split(raw)
lnorm = np.square(l*100.0/255-50)
upper = lnorm*0.015
lower = np.sqrt(lnorm + 20)
delt = np.divide((upper + lower), lower)*DELTA[frameIndex]*2.55
lmod = l + np.multiply(mask, delt)
calib = np.max(lmod)
if calib > 255:
lmod = np.uint8((lmod/calib)*255)
else:
lmod = np.uint8(lmod)
img = cv2.merge([lmod, a, b])
img = cv2.cvtColor(img, cv2.COLOR_LAB2BGR)
video.write(img)
else:
break
frameIndex+=1
if frameIndex%100==0:
print("{} frames of video encoded".format(frameIndex))
for col in [red,green,blue,red,green,blue,white]:
video.write(col)
inputVideo.release()
video.release()
cv2.destroyAllWindows()
if __name__ == "__main__":
np.random.seed(0)
print("python gen-rand-train-data.py video.mp4 10000 10 10 1.0 10.0 vidOut.mp4")
print("python gen-rand-train-data.py [1] [2] [3] [4] [5] [6] [7]")
VIDEO_IN_FILE=sys.argv[1]
NO_FRAMES=int(sys.argv[2])
GRID_HEIGHT=int(sys.argv[3])
GRID_WIDTH=int(sys.argv[4])
DELTA_MIN=float(sys.argv[5])
DELTA_MAX=float(sys.argv[6])
VIDEO_OUT_FILE=sys.argv[7]
randMsg=np.random.randint(low=0,high=2,size=(NO_FRAMES,GRID_HEIGHT,GRID_WIDTH),dtype=np.int32)
deltas=np.random.uniform(low=DELTA_MIN,high=DELTA_MAX,size=(NO_FRAMES))
print(deltas)
makeVideo(VIDEO_IN_FILE,randMsg,VIDEO_OUT_FILE,deltas)
print("FINISHED SUCCESSFULLY")
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment