Skip to content

Commit

Permalink
ready to merge
Browse files Browse the repository at this point in the history
  • Loading branch information
zeroAska committed Apr 7, 2018
2 parents 213277f + fdcbec3 commit 16db23b
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 64 deletions.
36 changes: 16 additions & 20 deletions VoIP.py
Original file line number Diff line number Diff line change
@@ -1,29 +1,25 @@
import cv2
import urllib2
import urllib.request as urllib2
import numpy as np
import sys

# Input the URL shown by IP Webcam
url = "35.3.102.178:8080"
stream = 'http://' + url + '/video'
print('Streaming from: ' + stream)
url = "35.3.71.126:8080"
tream = 'http://' + url + '/video'
print('Streaming from: ' + tream)

# Open ByteStram
videoStream = urllib2.urlopen(stream)
stream = urllib2.urlopen(tream)

# Collect Bytes and Process
packets = ''
bytes = bytes()
while True:
packets += videoStream.read(1024)
a = packets.find('\xff\xd8')
b = packets.find('\xff\xd9')
if a!=-1 and b!=-1:
jpg = packets[a:b+2]
packets = packets[b+2:]

# decode byte stream to form video
video = cv2.imdecode(np.fromstring(jpg, dtype=np.uint8),cv2.IMREAD_COLOR)
# display video
cv2.imshow('WebStream',video)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
bytes += stream.read(1024)
a = bytes.find(b'\xff\xd8')
b = bytes.find(b'\xff\xd9')
if a != -1 and b != -1:
jpg = bytes[a:b+2]
bytes = bytes[b+2:]
i = cv2.imdecode(np.fromstring(jpg, dtype=np.uint8), cv2.IMREAD_COLOR)
cv2.imshow('i', i)
if cv2.waitKey(1) == 27:
exit(0)
101 changes: 70 additions & 31 deletions data_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class Process:
remsamples() = prints total number of samples that remain from the selected lot of images
'''

def __init__(self, location, height = 224, width = 224, depth = 3):
def __init__(self, location, file = None, flag = False, height = 224, width = 224, depth = 3):
self.img2labels = dict()
self.idx2img = dict()
self.num_crops=1
Expand All @@ -37,17 +37,24 @@ def __init__(self, location, height = 224, width = 224, depth = 3):
self.remImages = []
self.remSamples = []

# flag for processing data from NCLT / Cambridge
self.flag = flag
self.file = file
# OS Walk for getting all image files
self.OSWalk()
# print number of images
self.numimages()
# populate labels dict
self.getLabels()

# populate labels dict
# False implies Cambridge Dataset, True is NCLT
if self.flag == False:
self.getLabels()
else:
self.getGround()

def OSWalk(self):
images = [os.path.join(root, name) for root, dirs, files in os.walk(self.location)
for name in files if name.endswith((".png", ".jpg", ".jpeg", ".gif"))]
for name in files if name.endswith((".png", ".jpg", ".jpeg", ".gif", ".tiff"))]
self.imageLocs = images

# for i in range(len(images)):
Expand All @@ -58,8 +65,6 @@ def OSWalk(self):
self.numImages = len(self.imageLocs)
self.remImages = np.ones((self.numImages), dtype = bool)



def generateData(self, num):
ctr = 0
indices = ['']*num
Expand Down Expand Up @@ -87,22 +92,22 @@ def generateData(self, num):
self.process(num, indices)

def centeredCrop(self, img, output_side_length):
height, width, depth = img.shape
new_height = output_side_length
new_width = output_side_length
if height > width:
new_height = output_side_length * height / width
else:
new_width = output_side_length * width / height
height_offset = (new_height - output_side_length) / 2
width_offset = (new_width - output_side_length) / 2
cropped_img = img[height_offset:height_offset + output_side_length,
width_offset:width_offset + output_side_length]
return cropped_img
height, width, depth = img.shape
new_height = output_side_length
new_width = output_side_length
if height > width:
new_height = output_side_length * height / width
else:
new_width = output_side_length * width / height
height_offset = (new_height - output_side_length) / 2
width_offset = (new_width - output_side_length) / 2
cropped_img = img[height_offset:height_offset + output_side_length,
width_offset:width_offset + output_side_length]
return cropped_img


def process(self, num, indices):
print('Generating Samples .... ')
print('Generating Samples .... Note: no img normalization ')

self.sampleImages = np.zeros((num*self.num_crops , self.height, self.width, self.depth), dtype=np.uint8)

Expand All @@ -117,6 +122,7 @@ def process(self, num, indices):
names.append(name)
means = np.mean(imgs, axis=0)
imgs = imgs - means

#temp_mean = np.mean(img, axis=0)
#temp_mean = np.mean(temp_mean, axis=0)
#temp_std = np.zeros(self.depth)
Expand All @@ -127,12 +133,19 @@ def process(self, num, indices):
# img[:,:,i] /= temp_std[i]

for i in range(num):
# rotate image if NCLT
img = imgs[i, :,:,:]
if self.flag==True:
rows, cols, _ = img.shape
M = cv2.getRotationMatrix2D((cols/2,rows/2),90,1)
img = cv2.warpAffine(img,M,(cols,rows))

# generate 128 random indices for crop
for j in range(idx, idx+self.num_crops ):
x = randint(0,31)
y = randint(0,230)
img = imgs[i, :,:,:]
self.sampleImages[j, :, :, :] = self.centeredCrop(img, 224)#img[x:x + self.height, y:y + self.width, :].copy()

self.sampleImages[j, :, :, :] = img[x:x + self.height, y:y + self.width, :].copy()
self.idx2img[j] = names[i]
idx += self.num_crops

Expand All @@ -157,11 +170,13 @@ def fetch(self, num):
self.remSamples[idx] = False

# gaussian normalization of image to have mean 0, variance 1
temp = self.sampleImages[idx, :, :, :].astype(float)
try:
labels[ctr] = self.img2labels[self.idx2img[idx]]
samples[ctr,:,:,:] = self.sampleImages[idx, :, :, :].astype(float)

except KeyError:
pdb.set_trace()
continue
ctr += 1

return [flag, samples, labels]
Expand All @@ -183,15 +198,39 @@ def getLabels(self):
line = lines[i].strip()
line = line.split()
self.img2labels[line[0]] = list(map(float,line[1:8]))

def getName(self, loc):
ctr = 0
for i in range(len(loc)):
if loc[i]=='/':
ctr += 1
if ctr==2:
return loc[i+1:]

def getGround(self):
filename = self.location + self.file
odom = np.loadtxt(filename, delimiter = ",")
length, _ = odom.shape

# the number we have to divide by to get a correct association
param = 4

for i in range(length):
name = int(odom[i, 0].item()/10**param)

x = float(odom[i, 1])
y = float(odom[i, 2])
z = float(odom[i, 3])
r = float(odom[i, 4])
p = float(odom[i, 5])
h = float(odom[i, 6])
data = [x,y,z,r,p,h]
self.img2labels[name] = data

def getName(self, loc):
if self.flag == False:
ctr = 0
for i in range(len(loc)):
if loc[i]=='/':
ctr += 1
if ctr==2:
return loc[i+1:]
else:
params = loc.split('/')
name = params[-1][:-9]
return int(name)


def reset(self):
Expand All @@ -217,4 +256,4 @@ def remsamples(self):

def remimages(self):
print('The number of images that remain are: ',np.sum(self.remImages))
return remImages
return self.remImages
22 changes: 15 additions & 7 deletions test.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import data_handler as DH
import sys
import numpy as np

import cv2
'''
Usage:
location : enter location of main directory
Expand All @@ -13,25 +12,34 @@
remimages() = prints total number of images that remain in the directory
remsamples() = prints total number of samples that remain from the selected lot of images
'''
print(sys.version)

location = "../ShopFacade/"
# location = "./2013-01-10/"
# file = 'groundtruth_2013-01-10.csv'

location = "/media/eecs568/My Passport/NCLT/2012-03-17/2012-03-17/"
file = 'groundtruth_2012-03-17.csv'

dh = DH.Process(location)
dh = DH.Process(location, file, True)

# pick number of images to pick samples from
numImages = 50
numImages = 100
dh.generateData(numImages)
dh.remimages()

dh.numsamples()
flag, images, labels = dh.fetch(60)
flag, images, labels = dh.fetch(2)
print(labels)
print(images.shape, len(labels), flag)
dh.remsamples()


# Trick to pick samples from selected images
numSamples = 60
flag = True
while flag==True:
flag, images, labels = dh.fetch(numSamples)
cv2.imshow('image', images[0,:,:,:])
cv2.waitKey(0)
pdb.set_trace()
dh.remsamples()

12 changes: 6 additions & 6 deletions train.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,15 @@ def delete_network_backups(filename_prefix):

class trainer():

def __init__(self,path_to_weight, path_to_data, beta, resume_training=False):
def __init__(self,path_to_weight, path_to_data, beta, use_quaternion=True, resume_training=False):
self.network_input_size = 224
self.output_dim = 7 if use_quaternion else 6
self.sess = tf.Session(config=tf.ConfigProto(log_device_placement=True))
if resume_training:
self.restore_network(path_to_weight)
else:
self.image_inputs = tf.placeholder(tf.float32, [None, self.network_input_size, self.network_input_size, 3])
self.label_inputs = tf.placeholder(tf.float32, [None, 7]) # [ X Y Z W P Q R]
self.label_inputs = tf.placeholder(tf.float32, [None, self.output_dim]) # [ X Y Z W P Q R]

self.network = vgg.VGG16({'data': self.image_inputs})
self.regen_regression_network()
Expand Down Expand Up @@ -84,9 +85,9 @@ def regen_regression_network(self):
fc9_input_shape = fc8.get_shape()

feed_in, dim = (fc8, fc9_input_shape[-1].value)
fc9_weights = tf.get_variable('weights', [dim, 7])
fc9_weights = tf.get_variable('weights', [dim, self.output_dim])
self.network.variable_summaries( fc9_weights, "_weights_fc" )
fc9_biases = tf.get_variable('biases', [7])
fc9_biases = tf.get_variable('biases', [self.output_dim])
op = tf.nn.xw_plus_b
fc9 = op(feed_in, fc9_weights, fc9_biases, name=scope.name)
self.init_vars = [fc8_weights, fc8_biases , fc9_weights, fc9_biases]
Expand All @@ -109,8 +110,7 @@ def restore_network(self, path_to_weight):

def build_loss(self, beta=100):
self.translation_loss = tf.sqrt(tf.nn.l2_loss(self.regression_out[0:3] - self.label_inputs[0:3]))
self.rotation_loss = tf.sqrt(tf.nn.l2_loss( self.regression_out[3:7] - self.label_inputs[3:7] ))
#self.rotation_loss = tf.sqrt(tf.nn.l2_loss( self.regression_out[3:7]/tf.norm(self.regression_out[3:7]) - self.label_inputs[3:7] ))
self.rotation_loss = tf.sqrt(tf.nn.l2_loss( self.regression_out[3:] - self.label_inputs[3:] ))
self.loss = self.translation_loss + beta * self.rotation_loss
tf.identity(self.loss, name="final_loss")

Expand Down

0 comments on commit 16db23b

Please sign in to comment.