Beispiel #1
0
        public MainWindow()
        {
            InitializeComponent();

            headset = new FVRHeadset(EFVR_ClientCapabilities.Gaze | EFVR_ClientCapabilities.Orientation);

            headset.IsHardwareConnected(out bool isConnected);
            Debug.Assert(isConnected, "Hardware is not connected!");

            headset.IsHardwareReady(out bool isReady);
            Debug.Assert(isReady, "Hardware capabilities are not ready!");

            OpenFileDialog dialog = new OpenFileDialog();

            if (dialog.ShowDialog() == true)
            {
                filename = dialog.FileName;
            }
            else
            {
                MessageBox.Show("File is not selected!");
                return;
            }

            video = new OpenCvSharp.VideoCapture(filename);
            if (!video.IsOpened())
            {
                MessageBox.Show("Video is not valid!");
                return;
            }

            video_width  = (int)video.Get(OpenCvSharp.CaptureProperty.FrameWidth);
            video_height = (int)video.Get(OpenCvSharp.CaptureProperty.FrameHeight);

            OpenCvSharp.Cv2.NamedWindow("coord", OpenCvSharp.WindowMode.Normal);
            OpenCvSharp.Cv2.ResizeWindow("coord", 1600, 900);

            renderer = new EquirectToRect(new GLControl(), new System.Drawing.Size(1400, 900), 100 * (float)Math.PI / 180);
            renderer.Viewer.Paint += (sender, e) =>
            {
                renderer.Render(currentFrame);
            };
            renderer.Viewer.Dock = System.Windows.Forms.DockStyle.Fill;
            viewer.Child         = renderer.Viewer;

            renderingTimer          = new DispatcherTimer();
            renderingTimer.Interval = TimeSpan.FromMilliseconds(1);
            renderingTimer.Tick    += (s, e) =>
            {
                video.Read(currentFrame);
                if (currentFrame.Empty())
                {
                    video.Set(OpenCvSharp.CaptureProperty.PosFrames, 0);
                    return;
                }

                headset.GetHMDPose(out SFVR_Pose pose);

                using (OpenCvSharp.Mat coordmap = currentFrame.Clone())
                {
                    SFVR_Vec3 direction = pose.orientation * SFVR_Vec3.Forward;
                    GetCoord(direction, video_width, video_height, out int x, out int y);
                    OpenCvSharp.Cv2.Circle(coordmap, new OpenCvSharp.Point(x, y), 50, new OpenCvSharp.Scalar(0, 0, 255), -1);

                    OpenCvSharp.Cv2.ImShow("coord", coordmap);
                    OpenCvSharp.Cv2.WaitKey(1);
                }

                SFVR_Vec3 eulerAngle = pose.orientation.EulerAngles;

                renderer.Pitch = -MathHelper.DegreesToRadians(eulerAngle.x);
                renderer.Yaw   = MathHelper.DegreesToRadians(eulerAngle.y);
                renderer.Roll  = MathHelper.DegreesToRadians(eulerAngle.z);

                renderer.Viewer.Invalidate();
            };
            renderingTimer.Start();
        }
Beispiel #2
0
        static int Main(string[] args)
        {
            // print usage
            Console.WriteLine("Usage: dotnet csharp_demo.dll [OPTIONS]");
            Console.WriteLine("Examples:");
            Console.WriteLine("    To specify license directory (for online license for example): dotnet csharp_demo.dll --license_dir license_dir");
            Console.WriteLine("");

            // parse arguments
            bool    error   = false;
            Options options = new Options();

            CommandLine.Parser.Default.ParseArguments <Options>(args)
            .WithParsed <Options>(opts => options  = opts)
            .WithNotParsed <Options>(errs => error = true);

            // exit if argument parsign error
            if (error)
            {
                return(1);
            }

            // print values of arguments
            Console.WriteLine("Arguments:");
            foreach (var opt in options.GetType().GetProperties())
            {
                Console.WriteLine("--{0} = {1}", opt.Name, opt.GetValue(options, null));
            }
            Console.WriteLine();

            try
            {
                OpenCvSharp.Window window = new OpenCvSharp.Window("demo");
                // create worker
                // (we are expecting run from bin folder)
                Worker worker = new Worker(
                    options.config_dir,
                    options.capturer_config,
                    options.license_dir);

                // here we trying camera open with opencv
                const int desiredFrameWidht  = 1280;
                const int desiredFrameHeight = 720;

                int camera_id = 0;

                // VideoCapture camera;
                OpenCvSharp.VideoCapture camera = new OpenCvSharp.VideoCapture();


                for (; camera_id < 10; ++camera_id)
                {
                    camera.Open(camera_id);
                    camera.Set(OpenCvSharp.CaptureProperty.FrameWidth, desiredFrameWidht);
                    camera.Set(OpenCvSharp.CaptureProperty.FrameHeight, desiredFrameHeight);
                    OpenCvSharp.Mat frame = new OpenCvSharp.Mat();
                    for (int i = 0; i < 10; i++)
                    {
                        frame = camera.RetrieveMat();
                    }

                    if (frame.Empty())
                    {
                        Console.WriteLine("webcam " + camera_id + " not opened");
                        camera.Release();
                    }
                    else
                    {
                        break;
                    }
                }

                for (;;)
                {
                    OpenCvSharp.Mat frame;

                    //camera.Grab();
                    frame = camera.RetrieveMat();

                    if (frame.Empty())
                    {
                        break;
                    }

                    // give a frame to the worker
                    worker.work(frame);

                    if (27 == (char)OpenCvSharp.Cv2.WaitKey(10))
                    {
                        camera.Release();
                        break;
                    }
                }

                // force free resources
                // otherwise licence error may occur
                // when create sdk object in next time
                worker.dispose();
            }
            catch (Exception e)
            {
                Console.WriteLine("! exception catched: '" + e + "' ... exiting");
                return(1);
            }
            Console.WriteLine("close program");
            return(0);
        }