def check_typical(self, idx, rot, zoom): grs = Grasps.load_from_jacquard_files( self.graspf[idx], scale=self.output_size / 1024.0) #因为图像每个都resize了,所以这里每个抓取框都要缩放 c = self.output_size // 2 grs.rotate(rot, (c, c)) grs.zoom(zoom, (c, c)) # 下面是新加的代码,用于测试典型学习的内容 angles = [round(gr.angle, 1) for gr in grs.grs] angle_dict = dict(Counter(angles)) list1, list2 = (list(t) for t in zip( *sorted(zip(angle_dict.values(), angle_dict.keys())))) first = list1[-1] if len(list1) > 1: second = list1[-2] total = first + second else: total = first percent = (total) / len(grs.grs) if percent > 0.70: # 生成样例学习 angle_shift = 0.2 typical1_min = list2[-1] - angle_shift typical1_max = list2[-1] + angle_shift if len(list1) > 1: typical2_min = list2[-2] - angle_shift typical2_max = list2[-2] + angle_shift else: typical2_min = 0 typical2_max = 0 grs1 = [] grs2 = [] grs3 = [] for gr in grs.grs[::]: if gr.angle < typical1_max and gr.angle > typical1_min: grs1.append(gr) elif gr.angle < typical2_max and gr.angle > typical2_min: grs2.append(gr) else: grs3.append(gr) grasp_true1 = Grasps(grs1) # grasp_true2 = Grasps(grs2) # grasp_true3 = Grasps(grs3) return percent, grasp_true1 return percent, Grasps(grs)
def get_grasp(self,idx,rot = 0,zoom=1.0): grs = Grasps.load_from_jacquard_files(self.graspf[idx],scale = self.output_size/1024.0)#因为图像每个都resize了,所以这里每个抓取框都要缩放 c = self.output_size//2 grs.rotate(rot,(c,c)) grs.zoom(zoom,(c,c)) pos_img,angle_img,width_img = grs.generate_img(self.output_size,self.output_size) return pos_img,angle_img,width_img
def get_raw_grasps(self, idx, rot=0, zoom=1.0): raw_grasps = Grasps.load_from_cornell_files(self.graspf[idx]) center, left, top = self._get_crop_attrs(idx) # 这是paddlepaddel这里报错而专门加的一句,rot.sequeeze() raw_grasps.rotate(rot.squeeze(), center) raw_grasps.offset((-top, -left)) raw_grasps.zoom(zoom, (self.output_size // 2, self.output_size // 2)) return raw_grasps
def get_grasp(self,idx,rot = 0,zoom=1.0): grs = Grasps.load_from_jacquard_files(self.graspf[idx],scale = self.output_size/1024.0)#因为图像每个都resize了,所以这里每个抓取框都要缩放 c = self.output_size//2 grs.rotate(rot,(c,c)) grs.zoom(zoom,(c,c)) pos_img,angle_img,width_img = grs.generate_img(self.output_size,self.output_size) # poto_data = (pos_grid_img,gt_points, gt_angles, gt_widths,gt_lengths) poto_data = grs.generate_grid_mask(shape = (self.output_size,self.output_size)) return pos_img,angle_img,width_img,poto_data
def _get_crop_attrs(self, idx): grasp_rectangles = Grasps.load_from_cornell_files(self.graspf[idx]) center = grasp_rectangles.center left = max( 0, min(center[1] - self.output_size // 2, 640 - self.output_size)) top = max( 0, min(center[0] - self.output_size // 2, 480 - self.output_size)) return center, left, top
def get_raw_grasps(self,idx,rot = 0,zoom = 1.0): ''' :功能 :读取返回指定id的抓取框信息斌进行一系列预处理(裁剪,缩放等)后以Grasps对象的形式返回 :参数 idx :int,要读取的数据id :返回 :Grasps,此id中包含的抓取 ''' raw_grasps = Grasps.load_from_jacquard_files(self.graspf[idx],scale = self.output_size/1024.0) c = self.output_size//2 raw_grasps.rotate(rot,(c,c)) raw_grasps.zoom(zoom,(c,c)) return raw_grasps
def get_raw_grasps(self, idx): ''' :功能 :读取返回指定id的抓取框信息斌进行一系列预处理(裁剪,缩放等)后以Grasps对象的形式返回 :参数 idx :int,要读取的数据id :返回 :Grasps,此id中包含的抓取 ''' raw_grasps = Grasps.load_from_cornell_files(self.graspf[idx]) center, left, top = self._get_crop_attrs(idx) raw_grasps.offset((-left, -top)) return raw_grasps
def get_grasp(self, idx, rot=0, zoom=1.0): grs = Grasps.load_from_cornell_files(self.graspf[idx]) center, left, top = self._get_crop_attrs(idx) #先旋转再偏移再缩放 grs.rotate(rot, center) grs.offset((-top, -left)) grs.zoom(zoom, (self.output_size // 2, self.output_size // 2)) pos_img, angle_img, width_img = grs.generate_img( shape=(self.output_size, self.output_size)) return pos_img, angle_img, width_img
def get_raw_grasps(self,idx,rot = 0.0,zoom = 1.0): ''' :功能 :读取返回指定id的抓取框信息斌进行一系列预处理(裁剪,缩放等)后以Grasps对象的形式返回 :参数 idx :int,要读取的数据id :返回 :Grasps,此id中包含的抓取 ''' raw_grasps = Grasps.load_from_cornell_files(self.graspf[idx]) center, left, top = self._get_crop_attrs(idx) raw_grasps.rotate(rot,center) raw_grasps.offset((-top,-left)) raw_grasps.zoom(zoom,(self.output_size//2,self.output_size//2)) return raw_grasps
def _get_crop_attrs(self, idx): ''' :功能 :读取多抓取框中心点的坐标,并结合output_size计算要裁剪的左上角点坐标 :参数 idx :int, :返回 :计算出来的多抓取框中心点坐标和裁剪区域左上角点坐标 ''' grasp_rectangles = Grasps.load_from_cornell_files(self.graspf[idx]) center = grasp_rectangles.center #按照ggcnn里面的话,这里本该加个限制条件,防止角点坐标溢出边界,但前面分析过,加不加区别不大,就不加了 left = center[0] - self.output_size // 2 top = center[1] - self.output_size // 2 return center, left, top
def _get_crop_attrs(self,idx): ''' :功能 :读取多抓取框中心点的坐标,并结合output_size计算要裁剪的左上角点坐标 :参数 idx :int, :返回 :计算出来的多抓取框中心点坐标和裁剪区域左上角点坐标 ''' grasp_rectangles = Grasps.load_from_cornell_files(self.graspf[idx]) center = grasp_rectangles.center #按照ggcnn里面的话,这里本该加个限制条件,防止角点坐标溢出边界,但前面分析过,加不加区别不大,就不加了 #分析错误,后面出现bug了,所以还是加上吧 left = max(0, min(center[1] - self.output_size // 2, 640 - self.output_size)) top = max(0, min(center[0] - self.output_size // 2, 480 - self.output_size)) return center,left,top#the center (column,row) must be changed to for later rotate considerations
def get_grasp(self, idx): ''' :功能 :读取返回指定id的抓取标注参数并将多个抓取框的参数返回融合 :参数 idx :int,要读取的数据id :返回 :以图片的方式返回定义一个抓取的多个参数,包括中心点,角度,宽度和长度,同时返回idx,方便后面validate的时候查找真实的抓取框用 ''' grs = Grasps.load_from_cornell_files(self.graspf[idx]) grs.offset((-(grs.center[0] - self.output_size // 2), -(grs.center[1] - self.output_size // 2))) pos_img, angle_img, width_img = grs.generate_img( shape=(self.output_size, self.output_size)) return pos_img, angle_img, width_img
def get_grasp(self,idx,rot=0, zoom=1.0): ''' :功能 :读取返回指定id的抓取标注参数并将多个抓取框的参数返回融合 :参数 idx :int,要读取的数据id :返回 :以图片的方式返回定义一个抓取的多个参数,包括中心点,角度,宽度和长度,同时返回idx,方便后面validate的时候查找真实的抓取框用 ''' grs = Grasps.load_from_cornell_files(self.graspf[idx]) center, left, top = self._get_crop_attrs(idx) #先旋转再偏移再缩放 grs.rotate(rot,center) grs.offset((-top,-left)) grs.zoom(zoom,(self.output_size//2,self.output_size//2)) pos_img,angle_img,width_img = grs.generate_img(shape = (self.output_size,self.output_size)) return pos_img,angle_img,width_img
def show_grasp(img, grasp, color=(0, 255, 0)): # 给定一张图及一个抓取(Grasps和Grasp类型均可),在图上绘制出抓取框 if not isinstance(grasp, Grasps): grasp = Grasps([grasp]) for gr in grasp.grs: # 有时候预测出负值会报错 try: for i in range(3): cv2.line(img, tuple(gr.points.astype(np.uint32)[i][::-1]), tuple(gr.points.astype(np.uint32)[i + 1][::-1]), color, 1) cv2.line(img, tuple(gr.points.astype(np.uint32)[3][::-1]), tuple(gr.points.astype(np.uint32)[0][::-1]), color, 1) except: pass return img
def get_grasp(self, idx): ''' :功能 :读取返回指定id的抓取标注参数并将多个抓取框的参数返回融合 :参数 idx :int,要读取的数据id :参数 pos :bool,是否生成返回位置映射图 :参数 angle :bool,是否生成返回角度映射图 :参数 width :bool,是否生成返回夹爪宽度映射图 :返回 :以图片的方式返回定义一个抓取的多个参数,包括中心点,角度,宽度和长度 ''' grs = Grasps.load_from_cornell_files(self.graspf[idx]) grs.offset((-(grs.center[0] - self.output_size // 2), -(grs.center[1] - self.output_size // 2))) pos_img, angle_img, width_img = grs.generate_img( shape=(self.output_size, self.output_size)) return pos_img, angle_img, width_img
def get_grasp(self, idx, rot=0, zoom=1.0): grs = Grasps.load_from_jacquard_files( self.graspf[idx], scale=self.output_size / 1024.0) #因为图像每个都resize了,所以这里每个抓取框都要缩放 c = self.output_size // 2 grs.rotate(rot, (c, c)) grs.zoom(zoom, (c, c)) # 下面是新加的代码,用于测试典型学习的内容 percent, grasp_true1 = self.check_typical(idx, rot, zoom) if percent > 0.70: pos_img, angle_img, width_img = grasp_true1.generate_img_n( self.output_size, self.output_size) else: # 不是的话啥都不用做 pos_img, angle_img, width_img = grs.generate_img( self.output_size, self.output_size) return pos_img, angle_img, width_img