{"id":2364,"date":"2025-01-12T09:57:25","date_gmt":"2025-01-12T08:57:25","guid":{"rendered":"https:\/\/alax.info\/blog\/?p=2364"},"modified":"2025-04-04T15:32:02","modified_gmt":"2025-04-04T14:32:02","slug":"demo-webcam-with-yolov4-object-detection-via-microsoft-media-foundation-and-directml","status":"publish","type":"post","link":"https:\/\/alax.info\/blog\/2364","title":{"rendered":"Demo: Webcam with YOLOv4 object detection via Microsoft Media Foundation and DirectML"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">As we see announcements for more and more powerful NPUs and AI support in newer consumer hardware, here is a new demo with Artificial Intelligence attached to a good old sample camera application based on the Microsoft Media Foundation <a href=\"https:\/\/learn.microsoft.com\/en-us\/windows\/win32\/api\/mfcaptureengine\/\">Capture Engine API<\/a>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This demo is a blend of several technologies: <a href=\"https:\/\/learn.microsoft.com\/en-us\/windows\/win32\/medfound\/microsoft-media-foundation-sdk\">Media Foundation<\/a>, <a href=\"https:\/\/learn.microsoft.com\/en-us\/windows\/win32\/direct3d11\/atoc-dx-graphics-direct3d-11\">Direct3D 11<\/a>, <a href=\"https:\/\/learn.microsoft.com\/en-us\/windows\/win32\/direct3d12\/direct3d-12-graphics\">Direct3D 12<\/a>, <a href=\"https:\/\/learn.microsoft.com\/en-us\/windows\/ai\/directml\/dml\">DirectML<\/a> (of <a href=\"https:\/\/learn.microsoft.com\/en-us\/windows\/ai\/\">Windows AI<\/a>), and <a href=\"https:\/\/learn.microsoft.com\/en-us\/windows\/win32\/direct2d\/direct2d-portal\">Direct2D<\/a>, all at once!<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The video data never leaves the video hardware. We have the video captured via Media Foundation, internally processed with the Windows Camera Frame Server service, and then shared through the Capture Engine API (this perhaps still does not really work as &#8220;shared access&#8221; but we are used to this). The data then is converted from Direct3D 11 to Direct3D 12 and, further, to a DirectML tensor. From there, we run the YOLOv4 model (see <a href=\"https:\/\/learn.microsoft.com\/en-us\/samples\/microsoft\/directml\/directml-yolov4\/\">DirectML YOLOv4 sample<\/a>) on the data while the video smoothly goes to preview presentation. As soon as the Machine Learning model produces predictions, we pass the data to a Direct2D overlay, which attaches a mask onto the video flow going to the presentation.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"800\" height=\"456\" src=\"https:\/\/alax.info\/blog\/wp-content\/uploads\/2025\/01\/2025-01-12-10-800x456.png\" alt=\"\" class=\"wp-image-2365\" srcset=\"https:\/\/alax.info\/blog\/wp-content\/uploads\/2025\/01\/2025-01-12-10-800x456.png 800w, https:\/\/alax.info\/blog\/wp-content\/uploads\/2025\/01\/2025-01-12-10-320x183.png 320w, https:\/\/alax.info\/blog\/wp-content\/uploads\/2025\/01\/2025-01-12-10-768x438.png 768w, https:\/\/alax.info\/blog\/wp-content\/uploads\/2025\/01\/2025-01-12-10-1536x876.png 1536w, https:\/\/alax.info\/blog\/wp-content\/uploads\/2025\/01\/2025-01-12-10-2048x1168.png 2048w, https:\/\/alax.info\/blog\/wp-content\/uploads\/2025\/01\/2025-01-12-10-600x342.png 600w, https:\/\/alax.info\/blog\/wp-content\/uploads\/2025\/01\/2025-01-12-10-1040x593.png 1040w\" sizes=\"auto, (max-width: 800px) 100vw, 800px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">The compiled application&#8217;s only dependency is the <a href=\"https:\/\/altushost-swe.dl.sourceforge.net\/project\/darknet-yolo.mirror\/darknet_yolo_v4_pre\/yolov4.weights?viasf=1\">yolov4.weights<\/a> file (you need to download it separately and place in the same directory as the executable, there is an <a href=\"https:\/\/github.com\/AlexeyAB\/darknet\/releases\/download\/darknet_yolo_v3_optimal\/yolov4.weights\">alternative download link<\/a>); the rest is the Windows API and the application itself. As the work is handled by the GPU, you will see Task Manager showing GPU load close to 100% while CPU load is minimal.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The model is trained on 608 by 608 pixel images, and so is the model input and rescaling. That is, the resolution of the camera video does not matter much, except that the overlay mask is more accurate with higher resolution video\u2014the overlay is burnt into the video stream itself. To show the recent progress in hardware capabilities, here are some new numbers:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Intel(R) UHD Graphics 770 integrated in the <a href=\"https:\/\/www.intel.com\/content\/www\/us\/en\/products\/sku\/230496\/intel-core-i913900k-processor-36m-cache-up-to-5-80-ghz\/specifications.html?wapkw=i9-13900k\">Intel\u00ae Core\u2122 i9-13900K<\/a> CPU achieves around 2.5 fps for real-time video processing.<\/li>\n\n\n\n<li>AMD Radeon 780M Graphics integrated in the <a href=\"https:\/\/www.amd.com\/en\/products\/processors\/laptop\/ryzen\/7000-series\/amd-ryzen-9-7940hs.html\">AMD Ryzen 9 7940HS<\/a> CPU runs the processing up to four times faster, achieving around 8.0 fps.<\/li>\n\n\n\n<li>[2025-04-04 Update] NVIDIA GeForce RTX 2060 SUPER is capable to process video at around 24 fps.<\/li>\n<\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">Intel&#8217;s CPU is still the good one but its video is not so, and it&#8217;s GPU at work now. The AMD Ryzen 9 7940HS includes AMD&#8217;s dedicated AI XDNA Technology. The NPU performance is rated at <a href=\"https:\/\/www.amd.com\/en\/products\/processors\/laptop\/ryzen\/7000-series\/amd-ryzen-9-7940hs.html\">up to 10 TOPS<\/a> (Tera Operations Per Second), upcoming <a href=\"https:\/\/support.microsoft.com\/en-us\/topic\/copilot-pc-hardware-requirements-35782169-6eab-4d63-a5c5-c498c3037364\">Copilot+ PCs are expected to have 40+ TOPS<\/a>, so the new hardware should be a good fit for this class of applications.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Have fun!<\/p>\n\n\n\n<div class=\"wp-block-file\"><a id=\"wp-block-file--media-1e95ab09-8353-4556-a122-3ad55d119d93\" href=\"https:\/\/alax.info\/blog\/wp-content\/uploads\/2025\/01\/Capture-20250112-f43fac8927726b3d389c12e7499ba3681259cc0c.zip\">Capture-20250112-f43fac8927726b3d389c12e7499ba3681259cc0c<\/a><a href=\"https:\/\/alax.info\/blog\/wp-content\/uploads\/2025\/01\/Capture-20250112-f43fac8927726b3d389c12e7499ba3681259cc0c.zip\" class=\"wp-block-file__button wp-element-button\" download aria-describedby=\"wp-block-file--media-1e95ab09-8353-4556-a122-3ad55d119d93\">Download<\/a><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">Minimal Requirements:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Windows 11 x64 &#8212; DirectML might run on Windows 10 as well, but for the simplicity of the demo it requires Windows 11; Windows 11 ARM64 is of certain interest as well, but I don&#8217;t have hardware at hand to check)<\/li>\n\n\n\n<li>DirectX 12 compatible video adapter, which is used across APIs &#8212; for the simplicity of the demo there is no option to offload to another GPU or NPU<\/li>\n\n\n\n<li>The <em>.weights<\/em> file downloaded and placed as mentioned, besides the application in the archive above<\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>As we see announcements for more and more powerful NPUs and AI support in newer consumer hardware, here is a new demo with Artificial Intelligence attached to a good old sample camera application based on the Microsoft Media Foundation Capture Engine API. This demo is a blend of several technologies: Media Foundation, Direct3D 11, Direct3D&hellip; <\/p>\n<p><a class=\"moretag\" href=\"https:\/\/alax.info\/blog\/2364\">Read the full article<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[38,524,544,606,604,424,486,495,70],"class_list":["post-2364","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-c","tag-direct2d","tag-direct3d-11","tag-direct3d-12","tag-directml","tag-media-foundation","tag-video","tag-webcam","tag-winapi"],"_links":{"self":[{"href":"https:\/\/alax.info\/blog\/wp-json\/wp\/v2\/posts\/2364","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/alax.info\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/alax.info\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/alax.info\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/alax.info\/blog\/wp-json\/wp\/v2\/comments?post=2364"}],"version-history":[{"count":0,"href":"https:\/\/alax.info\/blog\/wp-json\/wp\/v2\/posts\/2364\/revisions"}],"wp:attachment":[{"href":"https:\/\/alax.info\/blog\/wp-json\/wp\/v2\/media?parent=2364"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/alax.info\/blog\/wp-json\/wp\/v2\/categories?post=2364"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/alax.info\/blog\/wp-json\/wp\/v2\/tags?post=2364"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}