How do I make Affectiva call my callback functions? - c#

I recently installed the Affectiva SDK (http://www.affectiva.com/) and followed the tutorial on analyzing input from the camera (http://developer.affectiva.com/v3/android/analyze-camera/). Unfortunately, the project does not seem to be working. It is my understanding that the interface/callback functions from FaceListener, ImageListener, ProcessStatusListener need to be called when a face is detected etc (is this correct).
I am not getting any errors, but these functions are never called either (I've put Console.WriteLine statements in there, as well as placed breakpoints in Visual Studio). From time to time, a series of "Image Captured" statements are printed to the console, but I cannot yet reproduce how or why that happens. Does anyone know what I did wrong? Thank you in advance for any help.
Below is my code so far:
App.xaml.cs
using Affdex;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;
namespace Affectiva
{
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : Application, FaceListener, ImageListener, ProcessStatusListener
{
public static CameraDetector detector;
int camId = 10;
int camFPS = 60;
public App(){
detector = new CameraDetector();
String classifierPath = "C:\\Program Files (x86)\\Affectiva\\Affdex SDK\\data";
detector.setClassifierPath(classifierPath);
detector.setCameraId(camId);
detector.setCameraFPS(camFPS);
detector.setFaceListener(this);
detector.setImageListener(this);
detector.setProcessStatusListener(this);
detector.setDetectSmile(true);
detector.setDetectJoy(true);
detector.setDetectAllExpressions(true);
detector.setDetectAllEmotions(true);
detector.setDetectAllEmojis(true);
detector.setDetectAllAppearances(true);
}
public void onFaceFound(float f, int i)
{
Console.WriteLine("Face Found!");
}
public void onFaceLost(float f, int i)
{
Console.WriteLine("Face Lost!");
}
public void onImageResults(Dictionary<int, Face> faces, Frame f){
Console.WriteLine("OnImageResults - " + faces.Count);
if(faces.Count > 0)
Console.WriteLine(faces.First().Value.Appearance.Age);
}
public void onImageCapture(Frame f){
Console.WriteLine("Image Captured " + f.getHeight());
}
public void onProcessingFinished()
{
Console.WriteLine("Processing Finished");
}
public void onProcessingException(AffdexException e)
{
Console.WriteLine(e.Message);
}
}
}
MainWindow.xaml.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;
using WebEye.Controls.Wpf;
namespace Affectiva
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void OnStartButtonClick(object sender, RoutedEventArgs e)
{
var cameraId = webCameraControl.GetVideoCaptureDevices().First();
webCameraControl.StartCapture(cameraId);
App.detector.start();
}
}
}

I think you are looking at the wrong documentation.You can find the right documentation for Windows SDK here. A quick look at the code snippet, you are setting int camId = 10; which means that the detector is looking for Camera with id 10 on the system. By default the inbuilt camera has an id = 0. We set the default CameraId to 0 and the rate at which we process the frames to 30.Here is the default constructor definition for CameraDetector.
You can analyze the camera feed using this example. We also have some ready to go application available on github mainly Affdex-Me and csharp-sample-apps.

Related

Program.cs file wont accept name of form that I've created

I've been creating an application for university coursework, using Visual Studio Forms, and I was trying to launch the form I've made to check it's connecting to the database behind it, but it won't accept the name of the form as legitimate. I have no idea why, as it is appearing in the Solution Explorer.
This is the code for the program.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace FoxhillCustomer
{
internal static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new frmViewEditStaff());
}
}
}
This is the code for the frmViewEditStaff.cs:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace FoxhillViewEditStaff
{
public partial class frmViewEditStaff : Form
{
SqlDataAdapter daViewEditStaff;
DataSet dsFoxhillDentistryFinal = new DataSet();
SqlCommandBuilder cmdBViewEditStaff;
DataRow drViewEditStaff;
String connStr, sqlViewEditStaff;
int selectedTab = 0;
bool staffSelected = false;
int staffNoSelected = 0;
public frmViewEditStaff()
{
InitializeComponent();
}
private void frmViewEditStaff_Load(object sender, EventArgs e)
{
connStr = #"Data Source = .\sqlexpress; Initial Catalog = InTheDogHouse; Integrated Security = true";
sqlViewEditStaff = #"select * from Staff";
daViewEditStaff = new SqlDataAdapter(sqlViewEditStaff, connStr);
cmdBViewEditStaff = new SqlCommandBuilder(daViewEditStaff);
daViewEditStaff.FillSchema(dsFoxhillDentistryFinal, SchemaType.Source, "ViewEditStaff");
daViewEditStaff.Fill(dsFoxhillDentistryFinal, "ViewEditStaff");
dgvStaff.DataSource = dsFoxhillDentistryFinal.Tables["Staff"];
dgvStaff.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells);
tabStaff.SelectedIndex = 1;
tabStaff.SelectedIndex = 0;
}
}
}
Your form is in a namespace of FoxhillViewEditStaff. Your Program class is in a namespace of FoxhillCustomer, and you don't have a using directive for the FoxhillViewEditStaff namespace.
I'd strongly advise you to:
Use a single namespace for all the classes in your project
Rename all your forms to follow normal .NET naming conventions, to make the code clearer
You could just add a using directive, but it would be more sensible to just put the classes into the same namespace, IMO.

Get window that is NOT MainWindow WPF

I want to call the method from the codebehind of a window that is NOT the MainWindow in my WPF application, casting the window type as I do it.
ClientCallBack.cs:
using ChattingInterfaces;
using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
namespace ChatClient
{
[CallbackBehavior(ConcurrencyMode = ConcurrencyMode.Multiple)]
public class ClientCallback : IClient
{
public void GetMessage(string message, string userName)
{
//get casted instance of chat client window (NOT MainWindow!)
}
}
}
ChatWPFClient.xaml.cs:
using ChattingInterfaces;
using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
namespace ChatClient
{
/// <summary>
/// Interaction logic for ChatWPFClient.xaml
/// </summary>
public partial class ChatWPFClient : Window
{
public static IChattingService Server;
private static DuplexChannelFactory<IChattingService> _channelFactory;
public ChatWPFClient()
{
InitializeComponent();
_channelFactory = new DuplexChannelFactory<IChattingService>(new ClientCallback(), "ChattingServiceEndpoint");
Server = _channelFactory.CreateChannel();
}
private void sendMessage(object sender, RoutedEventArgs e)
{
MessageBox.Show("Not available yet!");
}
public void TakeMessage(string message, string userName)
{
chatBox.Text += userName + ": " + message + "\n";
}
}
}
How can I call the TakeMessage of this method in the other class so I can use that codebehind window to populate the XAML file for ChatWPFClient.xaml? Thanks in advance!
First create an interface that you can pass to the ClientCallback
public interface IMessageHandler
{
void TakeMessage(string message, string userName);
}
Then in the ClientCallBack take the interface as a parameter in the constructor.
[CallbackBehavior(ConcurrencyMode = ConcurrencyMode.Multiple)]
public class ClientCallback : IClient
{
private IMessageHandler messageHandler;
public ClientCallBack(IMessageHandler messageHandler)
{
this.messageHandler = messageHandler;
}
public void GetMessage(string message, string userName)
{
messageHandler.TakeMessage(message, userName);
}
}
Use the interface for the ChatWpfClient and pass the instance in the constructor.
public partial class ChatWPFClient : Window, IMessageHandler
{
...
public ChatWPFClient()
{
InitializeComponent();
_channelFactory = new DuplexChannelFactory<IChattingService>(new ClientCallback(this), "ChattingServiceEndpoint");
Server = _channelFactory.CreateChannel();
}
...
// This is a part of the interface now and needs to be implemented here
public void TakeMessage(string message, string userName)
{
chatBox.Text += userName + ": " + message + "\n";
}
}
Also you could just implement the IClient on your ChatWPFClient class and decorate with the CallBackBehavior attribute and just pass itself as the callback. But don't think this is recommended, seems weird.
_channelFactory = new DuplexChannelFactory<IChattingService>(this, "ChattingServiceEndpoint");

C#: Dynamicly Add Items to Scrollview Control in WPF Application

Currently I am trying to code a WPF application that will store books and users for an imaginary library(I need to prove someone wrong). In my code, I have a class for Books and one for users. Inside of each will be a static list that keeps track of them all. What I would like to do is list out all the books and users so the viewer can see them. I thought I could do this with a scrollview and add labels to it that store the information(This would be in a separate window than the main screen, you would get there by pressing a button). However, I have been having some trouble with this.
LibraryCore:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace LibraryCore
{
class User
{
}
class Books
{
public static List<Books> bookslist = new List<Books>();
public static void NewBook(string _title, string _author, string _publisher, int _isbn, int _count = 1)
{
bookslist.Add(new Books(_title, _author, _publisher, _isbn, _count));
}
public static void AddBook(string _title, int _amount)
{
bookslist[bookslist.FindIndex(b => b.Title.ToUpper() == _title.ToUpper())].Count += _amount;
}
public List<Books> currentLoans = new List<Books>();
public string Publisher { get; private set; }
public string Author { get; private set; }
public string Title { get; private set; }
public int ISBN { get; private set; }
public int Count { get; private set; }
Books(string _title, string _author, string _publisher, int _isbn, int _count = 1)
{
Title = _title;
Author = _author;
Publisher = _publisher;
ISBN = _isbn;
Count = _count;
}
}
}
MainWindow:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using LibraryCore;
namespace LibraryLikeWpf
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void btnNewBook_Click(object sender, RoutedEventArgs e)
{
Books.NewBook("Odessy", "SOME OLD GUY", "Athens Inc.", 0);
//Books.NewBook("OLD YELLER", "SOME OLD GUY", "Athens Inc.", 2);
//Books.NewBook("This old man", "SOME OLD GUY", "Athens Inc.", 1);
}
private void btnInfo_Click(object sender, RoutedEventArgs e)
{
BookList bookList = new BookList();
bookList.Show();
}
}
}
BookListWindow:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using LibraryCore;
namespace LibraryLikeWpf
{
/// <summary>
/// Interaction logic for BookList.xaml
/// </summary>
public partial class BookList : Window
{
public BookList()
{
InitializeComponent();
for(int b = 0; b < Books.bookslist.Count; b++)
{
Label lbl = new Label();
lbl.VerticalAlignment = VerticalAlignment.Top;
lbl.Content = String.Format("Title: {0} Author: {1} Publisher: {2} ISBN: {3}",Books.bookslist[b].Title,Books.bookslist[b].Author, Books.bookslist[b].Publisher, Books.bookslist[b].ISBN);
lbl.Width = 100000;
scrollGrid.Children.Add(lbl);
}
}
}
}
This code works, but if I add several Books, the BookListWindow will just overlap the labels and I would like to know how to change their position when they are instantiated. Also, the labels get cut off even though their width shouldn't inhibit that. Why does that happen and how can I fix it? Also, is there a better way to list out ALL of the items in a list in a better looking way?
If your are using XAML, try to use ListView with Binding.
Create a ObservableCollection of Books and bind this to your ListView, this way you can control show your items are shown.

wpf listview argumentoutofrangeexception in mscorlib

I have a listview in a simple wpf application, and it throws an unhandled exception: http://www.picz.ge/img/s2/1407/6/4/4d2baa8909d8.png
An unhandled exception of type 'System.ArgumentOutOfRangeException' occurred in mscorlib.dll
Additional information: Index was out of range. Must be non-negative
and less than the size of the collection.
I can't understand where is the mistake.
when I set time=2000; code works fine;
here is the code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Diagnostics;
namespace Wpftemp
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
Thread t;
List<Something> listview_source;
int time;
public MainWindow()
{
InitializeComponent();
listview_source = new List<Something>();
listview.ItemsSource = listview_source;
t = new Thread(f);
time = 100;
t.Start();
}
private void f()
{
while (true)
{
listview_source.Clear();
refreshe_source();
App.Current.Dispatcher.Invoke( delegate()
{
listview.Items.Refresh();
});
Thread.Sleep(time);
}
}
private void refreshe_source()
{
for (int i = 0; i < 150; i++)
{
listview_source.Add(new Something(DateTime.Now.Second));
}
}
struct Something
{
public Something(double i)
{
this.i = i;
}
double i;
public double var
{
get { return i; }
set { i = value; }
}
}
}
}
I think the problem is in Using Dispatcher because the code inside dispatcher.Invoke() will be executed after the Main Thread is ready to execute it, so it is not guaranteed when exactly the code inside Dispatcher.Invoke() will be executed, so when you increased the value of time you guarantee that the code has been executed. Remove Dispatcher or Increase the value time.

Hide/show Button at runtime

I need your help with c# and some graphic issues: I'm developing a very simple application. There is a unique form called DeltaPregView, a controller for the form called DeltaPregController and a class that contains the Main of the project:
Main Class:
using System;
using System.Linq;
using System.Collections.Generic;
using System.Windows.Forms;
namespace deltaPreg
{
static class Program
{
[MTAThread]
static void Main()
{
//create the view
DeltaPregView view = new DeltaPregView();
//link the view to the APP
Application.Run(view);
//initialize the controller of the APP
DeltaPregController controller = new DeltaPregController(view);
}
}
}
View for the class:
using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace deltaPreg
{
public partial class DeltaPregView : Form
{
public DeltaPregView()
{
InitializeComponent();
}
public void init()
{
prova.Visible = false;
}
}
}
and the Controller for the view:
using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
namespace deltaPreg
{
class DeltaPregController
{
#region variables
private DeltaPregView view;
#endregion
public DeltaPregController(DeltaPregView view)
{
this.view = view;
//start the reading process
start();
}
private void start()
{
view.init();
}
}
}
I would like to hide the button called "prova", but nothing changes in my program. I'm a newbie in the winforms management, thank you in advance.
The problem is that you print the form before you call the init function in DeltaPregView.
One way to solve this is by replacing those lines:
//link the view to the APP
Application.Run(view);
//initialize the controller of the APP
DeltaPregController controller = new DeltaPregController(view);
To:
//initialize the controller of the APP
DeltaPregController controller = new DeltaPregController(view);
//link the view to the APP
Application.Run(view);

Categories

Resources