Skip to content

Commit 95cd55d

Browse files
committed
Image Background Removal
1 parent 9ab3066 commit 95cd55d

2 files changed

Lines changed: 59 additions & 39 deletions

File tree

‎test/TensorFlowNET.Examples/ImageProcess/ImageBackgroundRemoval.cs‎

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.IO;
34
using System.Text;
5+
using Tensorflow;
46
using TensorFlowNET.Examples.Utility;
7+
using static Tensorflow.Python;
58

69
namespace TensorFlowNET.Examples.ImageProcess
710
{
@@ -19,18 +22,42 @@ public class ImageBackgroundRemoval : IExample
1922

2023
public string Name => "Image Background Removal";
2124

22-
string modelDir = "deeplabv3";
25+
string dataDir = "deeplabv3";
26+
string modelDir = "deeplabv3_mnv2_pascal_train_aug";
27+
string modelName = "frozen_inference_graph.pb";
2328

2429
public bool Run()
2530
{
31+
PrepareData();
32+
33+
// import GraphDef from pb file
34+
var graph = new Graph().as_default();
35+
graph.Import(Path.Join(dataDir, modelDir, modelName));
36+
37+
Tensor output = graph.OperationByName("SemanticPredictions");
38+
39+
with(tf.Session(graph), sess =>
40+
{
41+
// Runs inference on a single image.
42+
sess.run(output, new FeedItem(output, "[np.asarray(resized_image)]"));
43+
});
44+
2645
return false;
2746
}
2847

2948
public void PrepareData()
3049
{
31-
// get model file
32-
string url = "http://download.tensorflow.org/models/deeplabv3_mnv2_pascal_train_aug_2018_01_29.tar.gz";
33-
Web.Download(url, modelDir, "deeplabv3_mnv2_pascal_train_aug_2018_01_29.tar.gz");
50+
// get mobile_net_model file
51+
string fileName = "deeplabv3_mnv2_pascal_train_aug_2018_01_29.tar.gz";
52+
string url = $"http://download.tensorflow.org/models/{fileName}";
53+
Web.Download(url, dataDir, fileName);
54+
Compress.ExtractTGZ(Path.Join(dataDir, fileName), dataDir);
55+
56+
// xception_model, better accuracy
57+
/*fileName = "deeplabv3_pascal_train_aug_2018_01_04.tar.gz";
58+
url = $"http://download.tensorflow.org/models/{fileName}";
59+
Web.Download(url, modelDir, fileName);
60+
Compress.ExtractTGZ(Path.Join(modelDir, fileName), modelDir);*/
3461
}
3562
}
3663
}

‎test/TensorFlowNET.Examples/Utility/PbtxtParser.cs‎

Lines changed: 28 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -23,52 +23,45 @@ public static PbtxtItems ParsePbtxtFile(string filePath)
2323
string line;
2424
string newText = "{\"items\":[";
2525

26-
try
26+
using (System.IO.StreamReader reader = new System.IO.StreamReader(filePath))
2727
{
28-
using (System.IO.StreamReader reader = new System.IO.StreamReader(filePath))
28+
29+
while ((line = reader.ReadLine()) != null)
2930
{
31+
string newline = string.Empty;
3032

31-
while ((line = reader.ReadLine()) != null)
33+
if (line.Contains("{"))
3234
{
33-
string newline = string.Empty;
34-
35-
if (line.Contains("{"))
36-
{
37-
newline = line.Replace("item", "").Trim();
38-
//newText += line.Insert(line.IndexOf("=") + 1, "\"") + "\",";
39-
newText += newline;
40-
}
41-
else if (line.Contains("}"))
42-
{
43-
newText = newText.Remove(newText.Length - 1);
44-
newText += line;
45-
newText += ",";
46-
}
47-
else
48-
{
49-
newline = line.Replace(":", "\":").Trim();
50-
newline = "\"" + newline;// newline.Insert(0, "\"");
51-
newline += ",";
52-
53-
newText += newline;
54-
}
55-
35+
newline = line.Replace("item", "").Trim();
36+
//newText += line.Insert(line.IndexOf("=") + 1, "\"") + "\",";
37+
newText += newline;
38+
}
39+
else if (line.Contains("}"))
40+
{
41+
newText = newText.Remove(newText.Length - 1);
42+
newText += line;
43+
newText += ",";
5644
}
45+
else
46+
{
47+
newline = line.Replace(":", "\":").Trim();
48+
newline = "\"" + newline;// newline.Insert(0, "\"");
49+
newline += ",";
5750

58-
newText = newText.Remove(newText.Length - 1);
59-
newText += "]}";
51+
newText += newline;
52+
}
6053

61-
reader.Close();
6254
}
6355

64-
PbtxtItems items = JsonConvert.DeserializeObject<PbtxtItems>(newText);
56+
newText = newText.Remove(newText.Length - 1);
57+
newText += "]}";
6558

66-
return items;
67-
}
68-
catch (Exception ex)
69-
{
70-
return null;
59+
reader.Close();
7160
}
61+
62+
PbtxtItems items = JsonConvert.DeserializeObject<PbtxtItems>(newText);
63+
64+
return items;
7265
}
7366
}
7467
}

0 commit comments

Comments
 (0)