-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_section.py
91 lines (83 loc) · 2.68 KB
/
test_section.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import torch.utils.data
import cv2
import matplotlib.pyplot as plt
import numpy as np
from models.common import post_process_output
from utils.dataset_processing import evaluation, grasp
from utils.data import get_dataset
from skimage.transform import rotate
import time
from torchvision import transforms
class Section():
def __init__(self,pixel,i):
self.pixel = np.array(pixel)
self.sum = np.sum(self.pixel)
self.end = i
self.start = i - self.pixel.size
self.mid = int(self.pixel.size/2)
# Calculate depth between two finger and decide whether need to grasp
def myline(startx, starty, endx, endy):
line = []
if abs(endy - starty) > abs(endx - startx):
if endy > starty:
for y in range(starty, endy):
x = int((y - starty) * (endx - startx) / (endy - starty)) + startx
line.append([y, x])
else:
for y in range(endy, starty):
x = int((y - starty) * (endx - startx) / (endy - starty)) + startx
line.append([y, x])
return line
if abs(endy - starty) <= abs(endx - startx):
if endx > startx:
for x in range(startx, endx):
y = int((x - startx) * (endy - starty) / (endx - startx)) + starty
line.append([y, x])
else:
for x in range(endx, startx):
y = int((x - startx) * (endy - starty) / (endx - startx)) + starty
line.append([y, x])
return line
def section_plt(section):
plt.subplot(1, 1, 1)
num = len(section)
width = 0.2
index = np.arange(num)
p2 = plt.bar(index, section, width, label='num', color='#87CEFA')
plt.xlabel('clusters')
xtick_step = 5
plt.xticks(range(0, num, xtick_step), range(0, num, xtick_step))
plt.ylabel('pixel height')
plt.title('Grasp section distribution')
plt.show()
# plt.ion()
# plt.pause(1)
return
total_section = np.load('test_section.npy')
print(total_section)
section_plt(total_section)
Sections = []
sub_section = []
cur_flag = 0
for i in range(total_section.size):
if total_section[i] > 0.01:
if cur_flag == 0:
cur_flag = 1
sub_section.append(total_section[i])
continue
else:
sub_section.append(total_section[i])
continue
if total_section[i] < 0.01:
if cur_flag == 0:
continue
else:
cur_flag = 0
Sections.append(Section(sub_section,i))
max_sum = 0
main_Section = Section([],0)
for i in range(len(Sections)):
if Sections[i].sum>max_sum:
max_sum = Sections[i].sum
main_Section = Sections[i]
section_plt(main_Section.pixel)