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]:
160000

In [5]:
paths = [i for i in paths if '.txt' in i]

In [6]:
len(paths)


Out[6]:
80000

In [7]:
t1 = paths[0]

In [8]:
f = open(path+t1, 'r')

In [9]:
lines = f.readlines()

In [10]:
lines


Out[10]:
['536,292,609,301,606,323,533,313,1\n',
 '527,337,544,339,541,357,525,355,2\n',
 '638,351,658,353,655,370,636,367,3\n',
 '522,377,565,383,563,397,520,392,4\n',
 '605,388,622,390,620,405,603,402,5\n',
 '658,394,677,397,675,412,655,409,6\n',
 '514,417,746,446,739,492,507,463,7\n',
 '586,531,892,570,889,589,583,551,8\n']

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]:
array([[536, 292, 609, 301, 606, 323, 533, 313,   1],
       [527, 337, 544, 339, 541, 357, 525, 355,   2],
       [638, 351, 658, 353, 655, 370, 636, 367,   3],
       [522, 377, 565, 383, 563, 397, 520, 392,   4],
       [605, 388, 622, 390, 620, 405, 603, 402,   5],
       [658, 394, 677, 397, 675, 412, 655, 409,   6],
       [514, 417, 746, 446, 739, 492, 507, 463,   7],
       [586, 531, 892, 570, 889, 589, 583, 551,   8]], dtype=uint32)

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


1000
2000
3000
4000
5000
6000
7000
8000
9000
10000
11000
12000
13000
14000
15000
16000
17000
18000
19000
20000
21000
22000
23000
24000
25000
26000
27000
28000
29000
30000
31000
32000
33000
34000
35000
36000
37000
38000
39000
40000
41000
42000
43000
44000
45000
46000
47000
48000
49000
50000
51000
52000
53000
54000
55000
56000
57000
58000
59000
60000
61000
62000
63000
64000
65000
66000
67000
68000
69000
70000
71000
72000
73000
74000
75000
76000
77000
78000
79000
80000

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


1000
2000
3000
4000
5000
6000
7000
8000
9000
10000
11000
12000
13000
14000
15000
16000
17000
18000
19000
20000
21000
22000
23000
24000
25000
26000
27000
28000
29000
30000
31000
32000
33000
34000
35000
36000
37000
38000
39000
40000
41000
42000
43000
44000
45000
46000
47000
48000
49000
50000
51000
52000
53000
54000
55000
56000
57000
58000
59000
60000
61000
62000
63000
64000
65000
66000
67000
68000
69000
70000
71000
72000
73000
74000
75000
76000
77000
78000
79000
80000

In [11]:
boxes.shape


Out[11]:
(640000, 9)

In [12]:
sys.getsizeof(boxes)


Out[12]:
46080112

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


2017-10-23 19:50:54.741211
0:00:03.952691

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


2017-10-23 14:01:18.177875
0:00:15.398890

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


2017-10-23 14:01:38.790141
0:01:28.544892

In [31]:
clf.predict(boxes[10000:10008, :8])


Out[31]:
array([ 1.,  2.,  3.,  4.,  5.,  6.,  7.,  8.])

In [32]:
clf.predict(boxes[80000:80008, :8])


Out[32]:
array([ 7.,  2.,  7.,  7.,  2.,  7.,  7.,  7.])

In [33]:
boxes


Out[33]:
array([[ 536.,  292.,  609., ...,  533.,  313.,    1.],
       [ 527.,  337.,  544., ...,  525.,  355.,    2.],
       [ 638.,  351.,  658., ...,  636.,  367.,    3.],
       ..., 
       [ 570.,  303.,  592., ...,  569.,  327.,    6.],
       [ 416.,  360.,  669., ...,  414.,  433.,    7.],
       [ 507.,  529.,  837., ...,  506.,  558.,    8.]])

In [39]:
boxes_max = np.max(boxes[:, :8], axis=1)
boxes_min = np.min(boxes[:, :8], axis=1)

In [38]:
boxes[:8]


Out[38]:
array([[ 536.,  292.,  609.,  301.,  606.,  323.,  533.,  313.,    1.],
       [ 527.,  337.,  544.,  339.,  541.,  357.,  525.,  355.,    2.],
       [ 638.,  351.,  658.,  353.,  655.,  370.,  636.,  367.,    3.],
       [ 522.,  377.,  565.,  383.,  563.,  397.,  520.,  392.,    4.],
       [ 605.,  388.,  622.,  390.,  620.,  405.,  603.,  402.,    5.],
       [ 658.,  394.,  677.,  397.,  675.,  412.,  655.,  409.,    6.],
       [ 514.,  417.,  746.,  446.,  739.,  492.,  507.,  463.,    7.],
       [ 586.,  531.,  892.,  570.,  889.,  589.,  583.,  551.,    8.]])

In [41]:
print boxes_max
print boxes_min


[ 609.  544.  658. ...,  592.  669.  837.]
[ 292.  337.  351. ...,  303.  360.  506.]

In [42]:
print boxes_max.shape
print boxes_min.shape


(640000,)
(640000,)

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


(640000, 1)
(640000, 1)

In [48]:
boxes_max - boxes_min


Out[48]:
array([[ 317.],
       [ 207.],
       [ 307.],
       ..., 
       [ 289.],
       [ 309.],
       [ 331.]])

In [49]:
(boxes[:, :8] - boxes_min)/(boxes_max - boxes_min)


Out[49]:
array([[ 0.76971609,  0.        ,  1.        , ...,  0.0977918 ,
         0.76025237,  0.06624606],
       [ 0.9178744 ,  0.        ,  1.        , ...,  0.09661836,
         0.90821256,  0.08695652],
       [ 0.93485342,  0.        ,  1.        , ...,  0.06188925,
         0.92833876,  0.05211726],
       ..., 
       [ 0.92387543,  0.        ,  1.        , ...,  0.08304498,
         0.92041522,  0.08304498],
       [ 0.18122977,  0.        ,  1.        , ...,  0.27508091,
         0.17475728,  0.23624595],
       [ 0.00302115,  0.0694864 ,  1.        , ...,  0.20241692,
         0.        ,  0.1570997 ]])

In [50]:
a = (boxes[:, :8] - boxes_min)/(boxes_max - boxes_min)

In [51]:
a.shape


Out[51]:
(640000, 8)

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


2017-10-23 14:17:38.733661
0:00:33.870528

In [71]:
clf.predict(a[80016:80100])


Out[71]:
array([ 1.,  3.,  3.,  1.,  3.,  3.,  7.,  8.,  1.,  3.,  3.,  1.,  3.,
        3.,  1.,  8.,  1.,  3.,  3.,  3.,  3.,  3.,  1.,  8.,  1.,  2.,
        3.,  7.,  2.,  3.,  7.,  8.,  1.,  3.,  3.,  1.,  3.,  3.,  7.,
        8.,  1.,  3.,  3.,  1.,  3.,  3.,  7.,  8.,  1.,  1.,  3.,  7.,
        6.,  3.,  7.,  8.,  1.,  3.,  3.,  3.,  3.,  3.,  1.,  8.,  1.,
        3.,  3.,  1.,  3.,  3.,  7.,  8.,  1.,  3.,  3.,  1.,  3.,  3.,
        7.,  8.,  1.,  2.,  3.,  1.])

In [68]:
clf.predict(a[10000:10008])


Out[68]:
array([ 1.,  3.,  3.,  1.,  3.,  3.,  7.,  8.])

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


2017-10-23 14:20:28.124333
0:00:34.372978

In [80]:
clf.predict(a[80016:80096]).reshape((-1, 8))


Out[80]:
array([[ 1.,  3.,  3.,  1.,  3.,  3.,  7.,  8.],
       [ 1.,  3.,  3.,  1.,  3.,  3.,  1.,  8.],
       [ 1.,  3.,  3.,  3.,  3.,  3.,  1.,  8.],
       [ 1.,  2.,  3.,  7.,  2.,  3.,  7.,  8.],
       [ 1.,  3.,  3.,  1.,  3.,  3.,  7.,  8.],
       [ 1.,  3.,  3.,  1.,  3.,  3.,  7.,  8.],
       [ 1.,  1.,  3.,  7.,  6.,  3.,  7.,  8.],
       [ 1.,  3.,  3.,  3.,  3.,  3.,  1.,  8.],
       [ 1.,  3.,  3.,  1.,  3.,  3.,  7.,  8.],
       [ 1.,  3.,  3.,  1.,  3.,  3.,  7.,  8.]])

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]:
6.0

In [125]:
p = np.array(p)

In [126]:
p


Out[126]:
array([[0, 4, 2, 2, 2, 0, 0, 0],
       [0, 2, 2, 2, 2, 0, 0, 0]])

In [127]:
np.apply_along_axis(area, 1, p)


Out[127]:
array([ 6.,  4.])

In [129]:
boxes[:, :8]


Out[129]:
array([[ 536.,  292.,  609., ...,  323.,  533.,  313.],
       [ 527.,  337.,  544., ...,  357.,  525.,  355.],
       [ 638.,  351.,  658., ...,  370.,  636.,  367.],
       ..., 
       [ 570.,  303.,  592., ...,  327.,  569.,  327.],
       [ 416.,  360.,  669., ...,  445.,  414.,  433.],
       [ 507.,  529.,  837., ...,  573.,  506.,  558.]])

In [131]:
areas = np.apply_along_axis(area, 1, boxes[:, :8])

In [135]:
area0 = area(boxes[:, :8][0])

In [136]:
area0


Out[136]:
1598.0

In [137]:
area1 = area(boxes[:, :8][1])

In [138]:
area1


Out[138]:
302.0

In [134]:
areas


Out[134]:
array([  1598. ,    302. ,    328. , ...,    517.5,  18493. ,   9585. ])

In [151]:
areas = areas.reshape((-1, 8))

In [152]:
areas


Out[152]:
array([[  1598. ,    302. ,    328. , ...,    300. ,  10875. ,   6082.5],
       [  2194.5,    441. ,    430.5, ...,    414. ,  13914.5,   7056. ],
       [  1214. ,    241. ,    274. , ...,    125.5,   7626. ,   4645.5],
       ..., 
       [  1449. ,    409. ,    387.5, ...,    384. ,  15132. ,   5268.5],
       [  1865.5,    384. ,    364.5, ...,    344. ,  11440.5,   7328. ],
       [  2858.5,    542. ,    627.5, ...,    517.5,  18493. ,   9585. ]])

In [154]:
id_areas = areas[:, 7]

In [155]:
id_areas


Out[155]:
array([ 6082.5,  7056. ,  4645.5, ...,  5268.5,  7328. ,  9585. ])

In [170]:
id_areas_tile = np.tile(id_areas, (8, 1))

In [171]:
id_areas_tile


Out[171]:
array([[ 6082.5,  7056. ,  4645.5, ...,  5268.5,  7328. ,  9585. ],
       [ 6082.5,  7056. ,  4645.5, ...,  5268.5,  7328. ,  9585. ],
       [ 6082.5,  7056. ,  4645.5, ...,  5268.5,  7328. ,  9585. ],
       ..., 
       [ 6082.5,  7056. ,  4645.5, ...,  5268.5,  7328. ,  9585. ],
       [ 6082.5,  7056. ,  4645.5, ...,  5268.5,  7328. ,  9585. ],
       [ 6082.5,  7056. ,  4645.5, ...,  5268.5,  7328. ,  9585. ]])

In [172]:
id_areas_tile.shape


Out[172]:
(8, 80000)

In [173]:
id_areas_tile = id_areas_tile.T

In [174]:
id_areas_tile


Out[174]:
array([[ 6082.5,  6082.5,  6082.5, ...,  6082.5,  6082.5,  6082.5],
       [ 7056. ,  7056. ,  7056. , ...,  7056. ,  7056. ,  7056. ],
       [ 4645.5,  4645.5,  4645.5, ...,  4645.5,  4645.5,  4645.5],
       ..., 
       [ 5268.5,  5268.5,  5268.5, ...,  5268.5,  5268.5,  5268.5],
       [ 7328. ,  7328. ,  7328. , ...,  7328. ,  7328. ,  7328. ],
       [ 9585. ,  9585. ,  9585. , ...,  9585. ,  9585. ,  9585. ]])

In [175]:
id_areas_tile.shape


Out[175]:
(80000, 8)

In [176]:
areas / id_areas_tile


Out[176]:
array([[ 0.26272092,  0.04965064,  0.0539252 , ...,  0.04932182,
         1.78791615,  1.        ],
       [ 0.3110119 ,  0.0625    ,  0.0610119 , ...,  0.05867347,
         1.97200964,  1.        ],
       [ 0.26132817,  0.05187816,  0.05898181, ...,  0.02701539,
         1.64158863,  1.        ],
       ..., 
       [ 0.27503084,  0.0776312 ,  0.07355035, ...,  0.07288602,
         2.87216475,  1.        ],
       [ 0.25457151,  0.05240175,  0.04974072, ...,  0.04694323,
         1.5612036 ,  1.        ],
       [ 0.2982264 ,  0.05654669,  0.06546688, ...,  0.05399061,
         1.92936881,  1.        ]])

In [143]:
boxes[boxes[:, 8]==8]


Out[143]:
array([[ 586.,  531.,  892., ...,  583.,  551.,    8.],
       [ 581.,  436.,  917., ...,  581.,  457.,    8.],
       [ 500.,  651.,  749., ...,  498.,  669.,    8.],
       ..., 
       [ 614.,  550.,  871., ...,  614.,  570.,    8.],
       [ 666.,  606.,  926., ...,  668.,  634.,    8.],
       [ 507.,  529.,  837., ...,  506.,  558.,    8.]])

In [145]:
print boxes[7]
print boxes[15]


[ 586.  531.  892.  570.  889.  589.  583.  551.    8.]
[ 581.  436.  917.  430.  917.  451.  581.  457.    8.]

In [146]:
a = np.array([[1], [2], [3]])

In [147]:
a


Out[147]:
array([[1],
       [2],
       [3]])

In [148]:
a * 3


Out[148]:
array([[3],
       [6],
       [9]])

In [150]:
np.tile(a, (1, 3))


Out[150]:
array([[1, 1, 1],
       [2, 2, 2],
       [3, 3, 3]])

In [177]:
boxes.shape


Out[177]:
(640000, 9)

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]:
(640000, 18)

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 [ ]: