Windows 10 IoT Core image capture, upload and processing

One of my students wanted to do some image processing so to help her project along I am writing a series posts about capturing images on a Windows 10 IoT Core device. I’ll cover initiating the capturing of an image, uploading the image too Azure Blob storage, uploading the image to Azure blob storage associated with an Azure IoT Hub, then processing the images with the Azure Custom Vision Service.

USB Camera test rig

First step was to capture an image from a USB web camera and store it in the local file system.

/*
    Copyright ® 2019 Feb devMobile Software, All Rights Reserved

    MIT License
…
*/
namespace devMobile.Windows10IotCore.IoT.PhotoTimer
{
	using System;
	using System.Threading;
	using Windows.ApplicationModel.Background;
	using Windows.Foundation.Diagnostics;
	using Windows.Media.Capture;
	using Windows.Media.MediaProperties;
	using Windows.Storage;

	public sealed class StartupTask : IBackgroundTask
	{
		private readonly LoggingChannel logging = new LoggingChannel("devMobile Timer Photo demo", null, new Guid("4bd2826e-54a1-4ba9-bf63-92b73ea1ac4a"));
		private BackgroundTaskDeferral backgroundTaskDeferral = null;
		private Timer ImageUpdatetimer;
		private readonly TimeSpan ImageUpdateDueDefault = new TimeSpan(0, 0, 15);
		private readonly TimeSpan ImageUpdatePeriodDefault = new TimeSpan(0, 5, 0);
		private MediaCapture mediaCapture;
		private const string ImageFilenameFormat = "Image{0:yyMMddhhmmss}.jpg";

		public void Run(IBackgroundTaskInstance taskInstance)
		{
			LoggingFields startupInformation = new LoggingFields();

			this.logging.LogEvent("Application starting");

			try
			{
				mediaCapture = new MediaCapture();
				mediaCapture.InitializeAsync().AsTask().Wait();

				ImageUpdatetimer = new Timer(ImageUpdateTimerCallback, null, ImageUpdateDueDefault, ImageUpdatePeriodDefault);

			}
			catch (Exception ex)
			{
				this.logging.LogMessage("Camera configuration failed " + ex.Message, LoggingLevel.Error);
				return;
			}

			startupInformation.AddString("PrimaryUse", mediaCapture.VideoDeviceController.PrimaryUse.ToString());
			startupInformation.AddTimeSpan("Due", ImageUpdateDueDefault);
			startupInformation.AddTimeSpan("Period", ImageUpdatePeriodDefault);

			this.logging.LogEvent("Application started", startupInformation);

			//enable task to continue running in background
			backgroundTaskDeferral = taskInstance.GetDeferral();
		}

		private void ImageUpdateTimerCallback(object state)
		{
			DateTime currentTime = DateTime.UtcNow;

			try
			{
				string filename = string.Format(ImageFilenameFormat, currentTime);

				IStorageFile photoFile = KnownFolders.PicturesLibrary.CreateFileAsync(filename, CreationCollisionOption.ReplaceExisting).AsTask().Result;
				ImageEncodingProperties imageProperties = ImageEncodingProperties.CreateJpeg();
				mediaCapture.CapturePhotoToStorageFileAsync(imageProperties, photoFile).AsTask().Wait();

				LoggingFields imageInformation = new LoggingFields();
				imageInformation.AddDateTime("TakenAtUTC", currentTime);
				imageInformation.AddString("Filename", filename);
				imageInformation.AddString("Path", photoFile.Path);
				this.logging.LogEvent("Image saved to storage", imageInformation);
			}
			catch (Exception ex)
			{
				this.logging.LogMessage("Image capture or save to local storage failed " + ex.Message, LoggingLevel.Error);
			}
		}
	}
}

To get my camera to work I had to enable “pictures library”, “microphone” and “webcam” in the capabilities section of the application manifest.

As the application starts up and captures images it logs information to the Windows 10 IoT Core ETW logging

Windows 10 IoT Core Portal ETW Logging
Camera images in \user folders\pictures

The code is available on GitHub and is a bit of a work in progress.