Welcome to JetCobot repository
7.Garbage recognition
Garbage recognition
By testing the trained model, the trained object names can be recognized.
Code path:
~/jetcobot_ws/src/jetcobot_garbage_yolov5/Garbage_identification.ipynb
The specific code can be viewed in the code path.
Import various libraries
xxxxxxxxxximport cv2 as cvimport threadingfrom time import sleepimport ipywidgets as widgetsfrom IPython.display import displayfrom single_garbage_identify import single_garbage_identifymodel = attempt_load(model_path, map_location=device)The camera shows the garbage identification screen
xxxxxxxxxxdef camera():# 打开摄像头 Open cameracapture = cv.VideoCapture(0)capture.set(cv.CAP_PROP_FRAME_WIDTH, 640)capture.set(cv.CAP_PROP_FRAME_HEIGHT, 480)# 当摄像头正常打开的情况下循环执行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()breakimgbox.value = cv.imencode('.jpg', img)[1].tobytes()except KeyboardInterrupt:capture.release()
Garbage identification function
xxxxxxxxxxdef single_garbage_run(self, image): self.frame = cv.resize(image, (640, 480)) try: self.garbage_getName() except Exception: print("sqaure_pos empty") return self.frameList of junk names
xxxxxxxxxx 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')Click the toolbar of jupyterlab to run all program blocks, then pull to the bottom of the program to view the camera image, place the building block facing the camera, and the image will automatically recognize the corresponding garbage name.

Note: The building block needs to be facing the camera so that the camera screen can see the picture and name directly as shown in the picture above. If the direction difference is too large, it will affect the recognition rate.