kelpnetの作法 その6

# 概要

kelpnetの作法を調べてみた。
ライントレースやってみる。
教師データ、作ってみた。

# 写真

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/18104/71a75fbb-82b6-956e-b90c-818941415466.png)

# 結果

```
1,0.9370069,0.1758009,0.1534496,0.9659603,0
1,0.9381194,0.1772627,0.1473485,0.9657054,0
1,0.9398518,0.1796773,0.1377505,0.9653046,0
1,0.9560834,0.2026062,0.09490783,0.9627522,0
1,0.9568549,0.2039106,0.09386921,0.9625103,0
1,0.9605403,0.2105066,0.08937787,0.9611517,0
1,0.9640657,0.2175962,0.08603867,0.9594074,0
1,0.9767041,0.2478495,0.07985754,0.9500523,0
1,0.9777824,0.2508438,0.07962006,0.9489558,0
1,0.9826469,0.2657418,0.07834588,0.9432184,2
1,0.9820471,0.2547368,0.08453556,0.9470891,2
1,0.9810143,0.2370251,0.09541548,0.9526507,2
1,0.9783586,0.193597,0.1274238,0.963551,2
1,0.9780537,0.188939,0.1313823,0.9644483,2
1,0.9771624,0.1760049,0.142874,0.9667777,2
1,0.9753699,0.1516642,0.1672629,0.9703423,2
1,0.9495733,0.07180189,0.2887425,0.9763497,2
1,0.9479302,0.06940074,0.2940892,0.9764546,2
1,0.9425594,0.06237222,0.310661,0.9767237,2
1,0.9239203,0.04470607,0.3603735,0.9773159,2
```

# 使った技術

- wasdで、動かす。
- カメラの画像を取得。
- ピクセル取得。
- カメラの画像を描画。
- csvを書く。

# サンプルコード

```
using UnityEngine;
using System.Collections;
using System.IO;
using System;

public class pc1 : MonoBehaviour 
{
    
	[SerializeField]
	private Camera dispCamera;
    [SerializeField]
    private SpriteRenderer bod;
    [SerializeField]
    public float val;
	private Texture2D tex0;
	void Start () 
    {
        var tex = dispCamera.targetTexture;
		tex0 = new Texture2D(tex.width, tex.height, TextureFormat.ARGB32, false);
	}
	void Update () 
    {
        var x = Input.GetAxis("Horizontal") * Time.deltaTime * -50.0f;
        var y = Input.GetAxis("Vertical") * Time.deltaTime * 1.0f;
        transform.Rotate(0, 0, x);
        transform.Translate(0, y, 0);
        var tex = dispCamera.targetTexture;
        Texture2D tex1 = new Texture2D(40, 40, TextureFormat.ARGB32, false);
        Texture2D tex2 = new Texture2D(40, 40, TextureFormat.ARGB32, false);
        Texture2D tex3 = new Texture2D(40, 40, TextureFormat.ARGB32, false);
        Texture2D tex4 = new Texture2D(40, 40, TextureFormat.ARGB32, false);
        Texture2D tex5 = new Texture2D(40, 40, TextureFormat.ARGB32, false);
        float v1;
        float v2;
        float v3;
        float v4;
        float v5;
        RenderTexture.active = dispCamera.targetTexture;
		tex0.ReadPixels(new Rect(0, 0, tex.width, tex.height), 0, 0);
		tex0.Apply();
        tex1.ReadPixels(new Rect(10, 200, 40, 40), 0, 0);
        tex1.Apply();
        tex2.ReadPixels(new Rect(60, 200, 40, 40), 0, 0);
        tex2.Apply();
        tex3.ReadPixels(new Rect(110, 200, 40, 40), 0, 0);
        tex3.Apply();
        tex4.ReadPixels(new Rect(160, 200, 40, 40), 0, 0);
        tex4.Apply();
        tex5.ReadPixels(new Rect(210, 200, 40, 40), 0, 0);
        tex5.Apply();
        val = Sense(tex0);
        v1 = Sense(tex1);
        v2 = Sense(tex2);
        v3 = Sense(tex3);
        v4 = Sense(tex4);
        v5 = Sense(tex5);
        Sprite spr = Sprite.Create(tex0, new Rect(0.0f, 0.0f, tex.width, tex.height), new Vector2(0.5f, 0.5f));
        bod.sprite = spr;
        int act = 4;
        if (Input.GetKey(KeyCode.W))
        {
            act = 0;
        }
        if (Input.GetKey(KeyCode.A))
        {
            act = 1;
        }
        if (Input.GetKey(KeyCode.D))
        {
            act = 2;
        }
        if (act < 4)
        {
            StreamWriter sw;
            FileInfo fi;
            fi = new FileInfo(Application.dataPath + "/test.csv");
            sw = fi.AppendText();
            sw.Write(v1.ToString());
            sw.Write(",");
            sw.Write(v2.ToString());
            sw.Write(",");
            sw.Write(v3.ToString());
            sw.Write(",");
            sw.Write(v4.ToString());
            sw.Write(",");
            sw.Write(v5.ToString());
            sw.Write(",");
            sw.WriteLine(act.ToString());
            sw.Flush();
            sw.Close();
        }
	}
    private float Sense(Texture2D tex)
    {
        var cols = tex.GetPixels();
        var avg = new Color(0, 0, 0);
        foreach (var col in cols)
        {
            avg += col;
        }
        avg /= cols.Length;
        return avg.grayscale;
    }
}

```

以上。