From f99c9be9493912e7ac0be7b2164cf75e18efde99 Mon Sep 17 00:00:00 2001 From: dogvane Date: Wed, 26 Jul 2023 22:37:32 +0800 Subject: [PATCH] add image save as jpg and png funcion and testunit --- data/img001.bmp | Bin 0 -> 178662 bytes src/TensorFlowNET.Core/APIs/tf.image.cs | 7 +++++ src/TensorFlowNET.Core/APIs/tf.io.cs | 7 +++++ src/TensorFlowNET.Core/Operations/gen_ops.cs | 5 ++-- .../Operations/image_ops_impl.cs | 16 ++++++++++ .../TensorFlowNET.Graph.UnitTest/ImageTest.cs | 28 ++++++++++++++++++ 6 files changed, 61 insertions(+), 2 deletions(-) create mode 100644 data/img001.bmp diff --git a/data/img001.bmp b/data/img001.bmp new file mode 100644 index 0000000000000000000000000000000000000000..86cfa972f9d1620eb234ae74a55b6f98bce5f9e2 GIT binary patch literal 178662 zcmeI5L5}7+8AS_L!GIwHR=@(7vq@IKlno&!%$S2AEi1AU=ZpQ>zK&0+`zdj3$M@V* zf0xux@BR5N|M>fFfByINr||oa;rG9P{r=6*@50s3&)^>N;Wm8X_PT{~@`FGwHg4m_2MFAW9inaW zx&76co98NsjoX{Mv2^^h*torPxjP^JQEc4aS&wzIUy2>#1fS1e>jo^J{G2gdmnZXd zAp&<|hX|XLzDdl5oOC&AL2TT1AlA@E9U+-^b5+K~j_ijCVN+u>c+1S-VF?P}UA97do{ zY}_7J5HG4DP%SoY*8ykM00Ng*he7+RH~AMWHi>EPU3_M{*dgF1rEd~b+*srBT*St0 zBW#+izQ7zB_?VV&k@ZQ*8K(KtHi@`&AK4_9f6&Y~1!Oi{0!5dW((Q_AZ#|NML~2 zxb4^%TNw$A5gWISbui6~z%a3K+p9A6u@D$3Hf~!QVU7!d!D8dKOKWWMNMO9!xcyiP z6WR$(5gWJLi&Ng=55NDHjuC)%{+@i5@j@QX2ijCV|B~IMOsB7J{mRxhi z#_g8w=B}-CwR+Xtu+zoH?YEUF%&+#deW5YFg4noiEK6kCTRDx#_T5%QY}_`sKYf}} zKb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Y5zNpC^t?IZxrGxLttL>CvOu$9tnQmlvJRR@nCP7{_2?)x<}*Js7@G z?Bi4gokmJkKE_~x_SfOIomKnHS7OK4mDLJQ<4oU+$M^PowM!7(cEM=bq)zN(XNzWD zrj2};gIO>%A7Y zs|h1^HM%hial4K)V%KpSxeB){Xd`w7wDC)D`z>?CeoJiDeB6FT9Zd+Qov^or0)-q=n0o;BhkJyihG$-J8xfjGP^HF|^+l6*6#V*X}krV;kzN-VV z@5rn&j@y?_A@(JfwFYrJ?+dYWCM%BMcG4PRCqUM{#qD! image_ops_impl.decode_image(contents, channels: channels, dtype: dtype, name: name, expand_animations: expand_animations); + public Tensor encode_png(Tensor contents, string name = null) + => image_ops_impl.encode_png(contents, name: name); + + public Tensor encode_jpeg(Tensor contents, string name = null) + => image_ops_impl.encode_jpeg(contents, name: name); + + /// /// Convenience function to check if the 'contents' encodes a JPEG image. /// diff --git a/src/TensorFlowNET.Core/APIs/tf.io.cs b/src/TensorFlowNET.Core/APIs/tf.io.cs index be1e86e6..4cb18070 100644 --- a/src/TensorFlowNET.Core/APIs/tf.io.cs +++ b/src/TensorFlowNET.Core/APIs/tf.io.cs @@ -16,6 +16,7 @@ using System.Collections.Generic; using Tensorflow.IO; +using Tensorflow.Operations; namespace Tensorflow { @@ -46,6 +47,12 @@ namespace Tensorflow public Tensor[] restore_v2(Tensor prefix, string[] tensor_names, string[] shape_and_slices, TF_DataType[] dtypes, string name = null) => ops.restore_v2(prefix, tensor_names, shape_and_slices, dtypes, name: name); + + public void write_file(string filename, Tensor conentes, string name = null) + => write_file(Tensorflow.ops.convert_to_tensor(filename, TF_DataType.TF_STRING), conentes, name); + + public void write_file(Tensor filename, Tensor conentes, string name = null) + => gen_ops.write_file(filename, conentes, name); } public GFile gfile = new GFile(); diff --git a/src/TensorFlowNET.Core/Operations/gen_ops.cs b/src/TensorFlowNET.Core/Operations/gen_ops.cs index 5fa4c97d..ed756740 100644 --- a/src/TensorFlowNET.Core/Operations/gen_ops.cs +++ b/src/TensorFlowNET.Core/Operations/gen_ops.cs @@ -39083,13 +39083,14 @@ namespace Tensorflow.Operations /// /// creates directory if not existing. /// - public static Operation write_file(Tensor filename, Tensor contents, string name = "WriteFile") + public static Tensor write_file(Tensor filename, Tensor contents, string name = "WriteFile") { var dict = new Dictionary(); dict["filename"] = filename; dict["contents"] = contents; var op = tf.OpDefLib._apply_op_helper("WriteFile", name: name, keywords: dict); - return op; + op.run(); + return op.output; } /// diff --git a/src/TensorFlowNET.Core/Operations/image_ops_impl.cs b/src/TensorFlowNET.Core/Operations/image_ops_impl.cs index 318b8b14..0c827b7f 100644 --- a/src/TensorFlowNET.Core/Operations/image_ops_impl.cs +++ b/src/TensorFlowNET.Core/Operations/image_ops_impl.cs @@ -2047,6 +2047,22 @@ new_height, new_width"); }); } + public static Tensor encode_jpeg(Tensor contents, string name = null) + { + return tf_with(ops.name_scope(name, "encode_jpeg"), scope => + { + return gen_ops.encode_jpeg(contents, name:name); + }); + } + + public static Tensor encode_png(Tensor contents, string name = null) + { + return tf_with(ops.name_scope(name, "encode_png"), scope => + { + return gen_ops.encode_png(contents, name: name); + }); + } + public static Tensor is_jpeg(Tensor contents, string name = null) { return tf_with(ops.name_scope(name, "is_jpeg"), scope => diff --git a/test/TensorFlowNET.Graph.UnitTest/ImageTest.cs b/test/TensorFlowNET.Graph.UnitTest/ImageTest.cs index d671b609..6a430443 100644 --- a/test/TensorFlowNET.Graph.UnitTest/ImageTest.cs +++ b/test/TensorFlowNET.Graph.UnitTest/ImageTest.cs @@ -4,6 +4,7 @@ using System.Linq; using Tensorflow; using static Tensorflow.Binding; using System; +using System.IO; namespace TensorFlowNET.UnitTest { @@ -164,5 +165,32 @@ namespace TensorFlowNET.UnitTest Assert.AreEqual(result.size, 16ul); Assert.AreEqual(result[0, 0, 0, 0], 12f); } + + [TestMethod] + public void ImageSaveTest() + { + var imgPath = TestHelper.GetFullPathFromDataDir("img001.bmp"); + var jpegImgPath = TestHelper.GetFullPathFromDataDir("img001.jpeg"); + var pngImgPath = TestHelper.GetFullPathFromDataDir("img001.png"); + + File.Delete(jpegImgPath); + File.Delete(pngImgPath); + + var contents = tf.io.read_file(imgPath); + var bmp = tf.image.decode_image(contents); + Assert.AreEqual(bmp.name, "decode_image/DecodeImage:0"); + + var jpeg = tf.image.encode_jpeg(bmp); + tf.io.write_file(jpegImgPath, jpeg); + Assert.IsTrue(File.Exists(jpegImgPath)); + + var png = tf.image.encode_png(bmp); + tf.io.write_file(pngImgPath, png); + Assert.IsTrue(File.Exists(pngImgPath)); + + // 如果要测试图片正确性,可以注释下面两行代码 + File.Delete(jpegImgPath); + File.Delete(pngImgPath); + } } }