Skip to content

Commit

Permalink
Added Emgu TF MAUI demo.
Browse files Browse the repository at this point in the history
  • Loading branch information
Canming Huang committed Jun 23, 2023
1 parent 000b0e2 commit d54e8bf
Show file tree
Hide file tree
Showing 29 changed files with 1,006 additions and 87 deletions.
9 changes: 5 additions & 4 deletions Emgu.TF.Example/Inception.Console.NetCore/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using Emgu.TF;
using Emgu.TF.Models;
using System.Threading.Tasks;
using Emgu.Models;
using Tensorflow;

namespace Inception.Console.Netstandard
Expand Down Expand Up @@ -100,12 +101,12 @@ private static Session.Device[] GetSessionDevices(Session session)
return session.ListDevices(null);
}

private static void onDownloadProgressChanged(object sender, System.Net.DownloadProgressChangedEventArgs e)
private static void onDownloadProgressChanged(long? totalBytesToReceive, long bytesReceived, double? progressPercentage)
{
if (e.TotalBytesToReceive <= 0)
System.Console.WriteLine(String.Format("{0} bytes downloaded", e.BytesReceived, e.ProgressPercentage));
if (totalBytesToReceive <= 0)
System.Console.WriteLine(String.Format("{0} bytes downloaded", bytesReceived, progressPercentage));
else
System.Console.WriteLine(String.Format("{0} of {1} bytes downloaded ({2}%)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage));
System.Console.WriteLine(String.Format("{0} of {1} bytes downloaded ({2}%)", bytesReceived, totalBytesToReceive, progressPercentage));
}


Expand Down
2 changes: 1 addition & 1 deletion Emgu.TF.Example/Maui.Lite/InceptionPage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public InceptionPage()
private void onDownloadProgressChanged(object sender, System.Net.DownloadProgressChangedEventArgs e)
{
if (e.TotalBytesToReceive <= 0)
SetMessage(String.Format("{0} bytes downloaded", e.BytesReceived, e.ProgressPercentage));
SetMessage(String.Format("{0} bytes downloaded", e.BytesReceived));
else
SetMessage(String.Format("{0} of {1} bytes downloaded ({2}%)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage));
}
Expand Down
118 changes: 118 additions & 0 deletions Emgu.TF.Example/Maui/AboutPage.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
using System;
using System.Collections.Generic;
using System.Text;
using Tensorflow;
//using Xamarin.Forms;


namespace Emgu.TF.Maui.Demo
{
public class AboutPage : ContentPage
{
/// <summary>
/// Create and run a simple graph that add two numbers and returns the default session devices used.
/// </summary>
/// <returns></returns>
private static Session.Device[] GetSessionDevices()
{
SessionOptions so = new SessionOptions();
if (TfInvoke.IsGoogleCudaEnabled)
{
Tensorflow.ConfigProto config = new Tensorflow.ConfigProto();
config.GpuOptions = new Tensorflow.GPUOptions();
config.GpuOptions.AllowGrowth = true;
so.SetConfig(config.ToProtobuf());
}
int a = 1;
int b = 1;
//Creating tensor from value a
Tensor tensorA = new Tensor(a);
//Creating tensor from value b
Tensor tensorB = new Tensor(b);
//Create a new graph
Graph graph = new Graph();
//Place holder in the graph for tensorA
Operation opA = graph.Placeholder(DataType.Int32, null, "valA");
//Place holder in the graph for tensorB
Operation opB = graph.Placeholder(DataType.Int32, null, "valB");
//Adding the two tensor
Operation sumOp = graph.Add(opA, opB, "sum");

//Create a new session
using (Session session = new Session(graph, so))
{
//Execute the session and get the sum
Tensor[] results = session.Run(new Output[] { opA, opB }, new Tensor[] { tensorA, tensorB },
new Output[] { sumOp });

Session.Device[] devices = session.ListDevices(null);
return devices;
}
}

public AboutPage()
{
using (LogListenerSink logSink = new LogListenerSink(true))
{
Session.Device[] devices = GetSessionDevices();
StringBuilder sb = new StringBuilder();
foreach (Session.Device d in devices)
{
sb.Append(String.Format("<H4 style=\"color: blue;\">{1}: {0}</H4>", d.Name, d.Type));
}

String tensorflowVer = TfInvoke.Version;

Title = "About Emgu TF";
Content = new WebView()
{
WidthRequest = 400,
HeightRequest = 1000,
Source = new HtmlWebViewSource()
{
Html = String.Format(
@"<html>
<head>
<style>body {{ background-color: #EEEEEE; }}</style>
<style type=""text/css"">
textarea {{ width: 100%; margin: 0; padding: 0; border - width: 0; }}
</style>
</head>
<body>
<H1> Emgu TF Demos </H1>
<H3> Tensorflow version: {0} </H3>
<H3> OS: {1} </H3>
<H3> Framework: {2} </H3>
<H3> Processor: {3} </H3>
<H3> Default Session Devices: </H3> {4}
<H3> Tensorflow <a href=https://github.com/tensorflow/tensorflow/blob/master/LICENSE > License</a> </H3>
<H3><a href=http://www.emgu.com/wiki/index.php/Emgu_TF >Visit our website</a> <br/><br/><H3>
<H3><a href=mailto:support@emgu.com>Email Support</a> <br/><br/><H3>
<H3> IsGoogleCudaEnabled: {5} </H3>
<H3> IsBuiltWithROCm: {6} </H3>
<H3> IsBuiltWithNvcc: {7} </H3>
<H3> GpuSupportsHalfMatMulAndConv: {8} </H3>
<H3> IsMklEnabled: {9} </H3>
<H4> Log: </H4>
<textarea rows=""30"">{10}</textarea>"
+ @"
</body>
</html>",
tensorflowVer,
System.Runtime.InteropServices.RuntimeInformation.OSDescription,
System.Runtime.InteropServices.RuntimeInformation.FrameworkDescription,
System.Runtime.InteropServices.RuntimeInformation.ProcessArchitecture,
sb.ToString(),
TfInvoke.IsGoogleCudaEnabled,
TfInvoke.IsBuiltWithROCm,
TfInvoke.IsBuiltWithNvcc,
TfInvoke.GpuSupportsHalfMatMulAndConv,
TfInvoke.IsMklEnabled,
logSink.GetLog()
)
}
};
}
}
}
}
4 changes: 2 additions & 2 deletions Emgu.TF.Example/Maui/App.xaml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?xml version = "1.0" encoding = "UTF-8" ?>
<Application xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:Maui"
x:Class="Maui.App">
xmlns:local="clr-namespace:Emgu.TF.Maui.Demo"
x:Class="Emgu.TF.Maui.Demo.App">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
Expand Down
5 changes: 4 additions & 1 deletion Emgu.TF.Example/Maui/App.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
namespace Maui
namespace Emgu.TF.Maui.Demo
{
public partial class App : Application
{
public App()
{
InitializeComponent();

Emgu.CV.Platform.Maui.MauiInvoke.Init();
Emgu.TF.Platform.Maui.MauiInvoke.Init();

MainPage = new AppShell();
}
}
Expand Down
4 changes: 2 additions & 2 deletions Emgu.TF.Example/Maui/AppShell.xaml
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<?xml version="1.0" encoding="UTF-8" ?>
<Shell
x:Class="Maui.AppShell"
x:Class="Emgu.TF.Maui.Demo.AppShell"
xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:Maui"
xmlns:local="clr-namespace:Emgu.TF.Maui.Demo"
Shell.FlyoutBehavior="Disabled">

<ShellContent
Expand Down
2 changes: 1 addition & 1 deletion Emgu.TF.Example/Maui/AppShell.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace Maui
namespace Emgu.TF.Maui.Demo
{
public partial class AppShell : Shell
{
Expand Down
Loading

0 comments on commit d54e8bf

Please sign in to comment.