Sample code C# projects are provided along with the SDK. The examples below demonstrate the general usage of the Perfectly Clear SDK (class PerfectlyClear ).
Setting up License Protection
If you have been provided with a license protected SDK, then there are two ways that you can activate this license in your code. It is always safe to use the multi-threading approach, though it does take slightly more code. The scenarios below on the sections below show the single-threaded method, as it's simpler and our sample code is all single-threaded.
Single-Threaded
This method requires fewer calls to our SDK, but isn't intended for multithreaded applications where many concurrent calls will be made at once. The process is to setup the protection path when calling the class constructor.
Multi-Threaded
for (int i = 0; i < images.count; i++) {
PfC.Dispose()
}
Simple AI Sample - Using Scene Detection
This is the simplest way to use Perfectly Clear AI.
- Load image to correct:
Bitmap bm = new Bitmap(inname);
if (bm == null)
{
Console.WriteLine("Unable to open image.");
return;
}
- Initialize the AI Engines
string execPath = AppDomain.CurrentDomain.BaseDirectory;
Console.WriteLine("Cannot load AI SD engine. Make sure the pnn file and PFCAI libraries are in the executable directory or change binPath.");
return;
}
Console.WriteLine("Cannot load AI corrections engine. Make sure the pnn file and PFCAI libraries are in the executable directory or change binPath.");
return;
}
- Perform full correction using the auto function:
int aret = Pfc.AutoCorrect(ref bm);
if (bVerbose)
{
{
Console.WriteLine("Image processed successfully.");
}
else if (aret > 0)
{
Console.WriteLine("Noise removal return code: " + Pfc.LastStatus.NR_Status.ToString());
Console.WriteLine("Perfectly Clear core return code: " + Pfc.LastStatus.CORE_Status.ToString());
Console.WriteLine("Face beautification return code: " + Pfc.LastStatus.FB_Status.ToString());
Console.WriteLine("Red eye removal return code: " + Pfc.LastStatus.RE_Status.ToString());
}
else if (aret < 0)
{
Console.WriteLine("Perfectly Clear return code: " + aret.ToString());
}
}
- Save the corrected image
Detailed AI Sample - Using Scene Detection
To extend the example shown above, you can load custom AI Presets to apply, instead of using the default presets provided by this SDK:
if(presetfile != "") {
int retLoadPresets = Pfc.LoadScenePresets(presetfile);
if (0 != retLoadPresets) {
Console.WriteLine("Cannot load scene presets file '{0}' error '{1}'.", presetfile, retLoadPresets);
return;
}
}
And you can determine the scene ID and label:
int version = 0;
int detectedScene = Pfc.GetScene(&version);
Console.WriteLine("Model '{0}' assigned scene '{1}' to the image.\n", version, detectedScene);
AI Image Correction without Scene Detection
You can correct photos without scene detection, but still using the AI Image corrections that are new in this V10 SDK:
- load the correction parameters to apply from a .preset file
if (presetfile != "")
{
int rc = Pfc.ReadPresets(presetfile);
if (bVerbose)
{
Console.WriteLine("ReadPresets returns " + rc.ToString());
}
}
- then use these parameters when correcting the image
if (bVerbose)
{
{
Console.WriteLine("Image processed successfully.");
}
else if (aret > 0)
{
Console.WriteLine("Noise removal return code: " + Pfc.LastStatus.NR_Status.ToString());
Console.WriteLine("Perfectly Clear core return code: " + Pfc.LastStatus.CORE_Status.ToString());
Console.WriteLine("Face beautification return code: " + Pfc.LastStatus.FB_Status.ToString());
Console.WriteLine("Red eye removal return code: " + Pfc.LastStatus.RE_Status.ToString());
}
else if (aret < 0)
{
Console.WriteLine("Perfectly Clear return code: " + aret.ToString());
}
}
Using MemoryStreams
We have introduced the ability to read and write image data using .NET native MemoryStreams. If the application you are working on already has image file data loaded into MemoryStreams, this method allows you to use those directly, instead of needing to write image files to disk.
MemoryStream inMemoryCopy = new MemoryStream();
using (FileStream fs = File.OpenRead(inname))
{
fs.CopyTo(inMemoryCopy);
}
inMemoryCopy.Position = 0;
{
Console.WriteLine("Unable to open image.");
ShowHelp();
return;
}
Now, you can process the PFCImageFile like in the other samples, the most simple way would be:
Pfc.AutoCorrect(ref file, "my_preset.preset");
Now, the 'file' object has been corrected, so to get the corrected image data back into a MemoryStream:
{
outStream.Seek(0, SeekOrigin.Begin);
FileStream outFile = File.Create(outname);
outStream.CopyTo(outFile);
}
Loading From Bitmaps or with PfCImageFile
You can either load image data using our PFCImageFile class, or you can use standard Windows bitmaps. SimpleAiSample shows the bitmap method. If you are incorporating Perfectly Clear into an application that already is using Bitmaps, then this method will make it easy to incorporate Perfectly Clear into this.
Bitmap bm = new Bitmap(input_image_file_name);
In DotNetCore_PFCImageFile, we use the PfCImageFile class instead, shown in a simplified form here:
PfCImageFile class will handle color space conversion and metadata management automatically.
.NET Core Version Issues
In the .NET Core examples, there are PreBuildEvent steps to copy the required libraries into the proper build location. For example:
<PropertyGroup>
<PreBuildEvent>
xcopy /C /E /Y /R ..\..\..\..\..\bin\x64\PerfectlyClearPro.dll .\PerfectlyClearPro.dll*
xcopy /C /E /Y /R ..\..\..\..\..\bin\x64\PerfectlyClearAdapter.dll .
xcopy /C /E /Y /R ..\..\..\..\..\bin\x64\PFCImageFile.dll .
xcopy /C /E /Y /R ..\..\..\..\..\bin\x64\exiv2.dll .
</PreBuildEvent>
</PropertyGroup>
Depending on which version of Visual Studio and .NET Core version you are using, these steps might cause errors when you build. This is due to an extra folder level that Visual Studio builds in some cases. The solution is to remove one set of "..\" from these steps, like this:
<PropertyGroup>
<PreBuildEvent>
xcopy /C /E /Y /R ..\..\..\..\bin\x64\PerfectlyClearPro.dll .\PerfectlyClearPro.dll*
xcopy /C /E /Y /R ..\..\..\..\bin\x64\PerfectlyClearAdapter.dll .
xcopy /C /E /Y /R ..\..\..\..\bin\x64\PFCImageFile.dll .
xcopy /C /E /Y /R ..\..\..\..\bin\x64\exiv2.dll .
</PreBuildEvent>
</PropertyGroup>
- Copyright
- Copyright (C) 2021 EyeQ Imaging Inc