forked from microsoft/onnxruntime
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add general Xamarin sample for vision models. (#58)
* Add general Xamarin sample for vision modles. Currently demonstrates image classification and face recognition. More models can be added in the future for SSD etc.
- Loading branch information
1 parent
545f5aa
commit 72e3962
Showing
78 changed files
with
32,935 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/Models |
Binary file not shown.
19 changes: 19 additions & 0 deletions
19
mobile/examples/Xamarin/VisionSample/VisionSample.Forms.Android/Assets/AboutAssets.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
Any raw assets you want to be deployed with your application can be placed in | ||
this directory (and child directories) and given a Build Action of "AndroidAsset". | ||
|
||
These files will be deployed with your package and will be accessible using Android's | ||
AssetManager, like this: | ||
|
||
public class ReadAsset : Activity | ||
{ | ||
protected override void OnCreate (Bundle bundle) | ||
{ | ||
base.OnCreate (bundle); | ||
|
||
InputStream input = Assets.Open ("my_asset.txt"); | ||
} | ||
} | ||
|
||
Additionally, some Android functions will automatically load asset files: | ||
|
||
Typeface tf = Typeface.CreateFromAsset (Context.Assets, "fonts/samplefont.ttf"); |
39 changes: 39 additions & 0 deletions
39
mobile/examples/Xamarin/VisionSample/VisionSample.Forms.Android/MainActivity.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
using Android.App; | ||
using Android.Content.PM; | ||
using Android.Runtime; | ||
using Android.OS; | ||
using Microsoft.ML.OnnxRuntime; | ||
|
||
namespace VisionSample.Forms.Droid | ||
{ | ||
[Activity(Label = "VisionSample.Forms", Icon = "@mipmap/icon", Theme = "@style/MainTheme", MainLauncher = true, | ||
ScreenOrientation = ScreenOrientation.Portrait, | ||
ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation | ConfigChanges.UiMode | | ||
ConfigChanges.ScreenLayout | ConfigChanges.SmallestScreenSize)] | ||
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity | ||
{ | ||
protected override void OnCreate(Bundle savedInstanceState) | ||
{ | ||
base.OnCreate(savedInstanceState); | ||
|
||
Xamarin.Essentials.Platform.Init(this, savedInstanceState); | ||
global::Xamarin.Forms.Forms.Init(this, savedInstanceState); | ||
|
||
SessionOptionsContainer.Register( | ||
nameof(ExecutionProviderOptions.Platform), | ||
(sessionOptions) => sessionOptions.AppendExecutionProvider_Nnapi(NnapiFlags.NNAPI_FLAG_USE_NONE)); | ||
|
||
LoadApplication(new App()); | ||
} | ||
|
||
public override void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Permission[] grantResults) | ||
{ | ||
Xamarin.Essentials.Platform.OnRequestPermissionsResult(requestCode, permissions, grantResults); | ||
|
||
base.OnRequestPermissionsResult(requestCode, permissions, grantResults); | ||
} | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
...e/examples/Xamarin/VisionSample/VisionSample.Forms.Android/Properties/AndroidManifest.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="1" android:versionName="1.0" package="com.microsoft.onnxruntime_visionsample_forms"> | ||
<uses-sdk android:minSdkVersion="27" android:targetSdkVersion="30" /> | ||
<application android:label="VisionSample.Forms.Android" android:theme="@style/MainTheme"> | ||
<provider android:name="androidx.core.content.FileProvider" android:authorities="${applicationId}.fileprovider" android:exported="false" android:grantUriPermissions="true"> | ||
<meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/file_paths"></meta-data> | ||
</provider> | ||
</application> | ||
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> | ||
</manifest> |
39 changes: 39 additions & 0 deletions
39
mobile/examples/Xamarin/VisionSample/VisionSample.Forms.Android/Properties/AssemblyInfo.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
using System.Reflection; | ||
using System.Runtime.InteropServices; | ||
using Android.App; | ||
|
||
// General Information about an assembly is controlled through the following | ||
// set of attributes. Change these attribute values to modify the information | ||
// associated with an assembly. | ||
[assembly: AssemblyTitle("VisionSample.Forms.Android")] | ||
[assembly: AssemblyDescription("")] | ||
[assembly: AssemblyConfiguration("")] | ||
[assembly: AssemblyCompany("")] | ||
[assembly: AssemblyProduct("VisionSample.Forms.Android")] | ||
[assembly: AssemblyCopyright("Copyright © 2021")] | ||
[assembly: AssemblyTrademark("")] | ||
[assembly: AssemblyCulture("")] | ||
[assembly: ComVisible(false)] | ||
|
||
// Version information for an assembly consists of the following four values: | ||
// | ||
// Major Version | ||
// Minor Version | ||
// Build Number | ||
// Revision | ||
[assembly: AssemblyVersion("1.0.0.0")] | ||
[assembly: AssemblyFileVersion("1.0.0.0")] | ||
|
||
// Add some common permissions, these can be removed if not needed | ||
[assembly: UsesPermission(Android.Manifest.Permission.Internet)] | ||
[assembly: UsesPermission(Android.Manifest.Permission.WriteExternalStorage)] | ||
|
||
// Needed for Picking photo/video | ||
[assembly: UsesPermission(Android.Manifest.Permission.ReadExternalStorage)] | ||
|
||
// Needed for Taking photo/video | ||
[assembly: UsesPermission(Android.Manifest.Permission.Camera)] | ||
|
||
// Add these properties if you would like to filter out devices that do not have cameras, or set to false to make them optional | ||
[assembly: UsesFeature("android.hardware.camera", Required = true)] | ||
[assembly: UsesFeature("android.hardware.camera.autofocus", Required = true)] |
50 changes: 50 additions & 0 deletions
50
mobile/examples/Xamarin/VisionSample/VisionSample.Forms.Android/Resources/AboutResources.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
Images, layout descriptions, binary blobs and string dictionaries can be included | ||
in your application as resource files. Various Android APIs are designed to | ||
operate on the resource IDs instead of dealing with images, strings or binary blobs | ||
directly. | ||
|
||
For example, a sample Android app that contains a user interface layout (main.xml), | ||
an internationalization string table (strings.xml) and some icons (drawable-XXX/icon.png) | ||
would keep its resources in the "Resources" directory of the application: | ||
|
||
Resources/ | ||
drawable-hdpi/ | ||
icon.png | ||
|
||
drawable-ldpi/ | ||
icon.png | ||
|
||
drawable-mdpi/ | ||
icon.png | ||
|
||
layout/ | ||
main.xml | ||
|
||
values/ | ||
strings.xml | ||
|
||
In order to get the build system to recognize Android resources, set the build action to | ||
"AndroidResource". The native Android APIs do not operate directly with filenames, but | ||
instead operate on resource IDs. When you compile an Android application that uses resources, | ||
the build system will package the resources for distribution and generate a class called | ||
"Resource" that contains the tokens for each one of the resources included. For example, | ||
for the above Resources layout, this is what the Resource class would expose: | ||
|
||
public class Resource { | ||
public class drawable { | ||
public const int icon = 0x123; | ||
} | ||
|
||
public class layout { | ||
public const int main = 0x456; | ||
} | ||
|
||
public class strings { | ||
public const int first_string = 0xabc; | ||
public const int second_string = 0xbcd; | ||
} | ||
} | ||
|
||
You would then use R.drawable.icon to reference the drawable/icon.png file, or Resource.layout.main | ||
to reference the layout/main.xml file, or Resource.strings.first_string to reference the first | ||
string in the dictionary file values/strings.xml. |
Oops, something went wrong.