Commit 45a277f0 authored by Gihan Jayatilaka's avatar Gihan Jayatilaka

working!

parent 2a8e1db7
......@@ -3,8 +3,50 @@ import sys
import numpy as np
import cv2
from keras.models import load_model
import threading
GMM=False
alphas=[]
N=None
means=None
vars=None
meanVars=None
def videoToVarThread(rgbX,X,alp,channel):
mean=np.zeros(shape=rgbX.shape[1:],dtype=np.float16)
var=np.zeros(shape=rgbX.shape[1:],dtype=np.float16)
xNew=[]
for i in range(len(rgbX)):
fr=np.copy(rgbX[i])
var=var*(1-alp)+np.square(fr-mean)*alp
mean=mean*(1-alp)+ fr*alp
meanVar=(np.mean(var,axis=2))
xNew.append(meanVar)
if i%1000==0:
print("videoToVar({},X[{}]) complete".format(alp,i))
xNew=np.array(xNew)
xNew=(xNew-np.mean(xNew))/np.std(xNew)
X[:,:,:,channel]=xNew
print("Variance video size {} for channel {} with alpha {}".format(xNew.shape,channel,alp))
if __name__ == "__main__":
if GMM:
alphas=[0.01,0.02,0.05,0.1,0.2,0.5,0.8]
N=len(alphas)
means=np.zeros(shape=(299,299,3,N),dtype=np.float16)
vars=np.zeros(shape=(299,299,3,N),dtype=np.float16)
meanVars=np.zeros(shape=(299,299,N),dtype=np.float16)
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]
......@@ -38,32 +80,104 @@ if __name__ == "__main__":
zer=np.full(shape=(1,8),fill_value=0,dtype=np.float)
one=np.full(shape=(1,8),fill_value=1,dtype=np.float)
X=[]
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))
# fr=(fr/128.0)-1.0
# fr=fr.astype(np.float16)
X.append(fr)
fr=(fr.astype(np.float32)/128.0)-1
if len(X)==NO_FRAMES:
break
Xcolor=np.array(X)
print("Finished building Xcolor")
X=np.ndarray(shape=(Xcolor.shape[0],Xcolor.shape[1],Xcolor.shape[2],3+len(alphas)),dtype=np.float16)
X[:,:,:,:3]=(Xcolor/128.0)-1.0
print("Finished allocating space for X")
thr=[]
for a in range(len(alphas)):
thr.append(threading.Thread(target=videoToVarThread,args=(Xcolor,X,alphas[a],3+a)))
# Xvar=videoToVar(Xcolor,alphas[a])
# Xvar=(Xvar-np.mean(Xvar))/np.std(Xvar)
# X[:,:,:,3+a]=Xvar
# startedThreadCount=0
# while startedThreadCount<len(thr):
# for t in range(startedThreadCount,min(len(thr),startedThreadCount+CPUS)):
# thr[t].start()
# for t in range(startedThreadCount,min(len(thr),startedThreadCount+CPUS)):
# thr[t].join()
# startedThreadCount=min(len(thr),startedThreadCount+CPUS)
for a in range(len(thr)):
thr[a].start()
thr[a].join()
for a in range(len(thr)):
pass
# pass
# if GMM:
# for a in range(N):
# alp=ALPHA[a]
# vars[:,:,:,a]=vars[:,:,:,a]*(1.0-alp)+np.square(fr-means[:,:,:,a])*alp
# means[:,:,:,a]=means[:,:,:,a]*(1.0-alp)+ fr*alp
# # print(vars.shape,means.shape,meanVars.shape)
# meanVars=np.mean(vars,axis=2)
#
# meanVars=np.reshape(meanVars,newshape=(1,299,299,N))
# fr=np.reshape(fr,newshape=(1,299,299,3))
# # print("Before concatenation {} {}".format(fr.shape,meanVars.shape))
# fr=np.concatenate((fr,meanVars),axis=3)
# # print(fr.shape,"after concatenation")
#
#
#
# for i in range(fr.shape[3]):
# cv2.imshow("channel {}".format(i),np.reshape(fr[0,:,:,i],(299,299)) )
# cv2.waitKey(1)
# cv2.waitKey(10)
# #
# # fr=np.reshape(fr,newshape=(1,299,299,3))
# #
# # fr=(fr.astype(np.float32)/128.0)-1
Xcolor=None
yPred=model.predict(fr)
if DEBUG:
for i in range(X.shape[0]):
for j in range(3,X.shape[3]):
temp=np.reshape(X[i,:,:,j],(299,299))
print(temp.shape)
cv2.imshow("alpha {}".format(alphas[j-3]),temp)
cv2.waitKey(1)
cv2.waitKey(50)
yPred=np.maximum(yPred,zer)
yPred=np.minimum(yPred,one)
yPred=np.reshape(yPred,newshape=(4,2))
yPred[:,0]=yPred[:,0]*1920.0
yPred[:,1]=yPred[:,1]*1080.0
corners.append(yPred.astype(np.int32))
yPred=model.predict(X)
X=None
yPred=np.reshape(yPred,newshape=(len(yPred),4,2))
if len(corners)%100 == 0:
print("Finished cropping {} frames".format(len(corners)))
if len(corners)==NO_FRAMES:
break
yPred[:,:,0]=yPred[:,:,0]*1920.0
yPred[:,:,1]=yPred[:,:,1]*1080.0
corners=(yPred.astype(np.int32))
yPred=None
#
# if len(corners)%100 == 0:
# print("Finished cropping {} frames".format(len(corners)))
# if len(corners)==NO_FRAMES:
# break
......@@ -85,6 +199,15 @@ if __name__ == "__main__":
if not ret:
break
fr2=np.copy(fr)
# print(corners.shape,corners[frameIdx])
for c in range(4):
cv2.circle(fr2,center=(corners[frameIdx,c,0],corners[frameIdx,c,1]),radius=5,thickness=5,color=[255,255,255])
cv2.imshow("debug",cv2.resize(fr2,dsize=(960,540)))
cv2.waitKey(1)
destPoints=np.array([[0,0], [298,0], [298,298], [0,298]]).astype(np.float32)
sourcePoints=corners[frameIdx].astype(np.float32)
......@@ -101,8 +224,11 @@ if __name__ == "__main__":
# cv2.line(frameCopy,pt1=(yPred[j,0],yPred[j,1]),pt2=(yPred[(j+1)%4,0],yPred[(j+1)%4,1]),color=[255,0,0],thickness=1)
destFrame = cv2.warpPerspective(fr, h, (299,299))
# cv2.imshow("frame",destFrame)
# cv2.waitKey(100)
cv2.imshow("frame",destFrame)
cv2.waitKey(1)
vidOut.write(destFrame)
......@@ -115,6 +241,8 @@ if __name__ == "__main__":
if frameIdx==NO_FRAMES:
break
print("Frame no {}".format(frameIdx))
vidOut.release()
......
......@@ -22,6 +22,7 @@ if __name__ == "__main__":
fr=cv2.resize(fr,(299,299))
fr=np.reshape(fr,newshape=(1,299,299,3))
fr=(fr/128.0)-1.0
thisMsg=np.round(model.predict(fr)).astype(np.int)
......@@ -30,19 +31,23 @@ if __name__ == "__main__":
thisMsg=np.maximum(np.minimum(thisMsg,one),zer)
# print(thisMsg.shape)
cellsPerHeight=int(math.sqrt(thisMsg.shape[1]))
#assert isinstance(math.sqrt(thisMsg.shape[1]),int),"The number of bits in a frame is not a square number!!"
# print(thisMsg.shape)
assert cellsPerHeight*cellsPerHeight==thisMsg.shape[1],"The number of bits in a frame is not a square number!!"
thisMsg=np.reshape(thisMsg,newshape=(cellsPerHeight,cellsPerHeight))
thisMsg=np.transpose(thisMsg)
msg.append(thisMsg)
if len(msg)%100==0:
print("Decoded {} frames".format(len(msg)))
if len(msg)%1000==0:
break
msg=np.array(msg)
np.savez(OUTPUT_MSG_FILE,ar=msg)
......
#!/usr/bin/env bash
python ./code/video-cropping-1.py ./input/recorded.mp4 ./models/screen-detection.h5 ./dump/cropped.mp4 200 1000
python ./code/video-cropping-1.py ./input/recorded.mp4 ./models/screen-detection.h5 ./dump/cropped.mp4 0 1100
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
This diff is collapsed.
import numpy as np
import sys
print("analyze-bit-npz.py file1.npz ar file2.npz ar 1000 100")
print("analyze-bit-npz.py argv[1] [2] [3] [4] [5] [6]")
FILE_1=sys.argv[1]
AR_1=sys.argv[2]
FILE_2=sys.argv[3]
AR_2=sys.argv[4]
NO_FRAMES=int(sys.argv[5])
BITS_PER_FRAME=int(sys.argv[6])
trueBits=np.load(FILE_1)[AR_1]
predBits=np.load(FILE_2)[AR_2]
trueBits=trueBits.flatten()
predBits=predBits.flatten()
trueBits=trueBits[:NO_FRAMES]
predBits=predBits[:NO_FRAMES]
print(trueBits.shape,predBits.shape)
# trueBits=np.reshape(trueBits,newshape=NEW_SHAPE)
# predBits=np.reshape(predBits,newshape=NEW_SHAPE)
print("Accuracy= ",np.mean((trueBits==predBits).astype(np.float)))
......@@ -13,7 +13,7 @@ def genImage(label):
for x in range(noCellsSqrt):
frame[y*h:(y+1)*h,x*w:(x+1)*w,:]=label[y*noCellsSqrt+x]*255
frame=cv2.transpose(frame,-1)
# frame=cv2.transpose(frame,-1)
return frame
......@@ -32,8 +32,13 @@ if __name__ == "__main__":
if not ret:
break
temp=np.reshape(Y[f],(10,10))
# temp=np.transpose(temp)
cv2.imshow("Frame",fr)
cv2.imshow("Ground truth",genImage(np.reshape(Y[f],(100,))))
print("Frame idx={}".format(f))
cv2.imshow("Ground truth",genImage(np.reshape(temp,(100,))))
cv2.waitKey(0)
# cv2.destroyAllWindows()
cv2.destroyAllWindows()
import numpy as np
import cv2
import sys
print("python traverse-mp4-framebyframe.py video.mp4 0.5")
print("python traverse-mp4-framebyframe.py argv[1] [2]")
VIDEO_FILE=sys.argv[1]
RATIO=float(sys.argv[2])
vidIn=cv2.VideoCapture(VIDEO_FILE)
frIdx=0
while vidIn.isOpened():
ret,fr=vidIn.read()
if not ret: break
fr=cv2.resize(fr,dsize=(int(fr.shape[1]*RATIO),int(fr.shape[0]*RATIO)))
cv2.imshow("Frame {}".format(frIdx),fr)
cv2.waitKey(0)
cv2.destroyAllWindows()
frIdx+=1
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