Ejemplo n.º 1
0
        static Mat PutCaption(Mat image, CaptionRequest caption, System.Drawing.Font font)
        {
            //WriteLog($"PutCaption {caption?.Caption}");
            var size = image.Size();

            using (var bitmap = image.ToBitmap())
                using (var g = System.Drawing.Graphics.FromImage(bitmap))
                {
                    if (caption.CognitiveResult != null && caption.CognitiveResult.predictions != null)
                    {
                        foreach (var prediction in caption.CognitiveResult.predictions)
                        {
                            var rect = new System.Drawing.Rectangle(
                                RatioToAbs(prediction.boundingBox.left, size.Width),
                                RatioToAbs(prediction.boundingBox.top, size.Height),
                                RatioToAbs(prediction.boundingBox.width, size.Width),
                                RatioToAbs(prediction.boundingBox.height, size.Height)
                                );
                            g.DrawRectangle(System.Drawing.Pens.Azure, rect);
                            g.DrawString($"{prediction.tagName}({prediction.probability})", font, System.Drawing.Brushes.Azure,
                                         rect.Left,
                                         rect.Bottom + 2);
                        }
                    }

                    var captionSize = g.MeasureString(caption.Caption, font);
                    g.DrawString(caption.Caption, font, System.Drawing.Brushes.Azure,
                                 size.Width / 2 - captionSize.Width / 2,
                                 size.Height - captionSize.Height - 4);
                    // XXX なんかVideoCaputureしたMatに位置情報が入っている感じがする
                    var captionedImage = image.Clone();
                    bitmap.ToMat(captionedImage);
                    return(captionedImage);
                }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Initializes the ModuleClient and sets up the callback to receive
        /// messages containing temperature information
        /// </summary>
        static async Task Init()
        {
            MqttTransportSettings mqttSetting = new MqttTransportSettings(TransportType.Mqtt_Tcp_Only);

            ITransportSettings[] settings = { mqttSetting };

            // Open a connection to the Edge runtime
            ModuleClient ioTHubModuleClient = await ModuleClient.CreateFromEnvironmentAsync(settings);

            await ioTHubModuleClient.OpenAsync();

            WriteLog("IoT Hub module client initialized.");

            var connectionString = Environment.GetEnvironmentVariable("STORAGE_CONNECTION_STRING");
            var containerName    = Environment.GetEnvironmentVariable("RESULT_CONTAINER_NAME");
            var storageAccount   = CloudStorageAccount.Parse(connectionString);
            var cloudBlobClient  = storageAccount.CreateCloudBlobClient();

            cloudBlobContainer = cloudBlobClient.GetContainerReference(containerName);
            await cloudBlobContainer.CreateIfNotExistsAsync();

            // XXX 環境変数に
            videoInputStream = new VideoInputStream(0);
            videoInputStream.Start();

            var rtmpUri = Environment.GetEnvironmentVariable("RTMP_URI");

            // XXX Size
            videoLiveStream = new FfmpegRtmpVideoOutputStream(rtmpUri, fps, new Size(320, 240));
            videoLiveStream.Start();

            caption = new CaptionRequest {
                Caption = ""
            };
            captionFont = new System.Drawing.Font("noto", 15f);
            smallFont   = new System.Drawing.Font("noto", 10f);

            frameTimer          = new System.Timers.Timer(1.0 / fps);
            frameTimer.Elapsed += UpdateFrame;
            frameTimer.Start();

            http = new HttpRouter(new string[] { "http://+:80/" });
            http.Register("/caption", SetCaption);
            http.Register("/video/start", StartRecording);
            http.Register("/video/end", EndRecording);
            http.Register("/photo", TakePhoto);
            http.Register("/photo_with_caption", TakePhotoWithCaption);
            http.Start();

            await Task.CompletedTask;
        }
Ejemplo n.º 3
0
        //キャプション設定
        static Task SetCaption(HttpListenerContext context)
        {
            var request     = context.Request;
            var response    = context.Response;
            var requestJson = request.InputStream.ReadString();
            var requestObj  = JsonConvert.DeserializeObject <CaptionRequest>(requestJson);

            caption = requestObj;

            WriteLog($"SetCaption {requestJson}");
            response.StatusCode = (int)HttpStatusCode.NoContent;
            response.Close();

            return(Task.CompletedTask);
        }