// Start is called before the first frame update
    void Start()
    {
        plane.SetActive(true);
        Camera.main.GetComponent <Skybox>().enabled = true;

        height = Camera.main.pixelHeight;
        width  = Camera.main.pixelWidth;
        Debug.Log("Height : " + height + "  Width : " + width);

        trainingImages = count.trainingImages;
        testImages     = count.testImages;
        kpLen          = count.numOfkeypoints;

        cadObj.transform.SetParent(Camera.main.transform);

        initPos = cadObj.transform.position;
        initRot = cadObj.transform.rotation;

        decideAction();
        decideMethod();

        if (generate)
        {
            plane.SetActive(false);
            Camera.main.GetComponent <Skybox>().enabled = false;

            var mesh       = cadObj.GetComponent <MeshCollider>().sharedMesh;
            var vertices3D = mesh.vertices;
            var normal3D   = mesh.triangles;

            if (method == Methods.manual)
            {
                tw = new StreamWriter(gtKpFile);
            }
            else if (method == Methods.random)
            {
                randomPoints = new Vector3[kpLen];

                for (int i = 0; i < kpLen; i++)
                {
                    var num = UnityEngine.Random.Range(1, vertices3D.Length);
                    randomPoints[i] = cadObj.transform.TransformPoint(vertices3D[num]);
                }
            }

            TextWriter vertices = new StreamWriter("captures/GroundTruth/image_groundtruth_img-3DVertices.txt");
            //TextWriter verticesReal = new StreamWriter("captures/GroundTruth/image_groundtruth_img-3DRealVertices.txt");

            var vLen = vertices3D.Length;
            for (int i = 0; i < vLen; i++)
            {
                // Vector3 vSynth =  cadObj.transform.rotation * vertices3D[i];
                // vSynth = Vector3.Scale(vSynth , cadObj.transform.localScale);

                Vector3 vReal = vertices3D[i];
                //Vector3 vSynth = cadObj.transform.TransformPoint(vReal);
                //Vector3 vSynth = Vector3.Scale(vReal , cadObj.transform.localScale);

                vertices.WriteLine(vReal.x + ", " + vReal.y + ", " + vReal.z);
                //verticesSynth.WriteLine(vSynth.x + ", " + vSynth.y + ", " +vSynth.z);
            }

            Debug.Log($"vertices saved");
            //verticesReal.Flush();
            vertices.Flush();

            TextWriter faces = new StreamWriter("captures/GroundTruth/image_groundtruth_img-3DFaces.txt");
            var        fLen  = normal3D.Length;

            for (int i = 0; i < fLen; i = i + 3)
            {
                faces.WriteLine(normal3D[i] + ", " + normal3D[i + 1] + ", " + normal3D[i + 2]);
            }
            faces.Flush();

            Debug.Log($"totoal number of vertcies {vLen} , total number of faces {fLen}");

            synth.writeCameramatrix();
        }
        else if (control || convert)
        {
            pool = ShapePool.create(prefabs);
            string[] lines = File.ReadAllLines(gtKpFile);

            kpLen = lines.Length;
            if (kpLen < 2)
            {
                throw new Exception("Minimum 2 Keypoints need to be generated, try action Generate");
            }

            int i = 0;

            imgPts = new Point2f[kpLen];
            objPts = new Point3f[kpLen];

            TextWriter gt3d = new StreamWriter("captures/GroundTruth/image_groundtruth_img-3DGT.txt");

            string[] spearator = { "(", ", ", ") : (", ")", ":" };
            foreach (var l in lines)
            {
                string[] c = l.Split(spearator, StringSplitOptions.RemoveEmptyEntries);
                //Debug.Log($"length {c.Length}");
                //Debug.Log($"lines  : {c[0]} {c[1]} {c[2]} {c[3]} {c[4]}");
                imgPts[i] = new Point2f(float.Parse(c[0]), float.Parse(c[1]));
                objPts[i] = new Point3f(float.Parse(c[2]), float.Parse(c[3]), float.Parse(c[4]));

                Vector3 original3DVertices = cadObj.transform.InverseTransformPoint(new Vector3(float.Parse(c[2]), float.Parse(c[3]), float.Parse(c[4])));
                gt3d.WriteLine(original3DVertices.x + ", " + original3DVertices.y + ", " + original3DVertices.z);
                i++;
            }
            gt3d.Flush();

            synth.PointAndPnP(imgPts, objPts, "captures/GroundTruth/", "image_groundtruth_img");
        }
    }
Exemple #2
0
    // Start is called before the first frame update
    void Start()
    {
        height = Camera.main.pixelHeight;
        width  = Camera.main.pixelWidth;
        Debug.Log("Height : " + height + "  Width : " + width);

        trainingImages = count.trainingImages;
        testImages     = count.testImages;

        initPos = cadObj.transform.position;
        initRot = cadObj.transform.rotation;

        decideAction();
        decideMethod();

        if (generate)
        {
            if (method == Methods.manual)
            {
                tw = new StreamWriter(gtKpFile);
            }

            var mesh       = cadObj.GetComponent <MeshCollider>().sharedMesh;
            var vertices3D = mesh.vertices;
            var normal3D   = mesh.triangles;

            TextWriter vertices = new StreamWriter("captures/GroundTruth/image_groundtruth_img-vertices.txt");
            for (int i = 0; i < vertices3D.Length; i++)
            {
                Vector3 v = cadObj.transform.rotation * vertices3D[i];
                v = Vector3.Scale(v, cadObj.transform.localScale);

                vertices.WriteLine(v.x + ", " + v.y + ", " + v.z);
            }
            Debug.Log($"vertices saved");
            vertices.Flush();

            TextWriter faces = new StreamWriter("captures/GroundTruth/image_groundtruth_img-faces.txt");
            for (int i = 0; i < normal3D.Length; i = i + 3)
            {
                faces.WriteLine(normal3D[i] + ", " + normal3D[i + 1] + ", " + normal3D[i + 2]);
            }
            faces.Flush();
        }
        else if (control || convert)
        {
            pool = ShapePool.create(prefabs);
            string[] lines = File.ReadAllLines(gtKpFile);

            kpLen = lines.Length;
            if (kpLen < 2)
            {
                throw new Exception("Minimum 2 Keypoints need to be generated, try action Generate");
            }

            int i = 0;

            imgPts = new Point2f[kpLen];
            objPts = new Point3f[kpLen];

            string[] spearator = { "(", ", ", ") : (", ")", ":" };
            foreach (var l in lines)
            {
                string[] c = l.Split(spearator, StringSplitOptions.RemoveEmptyEntries);
                //Debug.Log($"length {c.Length}");
                //Debug.Log($"lines  : {c[0]} {c[1]} {c[2]} {c[3]} {c[4]}");
                imgPts[i] = new Point2f(float.Parse(c[0]), float.Parse(c[1]));
                objPts[i] = new Point3f(float.Parse(c[2]), float.Parse(c[3]), float.Parse(c[4]));
                i++;
            }
            //synth.PointAndPnP(imgPts, objPts);
        }
    }