Skip to content

jojohnathon/garbage-classification

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

29 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Garbage Classification

IDE Setup

Required Software

  • PyCharm: Download here
  • Python: Download here
    • Note: This project uses Python 3.12.0, but later versions should work as well.
    • Check your Python version:
      python --version

Python Version

Setting Up Your Environment

  1. Create a New Project in PyCharm

    • Open PyCharm and select "New Project".
    • Choose a location and ensure the correct Python interpreter is selected.
  2. Install Required Libraries

    • Open the terminal and run:
      pip install cvzone
      pip install opencv-python
      pip install numpy
    • Or install directly within PyCharm:

    Installation Screenshot


CVZone

CVZone GitHub Repository

Basic Setup

import cvzone
import cv2

Example Usage

img = cv2.imread('path_to_image.jpg')
imgGray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
cv2.imshow('Gray Image', imgGray)
cv2.waitKey(0)

Implementing YOLO

Loading YOLO in Your Program

from ultralytics import YOLO

Adding YOLO Weights

model = YOLO("../Yolo-Weights/yolov8n.pt")  # Nano
model = YOLO("../Yolo-Weights/yolov8s.pt")  # Small

YOLO Example

Expanding Object Detection


CVZone & Arduino Integration

Import Required Library

from cvzone.SerialModule import SerialObject

Using the Library

# Uses "pySerial" Package - check serialModule.py (External Libraries -> site-packages -> cvzone)
# Sends Arduino a true/false state. If a face is detected, the light should turn on.
if bboxs:
    arduino.sendData([1])
else:
    arduino.sendData([0])

Arduino Notes

  • Bounding boxes (bboxs) confirm object presence.
  • Ensure the camera is turned off when running the program.

Fine-Tuning & Dataset Labeling


YOLO Example Code

from ultralytics import YOLO
import cv2
import cvzone
import math
import time

cap = cv2.VideoCapture(0)  # Webcam
cap.set(3, 1280)
cap.set(4, 720)

model = YOLO("../Yolo-Weights/yolov8n.pt")

classNames = ["person", "bicycle", "car", "motorbike", "aeroplane", "bus", "train", "truck", "boat", "traffic light", ...]

prev_frame_time = 0
new_frame_time = 0

while True:
    new_frame_time = time.time()
    success, img = cap.read()
    results = model(img, stream=True)
    for r in results:
        boxes = r.boxes
        for box in boxes:
            x1, y1, x2, y2 = map(int, box.xyxy[0])
            w, h = x2 - x1, y2 - y1
            cvzone.cornerRect(img, (x1, y1, w, h))
            conf = math.ceil((box.conf[0] * 100)) / 100
            cls = int(box.cls[0])
            cvzone.putTextRect(img, f'{classNames[cls]} {conf}', (max(0, x1), max(35, y1)), scale=1, thickness=1)
    
    fps = 1 / (new_frame_time - prev_frame_time)
    prev_frame_time = new_frame_time
    print(fps)
    
    cv2.imshow("Image", img)
    cv2.waitKey(1)

Additional Resources


Releases

No releases published

Packages

No packages published

Contributors 3

  •  
  •  
  •