Kinect Facial Recognition and Training Images -
i using this facial recognition program kinect
the problem need able register , keep images training database. whenever run it, program works , able detect , recognize faces images not kept. code needs changed?
i need on appreciated.
/// <summary> /// initializes new instance of mainwindow class /// </summary> public mainwindow() { kinectsensor kinectsensor = null; // loop through kinects attached pc, , start first connected without error. foreach (kinectsensor kinect in kinectsensor.kinectsensors) { if (kinect.status == kinectstatus.connected) { kinectsensor = kinect; break; } } if (kinectsensor == null) { messagebox.show("no kinect found..."); application.current.shutdown(); return; } kinectsensor.skeletonstream.enable(); kinectsensor.colorstream.enable(colorimageformat.rgbresolution640x480fps30); kinectsensor.depthstream.enable(depthimageformat.resolution640x480fps30); kinectsensor.start(); allframesreadyframesource framesource = new allframesreadyframesource(kinectsensor); this.engine = new kinectfacialrecognitionengine(kinectsensor, framesource); this.engine.recognitioncomplete += this.engine_recognitioncomplete; this.initializecomponent(); this.trainedfaces.itemssource = this.targetfaces; } [dllimport("gdi32")] private static extern int deleteobject(intptr o); /// <summary> /// loads bitmap bitmap source /// </summary> private static bitmapsource loadbitmap(bitmap source) { intptr ip = source.gethbitmap(); bitmapsource bs = null; try { bs = system.windows.interop.imaging.createbitmapsourcefromhbitmap(ip, intptr.zero, int32rect.empty, system.windows.media.imaging.bitmapsizeoptions.fromemptyoptions()); } { deleteobject(ip); } return bs; } /// <summary> /// handles recognition complete events /// </summary> private void engine_recognitioncomplete(object sender, recognitionresult e) { recognitionresult.face face = null; if (e.faces != null) face = e.faces.firstordefault(); if (face != null) { if (!string.isnullorempty(face.key)) { // write key on image... using (var g = graphics.fromimage(e.processedbitmap)) { var rect = face.trackingresults.facerect; g.drawstring(face.key, new font("arial", 20), brushes.red, new system.drawing.point(rect.left, rect.top - 25)); } } if (this.taketrainingimage) { this.targetfaces.add(new bitmapsourcetargetface { image = (bitmap)face.grayface.clone(), key = this.namefield.text }); this.taketrainingimage = false; this.namefield.text = this.namefield.text.replace(this.targetfaces.count.tostring(), (this.targetfaces.count + 1).tostring()); if (this.targetfaces.count > 1) this.engine.settargetfaces(this.targetfaces); } } this.video.source = loadbitmap(e.processedbitmap); } /// <summary> /// starts training image countdown /// </summary> private void train(object sender, routedeventargs e) { this.trainbutton.isenabled = false; this.namefield.isenabled = false; var timer = new dispatchertimer(); timer.interval = timespan.fromseconds(2); timer.tick += (s2, e2) => { timer.stop(); this.namefield.isenabled = true; this.trainbutton.isenabled = true; taketrainingimage = true; }; timer.start(); } /// <summary> /// target face bitmapsource accessor face /// </summary> private class bitmapsourcetargetface : targetface { private bitmapsource bitmapsource; /// <summary> /// gets bitmapsource version of face /// </summary> public bitmapsource bitmapsource { { if (this.bitmapsource == null) this.bitmapsource = mainwindow.loadbitmap(this.image); return this.bitmapsource; } } } }
}
if trying save images folder, this:
... if (this.taketrainingimage) { this.targetfaces.add(new bitmapsourcetargetface { image = (bitmap)face.grayface.clone(), key = this.namefield.text }); //save image jpegbitmapencoder encoder = new jpegbitmapencoder(); bitmapframe outputframe = bitmapframe.create(loadbitmap(e.processedbitmap)); encoder.frames.add(face.grayface); encoder.qualitylevel = 96dpi; using (filestream file = file.openwrite("c://users//your name//documents//face trainer//images//face " + targetfaces.count + ".jpg")) { encoder.save(file); } this.taketrainingimage = false; this.namefield.text = this.namefield.text.replace(this.targetfaces.count.tostring(), (this.targetfaces.count + 1).tostring()); if (this.targetfaces.count > 1) this.engine.settargetfaces(this.targetfaces); } ....
then load files...
string[] files = system.io.directory.getfiles("c://users//your name//documents//face trainer//images//"); bitmap[] images = new bitmap[files.length]; (int = 0; < files.length; i++) { images[i] = (bitmap) image.fromfile(file, true); }
if trying add images database, follow this tutorial.
i recommend saving images file since beginning, , using databases requires more work. however, when more experienced databases effective @ sort of thing. luck!:)
Comments
Post a Comment