Draw in the air with your finger, pick colours and erase, all by gesture.
Built and tested by Aboobacker sidheeq K P · Industrial Robotics Institute
Python 3.11, OpenCV (opencv-python), MediaPipe, NumPy, PyCharm (recommended)
A webcam drawing app controlled entirely by hand gestures. MediaPipe tracks your index finger as a pen. One finger draws, two fingers lift the pen to move, and an open hand erases. Touch the colour boxes at the top to change colour, or the QUIT box to close.
| Component | Qty |
|---|---|
| Laptop / Computer | 1 |
| Webcam (built-in or external) | 1 |
Complete, tested Python code for this project. Copy it into Python 3.11 and upload it and run it.
import cv2
import mediapipe as mp
import numpy as np
cap = cv2.VideoCapture(0)
mpHands = mp.solutions.hands
hands = mpHands.Hands(max_num_hands=1,
min_detection_confidence=0.8,
min_tracking_confidence=0.8)
mpDraw = mp.solutions.drawing_utils
canvas = np.zeros((720, 1280, 3), dtype=np.uint8)
prev_x, prev_y = 0, 0
smooth_x, smooth_y = 0, 0
draw_color = (0, 255, 0)
brush_thickness = 7
eraser_thickness = 50
smoothening = 7
colors = [
(0, 0, 150, 60, (0, 0, 255)),
(150, 0, 300, 60, (0, 255, 0)),
(300, 0, 450, 60, (255, 0, 0)),
(450, 0, 600, 60, (0, 255, 255)),
(600, 0, 750, 60, (255, 255, 255)),
]
quit_btn = (1100, 0, 1280, 60)
def fingers_up(hand_landmarks):
tips = [4, 8, 12, 16, 20]
fingers = [hand_landmarks.landmark[tips[0]].x < hand_landmarks.landmark[tips[0] - 1].x]
for i in range(1, 5):
fingers.append(hand_landmarks.landmark[tips[i]].y < hand_landmarks.landmark[tips[i] - 2].y)
return fingers.count(True)
cv2.namedWindow("AI Air Drawing", cv2.WINDOW_NORMAL)
cv2.setWindowProperty("AI Air Drawing", cv2.WND_PROP_FULLSCREEN, cv2.WINDOW_FULLSCREEN)
while True:
ret, frame = cap.read()
if not ret:
break
frame = cv2.flip(frame, 1)
frame = cv2.resize(frame, (1280, 720))
rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
result = hands.process(rgb)
for (x1, y1, x2, y2, color) in colors:
cv2.rectangle(frame, (x1, y1), (x2, y2), color, -1)
cv2.rectangle(frame, quit_btn[:2], quit_btn[2:], (0, 0, 255), -1)
cv2.putText(frame, "QUIT", (1120, 40), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 2)
if result.multi_hand_landmarks:
for handLms in result.multi_hand_landmarks:
mpDraw.draw_landmarks(frame, handLms, mpHands.HAND_CONNECTIONS)
finger_count = fingers_up(handLms)
x = int(handLms.landmark[8].x * 1280)
y = int(handLms.landmark[8].y * 720)
smooth_x = smooth_x + (x - smooth_x) // smoothening
smooth_y = smooth_y + (y - smooth_y) // smoothening
if quit_btn[0] < smooth_x < quit_btn[2] and quit_btn[1] < smooth_y < quit_btn[3]:
cap.release()
cv2.destroyAllWindows()
exit()
if y < 60:
for (x1, y1, x2, y2, color) in colors:
if x1 < smooth_x < x2:
draw_color = color
if finger_count == 1:
if prev_x == 0 and prev_y == 0:
prev_x, prev_y = smooth_x, smooth_y
cv2.line(canvas, (prev_x, prev_y), (smooth_x, smooth_y), draw_color, brush_thickness)
prev_x, prev_y = smooth_x, smooth_y
elif finger_count == 2:
prev_x, prev_y = 0, 0
elif finger_count == 5:
cv2.circle(canvas, (smooth_x, smooth_y), eraser_thickness, (0, 0, 0), -1)
prev_x, prev_y = 0, 0
combined = cv2.add(frame, canvas)
cv2.imshow("AI Air Drawing", combined)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()A webcam drawing app controlled entirely by hand gestures. MediaPipe tracks your index finger as a pen.
You need: Laptop / Computer (1), Webcam (built-in or external) (1). Software: Python 3.11, OpenCV (opencv-python), MediaPipe, NumPy, PyCharm (recommended).
It is rated intermediate and takes about 1–2 hours to build, using the Laptop / PC with webcam. The guide on this page explains every connection and step.
The complete, tested Python code is free on this page. Copy it into Python 3.11 and upload it and run it.
No kit is needed. This project runs on a laptop or PC with webcam. For help with the setup, message IRI on WhatsApp at +91 86060 82634.