Step-by-Step Guide: Automated Quality Inspection System Using AI & Favoriot

This guide outlines how to develop an Automated Quality Inspection System using AI, a camera, and the Favoriot IoT platform. The provided code is in Python, along with detailed explanations.


Step 1: Hardware & Software Preparation

Before getting started, ensure you have:

  • Raspberry Pi (or any computer with a camera)
  • HD Camera (Raspberry Pi Camera or USB Camera)
  • Python Installed
  • OpenCV & TensorFlow Libraries for image analysis
  • Internet Connection to send data to Favoriot

Step 2: Install Required Software

Run the following commands to install the necessary libraries:

sudo apt update && sudo apt upgrade -y
sudo apt install python3-pip -y
pip3 install opencv-python numpy requests tensorflow


Step 3: Train an AI Model for Defect Detection

You can use Transfer Learning with a pre-trained model like MobileNetV2. Below is an example of training the model:

import tensorflow as tf
from tensorflow.keras.preprocessing.image import ImageDataGenerator

# Dataset configuration
dataset_path = "dataset_defects"
batch_size = 32
img_size = (224, 224)

datagen = ImageDataGenerator(rescale=1./255, validation_split=0.2)

train_data = datagen.flow_from_directory(
    dataset_path,
    target_size=img_size,
    batch_size=batch_size,
    class_mode="binary",
    subset="training"
)

val_data = datagen.flow_from_directory(
    dataset_path,
    target_size=img_size,
    batch_size=batch_size,
    class_mode="binary",
    subset="validation"
)

# Transfer Learning Model
base_model = tf.keras.applications.MobileNetV2(input_shape=(224, 224, 3), include_top=False, weights="imagenet")
base_model.trainable = False

model = tf.keras.Sequential([
    base_model,
    tf.keras.layers.GlobalAveragePooling2D(),
    tf.keras.layers.Dense(1, activation="sigmoid")
])

model.compile(optimizer="adam", loss="binary_crossentropy", metrics=["accuracy"])

# Train the model
model.fit(train_data, validation_data=val_data, epochs=10)

# Save the model
model.save("defect_detection_model.h5")


Step 4: Real-Time Defect Detection with Camera

This code captures images from the camera and detects defects using the trained AI model.

import cv2
import numpy as np
import tensorflow as tf

# Load trained model
model = tf.keras.models.load_model("defect_detection_model.h5")

# Start camera
cap = cv2.VideoCapture(0)

while True:
    ret, frame = cap.read()
    if not ret:
        break

    # Preprocess image
    img = cv2.resize(frame, (224, 224))
    img = np.expand_dims(img, axis=0) / 255.0

    # Predict defects
    prediction = model.predict(img)[0][0]

    # Display result on screen
    label = "Defect Detected!" if prediction > 0.5 else "Product OK"
    color = (0, 0, 255) if prediction > 0.5 else (0, 255, 0)

    cv2.putText(frame, label, (50, 50), cv2.FONT_HERSHEY_SIMPLEX, 1, color, 2)
    cv2.imshow("Quality Inspection", frame)

    # Exit when 'q' is pressed
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

cap.release()
cv2.destroyAllWindows()


Step 5: Send Defect Reports to Favoriot

The system automatically sends the data to the Favoriot IoT Platform if a defect is detected.

1. Create a Device on Favoriot

  • Log in to Favoriot.
  • Navigate to Devices → Add Device.
  • Copy the Device ID and API Key.

2. Code to Send Data to Favoriot

import requests
import json

# Favoriot credentials
DEVICE_ID = "YOUR_DEVICE_ID"
API_KEY = "YOUR_FAVORIOT_API_KEY"
URL = "https://apiv2.favoriot.com/v2/streams"

def send_data_to_favoriot(status):
    payload = {
        "device_developer_id": DEVICE_ID,
        "data": {
            "status": status
        }
    }
    
    headers = {
        "Content-Type": "application/json",
        "Apikey": API_KEY
    }
    
    response = requests.post(URL, data=json.dumps(payload), headers=headers)
    print("Response from Favoriot:", response.json())

# Example usage (send data if defect detected)
if prediction > 0.5:
    send_data_to_favoriot("Defect Detected!")
else:
    send_data_to_favoriot("Product OK")


Step 6: Monitor Data on Favoriot

  • Go to Favoriot Dashboard → Streams to view defect reports.
  • You can also create a dashboard to visualize real-time defect detection data.

Conclusion

With this system, you can:

Automatically detect defects using AI.
Display results in real-time on the screen.
Send defect reports to Favoriot for remote monitoring.

This system can be enhanced by adding Telegram notifications or advanced analytics on Favoriot.

[Disclaimer: This article provides a step-by-step guide. The source code may need adjustments to fit the final project design.]

Podcast also available on PocketCasts, SoundCloud, Spotify, Google Podcasts, Apple Podcasts, and RSS.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Share This

Share this post with your friends!

Discover more from IoT World

Subscribe now to keep reading and get access to the full archive.

Continue reading