1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
| using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Runtime.InteropServices; using Luxand;
namespace FaceDemo { public partial class Form1 : Form { private String cameraName;
// WinAPI procedure to release HBITMAP handles returned by FSDKCam.GrabFrame [DllImport("gdi32.dll")] private static extern bool DeleteObject(IntPtr hObject);
public Form1() { InitializeComponent(); }
private void Form1_Load(object sender, EventArgs e) { if (FSDK.FSDKE_OK != FSDK.ActivateLibrary("***************")) { MessageBox.Show("Please run the License Key Wizard (Start - Luxand - FaceSDK - License Key Wizard)", "Error activating FaceSDK", MessageBoxButtons.OK, MessageBoxIcon.Error); Application.Exit(); }
FSDK.InitializeLibrary(); FSDKCam.InitializeCapturing(); }
private void button3_Click(object sender, EventArgs e) { OpenFileDialog dlg = new OpenFileDialog(); dlg.Filter = "JPEG (*.jpg)|*.jpg|Windows bitmap (*.bmp)|*.bmp|All files|*.*"; dlg.Multiselect = true;
if (dlg.ShowDialog() == DialogResult.OK) { try { //假设面部是垂直的(HandleArbitraryRotations = false)以加快面部检测速度 FSDK.SetFaceDetectionParameters(true, true, 384); FSDK.SetFaceDetectionThreshold(3);
foreach (string fn in dlg.FileNames) { var image = new FSDK.CImage(fn); var facePositions = image.DetectMultipleFaces();
foreach (var facePosition in facePositions) { var faceImage = image.CopyRect( (int)(facePosition.xc - Math.Round(facePosition.w * 0.5)), (int)(facePosition.yc - Math.Round(facePosition.w * 0.5)), (int)(facePosition.xc + Math.Round(facePosition.w * 0.5)), (int)(facePosition.yc + Math.Round(facePosition.w * 0.5)));
faceImage.Save($"{Guid.NewGuid().ToString()}.png"); }
int tracker = 0; FSDK.CreateTracker(ref tracker); int err = 0; // 设置实时人脸检测参数 FSDK.SetTrackerMultipleParameters(tracker, "RecognizeFaces=false;DetectExpression=true; DetectAge=true; DetectGender=true; HandleArbitraryRotations=false; DetermineFaceRotationAngle=false; InternalResizeWidth=100; FaceDetectionThreshold=5;", ref err);
long[] IDs; long faceCount = 0; FSDK.FeedFrame(tracker, 0, image.ImageHandle, ref faceCount, out IDs, sizeof(long) * 256); // maximum 256 faces detected Array.Resize(ref IDs, (int)faceCount);
Image frameImage = image.ToCLRImage(); Graphics gr = Graphics.FromImage(frameImage);
for (int i = 0; i < IDs.Length; ++i) { FSDK.TFacePosition facePosition = new FSDK.TFacePosition(); FSDK.GetTrackerFacePosition(tracker, 0, IDs[i], ref facePosition);
int left = facePosition.xc - (int)(facePosition.w * 0.6); int top = facePosition.yc - (int)(facePosition.w * 0.5); int w = (int)(facePosition.w * 1.2); gr.DrawRectangle(Pens.LightGreen, left, top, w, w);
String AttributeValues; String AttributeValuesAge; String AttributeValuesExpression; if (0 == FSDK.GetTrackerFacialAttribute(tracker, 0, IDs[i], "Gender", out AttributeValues, 1024) && 0 == FSDK.GetTrackerFacialAttribute(tracker, 0, IDs[i], "Age", out AttributeValuesAge, 1024) && 0 == FSDK.GetTrackerFacialAttribute(tracker, 0, IDs[i], "Expression", out AttributeValuesExpression, 1024)) { float ConfidenceAge = 0.0f; float ConfidenceMale = 0.0f; float ConfidenceSmile = 0.0f; float ConfidenceFemale = 0.0f; float ConfidenceEyesOpen = 0.0f;
FSDK.GetValueConfidence(AttributeValuesExpression, "Smile", ref ConfidenceSmile); FSDK.GetValueConfidence(AttributeValuesExpression, "EyesOpen", ref ConfidenceEyesOpen); FSDK.GetValueConfidence(AttributeValuesAge, "Age", ref ConfidenceAge); FSDK.GetValueConfidence(AttributeValues, "Male", ref ConfidenceMale); FSDK.GetValueConfidence(AttributeValues, "Female", ref ConfidenceFemale);
StringBuilder sb = new StringBuilder();
sb.Append($"Smile:{((int)(ConfidenceSmile * 100)).ToString()}%,"); sb.Append($"Eyes open:{((int)(ConfidenceEyesOpen * 100)).ToString()}%,"); sb.Append($"Age:{(int)ConfidenceAge},"); sb.Append($"{(ConfidenceMale > ConfidenceFemale ? "Male" : "Female")}:"); sb.Append($"{(ConfidenceMale > ConfidenceFemale ? (int)(ConfidenceMale * 100) : (int)(ConfidenceFemale * 100)).ToString()}%");
StringFormat format = new StringFormat(); format.Alignment = StringAlignment.Center;
gr.DrawString(sb.ToString(), new Font("Arial", 16), new SolidBrush(Color.DarkRed), facePosition.xc, top + w + 5, format);
this.textBox1.Text = this.textBox1.Text + "\n" + sb.ToString(); } } pictureBox1.Image = frameImage;
GC.Collect(); // collect the garbage
// make UI controls accessible Application.DoEvents(); FSDK.FreeTracker(tracker);
FSDKCam.FinalizeCapturing(); } } catch (Exception ex) { MessageBox.Show(ex.Message.ToString(), "Exception"); } } } } }
|