Commit c75ec16f authored by Gihan Jayatilaka's avatar Gihan Jayatilaka

experimental setup

parent 36e2f607
import cv2
import numpy as np
from scipy import ndimage
from scipy.signal import convolve2d
import os
import sys
import random
import time
WIDTH = 1920
HEIGHT = 1080
NUMPATTERN = 20000
REPEAT = 20
GRID_X = 5
GRID_Y = 5
GAUSS = 0
DELTA = 5
NUMPAT33 = 384
FILE="mario"
DEBUG = 0
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))
EXTENSION=".mp4"
FILE_NAME=FILE+EXTENSION
if GAUSS == 1:
prefix = "gauss_"
else:
prefix = "none_"
def genPatten(index):
info = np.zeros((GRID_Y, GRID_X), np.float)
tem = patterns[index]
for i in range(GRID_X):
for j in range(GRID_Y):
if tem & 0x01 > 0:
info[j][i] = 0
else:
info[j][i] = 1
tem = tem >> 1
info = np.repeat(info, HEIGHT/GRID_Y, axis=0)
info = np.repeat(info, WIDTH/GRID_X, axis=1)
if GAUSS == 1:
info = ndimage.gaussian_filter(info, (int(WIDTH/GRID_X/16),int(HEIGHT/GRID_Y/16)), 0)
return info
#Generate patterns
patterns = np.zeros(NUMPATTERN, dtype=np.int)
codebook = np.zeros(NUMPAT33, dtype=np.int)
pattern33 = np.zeros(NUMPATTERN, dtype=np.int)
tem = np.arange(512)
random.shuffle(tem)
for i in range(NUMPAT33):
codebook[i] = tem[i]
for i in range(NUMPATTERN):
value = patterns[i]
while value in patterns[0:i+1]:
value = random.randint(0, 2**(GRID_X * GRID_Y))
patterns[i] = value
print("Finish preprocessing")
#<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< Initialize >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
video = cv2.VideoWriter(FILE+prefix + str(NUMPAT33) + "_" + str(DELTA) + "_mix.mp4",cv2.VideoWriter_fourcc('M','J','P','G'),60,(WIDTH,HEIGHT))
patfile = open(FILE+prefix + str(NUMPAT33) + "_" + str(DELTA) + "_mix.csv", '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])
video.write(red)
patfile.write("0\n")
video.write(green)
patfile.write("0\n")
video.write(blue)
patfile.write("0\n")
video.write(red)
patfile.write("0\n")
video.write(green)
patfile.write("0\n")
video.write(blue)
patfile.write("0\n")
for i in range(4):
video.write(white)
patfile.write("0\n")
inputVideo = cv2.VideoCapture(FILE_NAME)
frameindex = 0
# for i in range(200):
# ret, frame = inputVideo.read()
# frameindex += 1
for patindex in range(int(NUMPATTERN)):
ret, frame = inputVideo.read()
if ret:
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(patindex);
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(patterns[patindex]) + "\n")
print("Pattern: " + str(patindex) + ", Image name: " + str(frameindex))
else:
break
frameindex += 1
inputVideo.release()
video.write(red)
patfile.write("0,0,none\n")
video.write(green)
patfile.write("0,0,none\n")
video.write(blue)
patfile.write("0,0,none\n")
video.write(red)
patfile.write("0,0,none\n")
video.write(green)
patfile.write("0,0,none\n")
video.write(blue)
patfile.write("0,0,none\n")
patfile.close()
video.release()
cv2.destroyAllWindows()
\ No newline at end of file
import numpy as np
import cv2
import sys
import time
WIDTH = 1920
HEIGHT = 1080
video = cv2.VideoCapture('naturenone_384_5_mix.mp4')
pattern = open('naturenone_384_5_mix.csv')
label = open('nature_16090_5_split.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()
\ No newline at end of file
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"OUT_WIDTH = 1000\n",
"OUT_HEIGHT = 540"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Include basics\n",
"\n",
"import subprocess\n",
"import numpy as np\n",
"import cv2\n",
"import sys\n",
"import os\n",
"import time"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"#Extract frames\n",
"WIDTH = 1920\n",
"HEIGHT = 1080\n",
"OUT_WIDTH = 1000\n",
"OUT_HEIGHT = 540\n",
"OUTIMG = True\n",
"\n",
"inputFileName = \"feb24/rec_16090_5_split.mp4\"#Video recorded by the phone\n",
"tranFileName = \"feb24/tran_16090_5_split.csv\"#CSV timestamp,groundTruth generated by the PC\n",
"outputFileName = \"feb24/data_16090_5_split.npz\"#'arr0'--->frame recorded by camera, arr1------>label\n",
"\n",
"print((OUT_HEIGHT, OUT_WIDTH))\n",
"\n",
"SYN = 0\n",
"FINE = 0\n",
"\n",
"frameTS = []\n",
"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()\n",
"stamps = output.decode(\"utf-8\").rstrip().split('\\n')\n",
"for ts in stamps:\n",
" frameTS.append(float(ts))\n",
"print(\"Timestamps loaded\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"times = []\n",
"pat = []\n",
"print(\"Read tran file\")\n",
"with open(tranFileName, 'r') as tranfile:\n",
" line = tranfile.readline().rstrip()\n",
" attrs = line.split(\",\")\n",
" SYN = int(attrs[0])\n",
" FINE = float(attrs[1])\n",
" for line in tranfile:\n",
" tem = np.zeros((56,76), dtype='byte')\n",
" attrs = line.rstrip().split(\",\")\n",
" times.append(float(attrs[0]))\n",
" if attrs[len(attrs)-1] == 'none':\n",
" pat.append([None])\n",
" continue\n",
" for i in range(76):\n",
" for j in range(56):\n",
" tem[j,i] = int(attrs[i*56+j+1])\n",
" if(tem[j,i] > 1) or (tem[j,i] < 0):\n",
" print(\"ERORORORORO\")\n",
" pat.append(tem)\n",
"\n",
"print(\"Num samples: \" + str(len(times)))\n",
"violate = 0\n",
"imgset = []\n",
"tranindex = 1\n",
"index = 0\n",
"x_data = np.zeros((12000, int(OUT_HEIGHT/3), int(OUT_WIDTH/4), 3))\n",
"y_label = np.zeros((12000,9), dtype='byte')\n",
"video = cv2.VideoCapture(inputFileName)\n",
"#video.set(cv2.CAP_PROP_POS_FRAMES, 8990)\n",
"while(video.isOpened()):\n",
" ret, frame = video.read()\n",
" if ret:\n",
" ts = frameTS[index] - frameTS[SYN] + FINE\n",
" dist0 = times[tranindex-1] - times[0]\n",
" dist1 = times[tranindex] - times[0]\n",
" patindex = tranindex - 10\n",
" if ts >= dist0 and ts <= dist1:\n",
" imgset.append(frame)\n",
" elif ts > dist1:\n",
" if len(imgset) <= 2:\n",
" violate = violate + 1\n",
" if OUTIMG == True:\n",
" if (tranindex < 100) or (tranindex > 11800):\n",
" cv2.imwrite(\"tem/\" + str(patindex) + \".jpg\", imgset[len(imgset)-1])\n",
" if (patindex >= 0) and (patindex < 1000):\n",
" x_data[patindex,:,:] = cv2.resize(imgset[len(imgset)-1][HEIGHT-OUT_HEIGHT:HEIGHT, 0:OUT_WIDTH,:], (int(OUT_WIDTH/4),int(OUT_HEIGHT/3)))\n",
" # x_data[patindex,:,:,:] = imgset[len(imgset)-1][HEIGHOUT_HEIGHT/3):HEIGHT, 0:int(OUT_WIDTH/4),:]\n",
" #tem = pat33[tranindex]\n",
" for j in range(9):\n",
" y_label[patindex][j] = (pat[tranindex])[55,j] & 0x01\n",
" #tem = tem >> 1\n",
" if(patindex == 0) or (patindex == 11999):\n",
" cv2.imwrite(str(patindex) + \".jpg\", x_data[patindex,:,:,:])\n",
" print(y_label[patindex])\n",
" tranindex = tranindex + 1\n",
" imgset = [frame]\n",
" print(\"Total: \" + str(patindex) + \", Violate: \" + str(violate))\n",
" if tranindex >= len(times):\n",
" break\n",
" index = index + 1\n",
" else:\n",
" break\n",
"print(\"Zipping\")\n",
"video.release()\n",
"cv2.destroyAllWindows()\n",
"np.savez(outputFileName, x_data, y_label)\n",
"print(\"Finished\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"data = np.load(\"feb7/data_256_5_mix.npz\")\n",
"x_data = data['arr_0']\n",
"cv2.imwrite(\"test.jpg\", x_data[1000])\n",
"print(len(x_data))\n",
"y_label = data['arr_1']"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"x_data_new = np.zeros((len(x_data), 60, 60, 3), dtype='byte')\n",
"for i in range(len(x_data)):\n",
" x_data_new[i,:,:] = cv2.resize(x_data[i], (60, 60))\n",
"np.savez(\"feb7/data_256_5_mix_60.npz\", x_data_new, y_label)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import random\n",
"from scipy import ndimage\n",
"GRID_X = 160\n",
"GRID_Y = 90\n",
"DELTA = 3\n",
"\n",
"print(\"Started\")\n",
"videoin = cv2.VideoCapture(\"mix.mp4\")\n",
"video = cv2.VideoWriter(\"test_160_90_comp.avi\",cv2.VideoWriter_fourcc('M','J','P','G'),60,(WIDTH,HEIGHT))\n",
"def genmask(delt):\n",
" info = np.zeros((GRID_Y, GRID_X), np.float)\n",
" for i in range(GRID_X):\n",
" for j in range(GRID_Y):\n",
" p = random.randint(0,100)\n",
" if p > 50:\n",
" info[j][i] = 1\n",
" else:\n",
" info[j][i] = 0\n",
" info = np.repeat(info, HEIGHT/GRID_Y, axis=0)\n",
" info = np.repeat(info, WIDTH/GRID_X, axis=1)\n",
" info = ndimage.gaussian_filter(info, (4,4), 0)\n",
" return np.multiply(delt, info)\n",
"i = 0\n",
"while(videoin.isOpened()):\n",
" ret, frame = videoin.read()\n",
" if ret:\n",
" l,a,b = cv2.split(cv2.cvtColor(frame, cv2.COLOR_BGR2LAB))\n",
" lnorm = np.square(l*80.0/255-50)\n",
" upper = lnorm*0.015\n",
" lower = np.sqrt(lnorm + 20)\n",
" delt = np.divide((upper + lower), lower)*DELTA*2.55\n",
" mask = genmask(delt)\n",
" if i % 2 == 0:\n",
" lout = l + mask\n",
" else:\n",
" lout = l - mask\n",
" if np.min(lout) < 0:\n",
" lout = lout - np.min(lout)\n",
" if np.max(lout) > 255:\n",
" lout = lout*255.0/np.max(lout)\n",
" video.write(cv2.cvtColor(cv2.merge([lout.astype(np.uint8),a,b]), cv2.COLOR_LAB2BGR))\n",
" i += 1\n",
" else:\n",
" break\n",
"video.release()\n",
"print(\"Finished\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"WIDTH = 1920\n",
"HEIGHT = 1080\n",
"OUT_WIDTH = 1000\n",
"OUT_HEIGHT = 540\n",
"test = cv2.imread(\"tem/0.jpg\")\n",
"test = test[HEIGHT-OUT_HEIGHT:HEIGHT, 0:OUT_WIDTH,:]\n",
"cv2.imshow(\"Frame\",test)\n",
"cv2.waitKey()\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
}
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