Skip to content

Instantly share code, notes, and snippets.

View mdfarragher's full-sized avatar

Mark Farragher mdfarragher

View GitHub Profile
// build features and labels
var features = NetUtil.Var(new int[] { 7 }, DataType.Float);
var labels = NetUtil.Var(new int[] { 1 }, DataType.Float);
// the rest of the code goes here...
// file paths
private const string inputFilePath = "./pillow.jpg";
// export the modified image
Dlib.SaveJpeg(img, "output.jpg");
// create a color palette for plotting
var palette = new RgbPixel[]
{
new RgbPixel(0xe6, 0x19, 0x4b),
new RgbPixel(0xf5, 0x82, 0x31),
new RgbPixel(0xff, 0xe1, 0x19),
new RgbPixel(0xbc, 0xf6, 0x0c),
new RgbPixel(0x3c, 0xb4, 0x4b),
new RgbPixel(0x46, 0xf0, 0xf0),
new RgbPixel(0x43, 0x63, 0xd8),
// use the chinese whispers algorithm to find all face clusters
Dlib.ChineseWhispers(edges, 100, out var clusters, out var labels);
Console.WriteLine($" Found {clusters} unique person(s) in the image");
// the rest of the code goes here...
// compare each face with all other faces
var edges = new List<SamplePair>();
for (uint i = 0; i < descriptors.Count; ++i)
for (var j = i; j < descriptors.Count; ++j)
// record every pair of two similar faces
// faces are similar if they are less than 0.6 apart in the 128D embedding space
if (Dlib.Length(descriptors[i] - descriptors[j]) < 0.6)
edges.Add(new SamplePair(i, j));
// compare each face with all other faces
var edges = new List<SamplePair>();
for (uint i = 0; i < descriptors.Count; ++i)
for (var j = i; j < descriptors.Count; ++j)
// record every pair of two similar faces
// faces are similar if they are less than 0.6 apart in the 128D embedding space
if (Dlib.Length(descriptors[i] - descriptors[j]) < 0.6)
edges.Add(new SamplePair(i, j));
// put each fae in a 128D embedding space
// similar faces will be placed close together
Console.WriteLine("Recognizing faces...");
var descriptors = dnn.Operator(chips);
// the rest of the code goes here...
// detect all faces
var chips = new List<Matrix<RgbPixel>>();
var faces = new List<Rectangle>();
Console.WriteLine("Detecting faces...");
foreach (var face in detector.Operator(img))
{
// detect landmarks
var shape = predictor.Detect(img, face);
// extract normalized and rotated 150x150 face chip
using System.Collections.Generic;
using System;
using DlibDotNet;
namespace FaceClustering
{
/// <summary>
/// The program class.
/// </summary>
class Program