Commit 50c82b92 authored by Gihan Jayatilaka's avatar Gihan Jayatilaka

man

parent 3de59144
'''
To do:
Reed Solomon --> 2k-1 hamming distance
Punctured codes
'''
'''
This program intends to match the manchester pairs in a sequence with transiion frames
'''
import sys
import numpy as np
import cv2
import math
THRESHOLD=0.90
THRESHOLD_2=0.90
PARAM_1=0.8
def genImage(label):
label=label.flatten()
frame=np.zeros((299,299,3),dtype=np.uint8)
noCellsSqrt=int(math.sqrt(len(label)))
h=cellHeight=int(frame.shape[0]/noCellsSqrt)
w=cellWidth=int(frame.shape[1]/noCellsSqrt)
for y in range(noCellsSqrt):
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)
return frame
def similarity(fr1,fr2):
return np.mean((fr1==fr2).astype(np.float32))
def invertedSimilarity(fr1,fr2):
return np.mean((fr1==(1-fr2).astype(np.int)).astype(np.float32))
def areSimilar(fr1,fr2):
return similarity(fr1,fr2)>=THRESHOLD
def areOpposite(fr1,fr2):
return invertedSimilarity(fr1,fr2)>=THRESHOLD_2
if __name__ == "__main__":
print("python manchester-matching-1.py msg.npz video.mp4 0 1000")
print("python manchester-matching-1.py argv[1] argv[2] [3] [4]")
FILE_NAME_NPZ=sys.argv[1]
FILE_NAME_VIDEO=sys.argv[2]
VIDEO_FRAMES_TO_SKIP=int(sys.argv[3])
VIDEO_FRAMES_TO_PROCESS=int(sys.argv[4])
msgFile=np.load(FILE_NAME_NPZ)
msg=msgFile['ar']
msg=msg[VIDEO_FRAMES_TO_SKIP:]
msg=msg[:VIDEO_FRAMES_TO_PROCESS]
print("msg array size",msg.shape)
vidIn=cv2.VideoCapture(FILE_NAME_VIDEO)
for _ in range(VIDEO_FRAMES_TO_SKIP):
ret,fr=vidIn.read()
f=0
while vidIn.isOpened():
ret,fr=vidIn.read()
if not ret: break
cv2.imwrite("./temp/{}.jpg".format(f),fr)
f+=1
if f%1000==0:
print("Compled deflating {} of {} frames as jpeg".format(f,VIDEO_FRAMES_TO_PROCESS))
if f==VIDEO_FRAMES_TO_PROCESS:
break
vidIn.release()
similarSets=[]
SEEK_LEN=10
f=0
while f<msg.shape[0]-SEEK_LEN-1:
similarSet=[]
similarSet.append(f)
for k in range(1,SEEK_LEN+1):
if areSimilar(msg[f],msg[f+k]):
similarSet.append(f+k)
# if len(similarSet)>1:
# print(similarSet)
similarSets.append(similarSet)
f=max(similarSet)+1
print("similar sets",len(similarSets))
opposingSetPairs=[]
# vidIn=cv2.VideoCapture(FILE_NAME_VIDEO)
for s1Idx in range(len(similarSets)):
for s2Idx in range(s1Idx,min(len(similarSets),s1Idx+1+SEEK_LEN)):
sim=similarity(msg[similarSets[s1Idx][0]], msg[similarSets[s2Idx][0]])
opp=invertedSimilarity(msg[similarSets[s1Idx][0]], msg[similarSets[s2Idx][0]])
print("( {} , {} )".format(similarSets[s1Idx],similarSets[s2Idx]),sim,opp)
if areOpposite(msg[similarSets[s1Idx][0]], msg[similarSets[s2Idx][0]]):
opposingSetPairs.append([s1Idx,s2Idx])
# input("a")
#
# vidIn.set(cv2.CAP_PROP_POS_FRAMES,similarSets[s1Idx][0])
# ret,fr=vidIn.read()
# if not ret:
# break
# fr=cv2.resize(fr,(800,600))
# cv2.imshow("fr1",fr)
# vidIn.set(cv2.CAP_PROP_POS_FRAMES,similarSets[s2Idx][0])
# ret,fr=vidIn.read()
# if not ret:
# break
# fr=cv2.resize(fr,(800,600))
# cv2.imshow("fr2",fr)
# print(msg[similarSets[s1Idx][0]])
# print(msg[similarSets[s2Idx][0]])
# cv2.waitKey(0)
# cv2.destroyAllWindows()
print(len(opposingSetPairs))
for x in opposingSetPairs:
allFrameIdxs=[]
fr0=similarSets[x[0]]
dPlusLen=len(fr0)
for a in fr0: allFrameIdxs.append(a)
fr0=fr0[0]
fr1=similarSets[x[1]]
for a in fr1: allFrameIdxs.append(a)
fr1=fr1[0]
toShow=np.zeros((1000,1800,3),dtype=np.uint8)
'''vidIn.set(cv2.CAP_PROP_POS_FRAMES,VIDEO_FRAMES_TO_SKIP+fr0)
ret,fr=vidIn.read()
if not ret:
break'''
fr=cv2.imread("./temp/{}.jpg".format(fr0))
toShow[:450,:600,:]=cv2.resize(fr,(600,450))
'''vidIn.set(cv2.CAP_PROP_POS_FRAMES,VIDEO_FRAMES_TO_SKIP+fr1)
ret,fr=vidIn.read()
if not ret:
break'''
fr=cv2.imread("./temp/{}.jpg".format(fr1))
toShow[:450,600:1200,:]=cv2.resize(fr,(600,450))
print("D+",similarSets[x[0]])
print("D-",similarSets[x[1]])
noFramesToShow=len(allFrameIdxs)
# fH=int(400/noFramesToShow)
# fW=int(fH*(8.0/6.0))
fW=min(400,int(1600/noFramesToShow))
fH=int(fW*(4.0/6.0))
ans=np.zeros((fH,fW,3),np.float)
D=0.0
for f in range(noFramesToShow):
'''vidIn.set(cv2.CAP_PROP_POS_FRAMES,VIDEO_FRAMES_TO_SKIP+allFrameIdxs[f])
ret,fr=vidIn.read()
if not ret:
break'''
fr=cv2.imread("./temp/{}.jpg".format(allFrameIdxs[f]))
toShow[450:450+fH,f*fW:(f+1)*fW,:]=cv2.resize(fr,(fW,fH))
temp=None
if f<dPlusLen:
temp=genImage(msg[allFrameIdxs[f]])
D+=1.0
else:
temp=genImage(1-msg[allFrameIdxs[f]])
D+=PARAM_1
print(temp.shape)
temp2=cv2.resize(temp,(fW,fH),cv2.INTER_LINEAR)
toShow[450+fH:450+2*fH,f*fW:(f+1)*fW,:]=temp2
if f<dPlusLen:
ans+=temp2*1.0
else:
ans+=temp2*PARAM_1
ans=(ans/D).astype(np.uint8)
toShow[50:50+fH,1250:1250+fW,:]=ans
cv2.imshow("fr",toShow)
cv2.waitKey(0)
'''
for f in range(len(similarSets)):
for k in similarSets[f]:
vidIn.set(cv2.CAP_PROP_POS_FRAMES,k)
ret,fr=vidIn.read()
if not ret:
break
fr=cv2.resize(fr,(800,600))
cv2.imshow("{}".format(k),fr)
cv2.waitKey(0)
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