Commit 9d55ae16 authored by Gihan Jayatilaka's avatar Gihan Jayatilaka

deepcode

parent 94e6695b
#!/usr/bin/env bash
python video-cropping-1.py ./deepcode-dump/recorded.mp4 screen-detection-model.h5 ./deepcode-dump/cropped.mp4 200
python msg-decoding-1.py ./deepcode-dump/msg-bits.npz ./deepcode-dump/decoded-msg.txt
#!/usr/bin/env bash
python msg-encoding-1.py ./deepcode-dump/msg.txt ./deepcode-dump/temp.npz 10 10
python video-encoding-1.py ./video/screen-detection/red-ori.mp4 ./deepcode-dump/temp.npz ./deepcode-dump/encoded.mp4 1000
DEBUG=True
import sys
import numpy as np
CELLS_PER_HEIGHT=0
CELLS_PER_WIDTH=0
def rowToChar(row):
bin=row[:8]
c=np.sum(bin*np.power(np.full((8),2),np.arange(0,8)))
return chr(c)
if __name__ == "__main__":
print("python msg-decoding-1.py msg.npz")
INPUT_FILE=sys.argv[1]
OUTPUT_FILE=sys.argv[2]
file=np.load(INPUT_FILE)
msg=file['ar']
msg=np.reshape(msg,(msg.shape[0]*msg.shape[1],msg.shape[2]))
ans=""
for i in range(len(msg)):
ans=ans+rowToChar(msg[i])
fd=open(OUTPUT_FILE,"w")
fd.write(ans)
fd.close()
print("Finished writing {} charactes to file {}".format(len(ans),OUTPUT_FILE))
print(ans)
DEBUG=True
CELLS_PER_HEIGHT=0
CELLS_PER_WIDTH=0
import sys
import numpy as np
def charToBin(c):
row=np.full((8),ord(c))
row=np.floor_divide(np.fmod(row,np.power(np.full((8),2),np.arange(1,9))),np.power(np.full((8),2),np.arange(0,8)))
if DEBUG: print("Char {} converted to {} = {} in decimal".format(c,row,ord(c)))
return row
def charToRow(c):
global CELLS_PER_WIDTH
bin=charToBin(c).astype(np.int32)
ans=np.zeros((CELLS_PER_WIDTH),np.int32)
ans[:8]=bin
for i in range(8,CELLS_PER_WIDTH):
xorSum=0
for j in range(i):
xorSum=np.logical_xor(xorSum,ans[j])
ans[i]=xorSum
if DEBUG: print("Bin {} with parity is {}".format(bin.astype(np.int),ans.astype(np.int)))
return ans
def TESTcharToBin():
charToBin('a')
charToBin('b')
charToBin('A')
charToBin('B')
def TESTcharToRow():
charToRow('a')
charToRow('b')
charToRow('A')
charToRow('B')
if __name__ == "__main__":
print("python msg-encoding-1.py msg.txt code.npz")
TEXT_FILE=sys.argv[1]
OUTPUT_FILE=sys.argv[2]
CELLS_PER_HEIGHT=int(sys.argv[3])
CELLS_PER_WIDTH=int(sys.argv[4])
# if DEBUG: TESTcharToBin()
# if DEBUG: TESTcharToRow()
fd = open(TEXT_FILE,'r')
msg = []
for line in fd:
for c in line:
msg.append(c)
encodedFrames=[]
bitRows=[]
for c in msg:
bitRows.append(charToRow(c))
if len(bitRows)==CELLS_PER_HEIGHT:
encodedFrames.append(np.array(bitRows))
bitRows=[]
if len(bitRows) != 0:
while len(bitRows) < CELLS_PER_HEIGHT:
bitRows.append(np.zeros(CELLS_PER_WIDTH))
encodedFrames.append(np.array(bitRows))
encodedFrames=np.array(encodedFrames)
encodedFrames=encodedFrames.astype(np.int32)
np.savez(OUTPUT_FILE,ar=encodedFrames)
print("Length of message was {}".format(len(msg)))
print("Finish saving encoded msg of size {} to {}".format(encodedFrames.shape,OUTPUT_FILE))
import sys
import numpy as np
import cv2
from keras.models import load_model
if __name__ == "__main__":
print("python video-cropping-1.py recorded.mp4 screen-detection-model.h5 cropped.mp4 200")
RECORDED_VIDEO_FILE=sys.argv[1]
MODEL_FILE=sys.argv[2]
OUTPUT_FILE=sys.argv[3]
FRAMES_TO_SKIP=0
try:
FRAMES_TO_SKIP=int(sys.argv[4])
except:
print("Not skipping any frames by default")
# SLOW=False
# try:
# if sys.argv[5]=='slow':
# SLOW=True
# except:
# print("Fast execution by default")
vidIn=cv2.VideoCapture(RECORDED_VIDEO_FILE)
corners=[]
model=load_model(MODEL_FILE)
for f in range(FRAMES_TO_SKIP):
ret,fr=vidIn.read()
while vidIn.isOpened():
ret,fr=vidIn.read()
if not ret:
break
fr=cv2.resize(fr,dsize=(299,299),interpolation=cv2.INTER_LINEAR)
fr=np.reshape(fr,newshape=(1,299,299,3))
yPred=model.predict(fr)
yPred=np.reshape(yPred,newshape=(4,2))
corners.append(yPred)
corners=np.array(corners)
vidIn.release()
vidIn=cv2.VideoCapture(RECORDED_VIDEO_FILE)
vidOut = cv2.VideoWriter(OUTPUT_FILE,cv2.VideoWriter_fourcc('M','J','P','G'),60,(299,299))
for f in range(FRAMES_TO_SKIP):
ret,fr=vidIn.read()
frameIdx=0
while vidIn.isOpened():
ret,fr=vidIn.read()
if not ret:
break
destPoints=np.array([[0,0], [298,0], [298,298], [0,298]]).astype(np.float32)
sourcePoints=corners[frameIdx].astype(np.float32)
h=cv2.getPerspectiveTransform(sourcePoints,destPoints)
destFrame = cv2.warpPerspective(fr, h, (299,299))
vidOut.write(destFrame)
frameIdx+=1
vidOut.release()
print("Finished writing {} cropped frames to {}".format(frameIdx,OUTPUT_FILE))
import cv2
import numpy as np
from scipy import ndimage
from scipy.signal import convolve2d
import os
import sys
import random
random.seed(625742)
import time
DEBUG = 0
GRID_X = 3
GRID_Y = 3
GAUSS = 0
DELTA = 5
WIDTH = 1920
HEIGHT = 1080
'''if GAUSS == 1:
prefix = "gauss_"
else:
prefix = "none_"'''
#<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< Initialize >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
if __name__ == "__main__":
print("python video-encoding-1.py oriVideo.mp4 msgar.npz encodedVideo.mp4 1000")
READ_VIDEO_FILE=sys.argv[1]
READ_MSG_FILE=sys.argv[2]
WRITE_VIDEO_FILE=sys.argv[3]
NO_FRAMES_MAX = 1000
try:
NO_FRAMES_MAX=int(sys.argv[4])
except:
print("Setting number of frames max to {}".format(NO_FRAMES_MAX))
msg=np.load(READ_MSG_FILE)['ar']
GRID_Y=msg.shape[1]
GRID_X=msg.shape[2]
inputVideo = cv2.VideoCapture(READ_VIDEO_FILE)
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'),60,(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)
msgIndex= 0
frameIndex=0
while inputVideo.isOpened():
if frameIndex==NO_FRAMES_MAX:
break
ret, frame = inputVideo.read()
frameIndex+=1
if ret:
mask = msg[msgIndex].astype(np.float32)
msgIndex=(msgIndex+1)%len(msg)
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*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
for col in [red,green,blue,red,green,blue,white]:
video.write(col)
inputVideo.release()
video.release()
cv2.destroyAllWindows()
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