#YOLOv8 #ComputerVision #ObjectDetection #IndustrialAI #Python
Applying YOLOv8 for Industrial Automation: Counting Plastic Bottles
This lesson will guide you through a complete computer vision project using YOLOv8. The goal is to detect and count plastic bottles in an image from an industrial setting, such as a conveyor belt or a storage area.
---
Step 1: Setup and Installation
First, we need to install the necessary libraries. The
#Setup #Installation
---
Step 2: Loading the Model and the Target Image
We will load a pre-trained YOLOv8 model. These models are trained on the large COCO dataset, which already knows how to identify common objects like 'bottle'. Then, we'll load our industrial image. Ensure you have an image named
#ModelLoading #DataHandling
---
Step 3: Performing Detection on the Image
With the model and image loaded, we can now run the detection. The
#Inference #ObjectDetection
---
Step 4: Filtering and Counting the Bottles
The model detects many types of objects. Our task is to go through the results, filter for only the 'bottle' class, and count how many there are. We'll also store the locations (bounding boxes) of each detected bottle for visualization.
#DataProcessing #Filtering
---
Step 5: Visualizing the Results
A number is good, but seeing what the model detected is better. We will draw the bounding boxes and the final count directly onto the image to create a clear visual output.
#Visualization #OpenCV
Applying YOLOv8 for Industrial Automation: Counting Plastic Bottles
This lesson will guide you through a complete computer vision project using YOLOv8. The goal is to detect and count plastic bottles in an image from an industrial setting, such as a conveyor belt or a storage area.
---
Step 1: Setup and Installation
First, we need to install the necessary libraries. The
ultralytics library provides the YOLOv8 model, and opencv-python is essential for image processing tasks.#Setup #Installation
# Open your terminal or command prompt and run this command:
pip install ultralytics opencv-python
---
Step 2: Loading the Model and the Target Image
We will load a pre-trained YOLOv8 model. These models are trained on the large COCO dataset, which already knows how to identify common objects like 'bottle'. Then, we'll load our industrial image. Ensure you have an image named
factory_bottles.jpg in your project folder.#ModelLoading #DataHandling
import cv2
from ultralytics import YOLO
# Load a pre-trained YOLOv8 model (yolov8n.pt is the smallest and fastest)
model = YOLO('yolov8n.pt')
# Load the image from the industrial setting
image_path = 'factory_bottles.jpg' # Make sure this image is in your directory
img = cv2.imread(image_path)
# A quick check to ensure the image was loaded correctly
if img is None:
print(f"Error: Could not load image at {image_path}")
else:
print("YOLOv8 model and image loaded successfully.")
---
Step 3: Performing Detection on the Image
With the model and image loaded, we can now run the detection. The
ultralytics library makes this process incredibly simple. The model will analyze the image and identify all the objects it recognizes.#Inference #ObjectDetection
# Run the model on the image to get detection results
results = model(img)
print("Detection complete. Processing results...")
---
Step 4: Filtering and Counting the Bottles
The model detects many types of objects. Our task is to go through the results, filter for only the 'bottle' class, and count how many there are. We'll also store the locations (bounding boxes) of each detected bottle for visualization.
#DataProcessing #Filtering
# Initialize a counter for the bottles
bottle_count = 0
bottle_boxes = []
# The model's results is a list, so we loop through it
for result in results:
# Each result has a 'boxes' attribute with the detections
boxes = result.boxes
for box in boxes:
# Get the class ID of the detected object
class_id = int(box.cls)
# Check if the class name is 'bottle'
if model.names[class_id] == 'bottle':
bottle_count += 1
# Store the bounding box coordinates (x1, y1, x2, y2)
bottle_boxes.append(box.xyxy[0])
print(f"Total plastic bottles detected: {bottle_count}")
---
Step 5: Visualizing the Results
A number is good, but seeing what the model detected is better. We will draw the bounding boxes and the final count directly onto the image to create a clear visual output.
#Visualization #OpenCV
🔥1
gray_img = imread('pout.tif');
imhist(gray_img);
title('Histogram of a Low-Contrast Image');Output: A figure window with a bar chart showing the intensity distribution of the 'pout.tif' image.
#12.
histeq()Enhances contrast using histogram equalization.
low_contrast_img = imread('pout.tif');
high_contrast_img = histeq(low_contrast_img);
imshow(high_contrast_img);
title('Histogram Equalized Image');Output: A figure window displays a higher contrast version of the 'pout.tif' image.
#13.
imadjust()Adjusts image intensity values or colormap by mapping intensity values to new values.
img = imread('cameraman.tif');
adjusted_img = imadjust(img, [0.3 0.7], []);
imshow(adjusted_img);
title('Intensity Adjusted Image');Output: A figure window showing a high-contrast version of the cameraman image, where intensities between 0.3 and 0.7 are stretched to the full [0, 1] range.
#14.
imtranslate()Translates (shifts) an image horizontally and vertically.
img = imread('cameraman.tif');
translated_img = imtranslate(img, [25, 15]); % Shift 25 pixels right, 15 pixels down
imshow(translated_img);
title('Translated Image');Output: A figure window shows the cameraman image shifted to the right and down.
#15.
imsharpen()Sharpens an image using the unsharp masking method.
img = imread('peppers.png');
sharpened_img = imsharpen(img);
imshow(sharpened_img);
title('Sharpened Image');Output: A figure window displays a crisper, more detailed version of the peppers image.
---
#MATLAB #ImageProcessing #Filtering #Noise
#16.
imnoise()Adds a specified type of noise to an image.
img = imread('cameraman.tif');
noisy_img = imnoise(img, 'salt & pepper', 0.02);
imshow(noisy_img);
title('Image with Salt & Pepper Noise');Output: A figure window displays the cameraman image with random white and black pixels (noise).
#17.
fspecial()Creates a predefined 2-D filter kernel (e.g., for averaging, Gaussian blur, Laplacian).
h = fspecial('motion', 20, 45); % Create a motion blur filter
disp('Generated a 2D motion filter kernel.');
disp(h);Generated a 2D motion filter kernel.
(Output is a matrix representing the filter kernel)
#18.
imfilter()Filters a multidimensional image with a specified filter kernel.
img = imread('cameraman.tif');
h = fspecial('motion', 20, 45);
motion_blur_img = imfilter(img, h, 'replicate');
imshow(motion_blur_img);
title('Motion Blurred Image');Output: A figure window shows the cameraman image with a motion blur effect applied at a 45-degree angle.
#19.
medfilt2()Performs 2-D median filtering, which is excellent for removing 'salt & pepper' noise.
noisy_img = imnoise(imread('cameraman.tif'), 'salt & pepper', 0.02);
denoised_img = medfilt2(noisy_img);
imshow(denoised_img);
title('Denoised with Median Filter');Output: A figure window shows the noisy image significantly cleaned up, with most salt & pepper noise removed.
#20.
edge()Finds edges in an intensity image using various algorithms (e.g., Sobel, Canny).