Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TeachableMachine.Calculate() always returns "Value cannot be null. (Parameter 's')" using C# .NET API #363

Open
xX-GeoffreyG-Xx opened this issue Jan 24, 2024 · 0 comments
Labels
bug Something isn't working

Comments

@xX-GeoffreyG-Xx
Copy link

xX-GeoffreyG-Xx commented Jan 24, 2024

I'm trying to do an image analysis with Teachable Machine using the .NET (C#) tools package I found here:
https://www.nuget.org/packages/TeachableMachine
Which I was able to successfully install the package in VS2019 (using the Nuget Package Manager Console).

So then I tried to create a "C# Console" project that calls the TM API. The example provided (from the link above) for calling the TM API is super simple:

JArray result = TeachableMachine.Calculate("https://teachablemachine.withgoogle.com/models/modelID", "path_to_image.jpg");
Console.WriteLine(result.ToString());

But when I try to call Calculate() this way from my C# project, I ALWAYS get an exception with the message:
Value cannot be null. (Parameter 's')

I've tried calling this a number of different ways, including:

  1. Using the public HTTPS path to my TM model.
  2. Using the path to a local copy of my model directory.
  3. Using a web link to a public image.
  4. Using the path to a local image.

But, in all cases, I just get the same error message of: Value cannot be null. (Parameter 's'), which makes me think this is a bug and not something I'm doing wrong.

To Reproduce this Problem:

  1. Open Visual Studio 2019 (or 2022) and Create a new C# Console project and name it "TestTM."
  2. Once the new Console project has been created, go to Tools menu -> NuGet Package Manager -> Package Manager Console
  3. From the Package Manager Console, run the command: Install-Package TeachableMachine (The package will be added to the project dependencies.)
  4. Get any JPG image and name it "Sample.jpg." Then add it to the C# project by right clicking the TestTM project in the Solution Explorer -> Add -> Existing Item. In the file picker dialog, be sure to set the file types pulldown to "All Files" and then navigate to and choose the "Sample.jpg" file to add it to the C# project.
  5. In the Solution Explorer -> right click on the "Sample.jpg" -> choose Properties. In the properties, for the "Copy to Output Folder" property, choose the "Copy if Newer" option.
  6. Copy the following code into the Program.cs file, replacing any code existing in the file already:
using Newtonsoft.Json.Linq;
using System;
using System.IO;
using Teachable_Machine;

namespace TestTM
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                FileInfo fi = new FileInfo("Sample.jpg");
                DirectoryInfo di = new DirectoryInfo("ai_img_model");

                if (fi.Exists)
                {
                    if (di.Exists)
                    {
                        JArray result = TeachableMachine.Calculate("https://teachablemachine.withgoogle.com/models/bXy2kDNi", "Sample.jpg");
                        //JArray result = TeachableMachine.Calculate("https://teachablemachine.withgoogle.com/models/bXy2kDNi", "https://media.cnn.com/api/v1/images/stellar/prod/1434.jpg");
                        //JArray result = TeachableMachine.Calculate("https://teachablemachine.withgoogle.com/models/bXy2kDNi", fi.FullName);
                        //JArray result = TeachableMachine.Calculate(di.FullName, fi.FullName);
                        Console.WriteLine(result.ToString());
                    }
                    else
                    {
                        Console.WriteLine("Could not find Model directory: \n" + di.FullName);
                    }
                }
                else
                {
                    Console.WriteLine("Could not find image file: \n" + fi.FullName);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Exception: \n" + ex.Message);
            }
        }  // End of:  static void Main(string[] args)
    }  // End of:  class Program
}  // End of:  namespace TestTM
  1. Then compile and push the play button to run this code.

Resulting Behavior: An exception is caught in the Console with the message: Value cannot be null. (Parameter 's')
(I've attached the VS example files to reproduce this error.)
TestTM.zip

Expected Behavior: For the image to be processed and for the results to be returned and output to the console.

Test Environment:

  • Windows 10 Pro 64-bit, Build: 19045.3930
  • Fails in Both VS2019 x86 and VS2022 x64
  • Fails with .NET Framework 5.0, 6.0, and 8.0 and .NET Core 3.0
@xX-GeoffreyG-Xx xX-GeoffreyG-Xx added the bug Something isn't working label Jan 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant