R
Ringo
the LeafProject http://www.leafproject.org has a DLL for Face
recognition. it is written in C++ but they interface to it from Lisp.
I want to interface to it from C#.
Their Lisp definitions looks like this.
;;;; REGISTER THE VISION FACE RECOGNITION
DLL ;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(fli:register-module "FaceRecog.dll")
; an interface function:
; -1 = ERROR: camera failed to start
; 0 = camera on successfully
(fli:define-foreign-function
(Start-Camera "StartCamera")
()
:result-type :int)
; an interface function:
; -1 = ERROR: camera failed to stop
; 0 = camera off successfully
(fli:define-foreign-function
(Stop-Camera "StopCamera")
()
:result-type :int)
; an interface function:
; returns the number of face images in the database
(fli:define-foreign-function
(Get-Number-Images "GetNum")
()
:result-type :int)
; an interface function:
; -2 = ERROR: write error
; -1 = ERROR: could not create values.txt file
; 0 = eigens created and stored successfully
(fli:define-foreign-function
(Calc-Eigens "DoEigen")
()
:result-type :int)
; an interface function
; -1 = ERROR: could not rename or add face
; 0 = face added sucessfully
(fli:define-foreign-function
(Add-Face "AddFace")
()
:result-type :int)
; an interface function
; -4 = ERROR: could not save face image
; -3 = ERROR: could not read coefficient file
; -2 = ERROR: could not capture
; -1 = ERROR: could not load classifier cascade
; 0 or more = number of faces detected... if only one face detected,
then
; return string = image number as a string, eg. "6" means image 6
(fli:define-foreign-function
(Look-For-Faces "LookForFaces")
((return-string reference-return ef-mb-string :limit 256))))
:result-type :int
:lambda-list (&aux return-string)
:language :c
:calling-convention :cdecl
:result-type :int)
So from that I looked up Pinvoke and wrote this code.
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;
namespace FaceRecognition
{
public partial class Form1 : Form
{
[DllImport("facerecog.dll")]
public static extern int StartCamera();
[DllImport("FaceRecog.dll")]
public static extern int StopCamera();
[DllImport("FaceRecog.dll")]
public static extern int GetNum();
[DllImport("FaceRecog.dll")]
public static extern int DoEigen();
[DllImport("FaceRecog.dll")]
public static extern int AddFace();
[DllImport("FaceRecog.dll")]
public static extern int LookForFaces();
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
int result = StartCamera();
Console.WriteLine(" startcamera = " + result);
}
}
}
When I click the button to do StartCamera I get the following message
like it cannot find the DLL (which is in windows/system32). I have
tried moving the DLL to the C# apps directory too but that did not
help either. What am I doing wrong?
Thanks
Ringo
System.DllNotFoundException was unhandled
Message="Unable to load DLL 'facerecog.dll': The specified module
could not be found. (Exception from HRESULT: 0x8007007E)"
Source="FaceRecognition"
TypeName=""
StackTrace:
at FaceRecognition.Form1.StartCamera()
at FaceRecognition.Form1.button1_Click(Object sender, EventArgs
e) in E:\FaceRecognition\FaceRecognition\Form1.cs:line 78
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
at System.Windows.Forms.Control.WmMouseUp(Message& m,
MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ButtonBase.WndProc(Message& m)
at System.Windows.Forms.Button.WndProc(Message& m)
at
System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at
System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr
hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at
System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at
System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32
dwComponentID, Int32 reason, Int32 pvLoopData)
at
System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32
reason, ApplicationContext context)
at
System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32
reason, ApplicationContext context)
at System.Windows.Forms.Application.Run(Form mainForm)
at FaceRecognition.Program.Main() in e:\FaceRecognition
\FaceRecognition\Program.cs:line 17
at System.AppDomain.nExecuteAssembly(Assembly assembly,
String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile,
Evidence assemblySecurity, String[] args)
at
Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object
state)
at System.Threading.ExecutionContext.Run(ExecutionContext
executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
recognition. it is written in C++ but they interface to it from Lisp.
I want to interface to it from C#.
Their Lisp definitions looks like this.
;;;; REGISTER THE VISION FACE RECOGNITION
DLL ;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(fli:register-module "FaceRecog.dll")
; an interface function:
; -1 = ERROR: camera failed to start
; 0 = camera on successfully
(fli:define-foreign-function
(Start-Camera "StartCamera")
()
:result-type :int)
; an interface function:
; -1 = ERROR: camera failed to stop
; 0 = camera off successfully
(fli:define-foreign-function
(Stop-Camera "StopCamera")
()
:result-type :int)
; an interface function:
; returns the number of face images in the database
(fli:define-foreign-function
(Get-Number-Images "GetNum")
()
:result-type :int)
; an interface function:
; -2 = ERROR: write error
; -1 = ERROR: could not create values.txt file
; 0 = eigens created and stored successfully
(fli:define-foreign-function
(Calc-Eigens "DoEigen")
()
:result-type :int)
; an interface function
; -1 = ERROR: could not rename or add face
; 0 = face added sucessfully
(fli:define-foreign-function
(Add-Face "AddFace")
()
:result-type :int)
; an interface function
; -4 = ERROR: could not save face image
; -3 = ERROR: could not read coefficient file
; -2 = ERROR: could not capture
; -1 = ERROR: could not load classifier cascade
; 0 or more = number of faces detected... if only one face detected,
then
; return string = image number as a string, eg. "6" means image 6
(fli:define-foreign-function
(Look-For-Faces "LookForFaces")
((return-string reference-return ef-mb-string :limit 256))))
:result-type :int
:lambda-list (&aux return-string)
:language :c
:calling-convention :cdecl
:result-type :int)
So from that I looked up Pinvoke and wrote this code.
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;
namespace FaceRecognition
{
public partial class Form1 : Form
{
[DllImport("facerecog.dll")]
public static extern int StartCamera();
[DllImport("FaceRecog.dll")]
public static extern int StopCamera();
[DllImport("FaceRecog.dll")]
public static extern int GetNum();
[DllImport("FaceRecog.dll")]
public static extern int DoEigen();
[DllImport("FaceRecog.dll")]
public static extern int AddFace();
[DllImport("FaceRecog.dll")]
public static extern int LookForFaces();
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
int result = StartCamera();
Console.WriteLine(" startcamera = " + result);
}
}
}
When I click the button to do StartCamera I get the following message
like it cannot find the DLL (which is in windows/system32). I have
tried moving the DLL to the C# apps directory too but that did not
help either. What am I doing wrong?
Thanks
Ringo
System.DllNotFoundException was unhandled
Message="Unable to load DLL 'facerecog.dll': The specified module
could not be found. (Exception from HRESULT: 0x8007007E)"
Source="FaceRecognition"
TypeName=""
StackTrace:
at FaceRecognition.Form1.StartCamera()
at FaceRecognition.Form1.button1_Click(Object sender, EventArgs
e) in E:\FaceRecognition\FaceRecognition\Form1.cs:line 78
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
at System.Windows.Forms.Control.WmMouseUp(Message& m,
MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ButtonBase.WndProc(Message& m)
at System.Windows.Forms.Button.WndProc(Message& m)
at
System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at
System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr
hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at
System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at
System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32
dwComponentID, Int32 reason, Int32 pvLoopData)
at
System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32
reason, ApplicationContext context)
at
System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32
reason, ApplicationContext context)
at System.Windows.Forms.Application.Run(Form mainForm)
at FaceRecognition.Program.Main() in e:\FaceRecognition
\FaceRecognition\Program.cs:line 17
at System.AppDomain.nExecuteAssembly(Assembly assembly,
String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile,
Evidence assemblySecurity, String[] args)
at
Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object
state)
at System.Threading.ExecutionContext.Run(ExecutionContext
executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()