5/03/2025

Google Coral USB Edge TPU Implementation Guide

 

Google Coral USB Edge TPU Implementation Guide

1. Installation and Troubleshooting

1.1 Hardware Requirements

  • Google Coral USB Accelerator
  • USB 3.0 port (for best performance)
  • Linux host system (Ubuntu, Debian, etc.)

1.2 Installation Steps

# 1. Add the Coral package repository
echo "deb https://packages.cloud.google.com/apt coral-edgetpu-stable main" | sudo tee /etc/apt/sources.list.d/coral-edgetpu.list

# 2. Add the package key
curl https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add -

# 3. Update package lists
sudo apt-get update

# 4. Install the Edge TPU runtime
sudo apt-get install libedgetpu1-std

# 5. Create a conda environment with Python 3.9
conda create -n coral_env python=3.9
conda activate coral_env

# 6. Install necessary packages
pip install numpy pillow opencv-python

# 7. Install PyCoral from Google's repository
pip install --extra-index-url https://google-coral.github.io/py-repo/ pycoral

1.3 Common Issues and Solutions

1.3.1 "Failed to load delegate from libedgetpu.so.1" Error

This is a common error that occurs when the Edge TPU library doesn't have executable permissions or when the user doesn't have the right permissions to access the device.

Solution:

# Add executable permission to the library
sudo chmod +x /usr/lib/x86_64-linux-gnu/libedgetpu.so.1.0

# Add your user to the plugdev group
sudo usermod -aG plugdev $USER

# Create proper udev rules
sudo bash -c 'cat > /etc/udev/rules.d/99-edgetpu.rules << EOF
SUBSYSTEM=="usb", ATTRS{idVendor}=="1a6e", ATTRS{idProduct}=="089a", MODE="0664", GROUP="plugdev"
EOF'

# Reload udev rules
sudo udevadm control --reload-rules && sudo udevadm trigger

# Unplug and replug your device

1.3.2 Verifying Installation

Create a simple Python script to check if the Edge TPU is detected:

# check_coral.py
from pycoral.utils import edgetpu

print("Testing Edge TPU...")
devices = edgetpu.list_edge_tpus()
print(f"Available Edge TPUs: {devices}")
if devices:
    print("Edge TPU is working correctly!")
else:
    print("No Edge TPU detected.")

Run with:

python check_coral.py

2. Edge TPU Inference with YOLOv10/YOLOv11

2.1 Complete Implementation

import time
import cv2
import numpy as np
import os
import sys
from PIL import Image
from pycoral.utils import edgetpu
from pycoral.adapters import common


class YOLOv10EdgeTPU:

    def __init__(self, path, conf_thres=0.3, iou_thres=0.5, input_size=(640, 640)):
        self.conf_threshold = conf_thres
        self.iou_threshold = iou_thres
        # Set default input size
        self.default_input_height, self.default_input_width = input_size

        # Initialize model
        self.initialize_model(path)

    def __call__(self, image):
        return self.detect_objects(image)

    def initialize_model(self, path):
        # Load Edge TPU model
        self.interpreter = edgetpu.make_interpreter(path)
        self.interpreter.allocate_tensors()
        
        # Get model info
        self.get_input_details()
        self.get_output_details()

    def detect_objects(self, image):
        input_tensor = self.prepare_input(image)

        # Perform inference on the image
        outputs = self.inference(input_tensor)

        # Process outputs
        self.boxes, self.scores, self.class_ids = self.process_output(outputs)
        
        # Ensure boxes, scores, and class_ids have the same length
        if len(self.boxes) != len(self.scores) or len(self.boxes) != len(self.class_ids):
            return []   

        detections = []
        for i in range(len(self.boxes)):
            detection = {
                'box': self.boxes[i].tolist(),  # Convert numpy array to Python list
                'score': self.scores[i],
                'class_id': self.class_ids[i]
            }
            detections.append(detection)

        return detections

    def prepare_input(self, image):
        self.img_height, self.img_width = image.shape[:2]

        # Convert to RGB (OpenCV loads as BGR)
        input_img = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)

        # Use the model's input dimensions
        input_width = self.input_width
        input_height = self.input_height

        # Resize input image
        input_img = cv2.resize(input_img, (input_width, input_height))

        # Scale input pixel values to 0 to 1
        input_img = input_img.astype(np.float32) / 255.0
        
        # Add batch dimension
        input_tensor = np.expand_dims(input_img, axis=0)

        return input_tensor

    def inference(self, input_tensor):
        start = time.perf_counter()
        
        # Set input tensor
        common.set_input(self.interpreter, input_tensor)
        
        # Run inference
        self.interpreter.invoke()
        
        # Get all output tensors
        outputs = []
        for i in range(len(self.output_details)):
            outputs.append(self.interpreter.get_tensor(self.output_details[i]['index']))
            
        infer_time = (time.perf_counter() - start) * 1000
        print(f"Inference time: {infer_time:.2f} ms")
        return outputs

    def process_output(self, outputs):
        """Process output for YOLOv10/YOLOv11 TFLite model with shape [1, 5, 8400]"""
        # Get the first output tensor
        predictions = outputs[0]  # Shape [1, 5, 8400]
        print(f"Output 0 shape: {predictions.shape}")
        
        # Transpose the predictions from [1, 5, 8400] to [8400, 5]
        predictions = predictions[0].T  # Now shape is [8400, 5]
        print(f"Transposed predictions shape: {predictions.shape}")
        
        # Extract confidence scores
        objectness_scores = predictions[:, 4]  # Last column is confidence
        
        # Filter by confidence threshold
        valid_indices = np.where(objectness_scores > self.conf_threshold)[0]
        if len(valid_indices) == 0:
            return [], [], []
        
        valid_boxes = predictions[valid_indices, :4]  # First 4 values are bbox coords
        valid_scores = predictions[valid_indices, 4]  # 5th value is confidence
        
        # For single-class model, all class IDs are 0
        valid_class_ids = np.zeros(len(valid_scores), dtype=np.int32)
        
        # Convert normalized coordinates to pixel coordinates
        # For YOLO, the output is in normalized format (0-1)
        valid_boxes[:, 0] *= self.img_width   # x
        valid_boxes[:, 1] *= self.img_height  # y
        valid_boxes[:, 2] *= self.img_width   # width
        valid_boxes[:, 3] *= self.img_height  # height
        
        # Convert from [cx, cy, width, height] to [x1, y1, x2, y2]
        boxes_xyxy = self.cxcywh_to_xyxy(valid_boxes)
        
        # Apply non-maximum suppression
        indices = self.nms(boxes_xyxy, valid_scores)
        
        print(f"After NMS: {len(indices)} detections")
        return boxes_xyxy[indices], valid_scores[indices], valid_class_ids[indices]
    
    def cxcywh_to_xyxy(self, boxes):
        """Convert boxes from center_x, center_y, width, height to x1, y1, x2, y2 format"""
        xyxy = np.zeros_like(boxes)
        xyxy[:, 0] = boxes[:, 0] - boxes[:, 2] / 2  # x1 = cx - w/2
        xyxy[:, 1] = boxes[:, 1] - boxes[:, 3] / 2  # y1 = cy - h/2
        xyxy[:, 2] = boxes[:, 0] + boxes[:, 2] / 2  # x2 = cx + w/2
        xyxy[:, 3] = boxes[:, 1] + boxes[:, 3] / 2  # y2 = cy + h/2
        return xyxy
    
    def nms(self, boxes, scores):
        """Apply non-maximum suppression"""
        # Get indices of boxes sorted by scores in descending order
        indices = np.argsort(scores)[::-1]
        
        keep = []
        while indices.size > 0:
            # Pick the box with highest score
            current = indices[0]
            keep.append(current)
            
            # If only one box left, break
            if indices.size == 1:
                break
                
            # Compute IoU of the picked box with the rest
            ious = self.box_iou(boxes[current:current+1], boxes[indices[1:]])
            
            # Remove boxes with IoU over threshold
            mask = ious < self.iou_threshold
            indices = indices[1:][mask]
            
        return keep
    
    def box_iou(self, box1, box2):
        """
        Calculate IoU between box1 and box2
        box1, box2: [x1, y1, x2, y2]
        """
        # Calculate intersection area
        x1 = np.maximum(box1[0, 0], box2[:, 0])
        y1 = np.maximum(box1[0, 1], box2[:, 1])
        x2 = np.minimum(box1[0, 2], box2[:, 2])
        y2 = np.minimum(box1[0, 3], box2[:, 3])
        
        intersection = np.maximum(0, x2 - x1) * np.maximum(0, y2 - y1)
        
        # Calculate union area
        box1_area = (box1[0, 2] - box1[0, 0]) * (box1[0, 3] - box1[0, 1])
        box2_area = (box2[:, 2] - box2[:, 0]) * (box2[:, 3] - box2[:, 1])
        union = box1_area + box2_area - intersection
        
        # Calculate IoU
        iou = intersection / union
        
        return iou

    def draw_detections(self, image, draw_scores=True, mask_alpha=0.4):
        """Draw detection boxes on image with scores if requested"""
        img_copy = image.copy()
        
        for i, box in enumerate(self.boxes):
            x1, y1, x2, y2 = [int(val) for val in box]
            
            # Draw box with thicker lines for visibility
            cv2.rectangle(img_copy, (x1, y1), (x2, y2), (0, 255, 0), 3)
            
            # Draw score if requested
            if draw_scores and i < len(self.scores):
                score = self.scores[i]
                class_id = self.class_ids[i] if i < len(self.class_ids) else 0
                
                # For your license plate detector
                label = f"License Plate: {score:.2f}"
                
                # Calculate text size and position
                text_size = cv2.getTextSize(label, cv2.FONT_HERSHEY_SIMPLEX, 0.7, 2)[0]
                
                # Draw text background
                cv2.rectangle(img_copy, (x1, y1 - text_size[1] - 5), (x1 + text_size[0], y1), (0, 255, 0), -1)
                
                # Draw text
                cv2.putText(img_copy, label, (x1, y1 - 5), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 0), 2)
        
        return img_copy

    def get_input_details(self):
        input_details = self.interpreter.get_input_details()
        self.input_details = input_details
        
        # Print input details for debugging
        print(f"Input details: {input_details}")
        
        # Get input shape
        self.input_shape = input_details[0]['shape']
        print(f"Input shape: {self.input_shape}")
        
        # Get input dimensions
        if len(self.input_shape) == 4:
            _, self.input_height, self.input_width, _ = self.input_shape
        else:
            self.input_height = self.default_input_height
            self.input_width = self.default_input_width
            
        print(f"Input dimensions: {self.input_width}x{self.input_height}")

    def get_output_details(self):
        self.output_details = self.interpreter.get_output_details()
        print(f"Output details: {self.output_details}")


def run_benchmark(detector, img, num_runs=10):
    """Run inference multiple times to benchmark performance"""
    print(f"\nRunning benchmark with {num_runs} iterations...")
    times = []
    
    # Prepare input once
    input_tensor = detector.prepare_input(img)
    common.set_input(detector.interpreter, input_tensor)
    
    for i in range(num_runs):
        start = time.perf_counter()
        detector.interpreter.invoke()
        inference_time = (time.perf_counter() - start) * 1000
        times.append(inference_time)
        print(f"Run {i+1}: {inference_time:.2f} ms")
    
    avg_time = sum(times) / len(times)
    print(f"\nAverage inference time: {avg_time:.2f} ms")
    print(f"Min: {min(times):.2f} ms, Max: {max(times):.2f} ms")
    print(f"FPS: {1000/avg_time:.2f}")
    
    return avg_time


if __name__ == '__main__':
    # Get model path from command line or use default
    model_path = "float32_edgetpu.tflite"
    img_path = "cs1.png"
    
    # Parse command line arguments
    if len(sys.argv) > 1:
        for i, arg in enumerate(sys.argv):
            if arg == "--model" and i+1 < len(sys.argv):
                model_path = sys.argv[i+1]
            elif arg == "--image" and i+1 < len(sys.argv):
                img_path = sys.argv[i+1]
    
    # Check if files exist
    if not os.path.exists(model_path):
        print(f"Error: Model file not found at {model_path}")
        sys.exit(1)
        
    if not os.path.exists(img_path):
        print(f"Error: Image file not found at {img_path}")
        sys.exit(1)

    # Initialize YOLOv10 EdgeTPU detector
    detector = YOLOv10EdgeTPU(model_path, conf_thres=0.3, iou_thres=0.5, input_size=(640, 640))

    # Load image
    img = cv2.imread(img_path)
    if img is None:
        print(f"Failed to load image from path: {img_path}")
        sys.exit(1)
    print(f"Using image: {img_path}")
    print(f"Image shape: {img.shape}")

    # Detect Objects
    detections = detector(img)
    print(f"Found {len(detections)} detections")
    
    # Log all detections
    for i, det in enumerate(detections):
        print(f"Detection {i+1}: box={det['box']}, score={det['score']:.4f}, class={det['class_id']}")
    
    # Draw detections
    output_img = detector.draw_detections(img, draw_scores=True)
    output_path = "edgetpu_detections.jpg"
    cv2.imwrite(output_path, output_img)
    print(f"Output saved to {output_path}")
    
    # Run benchmark if requested
    if "--benchmark" in sys.argv:
        run_benchmark(detector, img, num_runs=10)

2.2 Usage

# Basic inference
python edge_tpu_yolov10.py

# Specify custom model and image
python edge_tpu_yolov10.py --model /path/to/model_edgetpu.tflite --image /path/to/image.jpg

# Run benchmark
python edge_tpu_yolov10.py --benchmark

3. Comparison: Edge TPU vs GPU

The Edge TPU provides hardware acceleration for TensorFlow Lite models specifically compiled for it. Here's how it compares to GPU inference:

  • Edge TPU: ~150 ms inference time (lower with benchmarking)
  • GPU (NVIDIA): Can be much faster, depending on the GPU model
  • Benefits of Edge TPU: Low power consumption, no need for a powerful GPU, portable

4. Tips and Best Practices

  1. Model Compilation:
    • Use the Edge TPU Compiler to convert TFLite models for Edge TPU
    • INT8 quantized models work best with Edge TPU
  2. Performance Optimization:
    • Use USB 3.0 ports for faster data transfer
    • Consider using the high-performance version (libedgetpu1-max) if you need more speed and can manage the heat
  3. Troubleshooting:
    • Always check permissions (chmod +x on the library file)
    • Make sure your user is in the plugdev group
    • Check the udev rules
  4. Hardware Limitations:
    • Edge TPU has limited memory (8MB shared between all models)
    • Not all operations are supported (mainly INT8 quantized operations)

This implementation provides a solid foundation for running YOLO object detection on the Google Coral Edge TPU with proper pre-processing and post-processing for optimal results.

4/17/2025

.screenrc setup for better usage

 

.screenrc

code ~/.screenrc or vim or nano

and put this 

# Current settings for scrolling and encoding
termcapinfo xterm* ti@:te@
defutf8 on
term screen-256color
defscrollback 10000
encoding utf8

# Add these lines for better keyboard handling
bindkey -k ku stuff \033[A
bindkey -k kd stuff \033[B
bindkey -k kl stuff \033[D
bindkey -k kr stuff \033[C

# Allow alternate screen
altscreen on

# Set terminal to xterm-256color for better compatibility
terminfo xterm-256color hs@:cs=\E[%i%p1%d;%p2%dr:im=\E[4h:ei=\E[4l

# Make bash history work properly
shell -$SHELL

#study.marearts.com



detach or recreate screen

Thank you.

4/10/2025

Mounting Remote Data for GPU Training

 

Mounting Remote Data for GPU Training

This guide explains how to access data from Computer A (data server) on Computer B (GPU machine) for machine learning training workflows.

Overview

When training machine learning models, you often need:

  1. A computer with GPU capabilities for training (Computer B)
  2. Access to training data stored on another machine (Computer A)

This tutorial will show you how to securely connect these computers using SSH, allowing the GPU machine to access data without copying everything locally.

Prerequisites

  • SSH access to both computers
  • Admin/sudo privileges on both machines
  • Basic knowledge of terminal commands

Step 1: Generate SSH Key on GPU Computer (if needed)

If you don't already have an SSH key on your GPU computer (Computer B):

# On Computer B (GPU)
ssh-keygen -t rsa -b 4096

Press Enter to accept default locations and add a passphrase if desired.

Step 2: Copy SSH Public Key to Data Computer

# On Computer B (GPU)
# View your public key
cat ~/.ssh/id_rsa.pub

# Copy the output to clipboard

Now transfer this key to Computer A (data server):

# Option 1: Using ssh-copy-id (easiest)
ssh-copy-id username@computerA

# Option 2: Manual setup
# First, SSH into Computer A
ssh username@computerA

# Then on Computer A, create .ssh directory if it doesn't exist
mkdir -p ~/.ssh
chmod 700 ~/.ssh

# Add your public key to authorized_keys
echo "ssh-rsa AAAA...your key here..." >> ~/.ssh/authorized_keys
chmod 600 ~/.ssh/authorized_keys

# Exit back to Computer B
exit

Step 3: Test the SSH Connection

Ensure you can connect without a password:

# On Computer B (GPU)
ssh username@computerA

If successful, you should connect without entering a password.

Step 4: Mount Remote Data using SSHFS

Install SSHFS on your GPU computer:

# On Computer B (GPU)
# For Ubuntu/Debian
sudo apt-get update
sudo apt-get install sshfs

# For CentOS/RHEL/Fedora
sudo dnf install fuse-sshfs

Create a mount point and mount the remote directory:

# On Computer B (GPU)
# Create mount directory
mkdir -p ~/data_mount

# Mount the remote directory
sshfs username@computerA:/path/to/data ~/data_mount

# Verify the mount worked
ls ~/data_mount

Step 5: Using the Mounted Data for Training

Now you can access the data in your training scripts as if it were local:

# Example PyTorch script
import torch
from torch.utils.data import Dataset, DataLoader

# Point to your mounted data directory
data_dir = "~/data_mount/dataset"

# Your training code...

Additional Options

Automating Mount on Startup

To automatically mount the remote directory when your GPU computer starts:

  1. Edit your fstab file:

    sudo nano /etc/fstab
    
  2. Add this line (all on one line):

    username@computerA:/path/to/data /home/username/data_mount fuse.sshfs defaults,_netdev,user,idmap=user,follow_symlinks,identityfile=/home/username/.ssh/id_rsa,allow_other,reconnect 0 0
    
  3. Save and exit

Unmounting

To unmount the remote directory:

# On Computer B (GPU)
fusermount -u ~/data_mount

Performance Considerations

  • For better performance with large datasets, try these SSHFS options:

    sshfs username@computerA:/path/to/data ~/data_mount -o Compression=no,big_writes,cache=yes,kernel_cache
    
  • If you experience frequent disconnections, add reconnect options:

    sshfs username@computerA:/path/to/data ~/data_mount -o reconnect,ServerAliveInterval=15,ServerAliveCountMax=3
    

Alternative: NFS for Better Performance

For production setups with large datasets, consider using NFS instead of SSHFS for better performance.

Troubleshooting

Connection Issues

  • Verify SSH keys are correctly set up
  • Check firewall settings on both computers
  • Ensure the SSH service is running: sudo systemctl status sshd

Permission Problems

  • Check file permissions on the data directory
  • Verify your user has read access to the data files

Mount Errors

  • Make sure FUSE is installed and configured properly
  • Check if the mount point directory exists and is empty

Security Considerations

  • Use key-based authentication only (disable password login)
  • Consider restricting SSH access by IP address
  • Use a non-standard SSH port for additional security

For any issues or questions, please contact your system administrator.

4/02/2025

Explanation for data Normalization and Min/Max calculation.

Let me explain how a specific normalized feature value is calculated using one concrete example.

Let's take the feature "GroupSize" which has:

  • Min value: -0.045121
  • Max value: 103.032967

These values are post-normalization, but we can work backwards to understand how they were calculated.

The Normalization Formula

The normalization function you're using is:

normalized_features = (features - mean) / std

Where:

  • features are the original, raw values
  • mean is the average of all values for that feature in the training set
  • std is the standard deviation of all values for that feature in the training set

Working Through An Example

Let's say we have these raw values for GroupSize in the training set:

  • Raw values: [0, 0, 0, 0, 0, 1, 1, 1, 32, 64]

First, we calculate the mean:

  • Mean = (0+0+0+0+0+1+1+1+32+64)/10 = 9.9

Then we calculate the standard deviation:

  • Each deviation: [-9.9, -9.9, -9.9, -9.9, -9.9, -8.9, -8.9, -8.9, 22.1, 54.1]
  • Squared deviations: [98.01, 98.01, 98.01, 98.01, 98.01, 79.21, 79.21, 79.21, 488.41, 2926.81]
  • Average squared deviation: 4143.9/10 = 414.39
  • Standard deviation = √414.39 ≈ 20.36

Now, we can normalize each value:

  • For 0: (0 - 9.9) / 20.36 = -0.486
  • For 1: (1 - 9.9) / 20.36 = -0.437
  • For 32: (32 - 9.9) / 20.36 = 1.085
  • For 64: (64 - 9.9) / 20.36 = 2.657

Explaining the Min/Max Values

Going back to your data:

  • The min value for GroupSize (-0.045121) represents the original minimum value in your dataset after normalization
  • The max value (103.032967) represents the original maximum value after normalization

For GroupSize, this extreme range suggests:

  1. Your raw data has a wide range of values
  2. The high maximum suggests outliers that are far from the mean, creating a highly skewed distribution
  3. The standard deviation is relatively small compared to the maximum value

Concrete Calculation

If we assume the mean of raw GroupSize is ฮผ and standard deviation is ฯƒ, then:

  • Minimum normalized value: (min_raw - ฮผ) / ฯƒ = -0.045121
  • Maximum normalized value: (max_raw - ฮผ) / ฯƒ = 103.032967

This tells us that your maximum raw value is over 103 standard deviations away from the mean, which is extremely far! This confirms that your raw data has a heavily skewed distribution with significant outliers.

The fact that most normalized values for GroupSize are close to the minimum (-0.045121) suggests that the most common value is slightly below the mean, while a few extreme outliers are pulling the mean upward.

This type of skewed distribution is exactly why techniques like masking and autoencoder approaches are beneficial - they can help the model learn robust representations even with such extreme distributions.

3/24/2025

Setup Online annotation with LabelMe

Complete Tutorial: LabelMe with Login System and Dataset Management

This guide provides step-by-step instructions for setting up LabelMe with a custom login system and proper dataset management. We'll cover the entire workflow: login → annotation → save → logout.

System Overview

We'll build a system with the following components:

  1. LabelMe running in a Docker container with a configured dataset folder
  2. PHP-based authentication system
  3. Web server (Apache) to host the login portal
  4. Dataset management structure

Prerequisites

  • A Linux server (Ubuntu 20.04 LTS or newer recommended)
  • Root or sudo access to the server
  • Docker and Docker Compose installed
  • Apache web server with PHP support

Part 1: Server Preparation

Step 1: Update System and Install Required Packages

# Update package lists
sudo apt update
sudo apt upgrade -y

# Install necessary packages
sudo apt install -y docker.io docker-compose apache2 php libapache2-mod-php php-json
sudo systemctl enable docker
sudo systemctl start docker

# Add your user to docker group to avoid using sudo with docker commands
sudo usermod -aG docker $USER
# Log out and log back in for this to take effect

Step 2: Create Project Directory Structure

# Create main project directory
mkdir -p ~/labelme-project
cd ~/labelme-project

# Create directories for different components
mkdir -p docker-labelme
mkdir -p web-portal
mkdir -p datasets/{project1,project2}
mkdir -p annotations

# Add some sample images to project1 (optional)
# You can replace this with your own dataset copying commands
mkdir -p datasets/project1/images
# Copy some sample images if you have them
# cp /path/to/your/images/*.jpg datasets/project1/images/

Part 2: Set Up LabelMe Docker Container

Step 1: Create Docker Compose Configuration

Create a file docker-labelme/docker-compose.yml:

cd ~/labelme-project/docker-labelme
nano docker-compose.yml

Add the following content:

version: '3'
services:
  labelme:
    image: wkentaro/labelme
    container_name: labelme-server
    ports:
      - "8080:8080"
    volumes:
      - ../datasets:/data
      - ../annotations:/home/developer/.labelmerc
    environment:
      - LABELME_SERVER=1
      - LABELME_PORT=8080
      - LABELME_HOST=0.0.0.0
    command: labelme --server --port 8080 --host 0.0.0.0 /data
    restart: unless-stopped

Step 2: Create LabelMe Configuration File

This step ensures annotations are saved in the proper format and location:

cd ~/labelme-project
nano annotations/.labelmerc

Add the following content:

{
  "auto_save": true,
  "display_label_popup": true,
  "store_data": true,
  "keep_prev": false,
  "flags": null,
  "flags_2": null,
  "flags_3": null,
  "label_flags": null,
  "labels": ["person", "car", "bicycle", "dog", "cat", "tree", "building"],
  "file_search": true,
  "show_label_text": true
}

Customize the labels list according to your annotation needs.

Step 3: Start LabelMe Container

cd ~/labelme-project/docker-labelme
docker-compose up -d

Verify it's running:

docker ps

You should see the labelme-server container running and listening on port 8080.

Part 3: Set Up Web Portal with Login System

Step 1: Create the Login Page

cd ~/labelme-project/web-portal
nano index.php

Add the following content:

<?php
// Display errors during development (remove in production)
ini_set('display_errors', 1);
error_reporting(E_ALL);

// Start session
session_start();

// Check if there's an error message
$error_message = isset($_SESSION['error_message']) ? $_SESSION['error_message'] : '';
// Clear error message after displaying it
unset($_SESSION['error_message']);
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>LabelMe Login</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            background-color: #f4f4f4;
            margin: 0;
            padding: 0;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
        }
        .login-container {
            background-color: white;
            padding: 30px;
            border-radius: 8px;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
            width: 350px;
        }
        h2 {
            text-align: center;
            color: #333;
            margin-bottom: 20px;
        }
        input[type="text"],
        input[type="password"] {
            width: 100%;
            padding: 12px;
            margin: 8px 0;
            display: inline-block;
            border: 1px solid #ccc;
            box-sizing: border-box;
            border-radius: 4px;
        }
        button {
            background-color: #4CAF50;
            color: white;
            padding: 14px 20px;
            margin: 10px 0;
            border: none;
            cursor: pointer;
            width: 100%;
            border-radius: 4px;
            font-size: 16px;
        }
        button:hover {
            opacity: 0.8;
        }
        .error-message {
            color: #f44336;
            text-align: center;
            margin-top: 10px;
        }
        .logo {
            text-align: center;
            margin-bottom: 20px;
        }
    </style>
</head>
<body>
    <div class="login-container">
        <div class="logo">
            <h2>LabelMe Annotation</h2>
        </div>
        <form id="loginForm" action="auth.php" method="post">
            <div>
                <label for="username"><b>Username</b></label>
                <input type="text" placeholder="Enter Username" name="username" required>
            </div>
            <div>
                <label for="password"><b>Password</b></label>
                <input type="password" placeholder="Enter Password" name="password" required>
            </div>
            <div>
                <label for="project"><b>Select Project</b></label>
                <select name="project" style="width: 100%; padding: 12px; margin: 8px 0; display: inline-block; border: 1px solid #ccc; box-sizing: border-box; border-radius: 4px;">
                    <option value="project1">Project 1</option>
                    <option value="project2">Project 2</option>
                </select>
            </div>
            <button type="submit">Login</button>
            <?php if (!empty($error_message)): ?>
                <div class="error-message"><?php echo htmlspecialchars($error_message); ?></div>
            <?php endif; ?>
        </form>
    </div>
</body>
</html>

Step 2: Create the Authentication Script

cd ~/labelme-project/web-portal
nano auth.php

Add the following content:

<?php
// Start session management
session_start();

// Display errors during development (remove in production)
ini_set('display_errors', 1);
error_reporting(E_ALL);

// Configuration - Store these securely in production
$users = [
    'admin' => [
        'password' => password_hash('admin123', PASSWORD_DEFAULT), // Use hashed passwords
        'role' => 'admin'
    ],
    'user1' => [
        'password' => password_hash('user123', PASSWORD_DEFAULT),
        'role' => 'annotator'
    ],
    'user2' => [
        'password' => password_hash('user456', PASSWORD_DEFAULT),
        'role' => 'annotator'
    ]
];

// Base path to the LabelMe application
$labelme_base_url = 'http://localhost:8080'; // Change this to your LabelMe server address

// Handle login form submission
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $username = isset($_POST['username']) ? $_POST['username'] : '';
    $password = isset($_POST['password']) ? $_POST['password'] : '';
    $project = isset($_POST['project']) ? $_POST['project'] : 'project1';
    
    // Validate credentials
    if (isset($users[$username]) && password_verify($password, $users[$username]['password'])) {
        // Set session variables
        $_SESSION['logged_in'] = true;
        $_SESSION['username'] = $username;
        $_SESSION['role'] = $users[$username]['role'];
        $_SESSION['project'] = $project;
        $_SESSION['last_activity'] = time();
        
        // Redirect to LabelMe
        header("Location: labelme.php");
        exit;
    } else {
        // Failed login
        $_SESSION['error_message'] = "Invalid username or password";
        header("Location: index.php");
        exit;
    }
}

// For logout
if (isset($_GET['logout'])) {
    // Log this logout
    $log_file = 'user_activity.log';
    $log_message = date('Y-m-d H:i:s') . " - User: " . ($_SESSION['username'] ?? 'unknown') . 
                " - Action: Logged out\n";
    file_put_contents($log_file, $log_message, FILE_APPEND);
    
    // Clear session data
    session_unset();
    session_destroy();
    
    // Redirect to login page
    header("Location: index.php");
    exit;
}
?>

Step 3: Create the LabelMe Proxy Page

cd ~/labelme-project/web-portal
nano labelme.php

Add the following content:

<?php
// Start session management
session_start();

// Display errors during development (remove in production)
ini_set('display_errors', 1);
error_reporting(E_ALL);

// Check if user is logged in
if (!isset($_SESSION['logged_in']) || $_SESSION['logged_in'] !== true) {
    // Not logged in, redirect to login page
    header("Location: index.php");
    exit;
}

// Security: Check for session timeout (30 minutes)
$timeout = 30 * 60; // 30 minutes in seconds
if (isset($_SESSION['last_activity']) && (time() - $_SESSION['last_activity'] > $timeout)) {
    // Session has expired
    session_unset();
    session_destroy();
    header("Location: index.php?timeout=1");
    exit;
}

// Update last activity time
$_SESSION['last_activity'] = time();

// Configuration
$labelme_base_url = 'http://localhost:8080'; // Change this to your LabelMe server address
$project = $_SESSION['project'] ?? 'project1';
$labelme_url = $labelme_base_url . '/' . $project;

// Log user activity
$log_file = 'user_activity.log';
$log_message = date('Y-m-d H:i:s') . " - User: " . $_SESSION['username'] . 
               " - Role: " . $_SESSION['role'] . 
               " - Project: " . $project . 
               " - Action: Accessed LabelMe\n";
file_put_contents($log_file, $log_message, FILE_APPEND);
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>LabelMe Annotation Tool</title>
    <style>
        body, html {
            margin: 0;
            padding: 0;
            height: 100%;
            overflow: hidden;
        }
        .header {
            background-color: #333;
            color: white;
            padding: 10px;
            display: flex;
            justify-content: space-between;
            align-items: center;
        }
        .user-info {
            font-size: 14px;
        }
        .logout-btn {
            background-color: #f44336;
            color: white;
            border: none;
            padding: 5px 10px;
            cursor: pointer;
            border-radius: 3px;
            text-decoration: none;
            margin-left: 10px;
        }
        .logout-btn:hover {
            background-color: #d32f2f;
        }
        .project-selector {
            margin-left: 20px;
        }
        iframe {
            width: 100%;
            height: calc(100% - 50px);
            border: none;
        }
    </style>
</head>
<body>
    <div class="header">
        <div>
            <h3 style="margin:0;">LabelMe Annotation Tool</h3>
            <span>Project: <strong><?php echo htmlspecialchars($project); ?></strong></span>
        </div>
        <div class="user-info">
            Logged in as: <strong><?php echo htmlspecialchars($_SESSION['username']); ?></strong> 
            (<?php echo htmlspecialchars($_SESSION['role']); ?>)
            
            <form method="post" action="" style="display:inline-block">
                <select name="project" class="project-selector" onchange="this.form.submit()">
                    <option value="project1" <?php echo $project == 'project1' ? 'selected' : ''; ?>>Project 1</option>
                    <option value="project2" <?php echo $project == 'project2' ? 'selected' : ''; ?>>Project 2</option>
                </select>
            </form>
            
            <a href="auth.php?logout=1" class="logout-btn">Logout</a>
        </div>
    </div>
    
    <iframe src="<?php echo $labelme_url; ?>" allow="fullscreen"></iframe>
</body>
</html>

<?php
// Handle project switching
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['project'])) {
    $newProject = $_POST['project'];
    $_SESSION['project'] = $newProject;
    
    // Log project switch
    $log_message = date('Y-m-d H:i:s') . " - User: " . $_SESSION['username'] . 
                  " - Action: Switched to project " . $newProject . "\n";
    file_put_contents($log_file, $log_message, FILE_APPEND);
    
    // Redirect to refresh the page with new project
    header("Location: labelme.php");
    exit;
}
?>

Step 4: Setup Apache Virtual Host

sudo nano /etc/apache2/sites-available/labelme-portal.conf

Add the following configuration:

<VirtualHost *:80>
    ServerName labelme.yourdomain.com  # Change this to your domain or IP
    DocumentRoot /home/username/labelme-project/web-portal  # Update with your actual path
    
    <Directory /home/username/labelme-project/web-portal>  # Update with your actual path
        Options Indexes FollowSymLinks
        AllowOverride All
        Require all granted
    </Directory>
    
    ErrorLog ${APACHE_LOG_DIR}/labelme-error.log
    CustomLog ${APACHE_LOG_DIR}/labelme-access.log combined
</VirtualHost>

Update the paths to match your actual user and directory structure.

Step 5: Enable the Site and Restart Apache

sudo a2ensite labelme-portal.conf
sudo systemctl restart apache2

Step 6: Set Proper Permissions

# Set appropriate permissions for the web files
cd ~/labelme-project
sudo chown -R www-data:www-data web-portal
sudo chmod -R 755 web-portal

# Ensure the annotation directory is writable
sudo chown -R www-data:www-data annotations
sudo chmod -R 777 annotations

# Ensure datasets are accessible
sudo chmod -R 755 datasets

Part 4: Dataset Management

Step 1: Organize Your Datasets

Structure your dataset directories as follows:

datasets/
├── project1/
│   ├── images/
│   │   ├── image1.jpg
│   │   ├── image2.jpg
│   │   └── ...
│   └── annotations/  # LabelMe will save annotations here
├── project2/
│   ├── images/
│   │   ├── image1.jpg
│   │   └── ...
│   └── annotations/
└── ...

Step 2: Add Scripts for Managing Datasets (Optional)

Create a script to add new projects:

cd ~/labelme-project
nano add-project.sh

Add the following content:

#!/bin/bash
# Script to add a new project to the LabelMe setup

# Check if a project name was provided
if [ -z "$1" ]; then
    echo "Usage: $0 <project_name>"
    exit 1
fi

PROJECT_NAME="$1"
PROJECT_DIR="$HOME/labelme-project/datasets/$PROJECT_NAME"

# Create project directory structure
mkdir -p "$PROJECT_DIR/images"
mkdir -p "$PROJECT_DIR/annotations"

# Set permissions
chmod -R 755 "$PROJECT_DIR"

# Update the web portal to include the new project
# (This is a simplified approach - you'll need to manually edit index.php and labelme.php)
echo "Project directory created at: $PROJECT_DIR"
echo "Now copy your images to: $PROJECT_DIR/images/"
echo "Remember to manually update index.php and labelme.php to include the new project"

Make the script executable:

chmod +x add-project.sh

Part 5: Testing the Complete System

Step 1: Access the Web Portal

Open your browser and navigate to:

  • http://your-server-ip/ or http://labelme.yourdomain.com/

Step 2: Login and Test the Workflow

  1. Log in with the credentials (e.g., username: admin, password: admin123)
  2. Select a project from the dropdown
  3. After login, you should see the LabelMe interface embedded in the page
  4. Test annotating an image:
    • Click on an image
    • Draw polygons/shapes around objects
    • Enter labels for the objects
    • Annotations are auto-saved to the corresponding project folder
  5. Try switching projects using the dropdown in the header
  6. Log out and verify you're redirected to the login page

Part 6: Security Enhancements (for Production)

Enable HTTPS

sudo apt install certbot python3-certbot-apache
sudo certbot --apache -d labelme.yourdomain.com

Improve Password Security

Edit the auth.php file to use a database instead of hardcoded users.

Troubleshooting

LabelMe Not Loading

If LabelMe doesn't load in the iframe:

  1. Check if LabelMe is running: docker ps
  2. Make sure port 8080 is accessible
  3. Check the Docker container logs: docker logs labelme-server

Permission Issues

If you encounter permission issues with annotations:

sudo chmod -R 777 ~/labelme-project/annotations
sudo chown -R www-data:www-data ~/labelme-project/datasets

Annotation not Saving

If annotations aren't saving properly:

  1. Check the .labelmerc configuration file
  2. Verify the permissions on the annotations directory
  3. Check for error messages in the Apache logs: sudo tail -f /var/log/apache2/error.log

Conclusion

You now have a complete LabelMe annotation system with:

  • Secure login/authentication
  • Project selection
  • Dataset organization
  • User activity logging
  • Session management

This setup allows your team to collaborate on annotation projects while maintaining control over who can access the system and what projects they can work on.