Skip to content
TheChatArchiveThe Chat Archive
gpt-3.5-turbo-0301· OpenAIAi2 WildChat-4.8M (AI2)

In the GUI window, when i click mouse to draw something a unnecessary line is being…

  1. User

    In the GUI window, when i click mouse to draw something a unnecessary line is being drawn. Irrespective of how many times I tried I'm unable to get rid of that. This causing me to get an irregular shape of my desired object. If possible include a eraser to erase such things and also check what went wrong in my code. function gestureRecognitionGUI() % Create the interface figure fig = figure('Name', 'Gesture Recognition For Vacuum Robot', 'Visible', 'off', ... 'Position', [500 500 600 600], 'NumberTitle', 'off');

    % Draw axes for gestures
    gestureAxes = axes('Parent', fig, 'Units', 'Normalized', 'Position', [.2 .2 .6 .6]);
    set(gestureAxes, 'ButtonDownFcn', @startDraw);
    set(get(gestureAxes, 'Children'), 'ButtonDownFcn', @startDraw)
    
    % Create a popup menu for choosing gesture classes
    classNames = {'Charge Battery', 'Area Selection', 'Clean Mop', ...
                  'Pause Operation', 'Start Operation', 'Vacuum Surface', 'Mop Surface'};
    gesturePopUp = uicontrol('Style', 'popupmenu', 'String', classNames, 'Units', 'Normalized', ...
                             'Position', [.15 .05 .7 .1]);
    
    % Create a button for clearing the axes
    clearButton = uicontrol('Style', 'pushbutton', 'String', 'Clear', 'Units', 'Normalized', ...
                            'Position', [.45 .85 .1 .05], 'Callback', @clearGestureAxes);
    
    % Set the figure to visible
    set(fig, 'Visible', 'on');
    
    function startDraw(src, ~)
        % This function responds to the mouse button press on the axes
        startP = get(src, 'CurrentPoint');
        set(src, 'UserData', startP(1,1:2))
        set(fig, 'WindowButtonMotionFcn', @drawing)
        set(fig, 'WindowButtonUpFcn', @stopDraw)
    end
    
    function drawing(~, ~)
        % This function follows mouse movement and draws the gesture
        if isequal(get(fig, 'CurrentObject'), gestureAxes)
            p1 = get(gestureAxes, 'UserData');
            p2 = get(gestureAxes, 'CurrentPoint');
            p2 = p2(1, 1:2);
            line([p1(1), p2(1)], [p1(2), p2(2)], 'Parent', gestureAxes, 'linewidth', 2, 'Color', 'red');
            set(gestureAxes, 'UserData', p2)
        end
    end
    
    function stopDraw(~, ~)
        % This function responds to the mouse button release on the axes
        set(fig, 'WindowButtonMotionFcn', '');
        
        % Save the gesture points as a mat file
        classname = classNames{get(gesturePopUp, 'Value')};
        gestureLines = findobj(gestureAxes, 'Type', 'Line');
        gesturePoints = [];
        for i = 1:length(gestureLines)
            gesturePoints = [gesturePoints; [gestureLines(i).XData' gestureLines(i).YData']];
        end
        
        % Check if the mat-file exists, if not, create it
         if ~(exist(fullfile('gestures/', sprintf('%s.mat', classname)), 'file') == 2)
            gestureData = {};
            save(fullfile('gestures/', sprintf('%s.mat', classname)), 'gestureData');
         end
        
        % Load and update the existing mat-file
        load(fullfile('gestures/', sprintf('%s.mat', classname)));
        if isempty(gestureData) || isempty(gestureData{end})
            gestureData{end + 1} = gesturePoints;
        else
            gestureData{end + 1} = cell(size(gestureData{1}));
            gestureData{end}{1} = gesturePoints;
        end
        save(fullfile('gestures/', sprintf('%s.mat', classname)), 'gestureData');
    end
    
    function clearGestureAxes(~, ~)
        % Clear the gesture drawing axes
        gestureLines = findobj(gestureAxes, 'Type', 'Line');
        delete(gestureLines);
    end
    

    end

  2. Assistant

    The extra line being drawn could be due to a problem with the mouse click event. Try modifying the startDraw function to reset the axes' UserData to an empty array or a null value before setting its new value. This may prevent any extra lines from being drawn.

    Also, to add an eraser, you can create a push button and set its callback function to delete the selected line(s) from the axes. Here's an example code snippet:

    % Create a button for erasing lines eraseButton = uicontrol('Style', 'pushbutton', 'String', 'Erase', 'Units', 'Normalized', ... 'Position', [.25 .85 .1 .05], 'Callback', @eraseLine);

    function eraseLine(~,~)
        % This function removes the last drawn line
        gestureLines = findobj(gestureAxes, 'Type', 'Line');
        % Delete only the last line drawn
        if ~isempty(gestureLines)
            delete(gestureLines(end));
        end
    end
    

    Additionally, to see if there is any issue with your code, you can try adding some debugging messages (using "disp" or "fprintf") at different points in your code to print out variable values or check if certain parts of the code are executed.

  3. User

    Can you exaclty tell me where to insert the eraseline snippet you've given?

  4. Assistant

    Sure! You can insert the eraseButton creation code snippet right after the clearButton code snippet (around line 20). And you can insert the eraseLine function right after the clearGestureAxes function (around line 77).

    Here's the updated code with the eraser functionality:

    function gestureRecognitionGUI()
        % Create the interface figure
        fig = figure('Name', 'Gesture Recognition For Vacuum Robot', 'Visible', 'off', ...
                     'Position', [500 500 600 600], 'NumberTitle', 'off');
        
        % Draw axes for gestures
        gestureAxes = axes('Parent', fig, 'Units', 'Normalized', 'Position', [.2 .2 .6 .6]);
        set(gestureAxes, 'ButtonDownFcn', @startDraw);
        set(get(gestureAxes, 'Children'), 'ButtonDownFcn', @startDraw)
        
        % Create a popup menu for choosing gesture classes
        classNames = {'Charge Battery', 'Area Selection', 'Clean Mop', ...
                      'Pause Operation', 'Start Operation', 'Vacuum Surface', 'Mop Surface'};
        gesturePopUp = uicontrol('Style', 'popupmenu', 'String', classNames, 'Units', 'Normalized', ...
                                 'Position', [.15 .05 .7 .1]);
        
        % Create a button for clearing the axes
        clearButton = uicontrol('Style', 'pushbutton', 'String', 'Clear', 'Units', 'Normalized', ...
                                'Position', [.45 .85 .1 .05], 'Callback', @clearGestureAxes);
        
        % Create a button for erasing lines
        eraseButton = uicontrol('Style', 'pushbutton', 'String', 'Erase', 'Units', 'Normalized', ...
                                'Position', [.25 .85 .1 .05], 'Callback', @eraseLine);
        
        % Set the figure to visible
        set(fig, 'Visible', 'on');
        
        function startDraw(src, ~)
            % This function responds to the mouse button press on the axes
            startP = get(src, 'CurrentPoint');
            set(src, 'UserData', startP(1,1:2))
            set(fig, 'WindowButtonMotionFcn', @drawing)
            set(fig, 'WindowButtonUpFcn', @stopDraw)
        end
        
        function drawing(~, ~)
            % This function follows mouse movement and draws the gesture
            if isequal(get(fig, 'CurrentObject'), gestureAxes)
                p1 = get(gestureAxes, 'UserData');
                p2 = get(gestureAxes, 'CurrentPoint');
                p2 = p2(1, 1:2);
                line([p1(1), p2(1)], [p1(2), p2(2)], 'Parent', gestureAxes, 'linewidth', 2, 'Color', 'red');
                set(gestureAxes, 'UserData', p2)
            end
        end
        
        function stopDraw(~, ~)
            % This function responds to the mouse button release on the axes
            set(fig, 'WindowButtonMotionFcn', '');
            
            % Save the gesture points as a mat file
            classname = classNames{get(gesturePopUp, 'Value')};
            gestureLines = findobj(gestureAxes, 'Type', 'Line');
            gesturePoints = [];
            for i = 1:length(gestureLines)
                gesturePoints = [gesturePoints; [gestureLines(i).XData' gestureLines(i).YData']];
            end
            
            % Check if the mat-file exists, if not, create it
             if ~(exist(fullfile('gestures/', sprintf('%s.mat', classname)), 'file') == 2)
                gestureData = {};
                save(fullfile('gestures/', sprintf('%s.mat', classname)), 'gestureData');
             end
            
            % Load and update the existing mat-file
            load(fullfile('gestures/', sprintf('%s.mat', classname)));
            if isempty(gestureData) || isempty(gestureData{end})
                gestureData{end + 1} = gesturePoints;
            else
                gestureData{end + 1} = cell(size(gestureData{1}));
                gestureData{end}{1} = gesturePoints;
            end
            save(fullfile('gestures/', sprintf('%s.mat', classname)), 'gestureData');
        end
        
        function clearGestureAxes(~, ~)
            % Clear the gesture drawing axes
            gestureLines = findobj(gestureAxes, 'Type', 'Line');
            delete(gestureLines);
        end
    
        function eraseLine(~,~)
            % This function removes the last drawn line
            gestureLines = findobj(gestureAxes, 'Type', 'Line');
            % Delete only the last line drawn
            if ~isempty(gestureLines)
                delete(gestureLines(end));
            end
        end
    
    end
    

    I hope this helps! Let me know if you have any further questions.

  5. User

    Can you modify the startDraw function to reset the axes’ UserData to an empty array or a null value before setting its new value. To prevent any extra lines from being drawn for the first few clicks.

  6. Assistant

    Yes! You can modify the startDraw function to set the UserData to an empty array ([]) or a null value (NaN) before setting the new value. This will prevent any extra lines from being drawn for the first few clicks. Here's the modified startDraw function:

    function startDraw(src, ~)
            % This function responds to the mouse button press on the axes
            set(src, 'UserData', []); % Reset the UserData to an empty array or NaN
            startP = get(src, 'CurrentPoint');
            set(src, 'UserData', startP(1,1:2))
            set(fig, 'WindowButtonMotionFcn', @drawing)
            set(fig, 'WindowButtonUpFcn', @stopDraw)
    end
    

    I hope this helps!

  7. User

    % Load the dataset load('Face.mat');

    % Divide the dataset into training and testing [trainData, testData, trainLabels, testLabels] = splitData(F, L);

  8. Assistant

    These two lines of code are loading the Face.mat dataset and then splitting it into training and testing sets:

    load('Face.mat');
    [trainData, testData, trainLabels, testLabels] = splitData(F, L);
    

    The load function loads the saved workspace variables from the Face.mat file, including F and L that contain the facial images and their corresponding labels, respectively.

    The splitData function takes F and L as inputs and returns four outputs: trainData, testData, trainLabels and testLabels. This function randomly splits the input data and labels into two sets: training and testing, with a default ratio of 70% training and 30% testing.

    Here's an example implementation of the splitData function:

    function [trainData, testData, trainLabels, testLabels] = splitData(data, labels, ratio)
    % splitData - split data and labels randomly into training and testing sets
    % 
    % Syntax: [trainData, testData, trainLabels, testLabels] = splitData(data, labels, ratio)
    % 
    % Inputs:
    %   data - input data of size (N x D), where N is the number of samples and D is the number of features
    %   labels - input labels of size (N x 1), where N is the number of samples
    %   ratio (optional) - ratio of training set to testing set (default is 0.7)
    % 
    % Outputs:
    %   trainData - training set data of size (M x D), where M is the number of training samples
    %   testData - testing set data of size (N-M x D), where N is the total number of samples and D is the number of features
    %   trainLabels - training set labels of size (M x 1), where M is the number of training samples
    %   testLabels - testing set labels of size (N-M x 1), where N is the total number of samples
    
        if nargin < 3
            ratio = 0.7; % default ratio of 70% training and 30% testing
        end
        
        % get unique labels and count of each label
        uniqueLabels = unique(labels);
        labelCounts = accumarray(labels, 1);
        
        trainData = [];
        testData = [];
        trainLabels = [];
        testLabels = [];
        
        % randomly split data for each unique label
        for i = 1:length(uniqueLabels)
            label = uniqueLabels(i);
            dataIdxs = find(labels == label);
            data = data(dataIdxs, :);
            n = size(data, 1);
            permIdxs = randperm(n);
            cutoff = round(ratio * n);
            trainData = [trainData; data(permIdxs(1:cutoff), :)];
            testData = [testData; data(permIdxs(cutoff+1:end), :)];
            trainLabels = [trainLabels; repmat(label, cutoff, 1)];
            testLabels = [testLabels; repmat(label, n-cutoff, 1)];
        end
    end
    

    The splitData function uses the randperm function to randomly permute the indices of each label's data, and then selects a part of them as training data and the remaining part as testing data.

    You can adjust the ratio input to specify the desired split ratio between training and testing sets.

  9. User

    Can you modify the code as per this dataset includes 12 images of 6 different subjects. For each subjects we have 2 samples of 6 different emotions. Use one of each samples in your training dataset and the other one in your testing dataset. This way you would have 36 images in the training dataset and 36 images in the testing dataset.

  10. Assistant

    Sure! Here's a modified version of the code that splits the Face.mat dataset according to the requirement of having one image of each emotion for each subject in the training set, and the other image of each emotion for each subject in the testing set:

    % Load the dataset
    load('Face.mat');
    
    % Define the emotion labels
    emotions = {'Anger', 'Disgust', 'Fear', 'Happy', 'Neutral', 'Sad'};
    
    % Create the training and testing sets
    trainData = []; testData = []; trainLabels = []; testLabels = [];
    for s = 1:6 % for each subject
        for e = 1:6 % for each emotion
            idxs = find(L(:,1) == s & L(:,2) == e); % get the indices of the images
            trainIdx = idxs(1); % use the first image for training
            testIdx = idxs(2); % use the second image for testing
            trainData = [trainData; F(trainIdx,:)]; % add the image data to the training set
            trainLabels = [trainLabels; e]; % add the emotion label to the training labels
            testData = [testData; F(testIdx,:)]; % add the image data to the testing set
            testLabels = [testLabels; e]; % add the emotion label to the testing labels
        end
    end
    
    % Shuffle the training and testing sets
    idxs = randperm(size(trainData,1));
    trainData = trainData(idxs,:);
    trainLabels = trainLabels(idxs);
    idxs = randperm(size(testData,1));
    testData = testData(idxs,:);
    testLabels = testLabels(idxs);
    

    In this modified code, we first define the list of emotion labels using the emotions variable. Then, we use a loop to go through each subject and emotion, and for each combination, we get the indices of the images that correspond to that subject and emotion. We then use the first image index for training, and the second image index for testing. We add the image data to the appropriate training or testing set and add the emotion label to the corresponding set of labels.

    Finally, we shuffle both the training and testing sets using the randperm function. This results in a training set with 36 images (6 subjects x 6 emotions x 1 image per emotion) and a testing set with 36 images (6 subjects x 6 emotions x 1 image per emotion).

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.