This project performs garbage classification using YOLOv5 for object detection. Each image is processed to detect objects (garbage) and save the results with bounding boxes and confidence scores.
-
os: A standard library for file and directory manipulation, used to navigate the dataset directory and manage output files.
-
shutil: Useful for managing and copying files and directories.
-
numpy: A widely-used library for numerical and array computations. Here, it is useful for data manipulation and image processing.
-
time: A standard library used for tracking the execution time of processes.
-
matplotlib.pyplot: Although imported, it may not be used directly in this code. It is often used for visualizations, such as plotting ROC curves, confusion matrices, or other analysis charts.
-
scikit-learn: Specifically, metrics like
confusion_matrix
,classification_report
, androc_auc_score
can be used for model evaluation. However, these metrics are not used in the code snippet provided. -
json: A standard library for parsing and storing data in JSON format. Not directly used here but can be helpful for configuration management.
-
seaborn: Another visualization library, often used alongside
matplotlib
to create appealing and informative charts. Although imported, it is not utilized directly in this code. -
cv2 (OpenCV): OpenCV is used for reading and manipulating images. Here, it loads images and performs operations such as drawing bounding boxes and labels for each detected object.
-
torch: The primary deep learning library used for loading and running the YOLOv5 model. Here, it loads the YOLOv5 model and applies it to perform object detection.
-
torchvision: A PyTorch library that provides various tools for computer vision, including models, datasets, and transformations. Although not explicitly called in this code, it is essential for certain computer vision tasks.
-
pathlib.Path: Provides an object-oriented approach to file and directory paths, allowing easy creation and management of the output directory.
- Install the required libraries:
pip install opencv-python-headless numpy torchvision
- Clone the YOLOv5 repository:
git clone https://github.com/ultralytics/yolov5.git
- Load the YOLOv5 model: This script uses the pretrained YOLOv5 model for object detection on images.
model = torch.hub.load('ultralytics/yolov5', 'yolov5s', pretrained=True)
- Iterate through Dataset and Perform Detection
for class_name in os.listdir(input_dir): class_path = os.path.join(input_dir, class_name) if os.path.isdir(class_path): for image_name in os.listdir(class_path): image_path = os.path.join(class_path, image_name) if image_path.lower().endswith(('.png', '.jpg', '.jpeg')): image = cv2.imread(image_path) if image is None: continue results = model(image) df = results.pandas().xyxy[0] for index, row in df.iterrows(): x1, y1, x2, y2, conf, cls, label = int(row['xmin']), int(row['ymin']), int(row['xmax']), int(row['ymax']), row['confidence'], int(row['class']), row['name'] cv2.rectangle(image, (x1, y1), (x2, y2), (0, 255, 0), 2) cv2.putText(image, f'{label} {conf:.2f}', (x1, y1 - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2) output_image_path = os.path.join(output_dir, f"detected_{image_name}") cv2.imwrite(output_image_path, image) print(f"Saved detected image to {output_image_path}")