@@ -339,6 +339,13 @@ namespace Tensorflow | |||||
=> image_ops_impl.decode_image(contents, channels: channels, dtype: dtype, | => image_ops_impl.decode_image(contents, channels: channels, dtype: dtype, | ||||
name: name, expand_animations: expand_animations); | 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); | |||||
/// <summary> | /// <summary> | ||||
/// Convenience function to check if the 'contents' encodes a JPEG image. | /// Convenience function to check if the 'contents' encodes a JPEG image. | ||||
/// </summary> | /// </summary> | ||||
@@ -16,6 +16,7 @@ | |||||
using System.Collections.Generic; | using System.Collections.Generic; | ||||
using Tensorflow.IO; | using Tensorflow.IO; | ||||
using Tensorflow.Operations; | |||||
namespace Tensorflow | namespace Tensorflow | ||||
{ | { | ||||
@@ -46,6 +47,12 @@ namespace Tensorflow | |||||
public Tensor[] restore_v2(Tensor prefix, string[] tensor_names, | public Tensor[] restore_v2(Tensor prefix, string[] tensor_names, | ||||
string[] shape_and_slices, TF_DataType[] dtypes, string name = null) | string[] shape_and_slices, TF_DataType[] dtypes, string name = null) | ||||
=> ops.restore_v2(prefix, tensor_names, shape_and_slices, dtypes, name: name); | => 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(); | public GFile gfile = new GFile(); | ||||
@@ -39083,13 +39083,14 @@ namespace Tensorflow.Operations | |||||
/// <remarks> | /// <remarks> | ||||
/// creates directory if not existing. | /// creates directory if not existing. | ||||
/// </remarks> | /// </remarks> | ||||
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<string, object>(); | var dict = new Dictionary<string, object>(); | ||||
dict["filename"] = filename; | dict["filename"] = filename; | ||||
dict["contents"] = contents; | dict["contents"] = contents; | ||||
var op = tf.OpDefLib._apply_op_helper("WriteFile", name: name, keywords: dict); | var op = tf.OpDefLib._apply_op_helper("WriteFile", name: name, keywords: dict); | ||||
return op; | |||||
op.run(); | |||||
return op.output; | |||||
} | } | ||||
/// <summary> | /// <summary> | ||||
@@ -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) | public static Tensor is_jpeg(Tensor contents, string name = null) | ||||
{ | { | ||||
return tf_with(ops.name_scope(name, "is_jpeg"), scope => | return tf_with(ops.name_scope(name, "is_jpeg"), scope => | ||||
@@ -4,6 +4,7 @@ using System.Linq; | |||||
using Tensorflow; | using Tensorflow; | ||||
using static Tensorflow.Binding; | using static Tensorflow.Binding; | ||||
using System; | using System; | ||||
using System.IO; | |||||
namespace TensorFlowNET.UnitTest | namespace TensorFlowNET.UnitTest | ||||
{ | { | ||||
@@ -164,5 +165,32 @@ namespace TensorFlowNET.UnitTest | |||||
Assert.AreEqual(result.size, 16ul); | Assert.AreEqual(result.size, 16ul); | ||||
Assert.AreEqual(result[0, 0, 0, 0], 12f); | 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); | |||||
} | |||||
} | } | ||||
} | } |