Cleaning up reading from a webcam.

This commit is contained in:
Tadas Baltrusaitis 2018-01-27 07:46:21 +00:00
parent 20276a00b2
commit da458f1ab6
4 changed files with 50 additions and 31 deletions

View file

@ -116,7 +116,7 @@ namespace OpenFaceOffline
public bool RecordPose { get; set; } = true; // Head pose (position and orientation) public bool RecordPose { get; set; } = true; // Head pose (position and orientation)
public bool RecordAUs { get; set; } = true; // Facial action units public bool RecordAUs { get; set; } = true; // Facial action units
public bool RecordGaze { get; set; } = true; // Eye gaze public bool RecordGaze { get; set; } = true; // Eye gaze
public bool RecordTracked { get; set; } = false; // Recording tracked videos or images public bool RecordTracked { get; set; } = true; // Recording tracked videos or images
// Visualisation options // Visualisation options
public bool ShowTrackedVideo { get; set; } = true; // Showing the actual tracking public bool ShowTrackedVideo { get; set; } = true; // Showing the actual tracking
@ -248,9 +248,15 @@ namespace OpenFaceOffline
processing_fps.AddFrame(); processing_fps.AddFrame();
} }
// Finalize the recording and flush to disk
recorder.Close();
// Post-process the AU recordings // Post-process the AU recordings
face_analyser.PostProcessOutputFile(recorder.GetCSVFile()); face_analyser.PostProcessOutputFile(recorder.GetCSVFile());
// Close the open video/webcam
reader.Close();
EndMode(); EndMode();
} }
@ -342,6 +348,7 @@ namespace OpenFaceOffline
lastFrameTime = CurrentTime; lastFrameTime = CurrentTime;
processing_fps.AddFrame(); processing_fps.AddFrame();
// TODO how to report errors from the reader here? exceptions? logging? Problem for future versions? // TODO how to report errors from the reader here? exceptions? logging? Problem for future versions?
} }
@ -676,13 +683,12 @@ namespace OpenFaceOffline
using (var fbd = new FolderBrowserDialog()) using (var fbd = new FolderBrowserDialog())
{ {
DialogResult result = fbd.ShowDialog(); DialogResult result = fbd.ShowDialog();
if (result == System.Windows.Forms.DialogResult.OK && !string.IsNullOrWhiteSpace(fbd.SelectedPath)) if (result == System.Windows.Forms.DialogResult.OK)
{ {
to_return = fbd.SelectedPath; to_return = fbd.SelectedPath;
} }
else else if(!string.IsNullOrWhiteSpace(fbd.SelectedPath))
{ {
// TODO warning message here
string messageBoxText = "Could not open the directory."; string messageBoxText = "Could not open the directory.";
string caption = "Invalid directory"; string caption = "Invalid directory";
MessageBoxButton button = MessageBoxButton.OK; MessageBoxButton button = MessageBoxButton.OK;
@ -760,9 +766,7 @@ namespace OpenFaceOffline
{ {
StopTracking(); StopTracking();
Dispatcher.Invoke(DispatcherPriority.Render, new TimeSpan(0, 0, 0, 2, 0), (Action)(() => // If camera selection has already been done, no need to re-populate the list as it is quite slow
{
if (cam_sec == null) if (cam_sec == null)
{ {
cam_sec = new CameraSelection(); cam_sec = new CameraSelection();
@ -786,11 +790,13 @@ namespace OpenFaceOffline
int width = cam_sec.selected_camera.Item2; int width = cam_sec.selected_camera.Item2;
int height = cam_sec.selected_camera.Item3; int height = cam_sec.selected_camera.Item3;
// processing_thread = new Thread(() => ProcessingLoop(cam_id, width, height)); SequenceReader reader = new SequenceReader(cam_id, width, height);
// processing_thread.Start();
processing_thread = new Thread(() => ProcessSequence(reader));
processing_thread.Name = "Webcam processing";
processing_thread.Start();
} }
}));
} }

View file

@ -98,11 +98,11 @@ namespace UtilitiesOF {
} }
// Can provide a webcam id // Can provide a webcam id
SequenceReader(int webcam_id) SequenceReader(int webcam_id, int width, int height)
{ {
m_sequence_capture = new Utilities::SequenceCapture(); m_sequence_capture = new Utilities::SequenceCapture();
bool success = m_sequence_capture->OpenWebcam(webcam_id); bool success = m_sequence_capture->OpenWebcam(webcam_id, width, height);
if (!success) if (!success)
{ {
@ -184,6 +184,10 @@ namespace UtilitiesOF {
return m_gray_frame; return m_gray_frame;
} }
void Close() {
m_sequence_capture->Close();
}
// Finalizer. Definitely called before Garbage Collection, // Finalizer. Definitely called before Garbage Collection,
// but not automatically called on explicit Dispose(). // but not automatically called on explicit Dispose().
// May be called multiple times. // May be called multiple times.
@ -343,6 +347,7 @@ namespace UtilitiesOF {
std::vector<std::pair<int, int>> common_resolutions; std::vector<std::pair<int, int>> common_resolutions;
common_resolutions.push_back(std::pair<int, int>(320, 240)); common_resolutions.push_back(std::pair<int, int>(320, 240));
common_resolutions.push_back(std::pair<int, int>(640, 480)); common_resolutions.push_back(std::pair<int, int>(640, 480));
common_resolutions.push_back(std::pair<int, int>(800, 600));
common_resolutions.push_back(std::pair<int, int>(960, 720)); common_resolutions.push_back(std::pair<int, int>(960, 720));
common_resolutions.push_back(std::pair<int, int>(1280, 720)); common_resolutions.push_back(std::pair<int, int>(1280, 720));
common_resolutions.push_back(std::pair<int, int>(1280, 960)); common_resolutions.push_back(std::pair<int, int>(1280, 960));

View file

@ -87,6 +87,8 @@ namespace Utilities
bool IsOpened(); bool IsOpened();
void Close();
int frame_width; int frame_width;
int frame_height; int frame_height;

View file

@ -256,6 +256,12 @@ bool SequenceCapture::OpenWebcam(int device, int image_width, int image_height,
} }
void SequenceCapture::Close()
{
if (capture.isOpened())
capture.release();
}
// Destructor that releases the capture // Destructor that releases the capture
SequenceCapture::~SequenceCapture() SequenceCapture::~SequenceCapture()
{ {