!40657 【算子众智】【电子科技大学】【数据算子】【read_image】read a image file
Merge pull request !40657 from dengjian/upstream_read_image
This commit is contained in:
commit
17bb175a36
|
@ -0,0 +1,12 @@
|
|||
mindspore.dataset.vision.ImageReadMode
|
||||
======================================
|
||||
|
||||
.. py:class:: mindspore.dataset.vision.ImageReadMode
|
||||
|
||||
图像文件读取方式枚举类。
|
||||
|
||||
可选枚举值为:ImageReadMode.UNCHANGED、ImageReadMode.GRAYSCALE、ImageReadMode.COLOR。
|
||||
|
||||
- **ImageReadMode.UNCHANGED** - 按照图像原始格式读取。
|
||||
- **ImageReadMode.GRAYSCALE** - 读取并转为单通道灰度数据。
|
||||
- **ImageReadMode.COLOR** - 读取并换为3通道RGB彩色数据。
|
|
@ -0,0 +1,24 @@
|
|||
mindspore.dataset.vision.read_image
|
||||
===================================
|
||||
|
||||
.. py:function:: mindspore.dataset.vision.read_image(filename, mode=ImageReadMode.UNCHANGED)
|
||||
|
||||
读取图像文件并解码为3通道RGB彩色数据或灰度数据。
|
||||
支持的文件类型有JPEG、PNG、BMP和TIFF。
|
||||
|
||||
参数:
|
||||
- **filename** (str) - 待读取图像文件路径。
|
||||
- **mode** (ImageReadMode, 可选) - 图像读取模式。它可以是 [ImageReadMode.UNCHANGED、ImageReadMode.GRAYSCALE、ImageReadMode.COLOR]
|
||||
中的任何一个。默认值:ImageReadMode.UNCHANGED。
|
||||
|
||||
- **ImageReadMode.UNCHANGED** - 按照图像原始格式读取。
|
||||
- **ImageReadMode.GRAYSCALE** - 读取并转为单通道灰度数据。
|
||||
- **ImageReadMode.COLOR** - 读取并换为3通道RGB彩色数据。
|
||||
|
||||
返回:
|
||||
- numpy.ndarray, 三维uint8类型数据,shape为(H, W, C)。
|
||||
|
||||
异常:
|
||||
- **TypeError** - 如果 `filename` 不是str类型。
|
||||
- **TypeError** - 如果 `mode` 不是 :class:`mindspore.dataset.vision.ImageReadMode` 类型。
|
||||
- **RuntimeError** - 如果 `filename` 不存在或不是普通文件或由于格式等原因无法正常读取。
|
|
@ -139,10 +139,12 @@ API样例中常用的导入模块如下:
|
|||
mindspore.dataset.vision.Border
|
||||
mindspore.dataset.vision.ConvertMode
|
||||
mindspore.dataset.vision.ImageBatchFormat
|
||||
mindspore.dataset.vision.ImageReadMode
|
||||
mindspore.dataset.vision.Inter
|
||||
mindspore.dataset.vision.SliceMode
|
||||
mindspore.dataset.vision.encode_jpeg
|
||||
mindspore.dataset.vision.get_image_num_channels
|
||||
mindspore.dataset.vision.get_image_size
|
||||
mindspore.dataset.vision.read_file
|
||||
mindspore.dataset.vision.read_image
|
||||
mindspore.dataset.vision.write_file
|
||||
|
|
|
@ -89,10 +89,12 @@ Utilities
|
|||
mindspore.dataset.vision.Border
|
||||
mindspore.dataset.vision.ConvertMode
|
||||
mindspore.dataset.vision.ImageBatchFormat
|
||||
mindspore.dataset.vision.ImageReadMode
|
||||
mindspore.dataset.vision.Inter
|
||||
mindspore.dataset.vision.SliceMode
|
||||
mindspore.dataset.vision.encode_jpeg
|
||||
mindspore.dataset.vision.get_image_num_channels
|
||||
mindspore.dataset.vision.get_image_size
|
||||
mindspore.dataset.vision.read_file
|
||||
mindspore.dataset.vision.read_image
|
||||
mindspore.dataset.vision.write_file
|
||||
|
|
8
mindspore/ccsrc/minddata/dataset/api/python/bindings/dataset/core/bindings.cc
Normal file → Executable file
8
mindspore/ccsrc/minddata/dataset/api/python/bindings/dataset/core/bindings.cc
Normal file → Executable file
|
@ -189,5 +189,13 @@ PYBIND_REGISTER(ConvertMode, 0, ([](const py::module *m) {
|
|||
.value("DE_COLOR_RGBA2GRAY", ConvertMode::COLOR_RGBA2GRAY)
|
||||
.export_values();
|
||||
}));
|
||||
|
||||
PYBIND_REGISTER(ImageReadMode, 0, ([](const py::module *m) {
|
||||
(void)py::enum_<ImageReadMode>(*m, "ImageReadMode", py::arithmetic())
|
||||
.value("DE_IMAGE_READ_MODE_UNCHANGED", ImageReadMode::kUNCHANGED)
|
||||
.value("DE_IMAGE_READ_MODE_GRAYSCALE", ImageReadMode::kGRAYSCALE)
|
||||
.value("DE_IMAGE_READ_MODE_COLOR", ImageReadMode::kCOLOR)
|
||||
.export_values();
|
||||
}));
|
||||
} // namespace dataset
|
||||
} // namespace mindspore
|
||||
|
|
|
@ -773,6 +773,14 @@ PYBIND_REGISTER(ReadFileOperation, 1, ([](py::module *m) {
|
|||
}));
|
||||
}));
|
||||
|
||||
PYBIND_REGISTER(ReadImageOperation, 1, ([](py::module *m) {
|
||||
(void)m->def("read_image", ([](const std::string &filename, ImageReadMode mode) {
|
||||
std::shared_ptr<Tensor> output;
|
||||
THROW_IF_ERROR(mindspore::dataset::ReadImage(filename, &output, mode));
|
||||
return output;
|
||||
}));
|
||||
}));
|
||||
|
||||
PYBIND_REGISTER(RescaleOperation, 1, ([](const py::module *m) {
|
||||
(void)
|
||||
py::class_<vision::RescaleOperation, TensorOperation, std::shared_ptr<vision::RescaleOperation>>(
|
||||
|
|
|
@ -1163,6 +1163,18 @@ Status ReadFile(const std::string &filename, mindspore::MSTensor *output) {
|
|||
|
||||
return Status::OK();
|
||||
}
|
||||
|
||||
// ReadImage Function.
|
||||
Status ReadImage(const std::string &filename, mindspore::MSTensor *output, ImageReadMode mode) {
|
||||
RETURN_UNEXPECTED_IF_NULL(output);
|
||||
|
||||
std::shared_ptr<Tensor> de_tensor;
|
||||
RETURN_IF_NOT_OK(mindspore::dataset::ReadImage(filename, &de_tensor, mode));
|
||||
CHECK_FAIL_RETURN_UNEXPECTED(de_tensor->HasData(),
|
||||
"ReadImage: get an empty tensor with shape " + de_tensor->shape().ToString());
|
||||
*output = mindspore::MSTensor(std::make_shared<DETensor>(de_tensor));
|
||||
return Status::OK();
|
||||
}
|
||||
#endif // not ENABLE_ANDROID
|
||||
|
||||
// Rescale Transform Operation.
|
||||
|
|
|
@ -71,6 +71,13 @@ enum class DATASET_API ConvertMode {
|
|||
COLOR_RGBA2GRAY = 11 ///< Convert RGBA image to GRAY image.
|
||||
};
|
||||
|
||||
/// \brief The mode for reading a image file.
|
||||
enum class DATASET_API ImageReadMode {
|
||||
kUNCHANGED = 0, ///< Remain the output in the original format.
|
||||
kGRAYSCALE = 1, ///< Convert the output into one channel grayscale data.
|
||||
kCOLOR = 2, ///< Convert the output into three channels RGB color data.
|
||||
};
|
||||
|
||||
// \brief Possible density function in Dither.
|
||||
enum DATASET_API DensityFunction {
|
||||
kTPDF = 0, ///< Use triangular probability density function.
|
||||
|
|
|
@ -1666,6 +1666,19 @@ class DATASET_API RandomVerticalFlipWithBBox final : public TensorTransform {
|
|||
/// \return The status code.
|
||||
Status DATASET_API ReadFile(const std::string &filename, mindspore::MSTensor *output);
|
||||
|
||||
/// \brief Read a image file and decode it into one or three channels data.
|
||||
/// \param[in] filename The path to the file to be read.
|
||||
/// \param[out] output The Tensor data.
|
||||
/// \param[in] mode The read mode used for optionally converting the image, can be one of
|
||||
/// [ImageReadMode::kUNCHANGED, ImageReadMode::kGRAYSCALE, ImageReadMode::kCOLOR]. Default:
|
||||
/// ImageReadMode::kUNCHANGED.
|
||||
/// - ImageReadMode::kUNCHANGED, remain the output in the original format.
|
||||
/// - ImageReadMode::kGRAYSCALE, convert the output into one channel grayscale data.
|
||||
/// - ImageReadMode::kCOLOR, convert the output into three channels RGB color data.
|
||||
/// \return The status code.
|
||||
Status DATASET_API ReadImage(const std::string &filename, mindspore::MSTensor *output,
|
||||
ImageReadMode mode = ImageReadMode::kUNCHANGED);
|
||||
|
||||
/// \brief Crop the given image and zoom to the specified size.
|
||||
class DATASET_API ResizedCrop final : public TensorTransform {
|
||||
public:
|
||||
|
|
|
@ -2261,6 +2261,57 @@ Status EncodeJpeg(const std::shared_ptr<Tensor> &image, std::shared_ptr<Tensor>
|
|||
return Status::OK();
|
||||
}
|
||||
|
||||
Status ReadFile(const std::string &filename, std::shared_ptr<Tensor> *output) {
|
||||
RETURN_UNEXPECTED_IF_NULL(output);
|
||||
|
||||
auto realpath = FileUtils::GetRealPath(filename.c_str());
|
||||
if (!realpath.has_value()) {
|
||||
RETURN_STATUS_UNEXPECTED("ReadFile: Invalid file path, " + filename + " does not exist.");
|
||||
}
|
||||
struct stat sb;
|
||||
stat(realpath.value().c_str(), &sb);
|
||||
if (S_ISREG(sb.st_mode) == 0) {
|
||||
RETURN_STATUS_UNEXPECTED("ReadFile: Invalid file path, " + filename + " is not a regular file.");
|
||||
}
|
||||
|
||||
RETURN_IF_NOT_OK(Tensor::CreateFromFile(realpath.value(), output));
|
||||
return Status::OK();
|
||||
}
|
||||
|
||||
Status ReadImage(const std::string &filename, std::shared_ptr<Tensor> *output, ImageReadMode mode) {
|
||||
RETURN_UNEXPECTED_IF_NULL(output);
|
||||
|
||||
auto realpath = FileUtils::GetRealPath(filename.c_str());
|
||||
if (!realpath.has_value()) {
|
||||
std::string err_msg = "ReadImage: Invalid file path, " + filename + " does not exist.";
|
||||
RETURN_STATUS_UNEXPECTED(err_msg);
|
||||
}
|
||||
struct stat sb;
|
||||
stat(realpath.value().c_str(), &sb);
|
||||
if (S_ISREG(sb.st_mode) == 0) {
|
||||
RETURN_STATUS_UNEXPECTED("ReadImage: Invalid file path, " + filename + " is not a regular file.");
|
||||
}
|
||||
|
||||
cv::Mat image;
|
||||
int cv_mode = static_cast<int>(mode) - 1;
|
||||
image = cv::imread(realpath.value(), cv_mode);
|
||||
if (image.data == nullptr) {
|
||||
RETURN_STATUS_UNEXPECTED("ReadImage: Can not read file " + filename);
|
||||
}
|
||||
|
||||
std::shared_ptr<CVTensor> output_cv;
|
||||
if (mode == ImageReadMode::kCOLOR || image.channels() > 1) {
|
||||
cv::Mat image_rgb;
|
||||
cv::cvtColor(image, image_rgb, cv::COLOR_BGRA2RGB);
|
||||
RETURN_IF_NOT_OK(CVTensor::CreateFromMat(image_rgb, kDefaultImageRank, &output_cv));
|
||||
} else {
|
||||
RETURN_IF_NOT_OK(CVTensor::CreateFromMat(image, kDefaultImageRank, &output_cv));
|
||||
}
|
||||
*output = std::static_pointer_cast<Tensor>(output_cv);
|
||||
|
||||
return Status::OK();
|
||||
}
|
||||
|
||||
Status WriteFile(const std::string &filename, const std::shared_ptr<Tensor> &data) {
|
||||
std::string err_msg;
|
||||
|
||||
|
@ -2321,22 +2372,5 @@ Status WriteFile(const std::string &filename, const std::shared_ptr<Tensor> &dat
|
|||
fs.close();
|
||||
return Status::OK();
|
||||
}
|
||||
|
||||
Status ReadFile(const std::string &filename, std::shared_ptr<Tensor> *output) {
|
||||
RETURN_UNEXPECTED_IF_NULL(output);
|
||||
|
||||
auto realpath = FileUtils::GetRealPath(filename.c_str());
|
||||
if (!realpath.has_value()) {
|
||||
RETURN_STATUS_UNEXPECTED("ReadFile: Invalid file path, " + filename + " does not exist.");
|
||||
}
|
||||
struct stat sb;
|
||||
stat(realpath.value().c_str(), &sb);
|
||||
if (S_ISREG(sb.st_mode) == 0) {
|
||||
RETURN_STATUS_UNEXPECTED("ReadFile: Invalid file path, " + filename + " is not a regular file.");
|
||||
}
|
||||
|
||||
RETURN_IF_NOT_OK(Tensor::CreateFromFile(realpath.value(), output));
|
||||
return Status::OK();
|
||||
}
|
||||
} // namespace dataset
|
||||
} // namespace mindspore
|
||||
|
|
|
@ -510,17 +510,30 @@ Status ApplyAugment(const std::shared_ptr<Tensor> &input, std::shared_ptr<Tensor
|
|||
/// \return The status code.
|
||||
Status EncodeJpeg(const std::shared_ptr<Tensor> &image, std::shared_ptr<Tensor> *output, int quality = 75);
|
||||
|
||||
/// \brief Write the one dimension uint8 data into a file using binary mode.
|
||||
/// \param[in] filename The path to the file to be written.
|
||||
/// \param[in] data The tensor data.
|
||||
/// \return The status code.
|
||||
Status WriteFile(const std::string &filename, const std::shared_ptr<Tensor> &data);
|
||||
|
||||
/// \brief Reads a file in binary mode.
|
||||
/// \param[in] filename The path to the file to be read.
|
||||
/// \param[out] output The binary data.
|
||||
/// \return The status code.
|
||||
Status ReadFile(const std::string &filename, std::shared_ptr<Tensor> *output);
|
||||
|
||||
/// \brief Reads a image file and decode it into one or three channels data.
|
||||
/// \param[in] filename The path to the image file to be read.
|
||||
/// \param[out] output Output Tensor.
|
||||
/// \param[in] mode The read mode used for optionally converting the image, can be one of
|
||||
/// [ImageReadMode::kUNCHANGED, ImageReadMode::kGRAYSCALE, ImageReadMode::kCOLOR]. Default:
|
||||
/// ImageReadMode::kUNCHANGED.
|
||||
/// - ImageReadMode::kUNCHANGED, remain the output in the original format.
|
||||
/// - ImageReadMode::kGRAYSCALE, convert the output into one channel grayscale data.
|
||||
/// - ImageReadMode::kCOLOR, convert the output into three channels RGB color data.
|
||||
/// \return The status code.
|
||||
Status ReadImage(const std::string &filename, std::shared_ptr<Tensor> *output,
|
||||
ImageReadMode mode = ImageReadMode::kUNCHANGED);
|
||||
|
||||
/// \brief Write the one dimension uint8 data into a file using binary mode.
|
||||
/// \param[in] filename The path to the file to be written.
|
||||
/// \param[in] data The tensor data.
|
||||
/// \return The status code.
|
||||
Status WriteFile(const std::string &filename, const std::shared_ptr<Tensor> &data);
|
||||
} // namespace dataset
|
||||
} // namespace mindspore
|
||||
#endif // MINDSPORE_CCSRC_MINDDATA_DATASET_KERNELS_IMAGE_IMAGE_UTILS_H_
|
||||
|
|
|
@ -84,5 +84,5 @@ from .transforms import AdjustBrightness, AdjustContrast, AdjustGamma, AdjustHue
|
|||
RandomSharpness, RandomSolarize, RandomVerticalFlip, RandomVerticalFlipWithBBox, Rescale, Resize, ResizedCrop, \
|
||||
ResizeWithBBox, RgbToHsv, Rotate, SlicePatches, Solarize, TenCrop, ToNumpy, ToPIL, ToTensor, ToType, \
|
||||
TrivialAugmentWide, UniformAugment, VerticalFlip, not_random
|
||||
from .utils import AutoAugmentPolicy, Border, ConvertMode, ImageBatchFormat, Inter, SliceMode, encode_jpeg, \
|
||||
get_image_num_channels, get_image_size, read_file, write_file
|
||||
from .utils import AutoAugmentPolicy, Border, ConvertMode, ImageBatchFormat, ImageReadMode, Inter, SliceMode, \
|
||||
encode_jpeg, get_image_num_channels, get_image_size, read_file, read_image, write_file
|
||||
|
|
|
@ -275,6 +275,31 @@ class ImageBatchFormat(IntEnum):
|
|||
return c_values.get(image_batch_format)
|
||||
|
||||
|
||||
class ImageReadMode(IntEnum):
|
||||
"""
|
||||
The read mode used for the image file.
|
||||
|
||||
Possible enumeration values are: ImageReadMode.UNCHANGED, ImageReadMode.GRAYSCALE, ImageReadMode.COLOR.
|
||||
|
||||
- ImageReadMode.UNCHANGED: remain the output in the original format.
|
||||
- ImageReadMode.GRAYSCALE: convert the output into one channel grayscale data.
|
||||
- ImageReadMode.COLOR: convert the output into three channels RGB color data.
|
||||
"""
|
||||
UNCHANGED = 0
|
||||
GRAYSCALE = 1
|
||||
COLOR = 2
|
||||
|
||||
@staticmethod
|
||||
def to_c_type(image_read_mode):
|
||||
"""
|
||||
Function to return C type for ImageReadMode.
|
||||
"""
|
||||
c_values = {ImageReadMode.UNCHANGED: cde.ImageReadMode.DE_IMAGE_READ_MODE_UNCHANGED,
|
||||
ImageReadMode.GRAYSCALE: cde.ImageReadMode.DE_IMAGE_READ_MODE_GRAYSCALE,
|
||||
ImageReadMode.COLOR: cde.ImageReadMode.DE_IMAGE_READ_MODE_COLOR}
|
||||
return c_values.get(image_read_mode)
|
||||
|
||||
|
||||
class Inter(IntEnum):
|
||||
"""
|
||||
Interpolation Modes.
|
||||
|
@ -366,6 +391,9 @@ def encode_jpeg(image, quality=75):
|
|||
RuntimeError: If the shape of `image` is not <H, W> or <H, W, 1> or <H, W, 3>.
|
||||
RuntimeError: If `quality` is less than 1 or greater than 100.
|
||||
|
||||
Supported Platforms:
|
||||
``CPU``
|
||||
|
||||
Examples:
|
||||
>>> import numpy as np
|
||||
>>> # Generate a random image with height=120, width=340, channels=3
|
||||
|
@ -468,6 +496,9 @@ def read_file(filename):
|
|||
TypeError: If `filename` is not of type str.
|
||||
RuntimeError: If `filename` does not exist or is not a common file.
|
||||
|
||||
Supported Platforms:
|
||||
``CPU``
|
||||
|
||||
Examples:
|
||||
>>> output = vision.read_file("/path/to/file")
|
||||
"""
|
||||
|
@ -476,6 +507,44 @@ def read_file(filename):
|
|||
raise TypeError("Input filename is not of type {0}, but got: {1}.".format(str, type(filename)))
|
||||
|
||||
|
||||
def read_image(filename, mode=ImageReadMode.UNCHANGED):
|
||||
"""
|
||||
Read a image file and decode it into one channel grayscale data or RGB color data.
|
||||
Supported file types are JPEG, PNG, BMP, TIFF.
|
||||
|
||||
Args:
|
||||
filename(str): The path to the image file to be read.
|
||||
mode(int, optional): The mode used for decoding the image. It can be any of
|
||||
[ImageReadMode.UNCHANGED, ImageReadMode.GRAYSCALE, IMageReadMode.COLOR]. Default: ImageReadMode.UNCHANGED.
|
||||
|
||||
- ImageReadMode.UNCHANGED, remain the output in the original format.
|
||||
|
||||
- ImageReadMode.GRAYSCALE, convert the output into one channel grayscale data.
|
||||
|
||||
- IMageReadMode.COLOR, convert the output into three channels RGB color data.
|
||||
|
||||
Returns:
|
||||
numpy.ndarray, three dimensions uint8 data in the shape of (Height, Width, Channels).
|
||||
|
||||
Raises:
|
||||
TypeError: If `filename` is not of type str.
|
||||
TypeError: If `mode` is not of type :class:`mindspore.dataset.vision.ImageReadMode` .
|
||||
RuntimeError: If `filename` does not exist, or not a regular file, or not a supported image file.
|
||||
|
||||
Supported Platforms:
|
||||
``CPU``
|
||||
|
||||
Examples:
|
||||
>>> from mindspore.dataset.vision import ImageReadMode
|
||||
>>> output = vision.read_image("/path/to/image_file", ImageReadMode.UNCHANGED)
|
||||
"""
|
||||
if not isinstance(filename, str):
|
||||
raise TypeError("Input filename is not of type {0}, but got: {1}.".format(str, type(filename)))
|
||||
if not isinstance(mode, ImageReadMode):
|
||||
raise TypeError("Input mode is not of type {0}, but got: {1}.".format(ImageReadMode, type(mode)))
|
||||
return cde.read_image(filename, ImageReadMode.to_c_type(mode)).as_array()
|
||||
|
||||
|
||||
def write_file(filename, data):
|
||||
"""
|
||||
Write the one dimension uint8 data into a file using binary mode.
|
||||
|
@ -491,6 +560,9 @@ def write_file(filename, data):
|
|||
RuntimeError: If the data type of `data` is not uint8.
|
||||
RuntimeError: If the shape of `data` is not a one-dimensional array.
|
||||
|
||||
Supported Platforms:
|
||||
``CPU``
|
||||
|
||||
Examples:
|
||||
>>> vision.write_file("/path/to/file", data)
|
||||
"""
|
||||
|
|
|
@ -1184,6 +1184,123 @@ TEST_F(MindDataTestPipeline, TestRandAugmentMagGreNMBError) {
|
|||
EXPECT_EQ(iter, nullptr);
|
||||
}
|
||||
|
||||
/// Feature: ReadFile
|
||||
/// Description: Test ReadFile with the an example file
|
||||
/// Expectation: Output is equal to the expected data
|
||||
TEST_F(MindDataTestPipeline, TestReadFileNormal) {
|
||||
MS_LOG(INFO) << "Doing MindDataTestPipeline-TestReadFileNormal.";
|
||||
mindspore::MSTensor output;
|
||||
const UINT8 *data;
|
||||
ASSERT_OK(mindspore::dataset::vision::ReadFile("./data/dataset/apple.jpg", &output));
|
||||
EXPECT_EQ(output.Shape()[0], 159109);
|
||||
EXPECT_EQ(output.DataType(), mindspore::DataType::kNumberTypeUInt8);
|
||||
data = (const UINT8 *) (output.Data().get());
|
||||
EXPECT_EQ(data[0], 255);
|
||||
EXPECT_EQ(data[1], 216);
|
||||
EXPECT_EQ(data[2], 255);
|
||||
EXPECT_EQ(data[50000], 0);
|
||||
EXPECT_EQ(data[100000], 132);
|
||||
EXPECT_EQ(data[150000], 64);
|
||||
EXPECT_EQ(data[159106], 63);
|
||||
EXPECT_EQ(data[159107], 255);
|
||||
EXPECT_EQ(data[159108], 217);
|
||||
}
|
||||
|
||||
/// Feature: ReadFile
|
||||
/// Description: Test ReadFile with invalid filename
|
||||
/// Expectation: Error is caught when the filename is invalid
|
||||
TEST_F(MindDataTestPipeline, TestReadFileException) {
|
||||
MS_LOG(INFO) << "Doing MindDataTestPipeline-TestReadFileException.";
|
||||
mindspore::MSTensor output;
|
||||
|
||||
// Test with a not exist filename
|
||||
ASSERT_ERROR(mindspore::dataset::vision::ReadFile("this_file_is_not_exist", &output));
|
||||
|
||||
// Test with a directory name
|
||||
ASSERT_ERROR(mindspore::dataset::vision::ReadFile("./data/dataset/", &output));
|
||||
}
|
||||
|
||||
/// Feature: ReadImage
|
||||
/// Description: Test ReadImage with JPEG, PNG, BMP, TIFF file
|
||||
/// Expectation: The Output is equal to the expected output
|
||||
TEST_F(MindDataTestPipeline, TestReadImageNormal) {
|
||||
MS_LOG(INFO) << "Doing MindDataTestPipeline-TestReadImage.";
|
||||
mindspore::MSTensor output;
|
||||
std::string folder_path = "./data/dataset/testFormats/";
|
||||
std::string filename;
|
||||
const UINT8 *data;
|
||||
|
||||
filename = folder_path + "apple.jpg";
|
||||
ASSERT_OK(mindspore::dataset::vision::ReadImage(filename, &output));
|
||||
EXPECT_EQ(output.Shape()[0], 226);
|
||||
EXPECT_EQ(output.Shape()[1], 403);
|
||||
EXPECT_EQ(output.Shape()[2], 3);
|
||||
data = (const UINT8 *) (output.Data().get());
|
||||
EXPECT_EQ(data[0], 221);
|
||||
EXPECT_EQ(data[1], 221);
|
||||
EXPECT_EQ(data[2], 221);
|
||||
EXPECT_EQ(data[100 * 403 * 3 + 200 * 3 + 0], 195);
|
||||
EXPECT_EQ(data[100 * 403 * 3 + 200 * 3 + 1], 60);
|
||||
EXPECT_EQ(data[100 * 403 * 3 + 200 * 3 + 2], 31);
|
||||
EXPECT_EQ(data[225 * 403 * 3 + 402 * 3 + 0], 181);
|
||||
EXPECT_EQ(data[225 * 403 * 3 + 402 * 3 + 1], 181);
|
||||
EXPECT_EQ(data[225 * 403 * 3 + 402 * 3 + 2], 173);
|
||||
ASSERT_OK(mindspore::dataset::vision::ReadImage(filename, &output, ImageReadMode::kUNCHANGED));
|
||||
EXPECT_EQ(output.Shape()[0], 226);
|
||||
EXPECT_EQ(output.Shape()[1], 403);
|
||||
EXPECT_EQ(output.Shape()[2], 3);
|
||||
ASSERT_OK(mindspore::dataset::vision::ReadImage(filename, &output, ImageReadMode::kGRAYSCALE));
|
||||
EXPECT_EQ(output.Shape()[0], 226);
|
||||
EXPECT_EQ(output.Shape()[1], 403);
|
||||
EXPECT_EQ(output.Shape()[2], 1);
|
||||
ASSERT_OK(mindspore::dataset::vision::ReadImage(filename, &output, ImageReadMode::kCOLOR));
|
||||
EXPECT_EQ(output.Shape()[0], 226);
|
||||
EXPECT_EQ(output.Shape()[1], 403);
|
||||
EXPECT_EQ(output.Shape()[2], 3);
|
||||
|
||||
filename = folder_path + "apple.png";
|
||||
ASSERT_OK(mindspore::dataset::vision::ReadImage(filename, &output));
|
||||
EXPECT_EQ(output.Shape()[0], 226);
|
||||
EXPECT_EQ(output.Shape()[1], 403);
|
||||
EXPECT_EQ(output.Shape()[2], 3);
|
||||
|
||||
filename = folder_path + "apple_4_channels.png";
|
||||
ASSERT_OK(mindspore::dataset::vision::ReadImage(filename, &output));
|
||||
EXPECT_EQ(output.Shape()[0], 226);
|
||||
EXPECT_EQ(output.Shape()[1], 403);
|
||||
EXPECT_EQ(output.Shape()[2], 3);
|
||||
|
||||
filename = folder_path + "apple.bmp";
|
||||
ASSERT_OK(mindspore::dataset::vision::ReadImage(filename, &output));
|
||||
EXPECT_EQ(output.Shape()[0], 226);
|
||||
EXPECT_EQ(output.Shape()[1], 403);
|
||||
EXPECT_EQ(output.Shape()[2], 3);
|
||||
|
||||
filename = folder_path + "apple.tiff";
|
||||
ASSERT_OK(mindspore::dataset::vision::ReadImage(filename, &output));
|
||||
EXPECT_EQ(output.Shape()[0], 226);
|
||||
EXPECT_EQ(output.Shape()[1], 403);
|
||||
EXPECT_EQ(output.Shape()[2], 3);
|
||||
}
|
||||
|
||||
/// Feature: ReadImage
|
||||
/// Description: Test ReadImage with invalid filename or not supported image file
|
||||
/// Expectation: Error is caught when the filename is invalid or it is a not supported image file
|
||||
TEST_F(MindDataTestPipeline, TestReadImageException) {
|
||||
MS_LOG(INFO) << "Doing MindDataTestPipeline-TestReadImageException.";
|
||||
std::string folder_path = "./data/dataset/testFormats/";
|
||||
mindspore::MSTensor output;
|
||||
|
||||
// Test with a not exist filename
|
||||
ASSERT_ERROR(mindspore::dataset::vision::ReadImage("this_file_is_not_exist", &output));
|
||||
|
||||
// Test with a directory name
|
||||
ASSERT_ERROR(mindspore::dataset::vision::ReadImage("./data/dataset/", &output));
|
||||
|
||||
// Test with a not supported gif file
|
||||
ASSERT_ERROR(mindspore::dataset::vision::ReadImage(folder_path + "apple.gif", &output));
|
||||
}
|
||||
|
||||
/// Feature: WriteFile
|
||||
/// Description: Test WriteFile by writing the data into a file using binary mode
|
||||
/// Expectation: The file should be writeen and removed successfully
|
||||
|
@ -1231,39 +1348,3 @@ TEST_F(MindDataTestPipeline, TestWriteFileException) {
|
|||
data_tensor = mindspore::MSTensor(std::make_shared<mindspore::dataset::DETensor>(input));
|
||||
ASSERT_ERROR(mindspore::dataset::vision::WriteFile(filename_2, data_tensor));
|
||||
}
|
||||
|
||||
/// Feature: ReadFile
|
||||
/// Description: Test ReadFile with the an example file
|
||||
/// Expectation: Output is equal to the expected data
|
||||
TEST_F(MindDataTestPipeline, TestReadFileNormal) {
|
||||
MS_LOG(INFO) << "Doing MindDataTestPipeline-TestReadFileNormal.";
|
||||
mindspore::MSTensor output;
|
||||
const UINT8 *data;
|
||||
ASSERT_OK(mindspore::dataset::vision::ReadFile("./data/dataset/apple.jpg", &output));
|
||||
EXPECT_EQ(output.Shape()[0], 159109);
|
||||
EXPECT_EQ(output.DataType(), mindspore::DataType::kNumberTypeUInt8);
|
||||
data = (const UINT8 *) (output.Data().get());
|
||||
EXPECT_EQ(data[0], 255);
|
||||
EXPECT_EQ(data[1], 216);
|
||||
EXPECT_EQ(data[2], 255);
|
||||
EXPECT_EQ(data[50000], 0);
|
||||
EXPECT_EQ(data[100000], 132);
|
||||
EXPECT_EQ(data[150000], 64);
|
||||
EXPECT_EQ(data[159106], 63);
|
||||
EXPECT_EQ(data[159107], 255);
|
||||
EXPECT_EQ(data[159108], 217);
|
||||
}
|
||||
|
||||
/// Feature: ReadFile
|
||||
/// Description: Test ReadFile with invalid filename
|
||||
/// Expectation: Error is caught when the filename is invalid
|
||||
TEST_F(MindDataTestPipeline, TestReadFileException) {
|
||||
MS_LOG(INFO) << "Doing MindDataTestPipeline-TestReadFileException.";
|
||||
mindspore::MSTensor output;
|
||||
|
||||
// Test with a not exist filename
|
||||
ASSERT_ERROR(mindspore::dataset::vision::ReadFile("this_file_is_not_exist", &output));
|
||||
|
||||
// Test with a directory name
|
||||
ASSERT_ERROR(mindspore::dataset::vision::ReadFile("./data/dataset/", &output));
|
||||
}
|
||||
|
|
Binary file not shown.
After Width: | Height: | Size: 78 KiB |
Binary file not shown.
After Width: | Height: | Size: 21 KiB |
|
@ -0,0 +1,168 @@
|
|||
# Copyright 2022 Huawei Technologies Co., Ltd
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
# ==============================================================================
|
||||
"""
|
||||
Testing read_image
|
||||
"""
|
||||
import numpy
|
||||
import pytest
|
||||
|
||||
from mindspore.dataset import vision
|
||||
from mindspore.dataset.vision import ImageReadMode
|
||||
|
||||
|
||||
def test_read_image_jpeg():
|
||||
"""
|
||||
Feature: read_image
|
||||
Description: Read the contents of a JPEG image file
|
||||
Expectation: The Output is equal to the expected output
|
||||
"""
|
||||
filename = "../data/dataset/testFormats/apple.jpg"
|
||||
output = vision.read_image(filename)
|
||||
assert output.shape == (226, 403, 3)
|
||||
assert output.dtype == numpy.uint8
|
||||
assert output[0, 0, 0] == 221
|
||||
assert output[0, 0, 1] == 221
|
||||
assert output[0, 0, 2] == 221
|
||||
assert output[100, 200, 0] == 195
|
||||
assert output[100, 200, 1] == 60
|
||||
assert output[100, 200, 2] == 31
|
||||
assert output[225, 402, 0] == 181
|
||||
assert output[225, 402, 1] == 181
|
||||
assert output[225, 402, 2] == 173
|
||||
output = vision.read_image(filename, ImageReadMode.UNCHANGED)
|
||||
assert output.shape == (226, 403, 3)
|
||||
output = vision.read_image(filename, ImageReadMode.GRAYSCALE)
|
||||
assert output.shape == (226, 403, 1)
|
||||
output = vision.read_image(filename, ImageReadMode.COLOR)
|
||||
assert output.shape == (226, 403, 3)
|
||||
|
||||
filename = "../data/dataset/testFormats/apple_grayscale.jpg"
|
||||
output = vision.read_image(filename)
|
||||
assert output.shape == (226, 403, 1)
|
||||
output = vision.read_image(filename, ImageReadMode.UNCHANGED)
|
||||
assert output.shape == (226, 403, 1)
|
||||
output = vision.read_image(filename, ImageReadMode.GRAYSCALE)
|
||||
assert output.shape == (226, 403, 1)
|
||||
output = vision.read_image(filename, ImageReadMode.COLOR)
|
||||
assert output.shape == (226, 403, 3)
|
||||
|
||||
|
||||
def test_read_image_png():
|
||||
"""
|
||||
Feature: read_image
|
||||
Description: Read the contents of a PNG image file
|
||||
Expectation: The Output is equal to the expected output
|
||||
"""
|
||||
filename = "../data/dataset/testFormats/apple.png"
|
||||
output = vision.read_image(filename)
|
||||
assert output.shape == (226, 403, 3)
|
||||
output = vision.read_image(filename, ImageReadMode.UNCHANGED)
|
||||
assert output.shape == (226, 403, 3)
|
||||
output = vision.read_image(filename, ImageReadMode.GRAYSCALE)
|
||||
assert output.shape == (226, 403, 1)
|
||||
output = vision.read_image(filename, ImageReadMode.COLOR)
|
||||
assert output.shape == (226, 403, 3)
|
||||
|
||||
filename = "../data/dataset/testFormats/apple_4_channels.png"
|
||||
output = vision.read_image(filename)
|
||||
assert output.shape == (226, 403, 3)
|
||||
output = vision.read_image(filename, ImageReadMode.UNCHANGED)
|
||||
assert output.shape == (226, 403, 3)
|
||||
output = vision.read_image(filename, ImageReadMode.GRAYSCALE)
|
||||
assert output.shape == (226, 403, 1)
|
||||
output = vision.read_image(filename, ImageReadMode.COLOR)
|
||||
assert output.shape == (226, 403, 3)
|
||||
|
||||
|
||||
def test_read_image_bmp():
|
||||
"""
|
||||
Feature: read_image
|
||||
Description: Read the contents of a BMP image file
|
||||
Expectation: The Output is equal to the expected output
|
||||
"""
|
||||
filename = "../data/dataset/testFormats/apple.bmp"
|
||||
output = vision.read_image(filename)
|
||||
assert output.shape == (226, 403, 3)
|
||||
output = vision.read_image(filename, ImageReadMode.UNCHANGED)
|
||||
assert output.shape == (226, 403, 3)
|
||||
output = vision.read_image(filename, ImageReadMode.GRAYSCALE)
|
||||
assert output.shape == (226, 403, 1)
|
||||
output = vision.read_image(filename, ImageReadMode.COLOR)
|
||||
assert output.shape == (226, 403, 3)
|
||||
|
||||
|
||||
def test_read_image_tiff():
|
||||
"""
|
||||
Feature: read_image
|
||||
Description: Read the contents of a TIFF image file
|
||||
Expectation: The Output is equal to the expected output
|
||||
"""
|
||||
filename = "../data/dataset/testFormats/apple.tiff"
|
||||
output = vision.read_image(filename)
|
||||
assert output.shape == (226, 403, 3)
|
||||
output = vision.read_image(filename, ImageReadMode.UNCHANGED)
|
||||
assert output.shape == (226, 403, 3)
|
||||
output = vision.read_image(filename, ImageReadMode.GRAYSCALE)
|
||||
assert output.shape == (226, 403, 1)
|
||||
output = vision.read_image(filename, ImageReadMode.COLOR)
|
||||
assert output.shape == (226, 403, 3)
|
||||
|
||||
|
||||
def test_read_image_exception():
|
||||
"""
|
||||
Feature: read_image
|
||||
Description: Test read_image with invalid parameter
|
||||
Expectation: Error is caught when the parameter is invalid
|
||||
"""
|
||||
|
||||
def test_invalid_param(filename_param, mode_param, error, error_msg):
|
||||
"""
|
||||
a function used for checking correct error and message with invalid parameter
|
||||
"""
|
||||
with pytest.raises(error) as error_info:
|
||||
vision.read_image(filename_param, mode_param)
|
||||
assert error_msg in str(error_info.value)
|
||||
|
||||
# Test with a not exist filename
|
||||
wrong_filename = "this_file_is_not_exist"
|
||||
error_message = "Invalid file path, " + wrong_filename + " does not exist."
|
||||
test_invalid_param(wrong_filename, ImageReadMode.COLOR, RuntimeError, error_message)
|
||||
|
||||
# Test with a directory name
|
||||
wrong_filename = "../data/dataset/"
|
||||
error_message = "Invalid file path, " + wrong_filename + " is not a regular file."
|
||||
test_invalid_param(wrong_filename, ImageReadMode.COLOR, RuntimeError, error_message)
|
||||
|
||||
# Test with a not supported gif file
|
||||
wrong_filename = "../data/dataset/testFormats/apple.gif"
|
||||
error_message = "Can not read file " + wrong_filename
|
||||
test_invalid_param(wrong_filename, ImageReadMode.COLOR, RuntimeError, error_message)
|
||||
|
||||
# Test with an invalid type for the filename
|
||||
error_message = "Input filename is not of type"
|
||||
test_invalid_param(0, ImageReadMode.UNCHANGED, TypeError, error_message)
|
||||
|
||||
# Test with an invalid type for the mode
|
||||
filename = "../data/dataset/testFormats/apple.jpg"
|
||||
error_message = "Input mode is not of type"
|
||||
test_invalid_param(filename, "0", TypeError, error_message)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
test_read_image_jpeg()
|
||||
test_read_image_png()
|
||||
test_read_image_bmp()
|
||||
test_read_image_tiff()
|
||||
test_read_image_exception()
|
Loading…
Reference in New Issue