实验环境

(1)硬件环境

机械臂一台:具备视觉识别与抓取功能的机械臂设备
摄像头:用于采集图像并进行垃圾识别的视觉设备
垃圾样本:多种可识别类别的垃圾物品(如易拉罐、报纸、果皮等)

(2)软件环境

Vmware虚拟环境:运行Ubuntu系统的虚拟化平台
Python 3.x 环境:支持PyTorch和计算机视觉库运行
OpenCV库:用于图像处理和视觉识别的计算机视觉库
PyTorch框架:用于加载和运行预训练的垃圾识别模型
Jupyter Notebook:用于运行和调试垃圾识别代码的交互式环境
预训练模型:model0.pt(垃圾识别深度学习模型)

实验步骤

注意:请确保摄像头视觉范围内出现待识别的垃圾物品。
  1. 启动虚拟机,在Windows电脑中,使用Vmware运行Ubuntu系统。
  2. 找到路径 /Home/Dofbot/6.AI_Visual/7.Garbage identification.ipynb,右击选择用Jupyter Notebook启动,输入密码 shujia
  3. 按照代码单元顺序执行程序,启动垃圾识别功能。
  4. 将垃圾物品置于摄像头前方,观察识别效果,系统会在物品区域绘制蓝色方框进行标记。
  5. 实验结束后,通过程序退出功能关闭摄像头和识别进程。

实验结果

垃圾识别结果

主要代码

代码路径:/Home/Dofbot/6.AI_Visual/7.Garbage identification.ipynb

1. 模型导入与初始化(garbage_identify.py):

#!/usr/bin/env python3
# coding: utf-8
import time
import torch
import rospy
import Arm_Lib
import cv2 as cv
import numpy as np
from time import sleep
from numpy import random

from utils.torch_utils import select_device
from models.experimental import attempt_load
from utils.general import non_max_suppression, scale_coords, xyxy2xywh, plot_one_box

# 模型路径
model_path = '/home/shujia/Dofbot/6.AI_Visual/model0.pt'
# 初始化设备
device = select_device()
# 加载模型
model = attempt_load(model_path, map_location=device)

2. 垃圾识别函数(single_garbage_identify.py):

def single_garbage_run(self, image):
    '''
    执行垃圾识别函数
    :param image: 原始图像
    :return: 识别后的图像,识别信息(name, pos)
    '''
    while 1:
        self.frame = cv.resize(image, (640, 480))
        try: 
            self.garbage_getName()
        except Exception: 
            print("sqaure_pos empty")
        return self.frame

3. 垃圾名字与分类列表(single_garbage_identify.py):

def garbage_getName(self):
    name = "None"
    if self.status == 'waiting':
        self.frame, msg = self.garbage_identify.garbage_run(self.frame)
        for key, pos in msg.items(): 
            name = key
        # 根据识别到的垃圾名称设置编号和类别
        if name == "Zip_top_can":              (self.garbage_num, self.garbage_class) = ('00', '01')
        if name == "Old_school_bag":           (self.garbage_num, self.garbage_class) = ('01', '01')
        if name == "Newspaper":                (self.garbage_num, self.garbage_class) = ('02', '01')
        if name == "Book":                     (self.garbage_num, self.garbage_class) = ('03', '01')
        if name == "Toilet_paper":             (self.garbage_num, self.garbage_class) = ('04', '02')
        if name == "Peach_pit":                (self.garbage_num, self.garbage_class) = ('05', '02')
        if name == "Cigarette_butts":          (self.garbage_num, self.garbage_class) = ('06', '02')
        if name == "Disposable_chopsticks":    (self.garbage_num, self.garbage_class) = ('07', '02')
        if name == "Egg_shell":                (self.garbage_num, self.garbage_class) = ('08', '03')
        if name == "Apple_core":               (self.garbage_num, self.garbage_class) = ('09', '03')
        if name == "Watermelon_rind":          (self.garbage_num, self.garbage_class) = ('10', '03')
        if name == "Fish_bone":                (self.garbage_num, self.garbage_class) = ('11', '03')
        if name == "Expired_tablets":          (self.garbage_num, self.garbage_class) = ('12', '04')
        if name == "Expired_cosmetics":        (self.garbage_num, self.garbage_class) = ('13', '04')
        if name == "Used_batteries":           (self.garbage_num, self.garbage_class) = ('14', '04')
        if name == "Syringe":                  (self.garbage_num, self.garbage_class) = ('15', '04')
        if name == "None":                     (self.garbage_num, self.garbage_class) = ('None', 'None')

4. 主线程(7.Garbage identification.ipynb):

def camera():
    # 打开摄像头
    capture = cv.VideoCapture(0)
    # 当摄像头正常打开的情况下循环执行
    while capture.isOpened():
        try:
            _, img = capture.read()
            img = cv.resize(img, (640, 480))
            # 调用垃圾识别函数
            img = single_garbage.single_garbage_run(img)
            if model == 'Exit':
                cv.destroyAllWindows()
                capture.release()
                break
            # 更新图像显示
            imgbox.value = cv.imencode('.jpg', img)[1].tobytes()
        except KeyboardInterrupt:
            capture.release()

可识别垃圾类别说明:

垃圾名称 编号 类别代码 类别说明
Zip_top_can(易拉罐)、Old_school_bag(旧书包)、Newspaper(报纸)、Book(书籍) 00-03 01 可回收物
Toilet_paper(卫生纸)、Peach_pit(桃核)、Cigarette_butts(烟蒂)、Disposable_chopsticks(一次性筷子) 04-07 02 其他垃圾
Egg_shell(蛋壳)、Apple_core(苹果核)、Watermelon_rind(西瓜皮)、Fish_bone(鱼骨) 08-11 03 厨余垃圾
Expired_tablets(过期药片)、Expired_cosmetics(过期化妆品)、Used_batteries(废旧电池)、Syringe(注射器) 12-15 04 有害垃圾
代码说明:本实验基于深度学习模型实现垃圾识别功能,通过PyTorch框架加载预训练的model0.pt模型。核心流程为:摄像头采集图像→预处理( resize 至640×480)→模型推理识别→标记识别结果(蓝色方框)→分类编号映射。系统可识别16种常见垃圾,并将其分为可回收物(01)、其他垃圾(02)、厨余垃圾(03)和有害垃圾(04)四类,为机械臂进行垃圾分类抓取提供精准的视觉定位与类别信息。