DEV Community

Raji moshood
Raji moshood

Posted on

How to Build an AI-Powered Image Recognition App from Scratch

AI-Powered Image Recognition App

AI-Powered Image Recognition App

With advancements in computer vision and deep learning, building an AI-powered image recognition app is easier than ever. In this guide, we’ll create an app that can recognize objects in images using OpenCV, TensorFlow, and React.


πŸ”Ή Overview of the Tech Stack

AI-Powered Image Recognition App

βœ… OpenCV – For image processing and preprocessing.

βœ… TensorFlow – For deep learning-based image recognition.

βœ… React.js – For building the front-end UI.

βœ… FastAPI/Flask – For serving the AI model as an API.


To recognize images, we need a deep learning model. We have two options:

1️⃣ Use a Pre-Trained Model (Recommended for beginners)

2️⃣ Train a Custom Model (For advanced use cases)

βœ… Option 1: Use a Pre-Trained Model (MobileNet, ResNet, or YOLO)

TensorFlow provides pre-trained models that can recognize thousands of objects.

import tensorflow as tf
import numpy as np
from tensorflow.keras.applications.mobilenet_v2 import MobileNetV2, preprocess_input, decode_predictions
from PIL import Image

# Load pre-trained MobileNetV2 model
model = MobileNetV2(weights="imagenet")

# Load and preprocess image
image = Image.open("test.jpg").resize((224, 224))
image_array = np.array(image)
image_array = np.expand_dims(image_array, axis=0)
image_array = preprocess_input(image_array)

# Predict the object in the image
predictions = model.predict(image_array)
decoded_predictions = decode_predictions(predictions, top=3)[0]

for pred in decoded_predictions:
    print(f"{pred[1]}: {pred[2]*100:.2f}%")
Enter fullscreen mode Exit fullscreen mode

βœ… This model can classify common objects like cars, animals, and furniture!


βœ… Option 2: Train a Custom Model

If you need a custom model for face recognition, medical imaging, or specialized objects, train your own dataset using TensorFlow and OpenCV.

Example: Training an Image Classifier with TensorFlow

import tensorflow as tf
from tensorflow.keras import layers, models

# Define model architecture
model = models.Sequential([
    layers.Conv2D(32, (3, 3), activation='relu', input_shape=(128, 128, 3)),
    layers.MaxPooling2D((2, 2)),
    layers.Conv2D(64, (3, 3), activation='relu'),
    layers.MaxPooling2D((2, 2)),
    layers.Flatten(),
    layers.Dense(128, activation='relu'),
    layers.Dense(10, activation='softmax')  # 10 classes
])

# Compile the model
model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])

# Train on custom dataset
model.fit(train_images, train_labels, epochs=10, validation_data=(test_images, test_labels))
Enter fullscreen mode Exit fullscreen mode

βœ… Once trained, save the model:

model.save("image_recognition_model.h5")
Enter fullscreen mode Exit fullscreen mode

πŸ”Ή Step 2: Deploy the Model as an API (FastAPI or Flask)

To use the AI model in a web app, we need an API that takes an image as input and returns predictions.

βœ… Using FastAPI for Model Inference

from fastapi import FastAPI, UploadFile, File
import tensorflow as tf
import numpy as np
from PIL import Image

app = FastAPI()
model = tf.keras.models.load_model("image_recognition_model.h5")

@app.post("/predict")
async def predict_image(file: UploadFile = File(...)):
    image = Image.open(file.file).resize((128, 128))
    image_array = np.expand_dims(np.array(image), axis=0) / 255.0
    predictions = model.predict(image_array)
    return {"predictions": predictions.tolist()}
Enter fullscreen mode Exit fullscreen mode

βœ… Run the API:

uvicorn app:app --reload
Enter fullscreen mode Exit fullscreen mode

This creates an endpoint (/predict) where the front end can send image files for recognition.


πŸ”Ή Step 3: Build the Front-End with React

βœ… Set Up React App

npx create-react-app image-recognition-app
cd image-recognition-app
npm install axios
Enter fullscreen mode Exit fullscreen mode

βœ… Create an Upload Form (App.js)

import React, { useState } from "react";
import axios from "axios";

function App() {
  const [selectedFile, setSelectedFile] = useState(null);
  const [prediction, setPrediction] = useState("");

  const uploadImage = async () => {
    const formData = new FormData();
    formData.append("file", selectedFile);

    const response = await axios.post(
      "http://127.0.0.1:8000/predict",
      formData
    );
    setPrediction(response.data.predictions);
  };

  return (
    <div>
      <h1>AI-Powered Image Recognition</h1>
      <input type="file" onChange={(e) => setSelectedFile(e.target.files[0])} />
      <button onClick={uploadImage}>Predict</button>
      {prediction && <h2>Prediction: {prediction}</h2>}
    </div>
  );
}

export default App;
Enter fullscreen mode Exit fullscreen mode

πŸ”Ή Step 4: Run and Test the App

βœ… Start the back-end server (FastAPI/Flask):

uvicorn app:app --reload
Enter fullscreen mode Exit fullscreen mode

βœ… Start the React app:

npm start
Enter fullscreen mode Exit fullscreen mode

βœ… Upload an image, and the AI model will predict the object in it! πŸŽ‰


πŸ”Ή Step 5: Deploy the App

To make the app available online:

βœ… Back-End (API):

  • Deploy on AWS Lambda, Google Cloud Functions, or Azure Functions.
  • Use Docker + Kubernetes for scalability.

βœ… Front-End (React App):

  • Deploy on Vercel, Netlify, or Firebase Hosting.

Example Dockerfile for Deployment:

FROM python:3.9
WORKDIR /app
COPY requirements.txt .
RUN pip install -r requirements.txt
COPY . .
CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "8000"]
Enter fullscreen mode Exit fullscreen mode

βœ… Deploy with AWS ECS or Google Cloud Run! πŸš€


πŸ”Ή Summary: Key Takeaways

βœ… Use TensorFlow & OpenCV for image processing.

βœ… Deploy AI models with FastAPI & Flask.

βœ… Build a simple React UI for user interaction.

βœ… Deploy on cloud platforms for scalability.

🎯 Now you have a fully functional AI-powered image recognition app! πŸš€

AI #MachineLearning #ImageRecognition #TensorFlow #React #FastAPI

Heroku

Build apps, not infrastructure.

Dealing with servers, hardware, and infrastructure can take up your valuable time. Discover the benefits of Heroku, the PaaS of choice for developers since 2007.

Visit Site

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

Best practices for optimal infrastructure performance with Magento

Running a Magento store? Struggling with performance bottlenecks? Join us and get actionable insights and real-world strategies to keep your store fast and reliable.

Tune in to the full event

DEV is partnering to bring live events to the community. Join us or dismiss this billboard if you're not interested. ❀️