If you don't already have an Emotion API key, follow these README instructions to get your key.
For this workshop you will need to have .NET Core installed as well as any text editor- my favorite is Visual Studio Code.
Fork, clone, or download this repository. From the command line, navigate to the [YOUR INSTALL LOCATION]/CognitiveServicesDemo/dotnet
directory. First we need to bring in all of our project dependencies by entering dotnet restore
. To see the starting point for our project, run it by entering dotnet run
and navigating to http://localhost:5000/
in your browser.
cd [YOUR INSTALL LOCATION]/CognitiveServicesDemo/dotnet
dotnet restore
dotnet run
Note: From here on, all file paths are relative to
[YOUR INSTALL LOCATION]/CognitiveServicesDemo/dotnet/
- Uncomment
var results = await MakeRequest(submittedPhoto);
on line 29 ofControllers/HomeController.cs
- Copy the contents of
CodeSnippets/MakeRequest.txt
and paste them inControllers/HomeController.cs
under the "Helper Functions Here" comment on line 64 - Paste your Emotion API key from step 0 where it says
{Your subscription key here}
on the fourth line of theMakeRequest
function we just copied. It should look like thisclient.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", "1234");
where 1234 is your key - Put a breakpoint on line 29 where we store the results to a variable, and then run the project
- Navigate to
http://localhost:5000/
, enter the URL of a photo and hit enter
After you step over the breakpoint, you should see the JSON returned by Cognitive Services as a string stored in the results
variable. Press continue in your debugger to execute the rest of the function. Our page still hasn't changed and we won't see our results yet.
- Uncomment
var topEmotion = ParseResults(results);
on line 32 ofControllers/HomeController.cs
- Copy the contents of
CodeSnippets/ParseResponse.txt
and paste them inControllers/HomeController.cs
under theMakeRequest
function we pasted in step 1 - Replace the existing
Max()
function in theScores
class with the contents ofCodeSnippets/TopEmotion.txt
in theModels/EmotionSet.cs
file - Put a breakpoint on line 32 where we store the top emotion to a variable, and then run the project
- Navigate to
http://localhost:5000/
, enter the URL of a photo and hit enter
After you step over the breakpoint, you should see the top emotion from the photo stored in the topEmotion
variable. Press continue in your debugger to execute the rest of the function. Our page still hasn't changed and we won't see our results yet.
- Uncomment
var suggestion = MakeSuggestion(topEmotion);
on line 35 ofControllers/HomeController.cs
- Copy the contents of
CodeSnippets/MakeSuggestion.txt
and paste them inControllers/HomeController.cs
under theParseResponse
function we pasted in step 2 - Delete the temporary
return View();
on line 41 - Uncomment
return RedirectToAction("Results", suggestion);
on line 38 to display the results to our site - Run the project and navigate to
http://localhost:5000/
, enter the URL of a photo and hit enter
Now our site is complete. When you enter the URL of a photo you should see an image of someone from The Office as well as a description based on the top emotion in the photo. Try it with different photos to see what you get!
Feel free to customize your site however you want! Change the theme, modify the suggestion to be based off of the lowest emotion score, or add another API call to make your site even smarter.
Note: All static files are stored in the
wwwroot/
folder. To add your own images, put them in thewwwroot/images/
folder and change the links in theMakeSuggestion
function from step 3.