├── LICENSE ├── README.md └── gren.color.traction.finish.py /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2024 mahdieslaminet 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # ColordetectionSure, here's an example README.md file for a color detection project: 2 | 3 | --- 4 | 5 | # Color Detection Project 6 | 7 | This project is designed to detect and recognize colors within images using Python and OpenCV. 8 | 9 | ## Overview 10 | 11 | Color detection is achieved by analyzing the pixels of an image and identifying predominant colors. This process involves: 12 | 13 | - **Loading an Image**: The script loads an image file from the specified path. 14 | - **Analyzing Colors**: It analyzes the colors in the image and identifies the dominant colors present. 15 | - **Displaying Results**: It displays the detected colors along with their corresponding RGB values. 16 | 17 | ## Setup 18 | 19 | ### Installation 20 | 21 | 1. Clone the repository: 22 | 23 | ``` 24 | git clone https://github.com/yourusername/color-detection.git 25 | ``` 26 | 27 | 2. Install the required dependencies: 28 | 29 | ``` 30 | pip install -r requirements.txt 31 | ``` 32 | 33 | ### Usage 34 | 35 | 1. Navigate to the project directory: 36 | 37 | ``` 38 | cd color-detection 39 | ``` 40 | 41 | 2. Run the script: 42 | 43 | ``` 44 | python color_detection.py --image 45 | ``` 46 | 47 | Replace `` with the path to the image file you want to analyze. 48 | 49 | ## Example 50 | 51 | To detect colors in an image named "example.jpg", run the following command: 52 | 53 | ``` 54 | python color_detection.py --image example.jpg 55 | ``` 56 | 57 | ## Dependencies 58 | 59 | - Python 3.x 60 | - OpenCV 61 | - NumPy 62 | 63 | ## Contribution 64 | 65 | Feel free to contribute to this project by forking the repository and creating a pull request. Your suggestions and improvements are welcomed! 66 | 67 | ## License 68 | 69 | This project is licensed under the [MIT License](LICENSE). 70 | 71 | --- 72 | 73 | This README.md provides an overview of the color detection project, including setup instructions, usage examples, dependencies, contribution guidelines, and licensing information. You can expand upon or modify this template based on your project's specific details and requirements. 74 | 75 | video of project 76 | https://drive.google.com/drive/folders/1Pt1JIKReJo03MDVOQDeXDrbFWlvEs8yc 77 | -------------------------------------------------------------------------------- /gren.color.traction.finish.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import requests 3 | import numpy as np 4 | import time 5 | import traceback 6 | import serial # Change the import statement 7 | from serial import Serial, SerialException 8 | 9 | serial_port = 'COM5' # Change this to your Arduino's serial port 10 | baud_rate = 9600 11 | 12 | url = 'http://192.168.41.25/capture' 13 | 14 | arduino = None 15 | 16 | # Set up initial tracking parameters 17 | # Define the lower and upper bounds for orange color in HSV 18 | green_lower = np.array([40, 40, 40]) 19 | green_upper = np.array([50, 255, 255]) 20 | # Create a list to store the tracked points 21 | points = [] 22 | 23 | try: 24 | arduino = Serial(serial_port, baud_rate, timeout=1) 25 | except SerialException as e: 26 | print(f"Serial Exception: {e}") 27 | exit() 28 | except Exception as e: 29 | print(f"Error: {e}") 30 | exit() 31 | 32 | flag = 0 33 | no_face_time = 0.0 34 | 35 | while True: 36 | try: 37 | # Get image from URL 38 | response = requests.get(url) 39 | img_array = np.array(bytearray(response.content), dtype=np.uint8) 40 | img = cv2.imdecode(img_array, -1) 41 | 42 | # Decode image 43 | frame = cv2.imdecode(img_array, -1) 44 | 45 | # Convert BGR to HSV 46 | hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV) 47 | 48 | # Threshold the HSV image to get only green colors 49 | mask = cv2.inRange(hsv, green_lower, green_upper) 50 | 51 | # Perform dilation and erosion to remove noise 52 | mask = cv2.erode(mask, None, iterations=2) 53 | mask = cv2.dilate(mask, None, iterations=2) 54 | 55 | frame_height, frame_width, _ = img.shape 56 | 57 | center_square = [(frame_width // 2 - 20, frame_height //2 - 20), 58 | (frame_width // 2 + 20, frame_height // 2 + 20)] 59 | 60 | # Find contours in the mask 61 | contours, _ = cv2.findContours(mask.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) 62 | 63 | cv2.rectangle(frame, center_square[0], center_square[1], (255, 0, 0), 2) 64 | 65 | # Reset the ball position (center) for each iteration 66 | center = None 67 | 68 | if len(contours) > 0: 69 | # Find the largest contour 70 | c = max(contours, key=cv2.contourArea) 71 | ((x, y), radius) = cv2.minEnclosingCircle(c) 72 | 73 | # Calculate moments for center 74 | M = cv2.moments(c) 75 | if M["m00"] != 0: 76 | center = (int(M["m10"] / M["m00"]), int(M["m01"] / M["m00"])) 77 | 78 | # Draw the circle and centroid on the frame 79 | cv2.circle(frame, (int(x), int(y)), int(radius), (0, 255, 255), 2) 80 | cv2.circle(frame, center, 5, (0, 0, 255), -1) 81 | 82 | 83 | if center is not None and center_square[0][0] < center[0] < center_square[1][0] \ 84 | and center_square[0][1] < center[1] < center_square[1][1]: 85 | pass 86 | else: 87 | if flag == 0: 88 | no_face_time = time.time()#no face detected = record time 89 | flag = 1 90 | 91 | if y < frame_height / 2 + 0.5: 92 | arduino.write(b'u') # Move tilt up 93 | elif y > frame_height / 2 + 0.5: 94 | arduino.write(b'd') # Move tilt down 95 | 96 | if x < frame_width / 2 + 0.5: 97 | arduino.write(b'l') # Move pan left 98 | elif x > frame_width / 2 + 0.5: 99 | arduino.write(b'r') # Move pan right 100 | 101 | else: 102 | if time.time() - no_face_time >= 5: # Check if 2 seconds have passed 103 | arduino.write(b's') # Move to default position 104 | print("No face detected.") 105 | flag = 0 # Reset flag 106 | no_face_time = 0.0 # Reset no_face_time 107 | 108 | # Add the center to the list of tracked points 109 | points.append(center) 110 | 111 | # Display the frame 112 | cv2.imshow("Frame", frame) 113 | 114 | # Break the loop on 'q' press 115 | if cv2.waitKey(1) & 0xFF == ord('q'): 116 | break 117 | 118 | except Exception as e: 119 | print(f"Error occurred: {e}") 120 | if arduino: 121 | arduino.close() 122 | cv2.destroyAllWindows() --------------------------------------------------------------------------------