Commit a44dbcfe authored by Gihan Jayatilaka's avatar Gihan Jayatilaka

Experimental setup 2

parent 6cae9119
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
NUMPATTERN = 20000
REPEAT = 20
GRID_X = 20
GRID_Y = 20
GAUSS = 0
DELTA = 5
NUMPAT33 = 384
READ_FILE = "../butterfly"
EXTENSION = ".mp4"
OUT_FILE = "{}-code-{}x{}".format(READ_FILE,GRID_Y,GRID_X)
WIDTH = 1920
HEIGHT = 1080
try:
FILE = sys.argv[1]
GAUSS = int(sys.argv[2])
DELTA = int(sys.argv[3])
except:
print("Using hardcoded gauss={} delta={} values".format(GAUSS,DELTA))
READ_FILE_NAME=READ_FILE+EXTENSION
OUT_VIDEO_FILE_NAME=OUT_FILE+".mp4"
OUT_CSV_FILE_NAME=OUT_FILE+".csv"
'''if GAUSS == 1:
prefix = "gauss_"
else:
prefix = "none_"'''
def genPatten(code):
patternMatrix = np.zeros((GRID_Y, GRID_X), np.float)
for i in range(GRID_X):
for j in range(GRID_Y):
patternMatrix[j][i] = code & 1
code = code >> 1
patternMatrix = np.repeat(patternMatrix, HEIGHT/GRID_Y, axis=0)
patternMatrix = np.repeat(patternMatrix, WIDTH/GRID_X, axis=1)
if GAUSS == 1:
patternMatrix = ndimage.gaussian_filter(patternMatrix, (int(WIDTH/GRID_X/16),int(HEIGHT/GRID_Y/16)), 0)
return patternMatrix
#<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< Initialize >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
video = cv2.VideoWriter(OUT_VIDEO_FILE_NAME,cv2.VideoWriter_fourcc('M','J','P','G'),60,(WIDTH,HEIGHT))
patfile = open(OUT_CSV_FILE_NAME, 'w')
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)
patfile.write("0\n")
inputVideo = cv2.VideoCapture(READ_FILE_NAME)
frameindex = 0
for patindex in range(int(NUMPATTERN)):
ret, frame = inputVideo.read()
if ret:
code=random.randint(0,(2**(GRID_X*GRID_Y))-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
mask = genPatten(code)
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)
patfile.write(str(code) + "\n")
print("Pattern: " + str(patindex) + ", Image name: " + str(frameindex))
else:
break
frameindex += 1
inputVideo.release()
for col in [red,green,blue,red,green,blue,white]:
video.write(col)
patfile.write("0\n")
patfile.close()
video.release()
cv2.destroyAllWindows()
import numpy as np
import cv2
import sys
import time
WIDTH = 1920
HEIGHT = 1080
video = cv2.VideoCapture('../butterfly-code-20x20.mp4')
pattern = open('../butterfly-code-20x20.csv')
label = open('../butterfly-code-timestamp-20x20.csv', 'w')
label.write("0,0\n")
black = np.zeros((HEIGHT, WIDTH, 3), np.uint8)
cv2.namedWindow("frame", cv2.WND_PROP_FULLSCREEN)
cv2.setWindowProperty("frame",cv2.WND_PROP_FULLSCREEN,cv2.WINDOW_FULLSCREEN)
cv2.imshow('frame',black)
cv2.waitKey()
while video.isOpened():
ret, frame = video.read()
pat = pattern.readline()
if ret:
label.write(str(time.clock_gettime(time.CLOCK_MONOTONIC)) + "," + pat)
cv2.imshow('frame', frame)
if cv2.waitKey(100) & 0xFF == ord('q'):
break
else:
break
video.release()
cv2.destroyAllWindows()
pattern.close()
label.close()
#!/usr/bin/env python
# coding: utf-8
# In[ ]:
'''
python exp-3-synchronize-frames-of-recorded-video.py inputFile.mp4 tranFile.csv outputFile noFrames noCells tempDir/
'''
# In[4]:
OUT_WIDTH = 1000
OUT_HEIGHT = 540
# In[5]:
# Include basics
import subprocess
import numpy as np
import cv2
import sys
import os
import time
# In[6]:
#Extract frames
WIDTH = 1920
HEIGHT = 1080
OUT_WIDTH = 1000
OUT_HEIGHT = 540
OUTIMG = True
inputFileName = "./OpenCamera/butterfly.mp4"#Video recorded by the phone
tranFileName = "./OpenCamera/butterfly_16090_5_split.csv"#CSV timestamp,groundTruth generated by the PC
outputFileName = "./OpenCamera/butterfly"#'arr0'--->frame recorded by camera, arr1------>label
if len(sys.argv)>=4:
inputFileName=sys.argv[1]
tranFileName=sys.argv[2]
outputFileName=sys.argv[3]
print((OUT_HEIGHT, OUT_WIDTH))
SYN = 0
FINE = 0
# In[7]:
frameTS = []
output = subprocess.Popen("ffprobe -v error -show_entries frame=pkt_pts_time -select_streams v -of csv=p=0 " + inputFileName, shell=True, stdout=subprocess.PIPE).stdout.read()
#print('FFPROBE output',output)
stamps = output.decode("utf-8").rstrip().split('\n')
for ts in stamps:
frameTS.append(float(ts))
print("Timestamps loaded")
# In[8]:
NO_FRAMES=20000
NO_CELLS=25
TEMP_DIR="tem/"
if len(sys.argv>=7):
NO_FRAMES=int(sys.argv[4])
NO_CELLS=int(sys.argv[5])
TEMP_DIR=sys.argv[6]
times = []
#pat55 = []
#pat33 = []
pat = []
print("Read tran file")
with open(tranFileName, 'r') as tranfile:
line = tranfile.readline().rstrip()
attrs = line.split(",")
SYN = int(attrs[0])
FIffprobeNE = float(attrs[1])
for line in tranfile:
attrs = line.rstrip().split(",")
times.append(float(attrs[0]))
pat.append(int(attrs[1]))
print("Num samples: " + str(len(times)))
violate = 0
imgset = []
tranindex = 1
index = 0
x_data = np.zeros((NO_FRAMES, int(OUT_HEIGHT/3), int(OUT_WIDTH/4), 3))
y_label = np.zeros((NO_FRAMES,NO_CELLS), dtype='byte')
video = cv2.VideoCapture(inputFileName)
#video.set(cv2.CAP_PROP_POS_FRAMES, 8990)
while(video.isOpened()):
ret, frame = video.read()
if ret:
ts = frameTS[index] - frameTS[SYN] + FINE
dist0 = times[tranindex-1] - times[0]
dist1 = times[tranindex] - times[0]
patindex = tranindex - 10
if ts >= dist0 and ts <= dist1:
imgset.append(frame)
elif ts > dist1:
if len(imgset) <= 2:
violate = violate + 1
if OUTIMG == True:
if (tranindex < 100) or (tranindex > NO_FRAMES-200):
cv2.imwrite(TEMP_DIR + str(patindex) + ".jpg", imgset[len(imgset)-1])
if (patindex >= 0) and (patindex < NO_FRAMES):
x_data[patindex,:,:] = cv2.resize(imgset[len(imgset)-1][HEIGHT-OUT_HEIGHT:HEIGHT, 0:OUT_WIDTH,:], (int(OUT_WIDTH/4),int(OUT_HEIGHT/3)))
tem = pat[tranindex]
for i in range(NO_CELLS):
y_label[patindex] = tem & 0x01
tem = tem >> 1
tranindex = tranindex + 1
imgset = [frame]
print("Total: " + str(patindex) + ", Violate: " + str(violate))
if tranindex >= len(times):
break
index = index + 1
if index==NO_FRAMES:
break
else:
break
print("Releasing video")
video.release()
cv2.destroyAllWindows()
noDataPoints=x_data.shape[0]
noBatches=math.ceil(noDataPoints/100)
for b in range(noBatches):
bb=b*100
np.savez(outputFileName+'{:02}'.format(x)+'.npz', X=x_data[bb:min(bb+100,x_data.shape[0])], Y=y_label[bb:min(bb+100,x_data.shape[0])])
np.savez(outputFileName+'.npz', X=x_data, Y=y_label)
print("Finished")
# In[ ]:
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"import cv2\n",
"import sys\n",
"\n",
"data=np.load('../butterfly-20x20.npz')\n",
"X=data['X']\n",
"Y=data['Y']\n",
"\n",
"\n",
"print(\"DEBUG: \",\"X\",X.shape,\"Y\",Y.shape)\n",
"\n",
"\n",
"\n",
"for i in range(X.shape[0]):\n",
" cv2.imshow(\"Image\",X[i])\n",
" cv2.waitKey(0)\n",
" print(i,Y[i])\n",
" \n",
"cv2.destroyAllWindows()\n",
"\n",
" \n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.5.6"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
#!/usr/bin/env python
# coding: utf-8
# In[ ]:
import numpy as np
import cv2
import sys
data=np.load('../video/butterfly.npz')
X=data['X']
Y=data['Y']
print("DEBUG: ","X",X.shape,"Y",Y.shape)
N=int(sys.argv[1])
print(Y[N])
cv2.imshow("Image",X[N])
cv2.waitKey(100000000)
for i in range(X.shape[0]):
#idx=int(input())
#cv2.ion()
cv2.imshow("Image",X[i])
cv2.waitKey(10)
print(i,Y[i])
cv2.destroyAllWindows()
# In[ ]:
# In[ ]:
{
"cells": [
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"X shape (10000, 360, 480, 3) Y shape (10000, 25)\n"
]
}
],
"source": [
"'''\n",
"This script is used to verify the dataset created\n",
"All you have to do is to change the FILE_NAME variable and run the program\n",
"\n",
"'''\n",
"\n",
"import numpy as np\n",
"import cv2\n",
"import sys\n",
"import random\n",
"import math\n",
"random.seed(625742)\n",
"FILE_NAME='../../video/nature-5x5.npz'\n",
"NO_FRAMES_TO_CHECK=100\n",
"SHELL=False\n",
"\n",
"\n",
"if SHELL:\n",
" if len(sys.argv)>=2:\n",
" FILE_NAME=sys.argv[1]\n",
" if len(sys.argv)==3:\n",
" NO_FRAMES_TO_CHECK=int(sys.argv[2])\n",
" \n",
" \n",
"\n",
"data=np.load(FILE_NAME)\n",
"X=data['X']\n",
"Y=data['Y']\n",
"\n",
"print(\"X shape\",X.shape,\"Y shape\",Y.shape)\n",
"\n",
"\n",
"def genImage(label):\n",
" frame=np.zeros((X.shape[1],X.shape[2],3),dtype=np.uint8)\n",
" noCellsSqrt=int(math.sqrt(len(label)))\n",
" h=cellHeight=int(frame.shape[0]/noCellsSqrt)\n",
" w=cellWidth=int(frame.shape[1]/noCellsSqrt)\n",
" for y in range(noCellsSqrt):\n",
" for x in range(noCellsSqrt):\n",
"# print(x,y,noCellsSqrt,h,w)\n",
" frame[y*h:(y+1)*h,x*w:(x+1)*w,:]=label[y*noCellsSqrt+x]*255\n",
" \n",
"# print(label[y*noCellsSqrt+x])\n",
" frame=cv2.flip(frame,-1)\n",
" return frame\n",
" \n",
"\n",
"for f in range(NO_FRAMES_TO_CHECK):\n",
" i=random.randint(0,X.shape[0]-1)\n",
" #print(i,Y[i],'\\r')\n",
" l=genImage(Y[i])\n",
"\n",
" cv2.imshow(\"Image\",X[i])\n",
" cv2.imshow(\"Label\",l)\n",
" cv2.waitKey(0)\n",
" \n",
"cv2.destroyAllWindows()\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.5.6"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
#!/usr/bin/env python
# coding: utf-8
# In[9]:
'''
This script is used to verify the dataset created
All you have to do is to change the FILE_NAME variable and run the program
'''
import numpy as np
import cv2
import sys
import random
import math
random.seed(625742)
FILE_NAME='../../video/nature-5x5.npz'
NO_FRAMES_TO_CHECK=100
SHELL=False
if SHELL:
if len(sys.argv)>=2:
FILE_NAME=sys.argv[1]
if len(sys.argv)==3:
NO_FRAMES_TO_CHECK=int(sys.argv[2])
data=np.load(FILE_NAME)
X=data['X']
Y=data['Y']
print("X shape",X.shape,"Y shape",Y.shape)
def genImage(label):
frame=np.zeros((X.shape[1],X.shape[2],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):
# print(x,y,noCellsSqrt,h,w)
frame[y*h:(y+1)*h,x*w:(x+1)*w,:]=label[y*noCellsSqrt+x]*255
# print(label[y*noCellsSqrt+x])
frame=cv2.flip(frame,-1)
return frame
for f in range(NO_FRAMES_TO_CHECK):
i=random.randint(0,X.shape[0]-1)
#print(i,Y[i],'\r')
l=genImage(Y[i])
cv2.imshow("Image",X[i])
cv2.imshow("Label",l)
cv2.waitKey(0)
cv2.destroyAllWindows()
# In[ ]:
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