Python入门:len、count、size、shape、get_shape

  • len():返回对象的长度,如list、dict等,注意不是length()函数 
len([1,2,3,4,5])# 返回值是5
  • count():计算包含对象个数 
[1,2,3,1].count(1)# 返回值为2 

size、shape、get_shape是其他模块中的函数,如:

numpy中:

  • size():计算数组和矩阵所有数据的个数 
  • shape ():得到矩阵每维的大小 
a = np.array([[1,2,3],[4,5,6]]) 
np.size(a)# 返回值为 6 
np.size(a,1)# 返回值为 3

np.shape(a)# 返回值为(2,3)

tfrecord中:

  • tf.shape()和get_shape()都可以得到tensor的尺寸
  • tf.shape(a)中a的数据类型可以是tensor, list, array 
  • a.get_shape()中a的数据类型只能是tensor,且返回的是一个元组(tuple)
x=tf.constant([[1,2,3],[4,5,6]]  
y=[[1,2,3],[4,5,6]]  
z=np.arange(24).reshape([2,3,4]))  
sess=tf.Session()  

x_shape=tf.shape(x)              #  x_shape 是一个tensor  
y_shape=tf.shape(y)              #  <tf.Tensor 'Shape_2:0' shape=(2,) dtype=int32>  
z_shape=tf.shape(z)              #  <tf.Tensor 'Shape_5:0' shape=(3,) dtype=int32>  
print sess.run(x_shape)          # 结果:[2 3]  
print sess.run(y_shape)          # 结果:[2 3]  
print sess.run(z_shape)          # 结果:[2 3 4]  

x_shape=x.get_shape()            # 返回的是TensorShape([Dimension(2), Dimension(3)]),不能使用sess.run() 因为返回的不是tensor或string,而是元组  
x_shape=x.get_shape().as_list()  # 可以使用 as_list()得到具体的尺寸,x_shape=[2 3]  
y_shape=y.get_shape()            # AttributeError: 'list' object has no attribute 'get_shape'  
z_shape=z.get_shape()            # AttributeError: 'numpy.ndarray' object has no attribute 'get_shape'

 

  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
程序提示AttributeError: 'ImageThread' object has no attribute '_dgl',优化程序 def __init__(self, pipeline, color_label, depth_label, interval, color_photo_dir, depth_photo_dir): super().__init__() self.pipeline = pipeline self.color_label = color_label self.depth_label = depth_label self.is_running = True self.interval = interval self.color_photo_dir = color_photo_dir self.depth_photo_dir = depth_photo_dir self.saved_color_photos = 0 self.saved_depth_photos = 0 def save_photo(self, color_image, depth_image): # 保存彩色图和深度图 filename = datetime.datetime.now().strftime("%Y-%m-%d-%H-%M-%S-{}.bmp".format(self.saved_color_photos)) color_image.save(os.path.join(self.color_photo_dir, filename), "BMP") depth_image.save(os.path.join(self.depth_photo_dir, filename), "BMP") # print(self.color_photo_dir) # 更新已保存照片数量标签 self.saved_color_photos += 1 self.saved_depth_photos += 1 self.saved_color_photos_signal.emit(self.saved_color_photos) self.saved_depth_photos_signal.emit(self.saved_depth_photos) def run(self): ROT = 3 while self.is_running: # 从相机获取彩色图和深度图 frames = self.pipeline.wait_for_frames() color_frame = frames.get_color_frame() depth_frame = frames.get_depth_frame() depth_image = np.asanyarray(depth_frame.get_data()) color_image = np.asanyarray(color_frame.get_data()) # 转换成 Qt 图像格式 depth_colormap = cv2.applyColorMap(cv2.convertScaleAbs(depth_image, alpha=0.03), cv2.COLORMAP_JET) # 将深度图像转换为伪彩色图像 color_image = QImage(color_image, color_image.shape[1], color_image.shape[0], color_image.shape[1] * 3, QImage.Format_RGB888) depth_colormap = QImage(depth_colormap, depth_colormap.shape[1], depth_colormap.shape[0], depth_colormap.shape[1] * 3, QImage.Format_RGB888) # 显示图像 self.color_label.setPixmap(QPixmap.fromImage(color_image)) self.depth_label.setPixmap(QPixmap.fromImage(depth_colormap)) v = self._dgl.qpin(ROT) if len(v) > 0: self._count += sum(v) if self._count > self._inspect_step: self.save_photo(color_image, depth_colormap) self._count -= self._inspect_step
05-26
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值