c# - 设置引用编号并将其与文本文件中的其他数据进行比较

标签 c# interaction eye-tracking

该项目基于眼动仪。让我简要介绍一下项目背后的想法,以便更好地理解我的问题。

我有 Tobii C 眼动仪的硬件。这个眼动仪将能够给出我正在看的地方的 X、Y 坐标。但是这个装置非常敏感。当我看 1 个点时,眼动仪会发出许多不同的坐标数据,但在我发现的 ± 100 范围内。即使你盯着一个点,你的眼睛也会不停地移动,因此会给出很多数据。然后将这么多数据( float )保存在文本文件中。现在我只需要 1 个数据(X 坐标)表示我正在注视的 1 个点,而不是 ± 100 范围内的许多数据并将其移动到新的文本文件。

我不知道我应该如何编写代码来做到这一点。

这些是文本文件中的 float 数字。

200
201
198
202
250
278
310
315
360
389
500
568
579
590

当我凝视点 1 时,数据为 200-300,在 ± 100 范围内。我想将 200 设置为引用点,用下一个数字减去自身,并检查结果值是否在 100 之内,如果是,则将其删除。引用点应继续对以下数字执行此操作,直到它超出 ± 100 范围。一旦超出 100 范围,现在这个数字是 310,那么现在这个数字是下一个引用点,做同样的事情并用下面的数字减去并检查是否100 以内的结果值。一旦超出 100 范围,下一个数字是 500,现在,这是新的引用点,并执行相同的操作。那是我的目标。简而言之,引用点应移至新文件中。

到目前为止,这是我的代码,它获取注视坐标并将它们存储在文本文件中。

 using System;
 using System.Collections.Generic;
 using System.IO;
 using System.Linq;
 using System.Text;
 using Tobii.Interaction;

namespace ConsoleApp1
{

class Program
{

    private static void programintro()
    {
        Console.WriteLine("Press Any Keys To Start");
    }
    public static void Main(string[] args)
    {

        programintro();
        Console.ReadKey();
        double currentX = 0.0;
        double currentY = 0.0;
        double timeStampCurrent = 0.0;
        double diffX = 0.0;
        double diffY = 0.0;
        int counter = 0;
        var host = new Host();
        host.EnableConnection();
        var gazePointDataStream = host.Streams.CreateGazePointDataStream();
        gazePointDataStream.GazePoint((gazePointX, gazePointY, timestamp) =>

        {
            diffX = gazePointX - currentX;
            diffY = gazePointY - currentY;
            currentX = gazePointX;
            currentY = gazePointY;
            timeStampCurrent = timestamp;
            if (diffX > 100 || diffX <= -100 || diffY >= 100 || diffY <= -100)
            {
                counter++;
                using (StreamWriter writer = new StreamWriter("C: \\Users\\Student\\Desktop\\FYP 2019\\ConsoleApp1\\ConsoleApp1\\Data\\TextFile1.txt", true))
                {
                    writer.WriteLine("Recorded Data " + counter + "\n=================================================================================================================\nX: {0} Y:{1}\nData collected at {2}", currentX, currentY, timeStampCurrent);
                    writer.WriteLine("=================================================================================================================");
                }
                Console.WriteLine("Recorded Data " + counter + "\n=================================================================================================================\nX: {0} Y:{1}\nData collected at {2}", currentX, currentY, timeStampCurrent);
                Console.WriteLine("=================================================================================================================");
            }
        });
        //host.DisableConnection();
        while (true)
        {
            if (counter <  10)
            {
                continue;
            }
            else
            {

                Environment.Exit(0);

            }
        }

Now my Question is how do I code to read the text file and set a reference number and subtracts itself with the next number and check if the resultant value within 100 and have a new reference number if it outside the ± 100 range. Those reference numbers are then stored in a new text file.

最佳答案

根据您的样本数据,此处的代码仅获取相差超过 100 的数字。

static void Main(string[] args)
{
  string filename = @"C:\PowershellScripts\test.txt"; // INPUT FILE
  String outputFile = @"C:\PowershellScripts\result.txt"; // OUTPUT FILE

  string[] data = File.ReadAllLines(filename); // READING FORM FILE
  int TotalLine = data.Length; // COUNT TOTAL NO OF ROWS
  List<string> FinalList = new List<string>(); // INITIALIZE LIST FOR FINAL RESULT

  if (TotalLine <= 0) // CHECK IF FILE HAS NO DATA
  {
      Console.WriteLine("No Data found !");
      return;
  }

  double CurrentNumber = double.Parse(data[0]), NextNumber = 0, diff = 0; // INITIALIZE OF LOCAL VARIABLES, CURRENT NUMBER = FIRST NO FROM FILE

  for (int cntr = 1; cntr < TotalLine; cntr++) // FOR LOOP FOR EACH LINE
  {
      NextNumber = double.Parse(data[cntr]); //PARSING NEXT NO
      diff = CurrentNumber - NextNumber; // GETTING DIFFERENCE

      if (diff <= 100 && diff >= -100) // MATCH THE DIFFERENCE
      {
          continue; // SKIP THE LOGIC IF DIFF IS LESS THEN 100
      }
      else
      {
          FinalList.Add(CurrentNumber.ToString()); // ADDING THE NO TO LIST
          CurrentNumber = NextNumber; // POINTING TO NEXT NO
      }

  }

  FinalList.Add(CurrentNumber.ToString()); // ADDING LAST NO.
  foreach (string d in FinalList) // FOR EACH LOOP TO PRINT THE FINAL LIST
      Console.WriteLine(d);

  File.WriteAllLines(outputFile, FinalList); // SAVING TO THE FILE

}

上面的程序将生成的输出是:

200
310
500

enter image description here

关于c# - 设置引用编号并将其与文本文件中的其他数据进行比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56625136/

相关文章:

c# - 无法向 ServiceControl 注册端点启动

c# - 眼动追踪-估计脸部位置

javascript - 下载 c# excel 字节数组并将其转换为 javascript blob

c# - 如何将队列的值设置到另一个队列?

facebook - 社交媒体链接

python - 不和谐.py 未找到 : 404 Not Found (error code: 10062): Unknown interaction

java - 是否有适用于 Android Java 或类似语言的现有虹膜/眼睛识别库?

c# - 如何在 Monotouch 中绑定(bind) NSObject<EAAccessoryDe​​legate>?

c++ - C++ 和 Rails 应用程序之间的交互