# Video Demo {%youtube OLvcHbxrx7E %} # PPT for the presentation in 2024/01/04 [PPT](https://drive.google.com/file/d/1WyCARhTa8HKj1XTdzUYo_Yj8KncDimOR/view?usp=sharing) # Design Draft ## Cockroach Box ![image](https://hackmd.io/_uploads/rkLhytHOp.png) ### 1. Camera: Catch real-time video to detect Cockroach by Yolo v8 and streaming on the website. ### 2. Hitter: Let the Acrylic board rotate to hit cockroach, which is powered by SG90. ### 3. Cleaner: Use two SG90 to let the board move forward and backward to clean the cockroach away from the camera's sight. > If we don't do it, the hitter will hit the cockroach continuously due to the false detection from camera. ## Website ![image](https://hackmd.io/_uploads/r1ihkFS_T.png) ### 1. Reference: https://www.instructables.com/Video-Streaming-Web-Server/ ### 2. Framework: Flask and Python ### 3. Streaming: Catch the video from Raspberry Pi for playing the real-time video in the cockroach box. ### 4. Pay and Hit bottom: Pay to let the hitter start to work for hitting the cockroach. # Necessary Equipment ## Something you should buy **1. Acrylic Board** [2mm透明 30cm x 30cm透明壓克力板](https://shopee.tw/2mm%E9%80%8F%E6%98%8E-30cm~40cm%E9%80%8F%E6%98%8E%E5%A3%93%E5%85%8B%E5%8A%9B%E6%9D%BF-i.7116182.9370521218) ![image](https://hackmd.io/_uploads/HJCgotSua.png) **2. Acrylic Knife** [壓克力板DIY切割刀 勾刀 塑膠板切割刀 鉤刀 PCB線路勾刀](https://shopee.tw/%E5%A3%93%E5%85%8B%E5%8A%9B%E5%88%80-%E5%A3%93%E5%85%8B%E5%8A%9B%E6%9D%BFDIY%E5%88%87%E5%89%B2%E5%88%80-%E5%8B%BE%E5%88%80-%E5%A1%91%E8%86%A0%E6%9D%BF%E5%88%87%E5%89%B2%E5%88%80-%E9%89%A4%E5%88%80-PCB%E7%B7%9A%E8%B7%AF%E5%8B%BE%E5%88%80-%E7%BE%8E%E5%B7%A5%E5%88%80-%E5%8F%B0%E7%81%A3%E7%8F%BE%E8%B2%A8-i.7116182.16170627036) ![image](https://hackmd.io/_uploads/rkyCoYBdT.png) **3. Acrylic Glue** [壓克力專用膠水](https://shopee.tw/%E5%A3%93%E5%85%8B%E5%8A%9B%E5%B0%88%E7%94%A8%E8%86%A0%E6%B0%B4-%E5%A3%93%E5%85%8B%E5%8A%9B%E9%BB%8F%E5%90%88%E5%B0%88%E7%94%A8-%E5%A3%93%E5%85%8B%E5%8A%9B%E8%86%A0%E6%B0%B4-%E5%A3%93%E5%85%8B%E5%8A%9B%E9%BB%8F%E8%91%97%E5%8A%91-%E5%A3%93%E5%85%8B%E5%8A%9B%E8%86%A0-%E9%BB%8F%E5%A3%93%E5%85%8B%E5%8A%9B%E5%B0%88%E7%94%A8-%E5%A3%93%E5%85%8B%E5%8A%9B%E5%BF%AB%E4%B9%BE-i.7116182.18348774644) ![image](https://hackmd.io/_uploads/S1KRiKBOa.png) **4. padlock** [壓克力配件 扣掛鎖 三件式圓鎖](https://shopee.tw/%E5%A3%93%E5%85%8B%E5%8A%9B%E9%85%8D%E4%BB%B6-%E6%89%A3%E6%8E%9B%E9%8E%96-%E4%B8%89%E4%BB%B6%E5%BC%8F%E5%9C%93%E9%8E%96-i.7116182.21119757785) ![image](https://hackmd.io/_uploads/H1s_hKBda.png) **5. SG90 servo motor bracket** [SG90 伺服馬達 支架 固定架含螺絲](https://shopee.tw/%E3%80%90%E6%A8%82%E6%84%8F%E5%89%B5%E5%AE%A2%E5%AE%98%E6%96%B9%E5%BA%97%E3%80%91%E3%80%8A%E9%99%84%E7%99%BC%E7%A5%A8%E3%80%8BSG90-%E4%BC%BA%E6%9C%8D%E9%A6%AC%E9%81%94-%E6%94%AF%E6%9E%B6-%E5%9B%BA%E5%AE%9A%E6%9E%B6%E5%90%AB%E8%9E%BA%E7%B5%B2-i.139069730.2911262401) ![image](https://hackmd.io/_uploads/Hy3fTFHup.png) **6. camera bracket** [樹莓派Raspberry Pi 3B+ 4B 通用攝像頭支架 壓克力支架](https://shopee.tw/%E3%80%90%E6%A8%82%E6%84%8F%E5%89%B5%E5%AE%A2%E5%AE%98%E6%96%B9%E5%BA%97%E3%80%91%E3%80%8A%E9%99%84%E7%99%BC%E7%A5%A8%E3%80%8B%E6%A8%B9%E8%8E%93%E6%B4%BERaspberry-Pi-3B-4B-%E9%80%9A%E7%94%A8%E6%94%9D%E5%83%8F%E9%A0%AD%E6%94%AF%E6%9E%B6-%E5%A3%93%E5%85%8B%E5%8A%9B%E6%94%AF%E6%9E%B6-(%E4%B8%8D%E5%90%AB%E6%94%9D%E5%83%8F%E9%A0%AD)-i.139069730.5952962263) ![41c48c633d940f71e5b677cd77903e43~1](https://hackmd.io/_uploads/SkRuatSOp.jpg) **7. Card sleeve hub** [伸縮鑰匙扣](https://shopee.tw/%E7%99%BB%E5%B1%B1%E6%89%A3%E4%BC%B8%E7%B8%AE%E9%91%B0%E5%8C%99%E6%89%A3-%E5%B9%B3%E9%9D%A2%E9%91%B0%E5%8C%99%E6%89%A3-%E5%8A%A0%E5%8E%9A%E5%B0%BC%E9%BE%8D%E7%B9%A9-%E6%88%B6%E5%A4%96%E7%A3%A8%E7%A0%82%E6%98%93%E6%8B%89%E6%89%A3-%E5%A1%91%E6%96%99%E5%9C%93%E5%BD%A2%E7%99%BB%E5%B1%B1%E6%89%A3%E4%BC%B8%E7%B8%AE%E6%98%93%E6%8B%89%E5%BE%97-i.5886119.11826623994?sp_atk=eddf3361-6dca-47d3-baaf-fb52f6cd192b&xptdk=eddf3361-6dca-47d3-baaf-fb52f6cd192b) ![image](https://hackmd.io/_uploads/ByAUk9Bd6.png) ## Already inside the box **1. SG90 360 degree roation** ![image](https://hackmd.io/_uploads/HyoQRYBO6.png) **2. Camera** ![image](https://hackmd.io/_uploads/SJemL0FHua.png) # Hardware ## Cockroach box 1. Cut the door on acrylic by acrylic knife > cut it back and forth 100 times, it will break. ![image](https://hackmd.io/_uploads/B1ZM-5HO6.png) 2. Cut Installation location for SG90 for the cleaner on acrylic by acrylic knife ![20240105_222710~1](https://hackmd.io/_uploads/HyEzf9Hu6.jpg) 3. Combine the 5 pieces of acrylic boards together as a box ![20240105_223043~1](https://hackmd.io/_uploads/BklCzcHdp.jpg) 4. Build the camera stage ![20240105_223220~1](https://hackmd.io/_uploads/H1EQX9SuT.jpg) ## Camera module 1. install Camera module in the camera bracket ![20240105_223401~1](https://hackmd.io/_uploads/SkZxEqSdp.jpg) 2. Paste it on the camera stage by acrylic glue ![20240105_223413~1](https://hackmd.io/_uploads/BkjmVcBua.jpg) ## Hitter 1. Find a unused acrylic board, paste padlock set on it ![20240105_223832~1](https://hackmd.io/_uploads/SktOH9B_6.jpg) 2. Fix the SG90 motor on the bracket, tie a line on the arm, and the line which is connected to (1.) board. Finally, paste bracket on the side of the cockroach box ![20240105_223852~1](https://hackmd.io/_uploads/SJu7U5S_a.jpg) 3. Paste a keyhole which is including the set of padlock on the wall ![20240105_223846 (1)~1](https://hackmd.io/_uploads/SytuP5r_6.jpg) 4. All setted up ![20240105_225218~1](https://hackmd.io/_uploads/S1pAv5rO6.jpg) ## Cleaner 1. Destroy Card sleeve hub, and get this ![20240103_182558~2](https://hackmd.io/_uploads/SJHRuqHOT.jpg) 2. Tie another card sleeve hub and this by a line like it ![20240103_215958~1](https://hackmd.io/_uploads/rJ6MKqHuT.jpg) 3. install SG90 motor(already install in the bracket) on the hole we cut in the previous chapter ![20240103_185719~1](https://hackmd.io/_uploads/BJwiF5Sda.jpg) 4. The arm will make the reel rotate ![20240104_230616~1](https://hackmd.io/_uploads/Sko79cru6.jpg) 5. Tied the paper board ![20240104_093254~1](https://hackmd.io/_uploads/S1ObiqHO6.jpg) # Software ## Environment settings ### 1. Install OS > DON'T INSTALL OS "BUSTER" as EECLASS said 1. install [Raspberry Pi Imager](https://www.raspberrypi.com/software/) and open it. 2. choose 64-bit OS ![image](https://hackmd.io/_uploads/SycDe2md6.png) 3. After installing into SD card, insert the SD card in the Raspiberry and plug in the power to turn on the Raspiberry. ### 2. Install Camera Module > The following script should run in RaspberryPi 1. Close the warning message for "pip" script ``` sudo mv /usr/lib/python3.11/EXTERNALLY-MANAGED /usr/lib/python3.11/EXTERNALLY-MANAGED.bk ``` 2. Install Meson ``` wget https://github.com/mesonbuild/meson/releases/download/1.3.1/meson-1.3.1.tar.gz ``` ``` tar xvf meson-1.3.1.tar.gz ``` ``` cd meson-1.3.1 ``` ``` python meson.py ``` >Set environment variables ``` export PATH="$PATH":/home/even/libcamera/meson-1.3.1 sudo apt install meson ``` 3. Install CMake ``` sudo apt install cmake ``` 4. Install Ply ``` pip install ply ``` 5. Install libcamera ``` git clone https://git.libcamera.org/libcamera/libcamera.git ``` ``` cd libcamera ``` ``` meson setup build ``` ``` ninja -C build install ``` 6. Let's give it a try ``` libcamera-hello ``` ### 3. Install ultralytics (yolo v8) ``` sudo apt-get update ``` ``` sudo apt-get upgrade -y ``` ``` sudo apt-get autoremove -y ``` ``` pip3 install ultralytics ``` Reboot the Raspberry ``` sudo reboot ``` ## Train a Cockroach Detection Model >The Following actions should run in your laptop ### 1. Prepare Dataset 1. Collect Dataset ![image](https://hackmd.io/_uploads/HkhEr2XuT.png) 2. Install LabelImg > install by pip ``` pip install labelImg ``` >open labelImg ``` labelImg ``` 3. Label Cockroach > Launch LabelImg to label Cockroach ![螢幕擷取畫面 2024-01-05 161447](https://hackmd.io/_uploads/HJVicNBup.jpg) ### 2. Cut Dataset ###### 資料分配比例 1. Train Set > 70% 2. Validate Set > 20% 3. Test Set > 10% ### 3. Write the .yaml File ![image](https://hackmd.io/_uploads/rJVsaaXdp.png) **The Example of train/val/test.txt File** ![image](https://hackmd.io/_uploads/B12GRaQd6.png) **File Structure** ![File Tree](https://hackmd.io/_uploads/HJ9EARVdT.png) ### 4. Package and upload to Google Drive **Share the file with public** ![image](https://hackmd.io/_uploads/BkydxkHOT.png) **Copy the File ID** ![image](https://hackmd.io/_uploads/H1JIW1B_p.png) **Use this ID in Colab to Download your dataset** ![image](https://hackmd.io/_uploads/ryXl-kSO6.png) > Because it will be pretty faster compare with uploading to Colab directl ### 5. Training 1. Use the following code to Train [Train Yolo v8 nano in Colab](https://colab.research.google.com/drive/1c-I2KMwe6cD4SWf-Iyz4xY00u2_Fb8ha?usp=sharing) ![image](https://hackmd.io/_uploads/rJ1Y0cr_6.png) 2. Get the .pt file **This is the File Tree of weight file for Yolo v8 in Colab** ![image](https://hackmd.io/_uploads/HkTGo4rda.png) ## Yolo in RaspberryPi > You need to put your .pt file in your RaspberryPi > But save in the dir of Ultralytics package is not necessary, just save wherever you like. ### 1. Get the center of the detect cockroach > Yolo will provide the coordinates of left-top and right-bottom of the detected object. > ![predictions~1](https://hackmd.io/_uploads/BJLIhBHd6.jpg) ``` def getMiddlePoint(A, B): return int((A+B)/2) ``` 2. Measure the Hittable area ![20240104_082846~1](https://hackmd.io/_uploads/H1mj3BBd6.jpg) ``` def isMeetCockroach(x ,y): RangeX = 853 RangeY = 418 if(x > RangeX and y > RangeY): return True else: return False ``` ### 3. Run Yolo > Catch streaming vid at tcp://127.0.0.1:8888 ``` model = YOLO('yolov8n_cockroach.pt') results = model('tcp://127.0.0.1:8888', stream=True) while True: for result in results: boxes = result.boxes probs = result.probs ``` ### 4. Hit Cockroach when it enter the areas that can be struck ``` def meetCockroach(): servoPIN = 13 GPIO.setmode(GPIO.BCM) GPIO.setup(servoPIN, GPIO.OUT) p = GPIO.PWM(servoPIN, 10) p.start(2.5) print("Hitted!") #Hit p.ChangeDutyCycle(5) time.sleep(1.75) p.stop() GPIO.cleanup() print("break!") # Take a break time.sleep(5) # reset servoPIN = 13 GPIO.setmode(GPIO.BCM) GPIO.setup(servoPIN, GPIO.OUT) p = GPIO.PWM(servoPIN, 10) p.start(2.5) print("Release!") #Release p.ChangeDutyCycle(10) time.sleep(1.75) p.stop() GPIO.cleanup() ``` ### 5. Total code ``` from ultralytics import YOLO import RPi.GPIO as GPIO import time def getMiddlePoint(A, B): return int((A+B)/2) def isMeetCockroach(x ,y): RangeX = 853 RangeY = 418 if(x > RangeX and y > RangeY): return True else: return False def meetCockroach(): servoPIN = 13 GPIO.setmode(GPIO.BCM) GPIO.setup(servoPIN, GPIO.OUT) p = GPIO.PWM(servoPIN, 10) p.start(2.5) print("Hitted!") #Hit p.ChangeDutyCycle(5) time.sleep(1.75) p.stop() GPIO.cleanup() print("break!") # Take a break time.sleep(5) # reset servoPIN = 13 GPIO.setmode(GPIO.BCM) GPIO.setup(servoPIN, GPIO.OUT) p = GPIO.PWM(servoPIN, 10) p.start(2.5) print("Release!") #Release p.ChangeDutyCycle(10) time.sleep(1.75) p.stop() GPIO.cleanup() model = YOLO('yolov8n_cockroach.pt') results = model('tcp://127.0.0.1:8888', stream=True) while True: for result in results: boxes = result.boxes for box in boxes: b = box.xyxy[0] # get box coordinates in (left, top, right, bottom) format middleX = getMiddlePoint(b.numpy()[0],b.numpy()[2]) middleY = getMiddlePoint(b.numpy()[1],b.numpy()[3]) print(middleX,middleY) if(isMeetCockroach(middleX ,middleY)): print("HELLO") meetCockroach() probs = result.probs ``` ## Control Hitter ### 1. The Shape of the Hitter ![20240104_231600(0)~1](https://hackmd.io/_uploads/ByeKOSrdT.jpg) ![20240104_230657~1](https://hackmd.io/_uploads/Hk6o_rrOp.jpg) ### 2. Turn clockwise to tighten the rope ``` import RPi.GPIO as GPIO import time servoPIN = 13 GPIO.setmode(GPIO.BCM) GPIO.setup(servoPIN, GPIO.OUT) p = GPIO.PWM(servoPIN, 10) p.start(2.5) try: p.ChangeDutyCycle(10) time.sleep(1.75) p.stop() except KeyboardInterrupt: p.stop() GPIO.cleanup() ``` ### 3. Turn counterclockwise to loosen the rope ``` import RPi.GPIO as GPIO import time servoPIN = 13 GPIO.setmode(GPIO.BCM) GPIO.setup(servoPIN, GPIO.OUT) p = GPIO.PWM(servoPIN, 10) p.start(2.5) try: p.ChangeDutyCycle(5) time.sleep(1.75) p.stop() except KeyboardInterrupt: p.stop() GPIO.cleanup() ``` ## Deploy to RasberryPi > The Following scripts should run in RasberryPi ### 1. Run Camera module ``` libcamera-vid -n -t 0 --width 1280 --height 960 --framerate 1 --inline --listen -o tcp://127.0.0.1:8888 ``` > The port tcp://127.0.0.1:8888 is waiting for any Process to connect. ![image](https://hackmd.io/_uploads/S1U4yBSOa.png) ### 2. Run the Code in Thonny ![image](https://hackmd.io/_uploads/HJH0MBrOp.png) ### 3. Make sure that Yolo is successfully connect with the camera ![Yolo 連結到camera](https://hackmd.io/_uploads/Sy9B7SHO6.png) ### 4. Start to detect Cockroach ![螢幕擷取畫面 2024-01-04 231237](https://hackmd.io/_uploads/Skkc7HS_6.png) # Still On the way ## Cleaner #### SG90 is out of control It is difficult to control two SG90 motor in the same code. {%youtube vQjAjwJGI4s %} ``` import RPi.GPIO as GPIO import time # Define GPIO pins for the servos servoRight = 12 servoLeft = 13 # Setup GPIO GPIO.setmode(GPIO.BCM) GPIO.setup(servoRight, GPIO.OUT) GPIO.setup(servoLeft, GPIO.OUT) # Initialize PWM for both servos pRight = GPIO.PWM(servoRight, 50) # 50Hz frequency pLeft = GPIO.PWM(servoLeft, 50) # 50Hz frequency # Start PWM with initial duty cycle (2.5% might be a neutral position) pRight.start(2.5) pLeft.start(2.5) try: # Turn servoRight to one direction pRight.ChangeDutyCycle(18) # Turn servoLeft to another direction pLeft.ChangeDutyCycle(50) time.sleep(5) except KeyboardInterrupt: pass # Cleanup pRight.stop() pLeft.stop() GPIO.cleanup() ``` ## Website ### 1. Conflict between Flask and Pycamera2 ![image](https://hackmd.io/_uploads/ByQi29rdT.png) ### 2. Conflict between Yolo and the other process > No idea. # Reference ## Yolo V8 offical website https://docs.ultralytics.com/modes/train/#key-features-of-train-mode