@@ -16,6 +16,7 @@ limitations under the License.
1616
1717using System ;
1818using System . Collections . Generic ;
19+ using System . Linq ;
1920using System . Text ;
2021using Tensorflow . Operations ;
2122using static Tensorflow . Binding ;
@@ -63,7 +64,7 @@ public static Tensor decode_image(Tensor contents, int channels = 0, TF_DataType
6364 Func < ITensorOrOperation > _bmp = ( ) =>
6465 {
6566 int bmp_channels = channels ;
66- var signature = string_ops . substr ( contents , 0 , 2 ) ;
67+ var signature = tf . strings . substr ( contents , 0 , 2 ) ;
6768 var is_bmp = math_ops . equal ( signature , "BM" , name : "is_bmp" ) ;
6869 string decode_msg = "Unable to decode bytes as JPEG, PNG, GIF, or BMP" ;
6970 var assert_decode = control_flow_ops . Assert ( is_bmp , new string [ ] { decode_msg } ) ;
@@ -98,7 +99,7 @@ public static Tensor decode_image(Tensor contents, int channels = 0, TF_DataType
9899
99100 return tf_with ( ops . name_scope ( name , "decode_image" ) , scope =>
100101 {
101- substr = string_ops . substr ( contents , 0 , 3 ) ;
102+ substr = tf . strings . substr ( contents , 0 , 3 ) ;
102103 return control_flow_ops . cond ( is_jpeg ( contents ) , _jpeg , check_png , name : "cond_jpeg" ) ;
103104 } ) ;
104105 }
@@ -128,16 +129,19 @@ public static Tensor is_jpeg(Tensor contents, string name = null)
128129 {
129130 return tf_with ( ops . name_scope ( name , "is_jpeg" ) , scope =>
130131 {
131- var substr = string_ops . substr ( contents , 0 , 3 ) ;
132- return math_ops . equal ( substr , "\xff \xd8 \xff " , name : name ) ;
132+ var substr = tf . strings . substr ( contents , 0 , 3 ) ;
133+ var jpg = Encoding . UTF8 . GetString ( new byte [ ] { 0xff , 0xd8 , 0xff } ) ;
134+ var jpg_tensor = tf . constant ( jpg ) ;
135+ var result = math_ops . equal ( substr , jpg_tensor , name : name ) ;
136+ return result ;
133137 } ) ;
134138 }
135139
136140 public static Tensor _is_png ( Tensor contents , string name = null )
137141 {
138142 return tf_with ( ops . name_scope ( name , "is_png" ) , scope =>
139143 {
140- var substr = string_ops . substr ( contents , 0 , 3 ) ;
144+ var substr = tf . strings . substr ( contents , 0 , 3 ) ;
141145 return math_ops . equal ( substr , @"\211PN" , name : name ) ;
142146 } ) ;
143147 }
0 commit comments