Skip to content

Commit

Permalink
Added Emgu TF Lite MAUI demo.
Browse files Browse the repository at this point in the history
  • Loading branch information
Canming Huang committed Apr 22, 2023
1 parent fb0bade commit acc6797
Show file tree
Hide file tree
Showing 48 changed files with 1,955 additions and 15 deletions.
49 changes: 49 additions & 0 deletions Emgu.TF.Example/Maui.Lite/AboutPage.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
//----------------------------------------------------------------------------
// Copyright (C) 2004-2023 by EMGU Corporation. All rights reserved.
//----------------------------------------------------------------------------


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Maui.Demo.Lite
{
public class AboutPage : ContentPage
{
public AboutPage(String htmlSource = null)
{
String tensorflowVer = Emgu.TF.Lite.TfLiteInvoke.Version;

if (htmlSource == null)
{
bool hasXnnPack = Emgu.TF.Lite.TfLiteInvoke.HasXNNPack;

htmlSource = String.Format(
@"<html>
<body>
<H1> Emgu TF Lite Examples </H1>
<H3> Tensorflow Lite version: {0} </H3>
<H3> Has XNNPack: {1} </H3>
<H3> Tensorflow Lite <a href=https://github.com/tensorflow/tensorflow/blob/master/LICENSE > license</a> </H3>
<H3><a href=https://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>"
+ @"
</body>
</html>", tensorflowVer, hasXnnPack);
}

Content =
new WebView()
{
WidthRequest = 1000,
HeightRequest = 1000,
Source = new HtmlWebViewSource()
{
Html = htmlSource
}
};
}
}
}
14 changes: 14 additions & 0 deletions Emgu.TF.Example/Maui.Lite/App.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?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.Demo.Lite"
x:Class="Maui.Demo.Lite.App">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Resources/Styles/Colors.xaml" />
<ResourceDictionary Source="Resources/Styles/Styles.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
12 changes: 12 additions & 0 deletions Emgu.TF.Example/Maui.Lite/App.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace Maui.Demo.Lite;

public partial class App : Application
{
public App()
{
InitializeComponent();
Emgu.CV.Platform.Maui.MauiInvoke.Init();
Emgu.TF.Lite.Platform.Maui.MauiInvoke.Init();
MainPage = new AppShell();
}
}
14 changes: 14 additions & 0 deletions Emgu.TF.Example/Maui.Lite/AppShell.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8" ?>
<Shell
x:Class="Maui.Demo.Lite.AppShell"
xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:Maui.Demo.Lite"
Shell.FlyoutBehavior="Disabled">

<ShellContent
Title="Home"
ContentTemplate="{DataTemplate local:MainPage}"
Route="MainPage" />

</Shell>
9 changes: 9 additions & 0 deletions Emgu.TF.Example/Maui.Lite/AppShell.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace Maui.Demo.Lite;

public partial class AppShell : Shell
{
public AppShell()
{
InitializeComponent();
}
}
122 changes: 122 additions & 0 deletions Emgu.TF.Example/Maui.Lite/InceptionPage.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
//----------------------------------------------------------------------------
// Copyright (C) 2004-2023 by EMGU Corporation. All rights reserved.
//----------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Drawing;
using System.Threading.Tasks;
using System.Diagnostics;
using System.Runtime.InteropServices;
using Emgu.CV;
using Emgu.CV.CvEnum;
using Emgu.CV.Platform.Maui.UI;
using Emgu.TF.Lite;
using Emgu.Models;
using Emgu.TF.Lite.Models;
using Size = Microsoft.Maui.Graphics.Size;

/*
#if __ANDROID__
using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
using Android.Graphics;
using Android.Preferences;
#elif __UNIFIED__ && !__IOS__
using AppKit;
using CoreGraphics;
#elif __IOS__
using UIKit;
using CoreGraphics;
#endif
*/

namespace Maui.Demo.Lite
{
public class InceptionPage : ButtonTextImagePage
{
private Inception _inception;

public InceptionPage()
: base()
{

var button = this.TopButton;
button.Text = "Perform Image Classification";
button.Clicked += OnButtonClicked;

_inception = new Inception();
_inception.OnDownloadProgressChanged += onDownloadProgressChanged;

}

private void onDownloadProgressChanged(object sender, System.Net.DownloadProgressChangedEventArgs e)
{
if (e.TotalBytesToReceive <= 0)
SetMessage(String.Format("{0} bytes downloaded", e.BytesReceived, e.ProgressPercentage));
else
SetMessage(String.Format("{0} of {1} bytes downloaded ({2}%)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage));
}

private void onDownloadCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
{
if (e != null && e.Error != null)
{
SetMessage(e.Error.Message);
return;
}
}

private async void OnButtonClicked(Object sender, EventArgs args)
{
SetMessage("Please wait while the Inception Model is being downloaded...");
await _inception.Init();
if (!_inception.Imported)
{
SetMessage("Failed to initialize Inception Model.");
return;
}
SetImage(null);
Mat[] imageFiles = await LoadImages(new string[] { "tulips.jpg" });

//handle user cancel
if (imageFiles == null || (imageFiles.Length > 0 && imageFiles[0] == null))
{
SetMessage("");
return;
}

Stopwatch watch = Stopwatch.StartNew();

Tensor t = _inception.InputTensor;
System.Drawing.Size s = new System.Drawing.Size(299, 299);
using (Mat resizedMat = new Mat(s, DepthType.Cv8U, 3))
using (Mat tensorMat = new Mat(
s,
DepthType.Cv32F,
3,
t.DataPointer,
3 * s.Width * Marshal.SizeOf<float>()))
{
CvInvoke.Resize(imageFiles[0], resizedMat, s);
CvInvoke.CvtColor(resizedMat, resizedMat, ColorConversion.Bgr2Rgb);
resizedMat.ConvertTo(tensorMat, DepthType.Cv32F, 1.0/255.0, -0.0);

}

var result = _inception.Invoke();
watch.Stop();
String resStr = String.Format("Object is {0} with {1}% probability. Recognition completed in {2} milliseconds.", result[0].Label, result[0].Probability * 100, watch.ElapsedMilliseconds);

SetImage(imageFiles[0]);
SetMessage(resStr);
}

}
}
8 changes: 8 additions & 0 deletions Emgu.TF.Example/Maui.Lite/MainPage.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="Maui.Demo.Lite.MainPage">



</ContentPage>
76 changes: 76 additions & 0 deletions Emgu.TF.Example/Maui.Lite/MainPage.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
namespace Maui.Demo.Lite;

public partial class MainPage : ContentPage
{

public MainPage()
{
InitializeComponent();


String aboutIcon = null;


ToolbarItem aboutItem = new ToolbarItem("About", aboutIcon,
() =>
{
this.Navigation.PushAsync(new AboutPage());
}
);
this.ToolbarItems.Add(aboutItem);

Button multiboxDetectionButton = new Button();
multiboxDetectionButton.Text = "Coco SSD Mobilenet";

Button mobilenetButton = new Button();
mobilenetButton.Text = "Mobilenet Object recognition";
/*
Button smartReplyButton = new Button();
smartReplyButton.Text = "Smart Reply";
*/

Button inceptionButton = new Button();
inceptionButton.Text = "Inception Flower recognition";
inceptionButton.Clicked += (sender, args) =>
{
this.Navigation.PushAsync(new InceptionPage());
};

Button modelCheckerButton = new Button();
modelCheckerButton.Text = "TF Lite model checker";

modelCheckerButton.Clicked += (sender, args) =>
{
this.Navigation.PushAsync(new ModelCheckerPage());
};


List<View> buttonList = new List<View>()
{
multiboxDetectionButton,
mobilenetButton,
inceptionButton,
modelCheckerButton
};





StackLayout buttonsLayout = new StackLayout
{
VerticalOptions = LayoutOptions.Start,
};

foreach (View b in buttonList)
buttonsLayout.Children.Add(b);

this.Content = new ScrollView()
{
Content = buttonsLayout,
};
}


}

73 changes: 73 additions & 0 deletions Emgu.TF.Example/Maui.Lite/Maui.Demo.Lite.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net7.0-ios;net7.0-android33.0</TargetFrameworks>
<TargetFrameworks Condition="$([MSBuild]::IsOSPlatform('windows'))">$(TargetFrameworks);net7.0-windows10.0.20348.0</TargetFrameworks>
<!-- Uncomment to also build the tizen app. You will need to install tizen by following this: https://github.com/Samsung/Tizen.NET -->
<!-- <TargetFrameworks>$(TargetFrameworks);net7.0-tizen</TargetFrameworks> -->
<OutputType>Exe</OutputType>
<RootNamespace>Emgu.TF.Lite.Maui.Demo</RootNamespace>
<UseMaui>true</UseMaui>
<SingleProject>true</SingleProject>
<ImplicitUsings>enable</ImplicitUsings>

<!-- Display name -->
<ApplicationTitle>Emgu TF Lite Maui Demo</ApplicationTitle>

<!-- App Identifier -->
<ApplicationId>com.emgu.tf.lite.maui.demo</ApplicationId>
<ApplicationIdGuid>f725d173-ae14-4a50-a033-127ffb80d906</ApplicationIdGuid>

<!-- Versions -->
<ApplicationDisplayVersion>1.0</ApplicationDisplayVersion>
<ApplicationVersion>1</ApplicationVersion>

<SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'ios'">11.0</SupportedOSPlatformVersion>
<SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'android'">21.0</SupportedOSPlatformVersion>
<SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'windows'">10.0.17763.0</SupportedOSPlatformVersion>
<TargetPlatformMinVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'windows'">10.0.17763.0</TargetPlatformMinVersion>
</PropertyGroup>

<ItemGroup>
<!-- App Icon -->
<MauiIcon Include="Resources\AppIcon\appicon.svg" ForegroundFile="Resources\AppIcon\appiconfg.svg" Color="#512BD4" />

<!-- Splash Screen -->
<MauiSplashScreen Include="Resources\Splash\splash.svg" Color="#512BD4" BaseSize="128,128" />

<!-- Images -->
<MauiImage Include="Resources\Images\*" />
<MauiImage Update="Resources\Images\dotnet_bot.svg" BaseSize="168,208" />

<!-- Custom Fonts -->
<MauiFont Include="Resources\Fonts\*" />

<!-- Raw Assets (also remove the "Resources\Raw" prefix) -->
<MauiAsset Include="Resources\Raw\**" LogicalName="%(RecursiveDir)%(Filename)%(Extension)" />
</ItemGroup>

<ItemGroup>
<None Remove="Resources\Raw\dog416.png" />
<None Remove="Resources\Raw\space_shuttle.jpg" />
<None Remove="Resources\Raw\surfers.jpg" />
<None Remove="Resources\Raw\tulips.jpg" />
</ItemGroup>


<ItemGroup>
<ProjectReference Include="..\..\Emgu.TF.Platform\Maui\UI\Emgu.TF.Lite.Platform.Maui.UI.csproj" />
<ProjectReference Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'ios'" Include="..\..\Emgu.TF.Lite\Emgu.TF.Lite.Netstandard.IOS\Emgu.TF.Lite.Netstandard.IOS.csproj" />
<ProjectReference Condition="!($([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'ios')" Include="..\..\Emgu.TF.Lite\Emgu.TF.Lite.Netstandard\Emgu.TF.Lite.Netstandard.csproj" />

<ProjectReference Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'ios'" Include="..\..\Emgu.TF.Lite.Models\Emgu.TF.Lite.Models.IOS\Emgu.TF.Lite.Models.IOS.csproj" />
<ProjectReference Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'android'" Include="..\..\Emgu.TF.Lite.Models\Emgu.TF.Lite.Models.Android\Emgu.TF.Lite.Models.Android.csproj" />
<ProjectReference Condition="!($([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'ios' or $([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'android')" Include="..\..\Emgu.TF.Lite.Models\Emgu.TF.Lite.Models.Netstandard\Emgu.TF.Lite.Models.Netstandard.csproj" />


<PackageReference Include="Emgu.CV.runtime.maui.mini" Version="4.7.0.5276" />
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="7.0.0" />
</ItemGroup>

<Import Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'windows'" Project="..\..\Emgu.TF.Runtime\Windows\Emgu.TF.Lite.Runtime.Windows.projitems" Label="Shared" />

</Project>
Loading

0 comments on commit acc6797

Please sign in to comment.