Commit 32500dde authored by Gihan Jayatilaka's avatar Gihan Jayatilaka

dc

parent 89329fb6
......@@ -10,10 +10,10 @@ CELLS_PER_WIDTH=0
def rowToChar(row):
bin=row[:8]
c=np.sum(bin*np.power(np.full((8),2),np.arange(0,8)))
c=np.sum(bin*np.power(np.full((8),2,dtype=np.int),np.arange(0,8)))
if not isinstance(c,int):
return chr(0)
# if not isinstance(c,np.int):
# return 'a'#chr(0)
return chr(c)
......@@ -28,7 +28,7 @@ if __name__ == "__main__":
msg=file['ar']
msg=np.reshape(msg,(msg.shape[0]*msg.shape[1],msg.shape[2]))
print(msg)
ans=""
for i in range(len(msg)):
......@@ -38,7 +38,6 @@ if __name__ == "__main__":
fd=open(OUTPUT_FILE,"w")
fd.write(ans)
fd.close()
print("Finished writing {} charactes to file {}".format(len(ans),OUTPUT_FILE))
print(ans)
......
......@@ -26,7 +26,7 @@ def charToRow(c):
ans[i]=xorSum
if DEBUG: print("Bin {} with parity is {}".format(bin.astype(np.int),ans.astype(np.int)))
return ans
return ans.astype(np.int)
def TESTcharToBin():
......
DEBUG = True
import sys
import numpy as np
import cv2
......@@ -13,12 +14,17 @@ if __name__ == "__main__":
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")
NO_FRAMES=99999999
try:
NO_FRAMES=int(sys.argv[5])
except:
print("Cropping the full video.")
MANUAL=False
try:
MANUAL=(sys.argv[6]=='manual')
except:
print("Automatic cropping")
vidIn=cv2.VideoCapture(RECORDED_VIDEO_FILE)
corners=[]
......@@ -29,6 +35,9 @@ if __name__ == "__main__":
for f in range(FRAMES_TO_SKIP):
ret,fr=vidIn.read()
zer=np.full(shape=(1,8),fill_value=0,dtype=np.float)
one=np.full(shape=(1,8),fill_value=1,dtype=np.float)
while vidIn.isOpened():
ret,fr=vidIn.read()
if not ret:
......@@ -37,12 +46,32 @@ if __name__ == "__main__":
fr=cv2.resize(fr,dsize=(299,299),interpolation=cv2.INTER_LINEAR)
fr=np.reshape(fr,newshape=(1,299,299,3))
fr=(fr.astype(np.float32)/128.0)-1
yPred=model.predict(fr)
yPred=np.maximum(yPred,zer)
yPred=np.minimum(yPred,one)
yPred=np.reshape(yPred,newshape=(4,2))
corners.append(yPred)
yPred[:,0]=yPred[:,0]*1920.0
yPred[:,1]=yPred[:,1]*1080.0
corners.append(yPred.astype(np.int32))
if len(corners)%100 == 0:
print("Finished cropping {} frames".format(len(corners)))
if len(corners)==NO_FRAMES:
break
corners=np.array(corners)
if DEBUG:
print("Shape of corners = {}".format(corners.shape))
vidIn.release()
vidIn=cv2.VideoCapture(RECORDED_VIDEO_FILE)
vidOut = cv2.VideoWriter(OUTPUT_FILE,cv2.VideoWriter_fourcc('M','J','P','G'),60,(299,299))
......@@ -60,16 +89,31 @@ if __name__ == "__main__":
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)
# if DEBUG:
# yPred=sourcePoints
# frameCopy=np.copy(fr)
# for j in range(yPred.shape[0]):
# #Predicted
# cv2.circle(frameCopy,center=(yPred[j,0],yPred[j,1]),radius=3,color=[255,0,0],thickness=3)
# 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)
vidOut.write(destFrame)
frameIdx+=1
if frameIdx%100==0:
print("Finished saving {} cropped frames".format(frameIdx))
frameIdx+=1
if frameIdx==NO_FRAMES:
break
vidOut.release()
......
'''
This is the code from screen-detection-2.py
Please do not update anything in this file.
If you want to update something, please change it in screen-detection-2.py
'''
DEBUG=True
RESIZE_RATIO=0.3
import sys
import cv2
import numpy as np
clicksXY=[]
finishGettingInput=False
vidIn = None
vidOut = None
frame = None
def resizedFrame(frame,ratio=RESIZE_RATIO):
return cv2.resize(frame,(int(frame.shape[1]*ratio),int(frame.shape[0]*ratio)))
def mouse_callback(event, x, y, flags, params):
global frame,vidIn,vidOut,clicksXY,finishGettingInput
if event==1:
clicksXY.append([x,y])
while len(clicksXY)>4:
clicksXY.pop(0)
print(x,y)
ret,frame=readFrame(vidIn)
assert ret, "Error in reading video"
for i in range(len(clicksXY)):
cv2.circle(frame,(clicksXY[i][0],clicksXY[i][1]),4,[255,255,255],thickness=4)
cv2.line(frame,(clicksXY[i][0],clicksXY[i][1]),(clicksXY[(i+1)%len(clicksXY)][0],clicksXY[(i+1)%len(clicksXY)][1]),[255,255,255],thickness=1)
cv2.imshow("image",frame)
if event==cv2.EVENT_FLAG_RBUTTON:
finishGettingInput=True
def readFrame(vid):
ret,frame=vid.read()
if not ret:
return False, None
else:
frame=resizedFrame(frame)
return True, frame
def mainFunc():
H=299
W=299
global frame,vidIn,vidOut,clicksXY
inputFile=sys.argv[1]#"./video/real00.avi"
outputFile="./tempOut.mp4"#sys.argv[2]
if len(sys.argv)>2:
outputFile=sys.argv[2]
SKIP_FRAMES=0
try:
SKIP_FRAMES=int(sys.argv[3])
except:
print("Not skipping any frames")
NO_FRAMES=99999999
try:
NO_FRAMES=int(sys.argv[4])
except:
print("Processing the full video")
vidIn=cv2.VideoCapture(inputFile)
vidOut=cv2.VideoWriter(outputFile,cv2.VideoWriter_fourcc('M','J','P','G'),10,(H,W))
ret,frame=readFrame(vidIn)
assert ret , "No frame returned"
cv2.imshow("image",frame)
cv2.setMouseCallback('image', mouse_callback)
while not finishGettingInput:
cv2.waitKey(1)
cv2.destroyWindow("image")
vidIn.release()
vidIn=cv2.VideoCapture(inputFile)
destPoints="[[0,0], [298,0], [298,298], [0,298]]"#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)
destPoints=np.array(eval(destPoints)).astype(np.float32)
print("Source points shape = {}, dest = {}".format(sourcePoints.shape,destPoints.shape))
h=cv2.getPerspectiveTransform(sourcePoints,destPoints)
for _ in range(SKIP_FRAMES):
ret,frame=readFrame(vidIn)
frameNo=0
while True:
ret,frame=readFrame(vidIn)
if not ret:
break
destFrame = cv2.warpPerspective(frame, h, (299,299))
cv2.imshow("Oriented frame",destFrame)
vidOut.write(destFrame)
for i in range(len(clicksXY)):
cv2.circle(frame,(clicksXY[i][0],clicksXY[i][1]),4,[255,255,255],thickness=4)
cv2.line(frame,(clicksXY[i][0],clicksXY[i][1]),(clicksXY[(i+1)%len(clicksXY)][0],clicksXY[(i+1)%len(clicksXY)][1]),[255,255,255],thickness=1)
cv2.imshow("Original frame",frame)
cv2.waitKey(1)
frameNo+=1
if frameNo==NO_FRAMES:
break
cv2.destroyAllWindows()
vidIn.release()
vidOut.release()
if __name__ == '__main__':
mainFunc()
......@@ -23,7 +23,13 @@ if __name__ == "__main__":
fr=np.reshape(fr,newshape=(1,299,299,3))
thisMsg=model.predict(fr)
thisMsg=np.round(model.predict(fr)).astype(np.int)
one=np.full(shape=thisMsg.shape,fill_value=1,dtype=np.int)
zer=np.full(shape=thisMsg.shape,fill_value=0,dtype=np.int)
thisMsg=np.maximum(np.minimum(thisMsg,one),zer)
# print(thisMsg.shape)
cellsPerHeight=int(math.sqrt(thisMsg.shape[1]))
......@@ -35,6 +41,9 @@ if __name__ == "__main__":
thisMsg=np.reshape(thisMsg,newshape=(cellsPerHeight,cellsPerHeight))
msg.append(thisMsg)
if len(msg)%100==0:
print("Decoded {} frames".format(len(msg)))
msg=np.array(msg)
np.savez(OUTPUT_MSG_FILE,ar=msg)
......
......@@ -107,6 +107,9 @@ if __name__ == "__main__":
else:
break
if frameIndex%100==0:
print("{} frames of video encoded".format(frameIndex))
for col in [red,green,blue,red,green,blue,white]:
video.write(col)
......
#!/usr/bin/env bash
python ./code/video-cropping-1.py ./input/recorded.mp4 ./models/screen-detection.h5 ./dump/cropped.mp4 200
python ./code/video-cropping-1.py ./input/recorded.mp4 ./models/screen-detection.h5 ./dump/cropped.mp4 200 1000
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
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