Commit 10009d29 authored by Gihan Jayatilaka's avatar Gihan Jayatilaka

RS

parent e7c53242
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,dtype=np.int),np.arange(0,8)))
# if not isinstance(c,np.int):
# return 'a'#chr(0)
c=chr(c)
if not ( (c>='a' and c<='z') or (c>='A' and c<='Z') or (c>='0' and c<='9') ):
return '?'
return c
def invertRow(rowBits):
global CELLS_PER_WIDTH
return np.abs(np.full((CELLS_PER_WIDTH),1)-rowBits)
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']
CELLS_PER_HEIGHT=msg.shape[1]
CELLS_PER_WIDTH=msg.shape[2]
ans=""
for frIdx in range(msg.shape[0]):
sum=np.zeros((CELLS_PER_WIDTH),dtype=np.float)
for rowIdx in range(msg.shape[1]):
if rowIdx%2==0:
sum+=msg[frIdx][rowIdx]
else:
sum+=invertRow(msg[frIdx][rowIdx])
sum/=CELLS_PER_HEIGHT
sum=sum[:8]
sumRound=np.round(sum).astype(np.int)
if np.max(np.abs(sumRound-sum)) < CELLS_PER_HEIGHT*0.2:
ans=ans+rowToChar(sumRound)
else:
ans=ans+"?"
print(ans)
#
# msg=np.reshape(msg,(msg.shape[0]*msg.shape[1],msg.shape[2]))
# print(msg)
#
# 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
import sys
import numpy as np
import unireedsolomon as r
import unireedsolomon.rs as rs
CELLS_PER_HEIGHT=0
CELLS_PER_WIDTH=0
def toBin(x,dim=0):
if dim==0:
ans=bin(ord(x))[2:]
ans=list(ans)
ans.reverse()
while len(ans)<8:
ans.append(0)
ans.reverse()
# print(x,ans)
# print("2 -- ", ans)
# ans.reverse()
# print("3 -- ", ans)
# print(ans)
# ans=list(ans)
ans=map(int,ans)
# print("3 -- ", ans)
ans=list(ans)
# print(ans)
ans=np.array(ans)
# print("ans shape",ans.shape)
return ans
elif dim==1:
# print("AA")
ar=[]
x=list(x)
# print("AR ",x)
for i in range(len(x)):
ar.append(toBin(x[i],dim-1))
# print("AR LEN",len(ar))
return np.array(ar)
elif dim==2:
ar=[]
# print("AR ",x)
for i in range(len(x)):
ar.append(toBin(x[i],dim-1))
# print("AR LEN",len(ar))
return np.array(ar)
if __name__ == "__main__":
coder=rs.RSCoder(n=12,k=6)
# 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'].astype(np.int32)
# print("MSG",msg)
# print("MSG.shape",msg.shape)
msg=np.reshape(msg,(-1,100))
#
# print("MSG",msg)
# print("MSG.shape",msg.shape)
frames=[]
for f in range(len(msg)):
fr=np.ndarray((96),dtype=np.int32)
fr[0:8]=msg[f][1:9]
fr[8:88]=msg[f][10:90]
fr[88:96]=msg[f][91:99]
frames.append(fr)
frames=np.array(frames)
# print("FRAMES",frames)
# print("FRAMES.shape",frames.shape)
frames=np.reshape(frames,(-1,12,8))
# print("FRAMES",frames)
# print("FRAMES.shape",frames.shape)
ans=""
for f in range(frames.shape[0]):
str=""
#chars=[]#np.zeros(shape=(12),dtype=np.int32)
for c in range(12):
# print(frames[f,c,:])
# print(np.power(2,np.arange(7,-1,-1)))
# print(frames[f,c,:]*np.power(2,np.arange(7,-1,-1)))
# print(sum(frames[f,c,:]*np.power(2,np.arange(7,-1,-1))))
# print(chr(sum(frames[f,c,:]*np.power(2,np.arange(7,-1,-1)))))
# print("G = ",toBin('G'))
a=np.sum(frames[f,c,:]*np.power(2,np.arange(7,-1,-1)))
# print("a,c = {},{}".format(a,chr(a)))
# print(a)
a=chr(a)
str=str+a
# print(str)
strDecoded=coder.decode(str)
print(strDecoded)
ans=ans+strDecoded[0]
print("DECODED : ",ans)
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):
ans[i]=np.random.randint(0,2)
if DEBUG: print("Bin {} with random bits is {}".format(bin.astype(np.int),ans.astype(np.int)))
return ans.astype(np.int)
def invertRow(rowBits):
global CELLS_PER_WIDTH
return np.abs(np.full((CELLS_PER_WIDTH),1)-rowBits)
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 10 10")
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 = []
encodedFrames=[]
for line in fd:
for c in line:
msg.append(c)
for _ in range(CELLS_PER_HEIGHT):
msg.append('\0')
for msgIdx in range(len(msg)):
thisFrame=np.ndarray((CELLS_PER_HEIGHT,CELLS_PER_WIDTH),dtype=np.int)
thisFrame[0]=charToRow(msg[msgIdx])
for r in range(1,CELLS_PER_HEIGHT):
thisFrame[r]=invertRow(thisFrame[r-1])
encodedFrames.append(thisFrame)
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))
'''
msg-encoding-3 :
'''
import sys
import numpy as np
import unireedsolomon as r
import unireedsolomon.rs as rs
def toBin(x,dim=0):
if dim==0:
ans=bin(ord(x))[2:]
ans=list(ans)
ans.reverse()
while len(ans)<8:
ans.append(0)
ans.reverse()
print(x,ans)
# print("2 -- ", ans)
# ans.reverse()
# print("3 -- ", ans)
# print(ans)
# ans=list(ans)
ans=map(int,ans)
# print("3 -- ", ans)
ans=list(ans)
print(ans)
ans=np.array(ans)
print("ans shape",ans.shape)
return ans
elif dim==1:
print("AA")
ar=[]
x=list(x)
print("AR ",x)
for i in range(len(x)):
ar.append(toBin(x[i],dim-1))
print("AR LEN",len(ar))
return np.array(ar)
elif dim==2:
ar=[]
print("AR ",x)
for i in range(len(x)):
ar.append(toBin(x[i],dim-1))
print("AR LEN",len(ar))
return np.array(ar)
def breakDownString(str):
ans=[]
while len(str)>6:
ans.append(str[:6])
str=str[6:]
ans.append(str)
return ans
if __name__ == "__main__":
print("python msg-encoding-1.py msg.txt code.npz 10 10")
TEXT_FILE=sys.argv[1]
OUTPUT_FILE=sys.argv[2]
CELLS_PER_HEIGHT=int(sys.argv[3])
CELLS_PER_WIDTH=int(sys.argv[4])
coder=rs.RSCoder(n=12,k=6)
originalMsg=""
fd = open(TEXT_FILE,'r')
for line in fd:
originalMsg=originalMsg+line
segmentedMsg=breakDownString(originalMsg)
print(segmentedMsg)
encodedMsg=[]
for m in segmentedMsg:
encodedMsg.append(coder.encode(m))
# encodedMsg=np.array(encodedMsg)
print("DEBUG: ",encodedMsg)
encodedMsg=toBin(encodedMsg,2)
print(encodedMsg.shape)
encodedMsg=np.reshape(encodedMsg,newshape=(-1,8))
print("Bit msg : ", encodedMsg)
print("Bit msg length : ",encodedMsg.shape)
encodedMsg=np.reshape(encodedMsg,newshape=(-1))
print(encodedMsg.shape)
if len(encodedMsg)%96!=0:
encodedMsg=np.concatenate((encodedMsg,np.zeros((len(encodedMsg)%96),dtype=np.int32)))
print(encodedMsg)
print(encodedMsg.shape)
noEncodedFrames=len(encodedMsg)//96
encodedFrames=[]
for f in range(noEncodedFrames):
# print("DEBUG")
newFrame=np.ndarray(shape=(100))
newFrame[0]=0
newFrame[1:9]=encodedMsg[f*96 + 0 : f*96 + 8]
newFrame[9]=0
newFrame[10:90]=encodedMsg[f*96 + 8 : f*96 + 88]
newFrame[90]=0
newFrame[91:99]=encodedMsg[f*96 + 88 : f*96 + 96]
newFrame[99]=0
encodedFrames.append(newFrame)
encodedFrames=np.array(encodedFrames)
print(encodedFrames.shape)
encodedFrames=np.reshape(encodedFrames,(-1,10,10))
print("Encoded frames", encodedFrames)
print("Encoded frames shape",encodedFrames.shape)
np.savez(OUTPUT_FILE,ar=encodedFrames)
print("Length of message was {}".format(len(originalMsg)))
print("Finish saving encoded msg of size {} to {}".format(encodedFrames.shape,OUTPUT_FILE))
......@@ -38,7 +38,8 @@ def videoToVarThread(rgbX,X,alp,channel):
if __name__ == "__main__":
ORIGINAL_H=1440
ORIGINAL_W=1920
if GMM:
alphas=[0.01,0.02,0.05,0.1,0.2,0.5,0.8]
N=len(alphas)
......@@ -69,6 +70,14 @@ if __name__ == "__main__":
print("Automatic cropping")
vidIn=cv2.VideoCapture(RECORDED_VIDEO_FILE)
ret,fr=vidIn.read()
if not ret:
print("Error reading video file {}".format(RECORDED_VIDEO_FILE))
quit()
ORIGINAL_H=fr.shape[0]
ORIGINAL_W=fr.shape[1]
corners=[]
......@@ -168,8 +177,8 @@ if __name__ == "__main__":
X=None
yPred=np.reshape(yPred,newshape=(len(yPred),4,2))
yPred[:,:,0]=yPred[:,:,0]*1920.0
yPred[:,:,1]=yPred[:,:,1]*1080.0
yPred[:,:,0]=yPred[:,:,0]*float(ORIGINAL_W)
yPred[:,:,1]=yPred[:,:,1]*float(ORIGINAL_H)
corners=(yPred.astype(np.int32))
yPred=None
......
......@@ -55,6 +55,8 @@ def mainFunc():
H=299
W=299
# H=1080
# W=1920
global frame,vidIn,vidOut,clicksXY
inputFile=sys.argv[1]#"./video/real00.avi"
......@@ -93,7 +95,9 @@ def mainFunc():
vidIn.release()
vidIn=cv2.VideoCapture(inputFile)
destPoints="[[0,0], [298,0], [298,298], [0,298]]"#sys.argv[3]##
# destPoints="[[0,0], [298,0], [298,298], [0,298]]"#sys.argv[3]##
destPoints="[[0,0], [{},0], [{},{}], [0,{}]]".format(W-1,W-1,H-1,H-1)#sys.argv[3]##
sourcePoints="[[{},{}], [{},{}], [{},{}], [{},{}]]".format(clicksXY[0][0],clicksXY[0][1],clicksXY[1][0],clicksXY[1][1],clicksXY[2][0],clicksXY[2][1],clicksXY[3][0],clicksXY[3][1])#sys.argv[4]
sourcePoints=np.array(eval(sourcePoints)).astype(np.float32)
......@@ -111,7 +115,7 @@ def mainFunc():
ret,frame=readFrame(vidIn)
if not ret:
break
destFrame = cv2.warpPerspective(frame, h, (299,299))
destFrame = cv2.warpPerspective(frame, h, (W,H))
cv2.imshow("Oriented frame",destFrame)
vidOut.write(destFrame)
for i in range(len(clicksXY)):
......
......@@ -46,8 +46,8 @@ if __name__ == "__main__":
if len(msg)%100==0:
print("Decoded {} frames".format(len(msg)))
if len(msg)%1000==0:
break
# if len(msg)%1000==0:
# break
msg=np.array(msg)
np.savez(OUTPUT_MSG_FILE,ar=msg)
......
......@@ -58,7 +58,7 @@ if __name__ == "__main__":
inputVideo.release()
inputVideo = cv2.VideoCapture(READ_VIDEO_FILE)
video = cv2.VideoWriter(WRITE_VIDEO_FILE,cv2.VideoWriter_fourcc('M','J','P','G'),60,(WIDTH,HEIGHT))
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)
......
#!/usr/bin/env bash
python ./code/video-cropping-2.py ./input/recorded.mp4 ./dump/cropped.mp4 0 5000
python ./code/video-decoding-1.py ./dump/cropped.mp4 ./models/bit-decoding.h5 ./dump/msg-bits.npz
python ./code/msg-decoding-2.py ./dump/msg-bits.npz ./output/decoded-msg.txt
#!/usr/bin/env bash
python ./code/video-cropping-1.py ./input/recorded.mp4 ./models/screen-detection.h5 ./dump/cropped.mp4 0 1100
python ./code/video-cropping-1.py ./input/recorded.mp4 ./models/screen-detection.h5 ./dump/cropped.mp4 5000 2000
python ./code/video-decoding-1.py ./dump/cropped.mp4 ./models/bit-decoding.h5 ./dump/msg-bits.npz
python ./code/msg-decoding-1.py ./dump/msg-bits.npz ./output/decoded-msg.txt
python ./code/msg-decoding-3.py ./dump/msg-bits.npz ./output/decoded-msg.txt
#!/usr/bin/env bash
python ./code/msg-encoding-1.py ./input/msg.txt ./dump/temp.npz 10 10
python ./code/msg-encoding-3.py ./input/msg.txt ./dump/temp.npz 10 10
python ./code/video-encoding-1.py ./input/original.mp4 ./dump/temp.npz ./output/encoded.mp4 1000
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