I am building a project which tracks my finger movement and draws on the screen based on my movement. For this I am using mediaPipe Hands solution. I want to integrate a feature to recognize shapes like circle, rectangle, etc. and redraws it in its ideal form.
(of Course a video stream)
I implemented a feature to recognize shapes drawn on the screen using OpenCV. I tried to capture the points traced by my finger in drawing mode and used those points to detect whether the shape being drawn was a circle or a rectangle. I was expecting it to accurately identify the shape once it was completed. However, what actually happened was that the system started detecting my finger as a circle as soon as I entered drawing mode , drawing a circle around each point my finger moved to.
How can i detect shapes which i am drawing and then redraw them perfectly?
def recognize_shape(points):
if len(points) < 5:
return None
# Calculate the bounding box of the points
x_coords, y_coords = zip(*points)
min_x, max_x = min(x_coords), max(x_coords)
min_y, max_y = min(y_coords), max(y_coords)
width, height = max_x - min_x, max_y - min_y
# Check for a circle using radius variance
center_x, center_y = np.mean(x_coords), np.mean(y_coords)
radii = [distance.euclidean((x, y), (center_x, center_y)) for x, y in points]
mean_radius = np.mean(radii)
radius_variance = np.var(radii)
if radius_variance < 1000: # Adjust the threshold for better accuracy
return ("circle", (int(center_x), int(center_y), int(mean_radius)))
# Check for a rectangle using aspect ratio
if 0.9 < width / height < 1.1: # Allow slight deviation for squares
if all(min_x <= x <= max_x and min_y <= y <= max_y for x, y in points):
return ("rectangle", (min_x, min_y, max_x, max_y))
return None
def draw_shape(shape, imgCanvas):
if shape[0] == "circle":
_, (center_x, center_y, radius) = shape
cv2.circle(imgCanvas, (center_x, center_y), radius, (0, 0, 255), 2)
elif shape[0] == "rectangle":
_, (min_x, min_y, max_x, max_y) = shape
cv2.rectangle(imgCanvas, (min_x, min_y), (max_x, max_y), (0, 0, 255), 2)
Kartik Suri is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
2