Get started with FacePing's face recognition API using C#
First, create a new C# project and install required packages:
dotnet new console -n FacePingDemo
cd FacePingDemo
dotnet add package Microsoft.Extensions.Configuration.Json
Add your API key to appsettings.json
:
{
"FacePing": {
"ApiKey": "your_api_key_here"
}
}
Create a new file FacePingClient.cs
with the basic client setup:
using System.Net.Http.Headers;
using System.Text.Json;
public class FacePingClient
{
private readonly HttpClient _client;
private readonly string _apiKey;
public FacePingClient(string apiKey)
{
_apiKey = apiKey;
_client = new HttpClient
{
BaseAddress = new Uri("https://api.faceping.ai")
};
_client.DefaultRequestHeaders.Authorization =
new AuthenticationHeaderValue("Bearer", _apiKey);
}
public async Task CreateGroupAsync(string groupName)
{
try
{
var response = await _client.PostAsync($"/groups/{groupName}", null);
if (response.IsSuccessStatusCode)
{
Console.WriteLine($"Group '{groupName}' created successfully");
return true;
}
Console.WriteLine($"Error creating group: {response.StatusCode}");
return false;
}
catch (Exception ex)
{
Console.WriteLine($"Error: {ex.Message}");
return false;
}
}
}
Create Program.cs
to use the client:
using Microsoft.Extensions.Configuration;
var config = new ConfigurationBuilder()
.AddJsonFile("appsettings.json")
.Build();
var apiKey = config["FacePing:ApiKey"];
var client = new FacePingClient(apiKey);
await client.CreateGroupAsync("my-group");
Add the upload method to FacePingClient.cs
:
public async Task UploadFaceAsync(string groupName, string imagePath)
{
if (!File.Exists(imagePath))
{
Console.WriteLine("Image file not found");
return false;
}
try
{
using var form = new MultipartFormDataContent();
using var fileContent = new ByteArrayContent(await File.ReadAllBytesAsync(imagePath));
fileContent.Headers.ContentType =
MediaTypeHeaderValue.Parse("image/jpeg");
form.Add(fileContent, "image", Path.GetFileName(imagePath));
var response = await _client.PostAsync(
$"/groups/{groupName}/faces",
form
);
if (response.IsSuccessStatusCode)
{
Console.WriteLine("Face uploaded successfully");
return true;
}
Console.WriteLine($"Error uploading face: {response.StatusCode}");
return false;
}
catch (Exception ex)
{
Console.WriteLine($"Error: {ex.Message}");
return false;
}
}
Use it in Program.cs
:
await client.UploadFaceAsync("my-group", "path/to/face.jpg");
Add the search method to FacePingClient.cs
:
public class MatchResult
{
public double Score { get; set; }
}
public class SearchResponse
{
public List Matches { get; set; } = new();
}
public async Task> SearchFacesAsync(string groupName, string imagePath)
{
if (!File.Exists(imagePath))
{
Console.WriteLine("Image file not found");
return new List();
}
try
{
using var form = new MultipartFormDataContent();
using var fileContent = new ByteArrayContent(await File.ReadAllBytesAsync(imagePath));
fileContent.Headers.ContentType =
MediaTypeHeaderValue.Parse("image/jpeg");
form.Add(fileContent, "image", Path.GetFileName(imagePath));
var response = await _client.PostAsync(
$"/groups/{groupName}/search",
form
);
if (response.IsSuccessStatusCode)
{
var content = await response.Content.ReadAsStringAsync();
var result = JsonSerializer.Deserialize(content);
Console.WriteLine($"Found {result?.Matches.Count} matches");
return result?.Matches ?? new List();
}
Console.WriteLine($"Error searching faces: {response.StatusCode}");
return new List();
}
catch (Exception ex)
{
Console.WriteLine($"Error: {ex.Message}");
return new List();
}
}
Use it in Program.cs
:
var matches = await client.SearchFacesAsync("my-group", "path/to/search-face.jpg");
foreach (var match in matches)
{
Console.WriteLine($"Match Score: {match.Score}");
}