Devices & Components
Arduino Uno Rev3
Resistor 220ohm
LCD 1602 IIC/I2C module
Jumper wire
Software & Tools
Python 3
Arduino IDE
Project description
Code
Arduino
cpp
1#include <LiquidCrystal.h> 2 3// RS, E, D4, D5, D6, D7 4LiquidCrystal lcd(12, 11, 5, 4, 3, 2); 5 6char colorCmd = 'N'; // 接收颜色命令 7 8void setup() { 9 lcd.begin(16, 2); // 16x2 LCD 10 lcd.print("Ready..."); 11 Serial.begin(9600); 12} 13 14void loop() { 15 if (Serial.available()) { 16 colorCmd = Serial.read(); 17 lcd.clear(); 18 lcd.setCursor(0,0); 19 lcd.print("Detected Color:"); 20 lcd.setCursor(0,1); 21 22 if (colorCmd == 'R') lcd.print("RED"); 23 else if (colorCmd == 'G') lcd.print("GREEN"); 24 else if (colorCmd == 'B') lcd.print("BLUE"); 25 else lcd.print("NONE"); 26 } 27}
Python
python
1import cv2 2import numpy as np 3import serial 4import time 5 6# Arduino port, COM5 is the USB port of arduino (this can be changed based on the COM port of your computer) 7arduino = serial.Serial('COM5', 9600) 8time.sleep(2) 9 10cap = cv2.VideoCapture(0) 11 12# HSV RGB range 13lower_red1 = np.array([0, 50, 50]) 14upper_red1 = np.array([10, 255, 255]) 15lower_red2 = np.array([160, 50, 50]) 16upper_red2 = np.array([180, 255, 255]) 17 18lower_green = np.array([35, 50, 50]) 19upper_green = np.array([85, 255, 255]) 20 21lower_blue = np.array([100, 50, 50]) 22upper_blue = np.array([140, 255, 255]) 23 24while True: 25 ret, frame = cap.read() 26 if not ret: 27 break 28 29 hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV) 30 31 # generate mask for every colour 32 mask_r = cv2.inRange(hsv, lower_red1, upper_red1) + cv2.inRange(hsv, lower_red2, upper_red2) 33 mask_g = cv2.inRange(hsv, lower_green, upper_green) 34 mask_b = cv2.inRange(hsv, lower_blue, upper_blue) 35 36 # find the percentage(pixel) of a colour in the camera 37 r_area = np.sum(mask_r > 0) 38 g_area = np.sum(mask_g > 0) 39 b_area = np.sum(mask_b > 0) 40 41 # find the colour that takes the most area 42 areas = {'R': r_area, 'G': g_area, 'B': b_area} 43 max_color = max(areas, key=areas.get) 44 45 # if area too small, set to none 46 if areas[max_color] < 50: 47 max_color = 'N' 48 49 # send to Arduino 50 arduino.write(max_color.encode()) 51 52 # visualize it 53 cv2.putText(frame, f"Max Color: {max_color}", (10,30), cv2.FONT_HERSHEY_SIMPLEX, 54 1, (0,255,0), 2) 55 cv2.imshow("Camera Frame", frame) 56 57 if cv2.waitKey(1) & 0xFF == ord('q'): 58 break 59 60cap.release() 61cv2.destroyAllWindows() 62arduino.close()
Downloadable files
arduino code
hand_ai.ino
python code
camera.py
Documentation
colour detecting system project report
colour_detection.pdf
Comments
Only logged in users can leave comments