Esempio n. 1
0
        private void OnTimerTick(object obj)
        {
            var start = DateTime.UtcNow;

            string fullImagePath    = null;
            string croppedImagePath = null;

            try
            {
                fullImagePath = _cameraFeedService.GetPhoto();

                string currentStatus = null;
                if (File.Exists(_options.StatusFilePath))
                {
                    currentStatus = File.ReadAllText(_options.StatusFilePath);
                }

                croppedImagePath = GetCroppedImagePath(fullImagePath);

                var newStatus = _imageClassifier.ClassifyImage(croppedImagePath);

                if (currentStatus != newStatus)
                {
                    _slackNotifier.SendNotification($"Status changed from {currentStatus} to {newStatus}",
                                                    fullImagePath);

                    File.WriteAllText(_options.StatusFilePath, newStatus);
                }
                successful++;

                if (successful == 1)
                {
                    Log.Logger.Information("Successfully processed the first image");
                }
            }
            catch (Exception e)
            {
                failed++;
                Serilog.Log.Error($"Error getting image and classifying it. {e.Message}\n{e.StackTrace}", e);
            }
            finally
            {
                var now           = DateTime.UtcNow;
                var nextRunTime   = start.Add(TimeSpan.FromSeconds(_options.IntervalSeconds));
                var delayTimespan = nextRunTime > now?nextRunTime.Subtract(now) : TimeSpan.Zero;

                _timer.Change(delayTimespan, Timeout.InfiniteTimeSpan);
            }

            SafeDeleteFile(fullImagePath);
            SafeDeleteFile(croppedImagePath);
        }
Esempio n. 2
0
        public Task <int> StartWorker(CancellationToken cancellationToken)
        {
            var taskCompletionSource = new TaskCompletionSource <int>();

            _thread = new Thread(() =>
            {
                try
                {
                    _classifier.LoadModel();
                }
                catch (ModelNotLoadedException exception)
                {
                    taskCompletionSource.SetException(exception);
                    return;
                }

                taskCompletionSource.SetResult(0);
                while (!_isCanceled)
                {
                    IImageClassificationTask classificationTask;
                    try
                    {
                        classificationTask = _queue.Receive(cancellationToken);
                    }
                    catch (OperationCanceledException)
                    {
                        _isCanceled = true;
                        return;
                    }

                    string label;
                    try
                    {
                        label = _classifier.ClassifyImage(classificationTask.Image);
                    }
                    catch (ModelEvaluationFailedException exception)
                    {
                        classificationTask.TaskCompletionSource.SetException(exception);
                        return;
                    }

                    classificationTask.TaskCompletionSource.SetResult(label);
                    _isCanceled = cancellationToken.IsCancellationRequested;
                }
            });
            _thread.Start();
            return(taskCompletionSource.Task);
        }