Java 类名:com.alibaba.alink.pipeline.image.ReadImageToTensor
Python 类名:ReadImageToTensor

功能介绍

将图片列转换为张量。

参数说明

名称 中文名称 描述 类型 是否必须? 取值范围 默认值
outputCol 输出结果列列名 输出结果列列名,必选 String ✓
relativeFilePathCol 文件路径列 文件路径列 String ✓
rootFilePath 文件路径 文件路径 String ✓
imageHeight 图片高度 图片高度 Integer
imageWidth 图片宽度 图片宽度 Integer
reservedCols 算法保留列名 算法保留列 String[] null

代码示例

Python 代码

  1. df_data = pd.DataFrame([
  2. 'sphx_glr_plot_scripted_tensor_transforms_001.png'
  3. ])
  4. batch_data = BatchOperator.fromDataframe(df_data, schemaStr = 'path string')
  5. ReadImageToTensor()\
  6. .setRootFilePath("https://pytorch.org/vision/stable/_images/")\
  7. .setRelativeFilePathCol("path")\
  8. .setOutputCol("tensor")\
  9. .transform(batch_data)\
  10. .print()

Java 代码

  1. import org.apache.flink.types.Row;
  2. import com.alibaba.alink.operator.batch.source.MemSourceBatchOp;
  3. import com.alibaba.alink.pipeline.image.ReadImageToTensor;
  4. import org.junit.Test;
  5. import java.util.Collections;
  6. import java.util.List;
  7. public class ReadImageToTensorTest {
  8. @Test
  9. public void testReadImageToTensor() throws Exception {
  10. List <Row> data = Collections.singletonList(
  11. Row.of("sphx_glr_plot_scripted_tensor_transforms_001.png")
  12. );
  13. MemSourceBatchOp memSourceBatchOp = new MemSourceBatchOp(data, "path string");
  14. new ReadImageToTensor()
  15. .setRootFilePath("https://pytorch.org/vision/stable/_images/")
  16. .setRelativeFilePathCol("path")
  17. .setOutputCol("tensor")
  18. .transform(memSourceBatchOp)
  19. .print();
  20. }
  21. }

运行结果

| path | tensor |
|—————————————————————————+————————————————|
| sphx_glr_plot_scripted_tensor_transforms_001.png | FLOAT#250,520,4#1.0 1.0 1.0… |