error when doing face tracking in matlab GUI

1 view (last 30 days)
Eliza Tham
Eliza Tham on 18 Mar 2015
Edited: Jia Zhen on 28 Apr 2015
Hi all, i am pretty new in matlab and seriously need help. I am doing a face recognition system but first i would like user to have their face detected and tracked before they could use the recognition system. currently, I am able to tracked the face but i am receiving this error message whenever I close the GUI.
Error using imaqdevice/getsnapshot (line 65) Image acquisition object OBJ is an invalid object.
Error in face_tracking>cameraon_Callback (line 136) Frame=getsnapshot(video);
I have highlighted in bold the line where the problem lies, i hope to see a reply from you guys who know whats wrong as my project is due soon.
THANKS
function varargout = face_tracking(varargin)
% FACE_TRACKING MATLAB code for face_tracking.fig
% FACE_TRACKING, by itself, creates a new FACE_TRACKING or raises the existing
% singleton*.
%
% H = FACE_TRACKING returns the handle to a new FACE_TRACKING or the handle to
% the existing singleton*.
%
% FACE_TRACKING('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in FACE_TRACKING.M with the given input arguments.
%
% FACE_TRACKING('Property','Value',...) creates a new FACE_TRACKING or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before face_tracking_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to face_tracking_OpeningFcn via varargin.
%
% *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one
% instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES
% Edit the above text to modify the response to help face_tracking
% Last Modified by GUIDE v2.5 18-Mar-2015 11:29:55
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @face_tracking_OpeningFcn, ...
'gui_OutputFcn', @face_tracking_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback', []);
if nargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT
% --- Executes just before face_tracking is made visible.
function face_tracking_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% varargin command line arguments to face_tracking (see VARARGIN)
% Choose default command line output for MAIN
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% --- Outputs from this function are returned to the command line.
function varargout = face_tracking_OutputFcn(hObject, eventdata, handles)
% varargout cell array for returning output args (see VARARGOUT);
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Get default command line output from handles structure
handles.output = hObject;
varargout{1} = handles.output;
% --- Executes when face_tracking is resized.
function face_tracking_ResizeFcn(hObject, eventdata, handles)
% hObject handle to face_tracking (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% --- Executes on button press in cameraon.
function cameraon_Callback(hObject, eventdata, handles)
% hObject handle to cameraon (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global video;
global videoFrame;
axes(handles.axes1);
video = videoinput('winvideo',1,'MJPG_640x480');
handles.video=video;
triggerconfig(video,'manual');
video.FramesPerTrigger = 1;
guidata(hObject,handles);
faceDetector=vision.CascadeObjectDetector('FrontalFaceCART');
faceDetector.MinSize=[20 20];
faceDetector.MergeThreshold = 20;
videoFrame=getsnapshot(video);
bbox=step(faceDetector,videoFrame);
if numel(bbox) == 0
errordlg('Face not detected. Please try again.');
set(handles.cameraon,'String','Start Camera')
stop(video);
else
axes(handles.axes1);
start(video);
end
% Get the skin tone information by extracting the Hue from the video frame
% converted to the HSV color space.
[hueChannel,~,~] = rgb2hsv(videoFrame);
% Detect the nose within the face region. The nose provides a more accurate
% measure of the skin tone because it does not contain any background
% pixels.
noseDetector = vision.CascadeObjectDetector('Nose');
faceImage = imcrop(videoFrame,bbox(1,:));
noseBBox = step(noseDetector,faceImage);
% The nose bounding box is defined relative to the cropped face image.
% Adjust the nose bounding box so that it is relative to the original video
% frame.
noseBBox(1,1:2) = noseBBox(1,1:2) + bbox(1,1:2);
% Create a tracker object.
tracker = vision.HistogramBasedTracker;
% Initialize the tracker histogram using the Hue channel pixels from the
% nose.
initializeObject(tracker, hueChannel, noseBBox(1,:));
for i=1:10000
**Frame=getsnapshot(video);**
hsv = rgb2hsv(Frame);
bbox = step(tracker, hsv(:,:,1));
videoOut = insertObjectAnnotation(Frame,'rectangle',bbox,'Face');
cla;
imshow(videoOut,[]);
drawnow;
pause(0.0001);
end
guidata(hObject,handles);
% --- Executes when user attempts to close face_tracking.
function face_tracking_CloseRequestFcn(hObject, eventdata, handles)
% hObject handle to face_tracking (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
user_response = modaldlg('Title','Confirm Close');
switch user_response
case {'No'}
% take no action
case 'Yes'
delete(hObject);
delete(imaqfind);
close all;
end
% --- Executes on button press in stopcamera.
function stopcamera_Callback(hObject, eventdata, handles)
% hObject handle to stopcamera (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
off=handles.video;
delete(off);
guidata(hObject,handles);
% --- Executes during object creation, after setting all properties.
function edit1_CreateFcn(hObject, eventdata, handles)
% hObject handle to edit1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: edit controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
  3 Comments
Dima Lisin
Dima Lisin on 28 Apr 2015
if size(bbox) is [0,4], that means nothing was detected or tracked. You need to check for this condition.
Jia Zhen
Jia Zhen on 28 Apr 2015
Edited: Jia Zhen on 28 Apr 2015
Hi, i already try to run using vision.CascadeObjectDetector for face detection. However it still say it cannot detect the face. I did not know what is wrong or where to fix it already. Could you help me in this matter.
Is it because the face detected is bigger than the size of the axes?

Sign in to comment.

Answers (1)

Dima Lisin
Dima Lisin on 18 Mar 2015
Edited: Dima Lisin on 18 Mar 2015
Hi Eliza,
It looks like you are still trying to read frames after your camera object is deleted. I would suggest using vision.VideoPlayer to display the frame, instead of imshow. Then you can use the isOpen method of vision.VideoPlayer (available as of R2014b) to determine that the user has closed the player window, and react accordingly.
Also, I would suggest using vision.PointTracker for tracking, instead of vision.HistogramBasedTracker. Take a look at this example of tracking multiple faces.
  4 Comments
Eliza Tham
Eliza Tham on 18 Mar 2015
Hi, I am unable to click on the hyperlink that you have mentioned. Can you indicate which part i should make the changes? i am not very as I am pretty new with matlab.
thank you.
Dima Lisin
Dima Lisin on 18 Mar 2015
Edited: Dima Lisin on 18 Mar 2015
It is one of the examples that ships with MATLAB. At your MATLAB prompt type visiondemos. This will bring up the doc page with all computer vision examples. Look for the example titled "Face Detection and Tracking Using the KLT Algorithm". You seem to have used this example in one of your previous questions.

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!