Troubleshooting DLIBDOTNET and FaceRecognitionDotNet CUDA libraries

I’ve been building a Facial Recognition C# application leveraging the DLIBDOTNET and FaceRecognitionDotNet libraries. I tested a variety of free libraries and this is the most accurate at reliably predicating/recognizing faces. It also does a decent job of differentiating people of color.

There are two libraries, the regular “FaceRecognitionDotNet” library and the CUDA versions. Although it isn’t necessary to have CUDA to use this library, the recognition will be much faster if you leverage the CUDA version.

It’s been a process to get the CUDA version of these two libraries to work but it’s been worth it. Here’s a guide that could help you troubleshoot if you are having difficulties getting this to work.

What you’ll need & Steps

Although not necessary, the setup may work more smoothly if you follow these steps in this order.

  1. 64 bit Intel CPU. Visit the GitHub repository for current supported CPUs.
  2. Nvidia CUDA supported GPU
  3. Visual Studios 2019 & NET CORE 2.0+. I initially wrote my application using the old school Visual Studios Forms. After banging my head against the wall, I did some research and found out that WPF type applications have more native support for GPU applications. After switching to .NET Core 2.0 and a WPF application, I finally got the CUDA version of this library to work.
  4. Nvidia CUDA development kit. The CUDA Nvidia development kit will detect your graphics card and will most likely install an older graphics card driver. Let it install the older version otherwise you will get an error message during the install. After the install completes, reinstall the most current Nvidia drivers for your graphics card. It’s also important to install Visual Studios before the CUDA development kit because the installation package will attempt to install integration tools for Visual Studios. After installation is complete, make sure CUDA was installed properly by running one or more of the sample projects. You shouldn’t receive error messages after building and running the sample project. This is how you know it was installed properly.
  5. NVIDIA cuDNN – Follow the instructions on NVIDIA’s website to install cuDNN. You are simply just copying files into the CUDA installation directory. Yes, there isn’t anymore to the cuDNN installation. I used version 10.0. I also tested with newer version of CUDA however these tests were unsuccessful so I decided to stick with 10.0. Remember, you need to install the CUDA development kit before attempting to install cuDNN.
  6. You will also need at least 25 Gigs free space on the drive that has Windows. Visual Studios, CUDA and NVIDIA graphic drivers are bulky.

This is using a barebones Visual Studios installation. The DLIBDOTNET notes on NuGet mention that you need these files. Visual C++ 2017 Redistributable Package, cublas64_92.dll, cudnn64_7.dll, curand64_92.dll and cusolver64_92.dll. I attempted to simply just install the latest Visual C++ 2019 Redistributable and that itself didn’t work so I installed a few other C++ and .NET CORE components. See below for my barebones install of Visual Studios.

Individual Components

.NET

  • .NET Core 2.1 LTS Runtime
  • .NET Core 3.1 LTS Runtime
  • .NET Core SDK
  • NuGet package manager
  • .NET Framework 4.6.1 targeting pack
  • .NET Framework 4.7.2 SDK
  • .NET Framework 4.7.2 targeting pack
  • .NET Framework 4.8 SDK
  • .NET Native
  • .NET Portable Library targeting pack
  • Development Tools plus .NET Core 2.1

Cloud, database, and server

  • CLR data types for SQL Server

Code tools

  • C# and Visual Basic Roslyn compliers
  • C++ 2019 Redistributable MSMs
  • C++ 2019 Redistributable Update
  • C++ Cmake tools for Windows
  • MSBuild
  • MSVC v142 – VS 2019 C++ x64/x86 build tools (v14.25)

Debugging and testing

  • .NET profiling tools
  • C++ AddressSanitizer (Experimental)
  • C++ profiling tools
  • Just-in-Time debugger

Development activities

  • C# and Visual Basic
  • C++ core features
  • F# language support
  • IntelliCode

Games and Graphics

  • Graphics debugger and GPU profiler for DirectX

SDK, libraries and frameworks

  • C++ ATL for latest v142 build tools (x86 & x64)
  • Windows 10 SDK(10.0.18362.0)

After you are done with the above steps, create a simple Windows WPF .NET CORE application and run call the “TryGetDriverVersion” function. Sample code below. If everything was installed properly, you should see an output like this

“***HELLO CUDA Version:10010”

using DlibDotNet;

//C# example code to check if DLIB Cuda is installed properly. Make sure you are using the CUDA version from NuGet

 Cuda.TryGetDriverVersion(out int version);
         Debug.WriteLine("***************************************HELLO CUDA Version:" + version.ToString()) ;

I’ve been able to get this to work with a few different CUDA supported GPUs following the instructions above. Enjoying coding.

Automatically highlight selected Excel row

Here’s a simple script that makes it easier to visually scan lengthy Excel spreadsheets. If you spend a lot of time in Excel spreadsheets, you’ll find this useful.

What you’ll need

  • .XLSM Excel workbook
  • Macros enabled
Excel spreadsheet with color highlighting
Excel VBA script that automatically highlights current selected row
Excel VBA script that automatically highlights current selected row

Although I like this above script, you do have to hard code a few values. Here’s another Exel video that is more straight forward. This method also highlights the columns. This video, assumes you already know how to enable the developer tab and create formulas however.

'Visual Basic for applications code
Private Sub Worksheet_SelectionChange(ByVal Target As Range)

    With ThisWorkbook.Names("CurrentRow")
    .Name = "CurrentRow"
    .RefersToR1C1 = "=" & ActiveCell.Row
'above "&" should be the ampersand symbol. 
    End With

End Sub


How to mute your computer microphone with a single keyboard key

With everyone working remote these days, it is nice to follow phone/video conference etiquette and mute your microphone if you are not the speaker. I was surprised to learn that there isn’t a dedicated mute key on most keyboards to mute your microphone. The mute button on my laptop and keyboard only mute the speakers. So here’s a script I put together that mutes your microphone. It relies on registry values so it is more dependable than hacking your system by emulating send keys values. This works best with keyboards or mice with customization capabilities. I’m using a Logitech keyboard. Alternatively, you can also use keyboard shortcuts.

What you’ll need before starting.
* Administrative or elevated privileges to your computer
* A little determination

Step 1: Identify your microphone registry values. Yes, this isn’t for the faint of heart.
Step 2: Modify Powershell script below. Replace “Placeholder” with your register value. Save file as “mute.ps1”.
Step 3. Create a simple .bat script that calls the PowerShell script with the proper execution permissions. (note, you may also need to run this code in an administrative PowerShell window. “set-executionpolicy unrestricted” if the code is still not executing. Call the file “mute.bat”.
Step 4: Update Folder ownership within Regedit for the folder containing the “microphone array” item. Change it from “System” to all users and applications.
Step 5: Customize keyboard key to reference this “mute.bat” script when the button is pressed.

Note, I implemented this on Windows 10 Pro. Version 1903. The script does modify a single Registry value so you may want get buy-in from your IT department prior to incorporating this.

Code for step 2: Here’s the Powershell code. Note, you will only have to change the “PLACEHOLDER” value.

$micStatus="Enabled"
$Key = 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\MMDevices\Audio\Capture\{PLACEHOLDER}'

if(Test-Path $key){
	$item = Get-ItemProperty -path $Key -Name DeviceState
	$item.DeviceState
}
else
{
	Write-Host 'path not found'
}
if( $item.DeviceState -Match "0")
        {
 
        Write-Host 'Mic Enabled'
                Set-ItemProperty -Path $Key -Name "DeviceState" -Value '1' -Force | Out-Null
				$micStatus="Enabled"
        }
else
 
        {
        Write-Host 'Mic Disabled'
 
                Set-ItemProperty -Path $Key -Name "DeviceState" -Value '10000001' -Force | Out-Null
				$micStatus="Disabled"
 
        }
		
Add-Type -AssemblyName System.Windows.Forms
$global:balmsg = New-Object System.Windows.Forms.NotifyIcon
$path = (Get-Process -id $pid).Path
$balmsg.Icon = [System.Drawing.Icon]::ExtractAssociatedIcon($path)
$balmsg.BalloonTipIcon = [System.Windows.Forms.ToolTipIcon]::Warning
$balmsg.BalloonTipText = 'Mic status of apps not updated'
$balmsg.BalloonTipTitle = "Microphone $micStatus"
$balmsg.Visible = $true
$balmsg.ShowBalloonTip(50000)

“mute.bat” code for Step 3

powershell.exe -executionpolicy bypass -file "C:\Users\C\Documents\work\mute.ps1"

Step: 4 – Error message you will see if you don’t update permissions.

You may likely get the above error message when you attempt to run the PowerShell script directly. In this case, you will need to update the Owner within the registry. After doing this your script should work properly.

Before mapping the mute.bat script to the keyboard, attempt running the script by double clicking the .bat file. After the script executes, you should see a notification message at the bottom right of Windows. You can also see the notification if you expand the notifications Window. Screenshot below. Navigate to sound settings to ensure the microphone was indeed disabled.

screenshot of microphone disabled notification
Screenshot of microphone disabled notification
screenshot of Windows 10 sound menu
Navigate to Sound setting by right clicking on the speaker icon in your toolbar then selecting “Open Sound settings”
Screenshot of Microphone when it still enabled.
Windows 10 Screenshot of Microphone disabled
Screenshot of Microphone when it is disabled. Notice, the input device is missing.

This script can also be retro fitted to disable web cams with a single keyboard press. You’ll just need to identify the registry values for the webcam. Dr Scripto also has an alternative method for disabling the webcam.

Outlook script to export /copy email data

Have you tried selecting an email in Outlook and copying it to learn that it copies the entire .msg file instead of just the raw data such as the subject line or just the sender? Perhaps you want to create a log of emails from certain senders so you can categorize the emails in Excel or just have a printed record. Here’s a handy visual basic for application(VBA) script that does this. I tested this with a folder of over 2,000 emails and it works well. It will likely take a while to execute if select a folder with a large number of emails. To use this code, you’ll have to create a macro in Outlook. Note, the output will be sent to the immediate window. This is similar to the debugging window in Outlook.

Sub PrintSubjectLineForSelectedFolder()
  
Dim olApp As Outlook.Application
Dim objNS As Outlook.NameSpace
Dim olFolder As Outlook.MAPIFolder
Dim msg As Object
 Dim myItems As Outlook.Items

Set olApp = Outlook.Application
Set objNS = olApp.GetNamespace("MAPI")
Set olFolder = objNS.GetDefaultFolder(olFolderInbox)

    Dim objFolderSelected As Folder
     Set objFolderSelected = objNS.PickFolder
    If TypeName(objFolderSelected) <> "Nothing" Then
    
        Debug.Print objFolderSelected
        Set olFolder = objFolderSelected
        'Set olFolder = olFolder.Folders("Test Results")' can also be hardcoded
      
        For Each msg In olFolder.Items

'Debug.Print TypeName(msg)
    If msg Is Nothing Then
'If varValue = Nothing Then

        Exit For
   
	Else

    If Not msg Is Nothing Then
 
        Debug.Print msg.Subject
        
       
   End If
   
   End If
   
Next

    Else
    
        Debug.Print vbCr &amp; "User selected Cancel No Folder selected"
    End If

    Set objFolderSelected = Nothing
    Set objNS = Nothing

End Sub