-
Notifications
You must be signed in to change notification settings - Fork 306
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add overload of LoadImage for SkiaSharp (#230)
- Loading branch information
1 parent
fd6babe
commit 8ee6bc9
Showing
19 changed files
with
395 additions
and
9 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
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
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,4 @@ | ||
dlib_face_recognition_resnet_model_v1.dat | ||
mmod_human_face_detector.dat | ||
shape_predictor_5_face_landmarks.dat | ||
shape_predictor_68_face_landmarks.dat |
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,90 @@ | ||
using System; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Net.Http; | ||
|
||
using FaceRecognitionDotNet; | ||
using Microsoft.Extensions.CommandLineUtils; | ||
using SkiaSharp; | ||
|
||
namespace SkiaSharpSample | ||
{ | ||
|
||
internal class Program | ||
{ | ||
|
||
#region Fields | ||
|
||
private static FaceRecognition _FaceRecognition; | ||
|
||
#endregion | ||
|
||
#region Methods | ||
|
||
private static void Main(string[] args) | ||
{ | ||
var app = new CommandLineApplication(false); | ||
app.Name = nameof(SkiaSharpSample); | ||
app.Description = "The program for measure face encoding performance"; | ||
app.HelpOption("-h|--help"); | ||
|
||
var modelsOption = app.Option("-m|--model", "model files directory path", CommandOptionType.SingleValue); | ||
|
||
app.OnExecute(() => | ||
{ | ||
if (!modelsOption.HasValue()) | ||
{ | ||
app.ShowHelp(); | ||
return -1; | ||
} | ||
var directory = modelsOption.Value(); | ||
if (!Directory.Exists(directory)) | ||
{ | ||
app.ShowHelp(); | ||
return -1; | ||
} | ||
_FaceRecognition = FaceRecognition.Create(directory); | ||
var testImages = new[] | ||
{ | ||
"obama-240p.jpg", | ||
"obama-480p.jpg", | ||
"obama-720p.jpg", | ||
"obama-1080p.jpg" | ||
}; | ||
const string url = "https://upload.wikimedia.org/wikipedia/commons/9/9d/Barack_Obama.jpg"; | ||
var binary = new HttpClient().GetByteArrayAsync(url).Result; | ||
using (var bitmap = SKBitmap.Decode(binary)) | ||
using (var searchImage = FaceRecognitionDotNet.Extensions.Skia.FaceRecognition.LoadImage(bitmap)) | ||
{ | ||
var searchEncodings = _FaceRecognition.FaceEncodings(searchImage); | ||
foreach (var path in testImages) | ||
{ | ||
var targetImage = FaceRecognition.LoadImageFile(path); | ||
var targetEncoding = _FaceRecognition.FaceEncodings(targetImage); | ||
var distance = FaceRecognition.FaceDistance(searchEncodings.First(), targetEncoding.First()); | ||
Console.WriteLine($"Distance: {distance} for {path}"); | ||
foreach (var encoding in targetEncoding) encoding.Dispose(); | ||
} | ||
foreach (var encoding in searchEncodings) encoding.Dispose(); | ||
} | ||
return 0; | ||
}); | ||
|
||
app.Execute(args); | ||
} | ||
|
||
#endregion | ||
|
||
} | ||
|
||
} |
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,43 @@ | ||
# SkiaSharpSharp Sample | ||
|
||
This sample requires to use CUDA. | ||
|
||
## How to use? | ||
|
||
## 1. Preparation | ||
|
||
This sample requires model files. | ||
|
||
## 2. Build | ||
|
||
1. Open command prompt and change to <SkiaSharpSharpSample_dir> | ||
1. Type the following command | ||
```` | ||
$ dotnet remove reference ../../src/FaceRecognitionDotNet\FaceRecognitionDotNet.csproj | ||
$ dotnet remove reference ../../src/FaceRecognitionDotNet\FaceRecognitionDotNet.Extensions.Skia.csproj | ||
$ dotnet add package FaceRecognitionDotNet | ||
$ dotnet add package FaceRecognitionDotNet.Extensions.Skia | ||
$ dotnet build -c Release | ||
```` | ||
|
||
## 3. Run | ||
|
||
1. Open command prompt and change to <SkiaSharpSharpSample_dir> | ||
1. Type the following sample command | ||
```` | ||
$ dotnet run -c Release -- "-m=models" | ||
Distance: 0.342289226629347 for obama-240p.jpg | ||
Distance: 0.346749102653167 for obama-480p.jpg | ||
Distance: 0.369195738248585 for obama-720p.jpg | ||
Distance: 0.361052041208253 for obama-1080p.jpg | ||
```` | ||
|
||
## 4. Parameters | ||
|
||
This program support the following argument and option. | ||
|
||
### Argument | ||
|
||
|Argument|Description| | ||
|:---|:---| | ||
|-m\|--model|Directory path includes model files| |
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,17 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>netcoreapp2.0</TargetFramework> | ||
<Authors>Takuya Takeuchi</Authors> | ||
<Description>Example of FaceRecognitionDotNet</Description> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="FaceRecognitionDotNet" Version="1.3.0.8" /> | ||
<PackageReference Include="FaceRecognitionDotNet.Extensions.Skia" Version="1.3.0.8" /> | ||
<PackageReference Include="Microsoft.Extensions.CommandLineUtils" Version="1.1.1" /> | ||
<PackageReference Include="SkiaSharp" Version="2.88.3" /> | ||
</ItemGroup> | ||
|
||
</Project> |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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,8 @@ | ||
Copyright (c) 2015-2016 Xamarin, Inc. | ||
Copyright (c) 2017-2018 Microsoft Corporation. | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
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
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
131 changes: 131 additions & 0 deletions
131
src/FaceRecognitionDotNet.Extensions.Skia/FaceRecognition.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,131 @@ | ||
using System; | ||
using System.Runtime.InteropServices; | ||
|
||
using DlibDotNet; | ||
using SkiaSharp; | ||
|
||
namespace FaceRecognitionDotNet.Extensions.Skia | ||
{ | ||
|
||
public sealed class FaceRecognition | ||
{ | ||
|
||
#region Methods | ||
|
||
/// <summary> | ||
/// Creates an <see cref="Image"/> from the specified existing bitmap image. | ||
/// </summary> | ||
/// <param name="bitmap">The <see cref="SKBitmap"/> from which to create the new <see cref="Image"/>.</param> | ||
/// <returns>The <see cref="Image"/> this method creates.</returns> | ||
/// <exception cref="ArgumentNullException"><paramref name="bitmap"/> is null.</exception> | ||
/// <exception cref="ArgumentOutOfRangeException">The specified <see cref="SKColorType"/> is not supported.</exception> | ||
public static Image LoadImage(SKBitmap bitmap) | ||
{ | ||
var width = bitmap.Width; | ||
var height = bitmap.Height; | ||
var colorType = bitmap.ColorType; | ||
|
||
Mode mode; | ||
int srcChannel; | ||
int dstChannel; | ||
switch (colorType) | ||
{ | ||
case SKColorType.Rgba8888: | ||
mode = Mode.Rgb; | ||
srcChannel = 4; | ||
dstChannel = 3; | ||
break; | ||
case SKColorType.Bgra8888: | ||
mode = Mode.Rgb; | ||
srcChannel = 4; | ||
dstChannel = 3; | ||
break; | ||
case SKColorType.Gray8: | ||
mode = Mode.Greyscale; | ||
srcChannel = 1; | ||
dstChannel = 1; | ||
break; | ||
default: | ||
throw new ArgumentOutOfRangeException($"{nameof(bitmap)}", $"The specified {nameof(SKColorType)} is not supported."); | ||
} | ||
|
||
unsafe | ||
{ | ||
var array = new byte[width * height * dstChannel]; | ||
fixed (byte* pArray = &array[0]) | ||
{ | ||
|
||
switch (srcChannel) | ||
{ | ||
case 1: | ||
{ | ||
var src = bitmap.GetPixels(); | ||
var stride = bitmap.RowBytes; | ||
|
||
for (var h = 0; h < height; h++) | ||
Marshal.Copy(IntPtr.Add(src, h * stride), array, h * width, width * dstChannel); | ||
} | ||
break; | ||
case 3: | ||
case 4: | ||
{ | ||
if (colorType == SKColorType.Rgba8888) | ||
{ | ||
var src = (byte*)bitmap.GetPixels(); | ||
var stride = bitmap.RowBytes; | ||
|
||
for (var h = 0; h < height; h++) | ||
{ | ||
var srcOffset = h * stride; | ||
var dstOffset = h * width * dstChannel; | ||
|
||
for (var w = 0; w < width; w++) | ||
{ | ||
pArray[dstOffset + w * dstChannel + 0] = src[srcOffset + w * srcChannel + 0]; | ||
pArray[dstOffset + w * dstChannel + 1] = src[srcOffset + w * srcChannel + 1]; | ||
pArray[dstOffset + w * dstChannel + 2] = src[srcOffset + w * srcChannel + 2]; | ||
} | ||
} | ||
} | ||
else | ||
{ | ||
var src = (byte*)bitmap.GetPixels(); | ||
var stride = bitmap.RowBytes; | ||
|
||
for (var h = 0; h < height; h++) | ||
{ | ||
var srcOffset = h * stride; | ||
var dstOffset = h * width * dstChannel; | ||
|
||
for (var w = 0; w < width; w++) | ||
{ | ||
// BGR order to RGB order | ||
pArray[dstOffset + w * dstChannel + 0] = src[srcOffset + w * srcChannel + 2]; | ||
pArray[dstOffset + w * dstChannel + 1] = src[srcOffset + w * srcChannel + 1]; | ||
pArray[dstOffset + w * dstChannel + 2] = src[srcOffset + w * srcChannel + 0]; | ||
} | ||
} | ||
} | ||
} | ||
break; | ||
} | ||
|
||
var ptr = (IntPtr)pArray; | ||
switch (mode) | ||
{ | ||
case Mode.Rgb: | ||
return new Image(new Matrix<RgbPixel>(ptr, height, width, width * 3), Mode.Rgb); | ||
case Mode.Greyscale: | ||
return new Image(new Matrix<byte>(ptr, height, width, width), Mode.Greyscale); | ||
} | ||
} | ||
} | ||
|
||
return null; | ||
} | ||
|
||
#endregion | ||
|
||
} | ||
|
||
} |
Oops, something went wrong.