AI & MLIntermediatePRJ-068

AI Air Drawing with Colour Palette and Eraser

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

Laptop / PC with webcam

Python 3.11, OpenCV (opencv-python), MediaPipe, NumPy, PyCharm (recommended)

LevelIntermediate
Build time1–2 hours
BoardLaptop / PC with webcam
Parts2 components

What is the AI Air Drawing with Colour Palette and Eraser?

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.

How it works

  1. The webcam captures live video, mirrored so it feels natural.
  2. MediaPipe finds the hand and its 21 landmarks; the index fingertip is the pointer.
  3. The program counts raised fingers: 1 = draw, 2 = move, 5 = erase.
  4. Touching the palette at the top changes colour; touching QUIT exits.
  5. Lines are smoothed and drawn on a canvas that is blended over the video.

Parts list

ComponentQty
Laptop / Computer1
Webcam (built-in or external)1

Software

  • Python 3.11
  • OpenCV (opencv-python)
  • MediaPipe
  • NumPy
  • PyCharm (recommended)

How to build the AI Air Drawing with Colour Palette and Eraser

  1. Install Python 3.11 and tick "Add Python to PATH".
  2. Open a terminal and run: pip install opencv-python mediapipe numpy
  3. Paste the code into a new Python file.
  4. Run it; the window opens full screen.
  5. Raise one finger to draw, two to move without drawing, and an open hand to erase.
  6. Touch a colour box to change colour; touch QUIT or press q to exit.

AI Air Drawing with Colour Palette and Eraser code

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()

Real-world uses

  • Interactive classroom whiteboards
  • Touch-free presentations
  • Kids’ art activities
  • Learning computer vision

Frequently asked questions

What is a AI Air Drawing with Colour Palette and Eraser?

A webcam drawing app controlled entirely by hand gestures. MediaPipe tracks your index finger as a pen.

What components are needed for the AI Air Drawing with Colour Palette and Eraser?

You need: Laptop / Computer (1), Webcam (built-in or external) (1). Software: Python 3.11, OpenCV (opencv-python), MediaPipe, NumPy, PyCharm (recommended).

Is the AI Air Drawing with Colour Palette and Eraser project good for beginners?

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.

Where can I get the code for the AI Air Drawing with Colour Palette and Eraser?

The complete, tested Python code is free on this page. Copy it into Python 3.11 and upload it and run it.

Do I need any hardware for the AI Air Drawing with Colour Palette and Eraser?

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.

Industrial Robotics Institute
Logo