Create an image analysis app using Azure Computer Vision with React App
An AI project for Frontend Development

Search for a command to run...
An AI project for Frontend Development

No comments yet. Be the first to comment.
You might need to ask yourself too

Let's talk about usability and 3 common mistakes you might be making when building that product. Confusing 'Account Setup' with 'Onboarding': Your onboarding flow is meant to serve a primary purpose,

Last year during Google's Built-In AI hackathon, I built an experimental Chrome extension called Clarity Lens. The product was inspired by thinking about the 'hidden 16%', the portion of the world's p

If you've used Youtube long enough, you'd realize it gradually became better in understanding your needs. I'm sure you can relate to this - you think of something, next thing, you open Youtube and it's being advertised, or you come across it on your ...

Trade-offs, accessibility concerns, and what CSS alone can’t solve

In this tutorial, I will be creating a simple image recognition app in order to explain how to integrate Azure Computer Vision into client-side applications. You could also adapt the steps here to build similar applications using other services in Azure Computer Vision.
Prerequisites: Knowledge of React.js, An Azure account (you can get it free here), Github, Hosting service e.g. Netlify
npx create-react-app <APP_NAME>
cd <APP_NAME>
Delete unused default files in boilerplate e.g. App.test.js, App.css
Install Azure Computer Vision & MS Rest Azure npm package
npm i @azure/ms-rest-azure-js @azure/cognitiveservices-computervision
Log in to the Azure Portal
Create a new Computer Vision resource in Azure
Fill out the form with the required information

Click on 'Review+Create'
The screen below shows after the resource has been deployed
Click on manage keys to copy the API key and endpoint
In the react app root folder, create a new file called .env to store Azure access information in your local environment i.e. <APP_NAME>/.env
Open the .gitignore file and add .env to the list
Open the .env file and save the API key and endpoint here
// <APP_NAME>/.env
REACT_APP_apiKey = <YOUR_API_KEY>
REACT_APP_endPoint = <YOUR_ENDPOINT>
npm start to view the app in your browserCreate a new folder called components in ./src
Create a new file called AzureVisionService.js
In the new file, copy and paste the code below, it contains information for requesting from the API. The documentation to better understand this part of the code is here. The describeImage method used is one of the available Azure Vision API methods. It describes an image with complete English sentences. Another similar method is the analyzeImage method which extracts a rich set of visual features based on the image content.
// ./scr/components/AzureVisionService
// Importing the Azure SDK client libraries
import { ComputerVisionClient } from '@azure/cognitiveservices-computervision';
import { ApiKeyCredentials } from '@azure/ms-rest-js';
// Authentication requirements
const key = process.env.REACT_APP_apiKey;
const endpoint = process.env.REACT_APP_endPoint;
// Cognitive service features
const options = {
maxCandidates: 5,
language: "en"
};
// Describe Image from URL
export const computerVision = async (url) => {
// authenticate to Azure service
const computerVisionClient = new ComputerVisionClient(
new ApiKeyCredentials({ inHeader: { 'Ocp-Apim-Subscription-Key': key } }), endpoint);
// describe image using the describeImage method
const analysis = await computerVisionClient.describeImage(url, options)
.then((result) => {
console.log("The result is:");
console.log(result);
return { "URL": url, ...result};
})
.catch((err) => {
console.log("An error occurred:");
console.error(err);
alert(err + "Upload an image with a smaller size");
});
// all information about image
console.log("This is:" +analysis);
if(analysis === undefined){
return "There is something wrong with the image"
}
return { "URL": url, ...analysis};
}
// ./src/App.js
import React, { useState } from 'react';
import { computerVision } from './components/AzureVisionService';
function App() {
const [file, setFile] = useState(null);
const [analysis, setAnalysis] = useState(null);
const [processing, setProcessing] = useState(false);
const handleChange = (e) => {
setFile(e.target.value)
}
const onFileUrlEntered = () => {
// hold UI
setProcessing(true);
setAnalysis(null);
computerVision(file).then((item) => {
// reset state/form
setAnalysis(item);
setFile("");
setProcessing(false);
});
};
// Display JSON data in readable format
const PrettyPrintJson = (data) => {
return (<div><pre>{JSON.stringify(data, null, 2)}</pre></div>);
}
const DisplayResults = () => {
return (
<div>
<h2>Computer Vision Analysis</h2>
<div><img src={analysis.URL} height="200" border="1" alt={(analysis.description && analysis.description.captions && analysis.description.captions[0].text ? analysis.description.captions[0].text : "can't find caption")} /></div>
{PrettyPrintJson(analysis)}
</div>
)
};
//Describe image
const Describe = () => {
return (
<div>
<h1>Describe image</h1>
{!processing &&
<div>
<div>
<label>URL</label>
<input type="text" placeholder="Enter an image URL" size="50" onChange={handleChange}></input>
<button onClick={onFileUrlEntered}>Describe</button>
</div>
</div>
}
{processing && <div>Processing...</div>}
<hr />
{analysis && DisplayResults()}
</div>
)
}
return (
<div>
<Describe/>
</div>
);
}
export default App;
You could use any preferred hosting service like Azure Static Web Apps, Netlify, Vercel, Firebase or Heroku. For this tutorial, I'll make use of Netlify. One important thing to note is that whichever hosting platform you use, remember to add the REACT_APP_apiKey and the REACT_APP_endPoint to the host environment variables.
The first step here is to push the local react app to Github. Create a new empty repository in Github, you should get the screen shown below
In your local react app folder, open a new terminal. Then run the following commands
// In bash terminal
git init
git add .
git commit -m "first commit"
git branch -M main
git remote add origin https://github.com/<USERNAME>/<REPO_NAME>.git
git push -u origin main
Your code is now pushed to Github. Next, open your hosting website and import the existing project from Github.
In the build and deploy setup interface, click on 'Advanced Settings' then 'Add New Variable' to store the secret keys
Then deploy your site
Your site is deployed!!