Building Cell Counting System using CV and Labellerr

In the fields of biotechnology and healthcare, progress is often measured in data points. From our experience in data-driven medical research, we know that one of the most common and critical tasks is cell counting.

For decades, this has been a slow, manual, and laborious process. A trained technician must sit at a microscope, painstakingly counting individual cells, frame by frame.

This manual method is not just a bottleneck; it's prone to human error and fatigue, leading to inconsistencies. Our expertise in computer vision and deep learning has shown us there is a better way.

We can build an authoritative system that is not only faster but more accurate. This creates a trustworthy and repeatable workflow, empowering labs to accelerate research, improve diagnostics, and streamline drug discovery.

The Solution

Computer vision (CV) can be trained to "see" and count cells just like a human expert, but with the speed and scalability of a machine. The solution is to fine-tune a state-of-the-art object detection model, such as YOLO (You Only Look Once).

This model is trained on a custom dataset of microscopic images. Once trained, it can instantly process a new image or a live video feed, performing two key tasks in real-time:

  1. Detection: It identifies and draws a bounding box around every single cell it has been trained to recognize (e.g., "Red Blood Cell").
  2. Counting: It then simply counts the total number of detections it has made in that frame.

This automated system can process thousands of cells instantly, delivering fast, accurate, and repeatable results, transforming a day's work into a matter of seconds.

How Labellerr Helps Biotechnology

The most powerful AI model is useless if it's trained on bad data. The single biggest challenge in building a specialized system like this is creating the large, high-quality "ground truth" dataset. This is where a data-centric platform like Labellerr becomes indispensable.

Instead of forcing a scientist to manually label thousands of individual cells in thousands of video frames, Labellerr enables a "smart-annotation" workflow:

  1. Upload Data: The researcher uploads their raw microscopic video footage directly to the platform.
  2. AI-Assisted Labeling: Using an AI-powered tool like SAM (Segment Anything Model) 2.0, the user only needs to click on a few cells in a single frame. The AI intelligently segments and labels them.
  3. Automated Tracking: Labellerr's tracker then takes these initial labels and automatically follows every cell, propagating the annotations across the entire video.
  4. Export Dataset: In minutes, what would have been weeks of manual labor becomes a complete, perfectly annotated dataset, ready to be exported in a standard JSON format for training the YOLO model.

Labellerr bridges the gap between expert scientific knowledge and the data requirements of advanced AI, making it possible to build these custom models efficiently.

Creating Cell Count Model using Labellerr

This code, based on the cookbook from our research, outlines the complete process from data preparation to final inference.

Check Cookbook

1. Setup and Data Conversion

First, we clone a utility library and use it to convert our exported JSON annotations from Labellerr into the format YOLO requires.


from YOLO_fine_tunes.utilities import convert_to_yolo_segmentation

# Define your file paths
annotation_json_path = 'path/to/your/annotation.json'
video_directory = 'path/to/your/dataset_videos/'
output_dir = 'yolo_dataset/'

# Convert the data, splitting it into training, validation, and test sets
convert_to_yolo_segmentation(
    json_path=annotation_json_path,
    video_dir=video_directory,
    save_dir=output_dir,
    use_split=True,
    split_ratio=(0.5, 0.4, 0.1) # 50% train, 40% valid, 10% test
)

Dataset Conversion to YOLO format

2. Train Your Custom YOLO Model

With the data formatted, we can train the model using a single command line instruction.


# Train a YOLOv8-segmentation model
yolo task=segment mode=train \
     model=yolov8x-seg.pt \
     data=yolo_dataset/data.yaml \
     epochs=100 \
     imgsz=640 \
     batch=8

Train your YOLO model

3. Inference on a Single Image

Once trained, you can load your custom model and run it on a new image to get a cell count.


from ultralytics import YOLO
import cv2
import matplotlib.pyplot as plt

# Load your custom-trained model
model = YOLO('runs/segment/train/weights/last.pt')

# Path to your test image
image_path = 'path/to/test_image.jpg'
img = cv2.imread(image_path)

# Run prediction
results = model.predict(image_path)

# Get the count of detected cells
cell_count = len(results[0].boxes)
print(f"Total cells detected: {cell_count}")

# Draw the count on the image
cv2.putText(img, f'Cell Count: {cell_count}', (10, 50), 
            cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2)

# Display the image
plt.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
plt.title(f'Detected Cells: {cell_count}')
plt.show()

Inference on a frame

4. Real-Time Counting on a Video

This logic uses OpenCV to process a video file frame by frame, perform inference, and write the annotated video with a live count to a new file.


import cv2
from ultralytics import YOLO

# Load your custom-trained model
model = YOLO('runs/segment/train/weights/last.pt')

# Open the source video file
video_path = 'path/to/test_video.mp4'
cap = cv2.VideoCapture(video_path)

# Get video properties for the output file
frame_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
frame_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
fps = int(cap.get(cv2.CAP_PROP_FPS))

# Define the codec and create VideoWriter object
output_path = 'output_cell_counting_video.mp4'
out = cv2.VideoWriter(output_path, cv2.VideoWriter_fourcc(*'mp4v'), fps, (frame_width, frame_height))

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

    # Run YOLO prediction on the frame
    results = model.predict(frame, verbose=False)
    
    # Get the count
    cell_count = len(results[0].boxes)
    
    # Annotate the frame with bounding boxes (optional)
    annotated_frame = results[0].plot()

    # Draw the total cell count on the frame
    cv2.putText(annotated_frame, f'Cell Count: {cell_count}', (10, 50), 
                cv2.FONT_HERSHEY_SIMPLEX, 1.5, (0, 0, 255), 3, cv2.LINE_AA)

    # Write the frame to the output video
    out.write(annotated_frame)

# Release everything
cap.release()
out.release()
cv2.destroyAllWindows()
print(f"Video processing complete. Saved to {output_path}")

Inference on video

Conclusion

The power of this automated system is undeniable. It showcases how computer vision can directly accelerate medical research, diagnostics, and drug discovery. But it's vital to remember that this powerful AI is built on a simple foundation: high-quality, accurately labeled data.

The model is only as smart, accurate, and reliable as the data it was trained on. The annotation step is the most critical part of the entire process.

Investing in a high-quality, expertly-labeled dataset is the true key to unlocking the full potential of AI and building tools that can genuinely change the world for the better.

FAQs

How does computer vision improve accuracy in cell counting compared to manual methods?

Computer vision models such as YOLO provide consistent, fatigue-free detection and counting, reducing human error and delivering repeatable results at scale.

Why is high-quality labeled data important for training a cell counting model?

The performance of any detection system depends on the quality of its training data. Accurate and well-segmented annotations ensure the model learns true cell structures and produces reliable predictions.

How does Labellerr accelerate dataset creation for biomedical AI applications?

Labellerr uses AI-assisted labeling, SAM2-based segmentation, and automated tracking to convert raw microscopy videos into complete training datasets within minutes.