The device associated with device ID 1 is already in use. ERROR!!! . One webcam used for function and gui display???

16 views (last 30 days)
I am trying to use a webcam in a function that i run with the gui while at the same time displaying the live video of the webcam in my gui. I can so far get the live video on my gui but when I try to start my function with the gui i get this error below. I have made vid global in my function not the gui and that's how i got the live video to display on the gui finally but i still cant figure out what to do to get both the function and my gui using the one webcam.
Thanks in advance!!!!!!
Error using videoinput (line 379)
winvideo: The device associated with device ID 1 is already in use. A new videoinput object cannot be created for this device while it is in use.
Error in AutofocusGUIGOOD (line 8) vid = videoinput('winvideo', 1, 'YUY2_640x480');
Error in LIBSGUI624>pushbutton1_Callback (line 89) AutofocusGUIGOOD(Ss , SR);
Error in gui_mainfcn (line 96) feval(varargin{:});
Error in LIBSGUI624 (line 43) gui_mainfcn(gui_State, varargin{:});
  2 Comments
Geoff Hayes
Geoff Hayes on 26 Jun 2014
Richard - from the above error message, it appears that the user (so you!) pushed a button on the GUI ( pushbutton1 ) and its callback ( pushbutton1_Callback * )fired, which called the *AutofocusGUIGOOD function. Line 8 is
vid = videoinput('winvideo', 1, 'YUY2_640x480');
And the device associated with device ID 1 is already in use. A new videoinput object cannot be created for this device while it is in use is thrown because (as you have already said) you are displaying the video from the webcam in the GUI which is being "read" from device 1.
The error message makes sense - the code cannot call the videoinput a second on the same device without deleting the first one.
So how or where are you (in your GUI) are you making the first videoinput call to create a video input object? Because it is with that object that your code will need to reference in the button callback (and so AutofocusGUIGOOD function).
And please clarify what you mean by I have made vid global in my function not the gui.
Richard
Richard on 26 Jun 2014
Here is the GUI Opening function callback(where videoinupt appeires first), pushbutton1 callback and the AutofoucusGUIGOOD function. I declared vid a global variable in the function with the hopes that it would move vid from the GUI(where i first call videoinput) to the AutofocusGUIGOOD function. I feel like this should be easy because all i want to do is use the same webcam (device) in different places at the same time. Can i make an object global for example make my webcam global. Thanks for the help and your patience I know I am not good with matlab and the terminology.
% --- Executes just before LIBSGUI626 is made visible. function LIBSGUI626_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 LIBSGUI626 (see VARARGIN)
% Choose default command line output for LIBSGUI626 handles.output = hObject;
% Construct a video input object associated with a winvideo device at ID 1.
vid = videoinput('winvideo', 1, 'YUY2_640x480');
%set(vid, 'SelectedSourceName', 'input1') % Select the source to use for
acquisition.
axes(handles.axes1);
hImage=image(zeros(160,120,3),'Parent',handles.axes1);
preview(vid,hImage);
% Update handles structure guidata(hObject, handles);
___________________________________________
% --- Executes on button press in pushbutton1. function pushbutton1_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
Ss = str2num(char(get(handles.edit1,'String')));
SR = str2num(char(get(handles.edit3,'String')));
if ~isempty(Ss) && ~isempty(SR)
AutofocusGUIGOOD(Ss , SR);
end
% handles structure with handles and user data (see GUIDATA)
________________________________________
AutofoucsGUIGOOD(Ss , SR)
Fvar=[];
h.MoveAbsoluteEx (0,0,1,0); % home
pause(5)
for z = 0:Ss:SR % Step size and Sweep range
h.MoveAbsoluteEx (0,z,1,0);
pause(1)
frame = getsnapshot(vid);
frame=frame(160:320, 240:400);
Fvar=[Fvar std2(im2double(frame))];
end
pause(2)
figure(3) z = 0:Ss:SR plot(z,Fvar) % graph of step and variance xlabel('z') ylabel('variance')
pause(2)
maxFvar=max(Fvar); [mmaxFvar , ind] = max(Fvar); maxX= z(ind); h.MoveAbsoluteEx (0,maxX,1,0);

Sign in to comment.

Accepted Answer

Geoff Hayes
Geoff Hayes on 26 Jun 2014
Edited: Geoff Hayes on 26 Jun 2014
Hi Richard - that is quite a number of lines of code that you pasted in the comment. In the future, please either format the code so that it is readable (just highlight the code portions and press *Code {}*0) or attach the code files to your question using the paperclip button.
You must have removed the vid = videoinput('winvideo', 1, 'YUY2_640x480'); line from AutofoucsGUIGOOD and are attempting to use vid as a global variable. In order to indicate that a variable is global, the function must do so prior to using the variable. Something like
global testVar;
testVar = 42;
Every function that references this global variable (either to initialize it or to make use of it in some way) must use this global varName; statement before using the variable. That is how it is done, but I am going to recommend that you DON'T follow this approach.
In the LIBSGUI626_OpeningFcn function you initialize the video input object. You can do this, but save the object to the handles structure whose purpose is not only to manage the list of handles to widgets on your GUI but can also be used to manage user defined data...like your video input object. Just do the following
% instantiate the video input object as a field within the handles struct
handles.vid = videoinput('winvideo', 1, 'YUY2_640x480');
% do same code as before just use handles.vid in place of just vid
axes(handles.axes1);
hImage=image(zeros(160,120,3),'Parent',handles.axes1);
preview(handles.vid,hImage);
% now save the updated handles object
guidata(hObject,handles);
Now the video input object can be referenced from wherever the handles structure is available. Such as in the pushbutton1_Callback function.
Within this function do the following to access the video input object and pass it as a parameter to the AutofoucsGUIGOOD function
if ~isempty(Ss) && ~isempty(SR) && isfield(handles,'vid')
AutofocusGUIGOOD(Ss, SR, handles.vid);
end
Almost done. If the video input object has been defined in the handles structure, then we pass it as the third input to AutofocusGUIGOOD.
Now just modify the AutofocusGUIGOOD function signature to the following
function AutofoucsGUIGOOD(Ss, SR, vid)
And that is it - the video input object is instantiated in the GUI, saved to the handles structure and passed along through the pushbutton1 callback to the AutofoucsGUIGOOD function.
Just remember that since you have opened the video input object, you need to close it (or delete it) as well. Make sure that is captured somewhere in the GUI (maybe in the figure _DeleteFcn).
  2 Comments
Geoff Hayes
Geoff Hayes on 30 Jun 2014
Richard's response moved here:
Thanks a ton for the tips Im new if you cant tell :D and what you suggested worked but i didn't need to do the last suggestion that you made about the figure_DeleteFcn can you please explain what this does more and how to do it. THANKS A TON AGAIN YOU ARE A GOD.
Geoff Hayes
Geoff Hayes on 30 Jun 2014
Glad to have been able to help, Richard.
As for the _DeleteFcn, the intention behind using this function is to close the opened video input object. It is good practice to always balance an open with a close (just like with file IO) so that nothing is left "hanging".
If the user closes your GUI by pressing the X button in the window bar (top left or top right corner) then this function is called and you can do any clean up activity before the GUI/app is done.
In GUIDE, just click on the figure (not any other widget) and from the menu select View --> View Callbacks --> DeleteFcn. The editor should open with something similar pasted in your m file
% --- Executes during object deletion, before destroying properties.
function figure1_DeleteFcn(hObject, eventdata, handles)
% hObject handle to figure1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
You may have something different than figure1 depending on if you named your figure.
Now just add something like
% make sure that the handles structure is not empty
if ~isempty(handles)
% check to see if vid is a field within that structure
if isfield(handles,'vid')
% remove video object from memory
delete(handles.vid);
end
end
And that is it. Just a good habit to get into.

Sign in to comment.

More Answers (1)

Richard
Richard on 30 Jun 2014
Thanks a ton for the tips Im new if you cant tell :D and what you suggested worked but i didn't need to do the last suggestion that you made about the figure_DeleteFcn can you please explain what this does more and how to do it. THANKS A TON AGAIN YOU ARE A GOD.

Community Treasure Hunt

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

Start Hunting!