In [1]:
import os
import numpy as np
import sys
import cv2
import matplotlib.pyplot as plt
from sklearn.svm import NuSVC, SVC
import datetime
In [2]:
path = '/media/zhaoke/b0685ee4-63e3-4691-ae02-feceacff6996/data/'
In [3]:
paths = os.listdir(path)
In [4]:
len(paths)
Out[4]:
In [5]:
paths = [i for i in paths if '.txt' in i]
In [6]:
len(paths)
Out[6]:
In [7]:
t1 = paths[0]
In [8]:
f = open(path+t1, 'r')
In [9]:
lines = f.readlines()
In [10]:
lines
Out[10]:
In [11]:
lines1 = [i.replace('\n', '').split(',') for i in lines]
In [22]:
lines1 = np.array(lines1).astype(np.uint32)
In [23]:
lines1
Out[23]:
In [24]:
boxes = np.empty((640000, 9))
In [10]:
cnt = 0
for txt in paths:
f = open(path+txt, 'r')
f.close()
cnt += 1
if cnt % 1000 == 0:
print cnt
In [9]:
boxes = np.empty((640000, 9))
cnt = 0
for txt in paths:
f = open(path+txt, 'r')
lines = f.readlines()
f.close()
lines = [i.replace('\n', '').split(',') for i in lines]
lines = np.array(lines).astype(np.uint32)
boxes[cnt*8:cnt*8+8] = lines
cnt += 1
if cnt % 1000 == 0:
print cnt
In [11]:
boxes.shape
Out[11]:
In [12]:
sys.getsizeof(boxes)
Out[12]:
In [13]:
clf = NuSVC()
In [14]:
start_time = datetime.datetime.now()
print start_time
clf.fit(boxes[:8000, :8], boxes[:8000, 8])
end_time = datetime.datetime.now()
print end_time - start_time
In [25]:
start_time = datetime.datetime.now()
print start_time
clf.fit(boxes[:16000, :8], boxes[:16000, 8])
end_time = datetime.datetime.now()
print end_time - start_time
In [26]:
start_time = datetime.datetime.now()
print start_time
clf.fit(boxes[:32000, :8], boxes[:32000, 8])
end_time = datetime.datetime.now()
print end_time - start_time
In [31]:
clf.predict(boxes[10000:10008, :8])
Out[31]:
In [32]:
clf.predict(boxes[80000:80008, :8])
Out[32]:
In [33]:
boxes
Out[33]:
In [39]:
boxes_max = np.max(boxes[:, :8], axis=1)
boxes_min = np.min(boxes[:, :8], axis=1)
In [38]:
boxes[:8]
Out[38]:
In [41]:
print boxes_max
print boxes_min
In [42]:
print boxes_max.shape
print boxes_min.shape
In [44]:
boxes_max = boxes_max.reshape((-1, 1))
boxes_min = boxes_min.reshape((-1, 1))
In [45]:
print boxes_max.shape
print boxes_min.shape
In [48]:
boxes_max - boxes_min
Out[48]:
In [49]:
(boxes[:, :8] - boxes_min)/(boxes_max - boxes_min)
Out[49]:
In [50]:
a = (boxes[:, :8] - boxes_min)/(boxes_max - boxes_min)
In [51]:
a.shape
Out[51]:
In [66]:
clf = SVC()
start_time = datetime.datetime.now()
print start_time
clf.fit(a[:32000], boxes[:32000, 8])
end_time = datetime.datetime.now()
print end_time - start_time
In [71]:
clf.predict(a[80016:80100])
Out[71]:
In [68]:
clf.predict(a[10000:10008])
Out[68]:
In [72]:
clf = SVC()
start_time = datetime.datetime.now()
print start_time
clf.fit(a[:32000], boxes[:32000, 8])
end_time = datetime.datetime.now()
print end_time - start_time
In [80]:
clf.predict(a[80016:80096]).reshape((-1, 8))
Out[80]:
In [116]:
#calculate the area
def area(p):
p = p.reshape((-1, 2))
return 0.5 * abs(sum(x0*y1 - x1*y0
for ((x0, y0), (x1, y1)) in segments(p)))
def segments(p):
return zip(p, np.concatenate((p[1:], [p[0]])))
In [123]:
p = [[0, 4, 2, 2, 2, 0, 0, 0], [0, 2, 2, 2, 2, 0, 0, 0]]
In [124]:
area(np.array(p[0]))
Out[124]:
In [125]:
p = np.array(p)
In [126]:
p
Out[126]:
In [127]:
np.apply_along_axis(area, 1, p)
Out[127]:
In [129]:
boxes[:, :8]
Out[129]:
In [131]:
areas = np.apply_along_axis(area, 1, boxes[:, :8])
In [135]:
area0 = area(boxes[:, :8][0])
In [136]:
area0
Out[136]:
In [137]:
area1 = area(boxes[:, :8][1])
In [138]:
area1
Out[138]:
In [134]:
areas
Out[134]:
In [151]:
areas = areas.reshape((-1, 8))
In [152]:
areas
Out[152]:
In [154]:
id_areas = areas[:, 7]
In [155]:
id_areas
Out[155]:
In [170]:
id_areas_tile = np.tile(id_areas, (8, 1))
In [171]:
id_areas_tile
Out[171]:
In [172]:
id_areas_tile.shape
Out[172]:
In [173]:
id_areas_tile = id_areas_tile.T
In [174]:
id_areas_tile
Out[174]:
In [175]:
id_areas_tile.shape
Out[175]:
In [176]:
areas / id_areas_tile
Out[176]:
In [143]:
boxes[boxes[:, 8]==8]
Out[143]:
In [145]:
print boxes[7]
print boxes[15]
In [146]:
a = np.array([[1], [2], [3]])
In [147]:
a
Out[147]:
In [148]:
a * 3
Out[148]:
In [150]:
np.tile(a, (1, 3))
Out[150]:
In [177]:
boxes.shape
Out[177]:
In [2]:
def calc_xy(p0, p1, p2):
cos = calc_cos(p0, p1, p2)
dis = calc_dis(p0, p2)
return dis * cos, dis * np.sqrt(1 - np.square(cos))
def calc_dis(p0, p1):
return np.sqrt(np.sum(np.square(p0-p1)))
def calc_cos(p0, p1, p2):
A = p1 - p0
B = p2 - p0
num = np.dot(A, B)
demon = np.linalg.norm(A) * np.linalg.norm(B)
return num / demon
In [181]:
test.shape
Out[181]:
In [9]:
#calculate the area
def area(p):
p = p.reshape((-1, 2))
return 0.5 * abs(sum(x0*y1 - x1*y0
for ((x0, y0), (x1, y1)) in segments(p)))
def segments(p):
return zip(p, np.concatenate((p[1:], [p[0]])))
In [10]:
def calc_new_xy(boxes):
box0 = boxes[0]
box1 = boxes[1]
x, y = calc_xy(box1[4:6], box1[6:], box0[:2])
dis = calc_dis(box1[4:6], box1[6:])
area0 = area(box0)
area1 = area(box1)
return x/dis, y/dis, area0/area1
In [ ]: