The complete implementation of intelligent classification trash can based on yolov5

Recently, I have done an undergraduate graduation project with my lab classmates. The purpose of the completion project is to make an intelligent sorting trash can. The camera takes an object, and then automatically identifies whether the object is recyclable garbage, harmful garbage or other garbage, and then controls it. The steering gear opens the corresponding trash can lid, and it can actually make kitchen waste, but because of the lack of data sets, in order to reduce the workload, I did not add a new kitchen waste data set (I don’t want to type so many labels myself Hahaha).

The following describes the overall process of the project: First, deploy the yolov5 environment on the computer. I use Anaconda+Pycharm, and the yolov5 code uses the standard code provided on github, but in order to fit our project, I use detect.py I added some of my own code to the file, and I will attach the overall yolov5 code at the end of the whole article for your convenience.

After the yolov5 environment is deployed, run the detect.py file, then the camera will open, and a window will pop up automatically, as shown below:

 At this time, press the Enter key to automatically take a photo and save it locally, and then press the ESC key to continue to execute. I use an absolute path, so if you want to deploy it locally, you need to modify it The path to save the photo is modified in the detect.py file, as follows:

output_dir = 'D:\yolov5'
i = 1
cap = cv2.VideoCapture(0)
while 1:
    ret, frame = cap.read()
    cv2.imshow('cap', frame)
    flag = cv2.waitKey(1)
    if flag == 13:

        output_path = os.path.join(output_dir, "1.jpg")
        cv2.imwrite(output_path, frame)
    i += 1
    if flag == 27:
         break

Just modify output_dir = 'D:\yolov5'.

After the previous step is completed, yolov5 will recognize the object in the photo just taken, and then the recognized object type will be stored in the class_label variable, and then this variable will be transmitted to the Raspberry Pi through the socket, and the Raspberry Pi will receive this variable. Make some judgments to determine which servo to turn on. After the steering gear is turned on, the next step is that the Raspberry Pi will send the received object type together with the temperature and humidity sensor information on the trash can to the notebook through the socket. The notebook has a front-end interface, and the transmitted information will be It exists in the database, so that all the information will be aggregated, and a simple completion work is completed.

Let’s talk about the details of each step in detail, and all files, codes, and finished pictures will be displayed one by one.

The first is the yolov5 code, how to deploy yolov5, there are many tutorials on the Internet, you can search the Internet, and secondly, I added the socket transmission code in detect.py. If you deploy, this place needs to modify the IP address. Change it to the IP address of the device that you accept variable data. For example, if I want to transmit variables to the Raspberry Pi, then I will fill in the IP address of the Raspberry Pi, and fill in the port number as 9000. The problem should be solved. The code is as follows:

import socket

sk = socket.socket()
ip_port = ("192.168.1.108", 9000)
sk.connect(ip_port)
sk.send(class_label.encode())

The received code should also be deployed on the Raspberry Pi side, the code is as follows:

def talk(conn):
    while 1: # cyclic communication
        try:
            channel = conn.recv(1024).decode('utf-8')
            if not channel:
                break
            print("Process <%s> message from client:% s" % (os.getpid(), channel))
            checkChenel(channel)
            conn.send(channel.encode('utf-8'))
        except:
            break
    conn.close()
 
 
def socket():
    import socket
    print(" start socket")
    server_sk = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    server_sk.bind(('192.168.1.108', 9000))
    server_sk.listen(5)
    while 1:
        new_sk,addr = server_sk.accept()
        p = Process(target=talk, args=(new_sk,))
        p.start()
    server_sk.close()

In this way, the two ends can communicate, so that the object type parameters can be transmitted. There is another important issue to pay attention to. When using a socket, you cannot use a single thread. You must start a double thread, otherwise you will report an error. In this way, you must add a code to create a new thread under the above code. ,code show as below:

 # Create a thread
mthread = threading.Thread(target=socket)
# # Start the newly created thread
mthread .start()
 
# Set frequency to 50hz, good for servos.
pwm.set_pwm_freq(50)
 
print('Moving servo on channel x , press Ctrl-C to quit...')
#while True:
# channel=int(input('pleas input channel:'))
# Move servo on channel O between extremes.
 

This allows normal parameter transfer. Then use the following code to control the angle corresponding to the rotation of the servo:

def checkChenel(channel):
    channel = int(channel)
    if channel == 1:
        set_servo_angle(channel, -15)
        time.sleep(5)
        set_servo_angle(channel, 110)
        time.sleep(0.5)
    elif channel == 2 :
        set_servo_angle(channel, 60)
        time.sleep(5)
        set_servo_angle(channel, 170)
        time.sleep(0.5)
    elif channel == 3 :
        set_servo_angle(channel, 60)
        time.sleep(5)
        set_servo_angle(channel, 170)
        time.sleep(0.5)
    elif channel == 4 :
        set_servo_angle(channel, 60)
        time.sleep(5)
        set_servo_angle(channel, 170)
        time.sleep(0.5)

The specific display of the above parts is as follows:

1.yolov5 recognizes what category a specific object belongs to:

At this time, the second type is recognized, that is, harmful garbage. This is modified in the yolov5 configuration file, as shown in the following figure:

 When configuring, these absolute addresses need to be modified. If there is an error, it is basically because the absolute address has not been modified. Please check it patiently and it will be OK.

2. Raspberry Pi receives the category information, controls the servo to open the lid of the trash can:

3. There is a temperature and humidity sensor outside the Raspberry Pi. After turning on the steering gear, the Raspberry Pi will summarize the garbage category information and the information of the external temperature and humidity sensor, and transmit it to the specially made front-end background through the socket, and store it in the database. , and then the category information and temperature and humidity information of this garbage classification will be displayed on the front-end interface.

The complete demonstration video is in my personal space, you can go in and view it

Smart Trash Can Demonstration Video

I have compiled all the codes and data sets, and you can pick them up if you need them.

 Link: https://pan.baidu.com/s/1x26GFAkIlbUnPvW2KJ2KZw?pwd=0412 
Extraction code: 0412

Guess you like

Origin blog.csdn.net/m0_61438337/article/details/123607966