The No-Code AI Philosophy
The essence of developing an AI object detection app without delving into server-side coding hinges on running the AI model directly within the user’s browser. This is achieved through a modern javascript Based ai library called Transformers.js. You can use pre-trained models hosted on Hugging Face. This combination reduces the dependency on backend infrastructure, which eliminates server management responsibilities and associated costs. Server-side code is not required. This allows the app to leverage the computational power of the user's device. Thus, enabling the processing of images without uploading them to a remote server.
Key components in the creation include:
- The Front-End Interface: Utilizes React and Next.js for a dynamic user experience.
- Browser-Based Processing: Running inference within the browser through Transformers.js.
- Background Model Inference: Employing web workers to avoid performance bottlenecks.
- Leveraging Free, Open-Source Models: Capitalizing on the community-driven resources on Hugging Face.
Step-by-step to implement your object detection app
Follow the given steps to implement your application:
1. Setting Up the Foundation: Next.js and React
Begin by initializing a Next.js application, which provides a solid framework for building dynamic web applications.
Make sure Node.js is properly installed. This Javascript framework integrates React components with server-side rendering which is suitable for modern apps.
npx create-next-app@latest
2. Integrating Transformers.js
Install Transformers.js, and add configurations to the Next.js setup. This will help your project easily use Javascript and other frontend AI Tools.
npm i @xenova/transformers
3. Building Blocks of User Interaction
Integrate a React Dropzone component, facilitating the image uploads. To increase usability, you can also customize the component and add in more code. Make sure to read the component documentation.
4. Deploying the AI Model
Initiate AI inference by using functions. It is recommended that the serverless app utilize Web Workers in order to avoid blocking the main thread, causing the page to not respond. You will also want to include a progress bar for loading so the user knows what is going on.
5. Visualizing the Results
Craft a React component designed to overlay bounding boxes around identified objects with labels on top of the images. You can use Javascript to customize the size, background color, and animation.
Understanding the Core Code Structure
The following code structure is used in Javascript (React, NextJS).
import {{ useState, useEffect, useRef, useCallback }} from 'react';
import Dropzone from '@/components/dropzone';
import Progress from '@/components/ui/progress';
export default function Home() {
const [result, setResult] = useState(null);
const [ready, setReady] = useState(false);
const [status, setStatus] = useState('');
const worker = useRef(null);
useEffect(() => {
if (worker.current) return;
worker.current = new Worker(new URL('../lib/worker.ts', import.meta.url), {{ type: 'module' }});
worker.current.addEventListener('message', onMessageReceived);
return () => worker.current.removeEventListener('message', onMessageReceived);
}, []);
const detect = useCallback(async (image: any) => { //image prop
if (!worker.current) return;
worker.current.postMessage({{ image }});
}, []);
const onMessageReceived = (e: any) => {{ //event listener
switch (e.data.status) {
case 'initiate':
setStatus('Initiate');
setReady(false);
break;
case 'progress':
setStatus('Progress');
setProgress(e.data.progress);
break;
case 'ready':
setStatus('Ready');
setReady(true);
break;
case 'complete':
setResult(e.data.result);
break;
default:
break;
}
}
return (
<div>
<h1 className="text-5xl font-bold">Object Detection</h1>
<h2 className="text-gray-500">With Hugging Face transformers</h2>
<Dropzone onDrop={{detect}}/>
{{/* preview */}}
</div>
)
}
This code sets up the drag and drop using const detect = useCallback(async (image: any)
so we can use the hook later in our app.