Browse Source

Added coord2arff files

Ioannis Agtzidis 5 years ago
parent
commit
e471b12439
2 changed files with 105 additions and 0 deletions
  1. 43 0
      coord2arff/Coord2Arff.m
  2. 62 0
      coord2arff/LoadCoordAndConf.m

+ 43 - 0
coord2arff/Coord2Arff.m

@@ -0,0 +1,43 @@
+% function Coord2Arff:
+% Converts coord files to ARFF files. The created files have the same name
+% as the initial ones with .arff ending and are stored on the directory of
+% this file.
+%
+% input:
+%   coordFile   - .coord file
+%
+% output:
+%   a .arff file with same basename as the .coord 
+
+function Coord2Arff(coordFile)
+    % get part of input file
+    [dir, basename, ext] = fileparts(coordFile);
+
+    % load data from coord file
+    [data, pixelX, pixelY, width, height, distance, conf] = LoadCoordAndConf(coordFile);
+
+    arffFile = [basename '.arff'];
+
+    fid = fopen(arffFile, 'w+');
+    % print experiment parameters
+    fprintf(fid, '@RELATION gaze_labels\n\n');
+    fprintf(fid, '%%@METADATA width_px %d\n', pixelX);
+    fprintf(fid, '%%@METADATA height_px %d\n', pixelY);
+    fprintf(fid, '%%@METADATA distance_mm %.2f\n', distance*1000);
+    fprintf(fid, '%%@METADATA width_mm %.2f\n', width*1000);
+    fprintf(fid, '%%@METADATA height_mm %.2f\n\n', height*1000);
+
+    fprintf(fid, '@ATTRIBUTE time NUMERIC\n');
+    fprintf(fid, '@ATTRIBUTE x NUMERIC\n');
+    fprintf(fid, '@ATTRIBUTE y NUMERIC\n');
+    fprintf(fid, '@ATTRIBUTE confidence NUMERIC\n\n');
+    fprintf(fid, '@DATA\n');
+
+    for i=1:size(data,1)
+        fprintf(fid, '%d,', data(i,1));
+        fprintf(fid, '%.2f,', data(i,2:3));
+        fprintf(fid, '%.2f\n', conf(i));
+    end
+
+    fclose(fid);
+end

+ 62 - 0
coord2arff/LoadCoordAndConf.m

@@ -0,0 +1,62 @@
+% function LoadCoordAndConf:
+% This function Loads data from the given input file and returns a data matrix. 
+% 
+% input:
+%   inputFile           - file that holds data to  load
+% output:
+%   data     - data matrix holding timestamp and coordinates nx3 where confidence > 0.1
+%   pixelX   - width in pixels
+%   pixelY   - height in pixels
+%   width    - width of monitor in meters
+%   height   - height of monitor in meters
+%   distance - distance from monitor in meters
+%   conf     - confince of return data. In range [0,1]
+
+function [data, pixelX, pixelY, width, height, distance, conf] = LoadCoordAndConf(inputfile)
+    pixelX = -1;
+    pixelY = -1;
+    width = -1;
+    height = -1;
+    distance = -1;
+
+    inputfileClean = strtrim(inputfile);
+    if (exist(inputfileClean) == 0)
+        error('File does not exist.');
+    endif
+    
+    % check if input file has header
+    fid = fopen(inputfileClean);
+    numOfHeaderLines = 0;
+    for i=1:20
+        l = fgetl(fid);
+        if (!ischar(l)) % detect end of file
+            break;
+        end
+        pos = strfind(l, 'geometry');
+        if (size(pos,1) > 0)
+            numOfHeaderLines = i;
+        end
+    end
+    fclose(fid);
+
+    if (numOfHeaderLines > 0)
+        data = importdata(inputfileClean, ' ', numOfHeaderLines);
+        matrix = data.data;
+        s = strsplit(data.textdata{numOfHeaderLines}, ' ');
+        distance = str2num(s{1,3});
+        width = str2num(s{1,5});
+        height = str2num(s{1,7});
+        s = strsplit(data.textdata{numOfHeaderLines-1}, ' ');
+        pixelX = str2num(s{1,2});
+        pixelY = str2num(s{1,3});
+    end
+
+    data = importdata(inputfileClean, ' ', numOfHeaderLines);
+    [rows, columns] = size(data.data);
+    if (columns ~= 4 & columns ~= 13)
+        error("Wrong type of input file.");
+    end
+
+    conf = data.data(:,4);
+    data = data.data(:,1:3);
+end