|
1 |
| - |
2 |
| -#### File: `virtual_painter.py` |
3 |
| -```python |
4 | 1 | import cv2
|
5 | 2 | import numpy as np
|
6 | 3 |
|
7 |
| -# Set up the color ranges for detection (e.g., blue pen) |
8 |
| -lower_blue = np.array([100, 150, 0]) |
9 |
| -upper_blue = np.array([140, 255, 255]) |
| 4 | +# HSV color range for detecting the color object (adjust this range for different colors) |
| 5 | +lower_bound = np.array([0, 120, 70]) |
| 6 | +upper_bound = np.array([10, 255, 255]) |
| 7 | + |
| 8 | +# Initialize variables |
| 9 | +my_points = [] # List to store points for drawing |
| 10 | + |
| 11 | +# Function to detect color and return the coordinates of the detected object |
| 12 | +def find_color(img, lower_bound, upper_bound): |
| 13 | + hsv_img = cv2.cvtColor(img, cv2.COLOR_BGR2HSV) # Convert to HSV color space |
| 14 | + mask = cv2.inRange(hsv_img, lower_bound, upper_bound) # Create mask for specific color |
| 15 | + |
| 16 | + # Find contours in the masked image |
| 17 | + contours, _ = cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) |
| 18 | + x, y, w, h = 0, 0, 0, 0 |
| 19 | + |
| 20 | + for contour in contours: |
| 21 | + area = cv2.contourArea(contour) |
| 22 | + if area > 500: # Filter by area size to remove noise |
| 23 | + x, y, w, h = cv2.boundingRect(contour) |
| 24 | + cv2.rectangle(img, (x, y), (x + w, y + h), (0, 255, 0), 2) # Draw a rectangle around the object |
| 25 | + return x + w // 2, y |
10 | 26 |
|
11 |
| -# Initialize a blank canvas to draw on |
12 |
| -canvas = np.zeros((480, 640, 3), dtype="uint8") |
| 27 | + return None |
13 | 28 |
|
14 |
| -# Start video capture |
| 29 | +# Function to draw on canvas based on detected points |
| 30 | +def draw_on_canvas(points, img): |
| 31 | + for point in points: |
| 32 | + cv2.circle(img, (point[0], point[1]), 10, (0, 0, 255), cv2.FILLED) # Draw red circles at each point |
| 33 | + |
| 34 | +# Capture video from webcam |
15 | 35 | cap = cv2.VideoCapture(0)
|
16 | 36 |
|
17 | 37 | while True:
|
18 |
| - ret, frame = cap.read() |
19 |
| - if not ret: |
| 38 | + success, img = cap.read() # Read frame from webcam |
| 39 | + if not success: |
20 | 40 | break
|
21 | 41 |
|
22 |
| - # Convert to HSV color space for color detection |
23 |
| - hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV) |
| 42 | + # Find the object in the current frame |
| 43 | + new_point = find_color(img, lower_bound, upper_bound) |
24 | 44 |
|
25 |
| - # Create a mask for detecting the blue color |
26 |
| - mask = cv2.inRange(hsv, lower_blue, upper_blue) |
27 |
| - |
28 |
| - # Find contours of the detected blue object |
29 |
| - contours, _ = cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) |
30 |
| - |
31 |
| - if contours: |
32 |
| - # Get the largest contour (assuming it's the pen) |
33 |
| - max_contour = max(contours, key=cv2.contourArea) |
34 |
| - |
35 |
| - # Get the center of the contour (i.e., the tip of the pen) |
36 |
| - (x, y), radius = cv2.minEnclosingCircle(max_contour) |
37 |
| - center = (int(x), int(y)) |
38 |
| - |
39 |
| - # Draw on the canvas at the detected position |
40 |
| - cv2.circle(canvas, center, 5, (255, 0, 0), -1) |
41 |
| - |
42 |
| - # Merge the original frame with the canvas |
43 |
| - combined = cv2.add(frame, canvas) |
44 |
| - |
45 |
| - # Display the result |
46 |
| - cv2.imshow('Virtual Painter', combined) |
47 |
| - |
| 45 | + # If a new point is detected, add it to the list |
| 46 | + if new_point: |
| 47 | + my_points.append(new_point) |
| 48 | + |
| 49 | + # Draw on the canvas using the points stored |
| 50 | + draw_on_canvas(my_points, img) |
| 51 | + |
| 52 | + # Display the frame |
| 53 | + cv2.imshow("Virtual Painter", img) |
| 54 | + |
| 55 | + # Break the loop on 'q' key press |
48 | 56 | if cv2.waitKey(1) & 0xFF == ord('q'):
|
49 | 57 | break
|
50 | 58 |
|
| 59 | +# Release the webcam and close windows |
51 | 60 | cap.release()
|
52 | 61 | cv2.destroyAllWindows()
|
0 commit comments