In this coding tutorial, learn how to detect pills using YOLO11 with the Medical-Pills Dataset. Also, explore its potential applications and benefits.
Artificial intelligence is being used in almost every industry, but its influence on healthcare, especially in pharmaceuticals, is particularly substantial. This year, the AI in the pharmaceutical market is valued at $1.94 billion, and it's expected to grow to $16.49 billion by 2034.
A key technological driver of this market is computer vision. Unlike traditional image processing, computer vision is a subfield of AI that enables machines to understand and analyze visual data in real time.
In the pharmaceutical industry, where even the smallest error can have serious consequences, Vision AI offers new and reliable ways to improve safety and precision.
For example, Ultralytics YOLO11 is a computer vision model designed for real-time tasks like object detection and instance segmentation, and it can be used for applications such as identifying pills or detecting defects in medical packaging.
In this article, we’ll walk through how to get hands-on with Vision AI by training YOLO11 to detect pills. We’ll also explore its real-world applications. Let’s get started!
Before we dive into how YOLO11 can be trained to detect pills, let's take a step back and understand what it means to train a model and the role of a dataset.
Training a model involves teaching it to recognize patterns by showing it many examples. In this case, a dataset is a collection of images where each image is labeled to indicate where the pills are located. This process helps the model learn from these examples so it can later identify pills in new images.
The Ultralytics Python package makes this process even easier by supporting a wide range of datasets in a streamlined YOLO file format. They offer hassle-free access to popular datasets and provide support for applications like pill detection.
For example, the Medical Pills Dataset is a dedicated proof-of-concept collection designed to showcase how object detection can improve pharmaceutical workflows through tasks such as quality control, sorting, and counterfeit detection.
Another factor to consider before you can start training with the Ultralytics Python package is choosing the right development environment. Here are three popular options:
There are other setup options you can explore in the official Ultralytics documentation, but the three mentioned above are easy to set up and use, making them great choices for getting started quickly.
In this guide, we'll focus on how to set up and train YOLO11 using Google Colab, Jupyter Notebooks, or a basic Python script, as the process is quite similar in each of these environments.
Also, this tutorial is quite similar to the one we previously covered on detecting wildlife using YOLO11. If you're interested in more details on any of the steps in this coding tutorial, you can check it out.
The Medical Pills Dataset includes 92 training images and 23 validation images, providing a solid split for both building and testing your model. Training images are used to teach the model, while validation images help evaluate how well the model performs on new, unseen data.
Each image in the dataset is labeled for a single class, pills. Bounding box annotations clearly mark each pill's location, making the dataset ideal for focused tasks like pill detection without the complexity of handling multiple object classes.
To support training with YOLO11, Ultralytics provides a YAML configuration file that defines key parameters like file paths, class names, and metadata needed for model training. Whether you're fine-tuning a pre-trained model or starting from scratch, this file makes the process much simpler and helps you get started quickly.
To begin, we'll set up an environment for training and testing the model. You can choose to use Google Colab, Jupyter Notebooks, or a simple Python file based on your preference. Just create a new notebook or Python file in the environment you choose.
Then, we can set up our environment and install the Ultralytics Python package using the command shown below. If you’re using a notebook-based environment (Google Colab or Jupyter), run the following command with an exclamation mark (!) at the beginning.
pip install ultralytics
Once installed, the next step is to download and train YOLO11 using the Medical Pills dataset. Since the dataset is supported by the Ultralytics Python package, the process is simple.
First, we can import the YOLO class from the Ultralytics package. Then, we can load a pre-trained YOLO11 model from the file “yolo11n.pt”, which is recommended because it is a nano model and lightweight.
Finally, we can start the training process by pointing the model to our dataset configuration (medical-pills.yaml) and setting the number of training epochs (one complete pass through the entire dataset) to 100, as shown below.
from ultralytics import YOLO
model = YOLO("yolo11n.pt")
results = model.train(data="medical-pills.yaml", epochs=100)
Training for multiple epochs allows the model to learn and improve its performance with each pass. You’ll be able to find logs and checkpoints saved in the “runs/train/” subfolder, which you can use to monitor progress and review the model’s performance.
After training is complete, the custom-trained YOLO11 model should be able to identify pills accurately. You can look for the final trained model weights in the “runs/detect/train/weights/” subfolder under the name “best.pt”.
To evaluate how well the model has learned to detect pills, we can run validation as follows:
metrics = model.val()
This process returns common object detection metrics, which provide insight into the model's performance. Here's a closer look at some of these metrics:
Together, these metrics offer a comprehensive view of how accurately the model detects pills in new, unseen data.
If your model doesn’t perform as well as expected, you can try training it for more epochs or fine-tuning other training parameters, such as the learning rate, which controls the size of the steps taken during model optimization, or the image size, to further improve its performance.
Once the YOLO11 model is trained and evaluated, the next step is to test how well it performs on new, unseen images. This helps simulate real-world conditions, such as detecting pills in different lighting, arrangements, or packaging styles.
To test the model, we have downloaded a sample image from Pexels, a free stock image website, and analyzed the image or ran a prediction using the custom-trained YOLO11 model as shown in code snippet below.
You can use this sample image or any other relevant image to evaluate how well the model performs in real-world scenarios.
results = model.predict("path/to/image.jpg", save=True, conf=0.3)
The save option tells the model to store the output image, and the confidence setting makes sure that only predictions with at least 30 percent certainty are included in the results.
When you run the prediction, the output will display a message telling you where the saved image is located - for example, "Results saved to runs/detect/train."
Your output image will be similar to the one shown here, with pills detected and highlighted using bounding boxes. The confidence scores displayed indicate the level of certainty for each detection.
Now that we’ve explored how to train YOLO11 using the Medical-Pills Dataset and run inferences on images for pill detection, let’s take a look at YOLO11’s real-world applications in the pharmaceutical industry.
Automated pill detection with YOLO11 can be applied to pharmaceutical sorting. Manual sorting is often slow, repetitive, and prone to errors that can compromise drug safety and compliance.
By using a fine-tuned YOLO11 model, we can accurately detect and sort pills based on visual attributes like size, shape, and color. This automation speeds up the process and helps ensure that products meet strict quality standards, making it a valuable tool in pharmaceutical operations.
Stocking the right medication on time is more than just a logistical task - it can affect patient care and costs. Running low on a critical pill may delay treatment, while overstocking can result in expired medications and wasted inventory. With numerous types of pills and packaging variations in the pharmaceutical industry, automated inventory systems can enable more accurate records.
Smart inventory systems can use computer vision models like Ultralytics YOLO11 to monitor stock levels in real time. The model can scan shelves and packaging areas using images or video to detect and count pills. As stock levels change, whether items are added, removed, or moved, the system can update the count automatically.
In pharmaceutical production, quality control is crucial to make sure that every pill is safe and effective. Even minor defects, such as a crack, uneven shape, or slight color variation, can lead to dosage errors or product recalls.
YOLO11 can help by automatically detecting pills that don't meet quality standards. The model can learn visual features and uses bounding boxes to flag issues like chips, faded imprints, or discoloration in real time. This allows for the early removal of faulty pills, reducing waste and guaranteeing that only quality-assured medication reaches patients.
On top of this, YOLO11 can be used to detect and count the pills as they are inspected, for accurate tracking while monitoring quality.
Now that we’ve explored how Vision AI can be applied in the pharmaceutical industry. Let’s take a quick look at some of the key benefits of using computer vision in this sector:
While there are many benefits to implementing Vision AI in the pharmaceutical industry, there are also some considerations to keep in mind when using such technologies:
In the future, AI will likely play a bigger role in making clinical trials faster, smarter, and more cost-effective. It can help design better trial protocols, choose the right patient groups, and monitor data in real time.
This may enable researchers to respond to issues as they come up, instead of after the fact. AI can also speed up the approval process by reducing manual paperwork and automating routine checks. Overall, the integration of AI in pharma workflows can result in fewer delays and faster access to new treatments.
Training Ultralytics YOLO11 on the Medical Pills Dataset shows how quickly and effectively the model can adapt to pharmaceutical tasks. Even with a small dataset, it can accurately detect pills, making it useful for things like sorting, quality control, and inventory tracking.
As datasets grow and models improve, the potential for Vision AI in pharma goes beyond just logistics. This technology could also support clinical trials by helping with consistent pill identification and tracking, and by assisting researchers in safely testing new drug combinations.
Explore our GitHub repository to learn more and be part of our growing community. Discover cutting-edge innovations in various sectors, from AI in agriculture to computer vision in healthcare. Check out our licensing options and launch your Vision AI projects today.
Begin your journey with the future of machine learning