def callback(self, ros_data): np_arr = np.frombuffer(ros_data.data, np.uint8) image_np = cv2.imdecode(np_arr, cv2.IMREAD_COLOR) # convert it to pil format im = cv2.cvtColor(image_np, cv2.COLOR_BGR2RGB) im_pil = Image.fromarray(im) # im_pil = im_pil.convert('RGB') transform = input_transform im_pil = transform(im_pil) # print(type(img)) # Make prediction im_pil = im_pil.cuda().unsqueeze(0) output = model.evaluate(im_pil) predict = torch.max(output, 1)[1].cpu().numpy() + 1 # Get color pallete for visualization mask = encoding.utils.get_mask_pallete(predict, 'minc_dataset') # print(type(mask)) # print(mask) mask.save('deeplab_resnest101_rgb_202008042350.png') # mask.show() # time.sleep(5) # mask.close() img = np.asarray(mask) # pil2ros = cv2.cvtColor(pil2ros, cv2.COLOR_RGB2BGR) img3_shape = np.insert(img.shape,2,[3]) img3 = np.zeros((img3_shape)).astype("uint8") img3[:,:,0] = img img3[:,:,1] = img img3[:,:,2] = img for i in range(24): img3 = np.where(img3==((i,i,i)),((mincpallete[i*3+2],mincpallete[i*3+1],mincpallete[i*3])),img3) img3 = np.array(img3, dtype=np.uint8) cv2.imshow("window",img3) k = cv2.waitKey(0) cv2.imwrite('deeplabresnest101_gray_202008042350.png',pil2ros) # print(pil2ros.shape) # show the mask cv2.imshow("Image window", pil2ros) cv2.waitKey(3) msg = CompressedImage() msg.header.stamp = rospy.Time.now() msg.format = "jpeg" msg.data = np.array(cv2.imencode('.jpg', pil2ros)[1]).tostring() self.image_pub.publish(msg)
def load_image(filename, size=None, scale=None, keep_asp=True, transform=input_transform): """Load the image for demos""" img = Image.open(filename).convert('RGB') if size is not None: if keep_asp: size2 = int(size * 1.0 / img.size[0] * img.size[1]) img = img.resize((size, size2), Image.ANTIALIAS) else: img = img.resize((size, size), Image.ANTIALIAS) elif scale is not None: img = img.resize((int(img.size[0] / scale), int(img.size[1] / scale)), Image.ANTIALIAS) if transform: img = transform(img) return img
def callback(self, ros_data): np_arr = np.frombuffer(ros_data.data, np.uint8) image_np = cv2.imdecode(np_arr, cv2.IMREAD_COLOR) cv2.imwrite(SAVENAME + DATE + '_orig_' + '.png', image_np) # convert it to pil format im = cv2.cvtColor(image_np, cv2.COLOR_BGR2RGB) im_pil = Image.fromarray(im) # im_pil = im_pil.convert('RGB') transform = input_transform im_pil = transform(im_pil) # print(type(img)) # Make prediction im_pil = im_pil.cuda().unsqueeze(0) output = model.evaluate(im_pil) predict = torch.max(output, 1)[1].cpu().numpy() + 1 # Get color pallete for visualization mask = encoding.utils.get_mask_pallete(predict, 'minc_dataset') # print(type(mask)) # print(mask) mask.save(SAVENAME + DATE + '_rgb_' + '.png') # mask.show() # time.sleep(5) # mask.close() pil2ros = np.asarray(mask) pil2ros = cv2.cvtColor(pil2ros, cv2.COLOR_RGB2BGR) cv2.imwrite(SAVENAME + DATE + '_gray_' + '.png', pil2ros) # print(pil2ros.shape) # show the mask cv2.imshow("Image window", pil2ros) cv2.waitKey(3) msg = CompressedImage() msg.header.stamp = rospy.Time.now() msg.format = "jpeg" msg.data = np.array(cv2.imencode('.jpg', pil2ros)[1]).tostring() self.image_pub.publish(msg)
def callback(self, ros_data): np_arr = np.frombuffer(ros_data.data, np.uint8) image_np = cv2.imdecode(np_arr, cv2.IMREAD_COLOR) # convert it to pil format im = cv2.cvtColor(image_np, cv2.COLOR_BGR2RGB) im_pil = Image.fromarray(im) # im_pil = im_pil.convert('RGB') transform = input_transform im_pil = transform(im_pil) # print(type(img)) # Make prediction im_pil = im_pil.cuda().unsqueeze(0) output = model.evaluate(im_pil) predict = torch.max(output, 1)[1].cpu().numpy() + 1 # Get color pallete for visualization mask = encoding.utils.get_mask_pallete(predict, 'ade20k') print(type(mask)) print(mask) # mask.save('output2.png') # mask.show() # time.sleep(5) # mask.close() pil2ros = np.asarray(mask) pil2ros = cv2.cvtColor(pil2ros, cv2.COLOR_RGB2BGR) # cv2.imwrite('cvformat.png',pil2ros) # print(pil2ros.shape) # show the mask cv2.imshow("Image window", pil2ros) cv2.waitKey(3) '''