AI & MLIntermediatePRJ-080

AI Morse Code Converter Using Hand Gestures

Type Morse code with finger pinches and convert it to text in real time.

Built and tested by Aboobacker sidheeq K P · Industrial Robotics Institute

Laptop / PC with webcam

Python 3.11.9, OpenCV, MediaPipe, NumPy, time (Python standard library), PyCharm IDE

LevelIntermediate
Build time2–3 hours
BoardLaptop / PC with webcam
Parts2 components

What is the AI Morse Code Converter Using Hand Gestures?

AI Morse Code Converter is a computer vision application that generates Morse code from hand gestures instead of a keyboard or Morse key. A webcam and MediaPipe hand tracking measure the distance and timing between thumb and index finger to create dots and dashes. The Morse signals are converted into English text live on screen, with gestures for delete, space and reset.

How it works

  1. The webcam captures live video and MediaPipe tracks hand landmarks.
  2. A pinch is detected when the thumb–index distance falls below an adaptive threshold based on hand size.
  3. A pinch shorter than 0.3 s adds a dot; a longer pinch adds a dash.
  4. After a 1-second pause, the Morse sequence is matched against a dictionary and converted to a letter (‘?’ if unknown).
  5. Gestures: full open hand clears all text, thumb+index+middle deletes the last letter, thumb+index+pinky adds a space.
  6. A semi-transparent side panel shows the full Morse alphabet as reference.

Parts list

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

Software

  • Python 3.11.9
  • OpenCV
  • MediaPipe
  • NumPy
  • time (Python standard library)
  • PyCharm IDE

How to build the AI Morse Code Converter Using Hand Gestures

  1. Install Python 3.11.9 from the official website, ticking ‘Add Python to PATH’.
  2. Install PyCharm Community Edition.
  3. Create a new PyCharm project using the Python 3.11.9 interpreter.
  4. In the PyCharm terminal run: pip install opencv-python mediapipe numpy.
  5. Verify the installation with pip list.
  6. Paste the Python code into a new file and run it.
  7. Pinch thumb and index finger briefly for a dot or longer for a dash; pause to complete a letter. Press Q to exit.

AI Morse Code Converter Using Hand Gestures code

Complete, tested Python code for this project. Copy it into Python 3.11.9 and upload it and run it.

import cv2
import mediapipe as mp
import time
import numpy as np

# Camera
cap = cv2.VideoCapture(0)

# Hand tracking
mpHands = mp.solutions.hands
hands = mpHands.Hands(max_num_hands=1,
                      min_detection_confidence=0.85,
                      min_tracking_confidence=0.85)
mpDraw = mp.solutions.drawing_utils

# Variables
morse_code = ""
text_output = ""
pinch_start = None
last_signal_time = 0
prev_finger_pos = None

# Morse dictionary (alphabetical order)
MORSE_DICT = {
    "A": ".-", "B": "-...", "C": "-.-.", "D": "-..", "E": ".",
    "F": "..-.", "G": "--.", "H": "....", "I": "..", "J": ".---",
    "K": "-.-", "L": ".-..", "M": "--", "N": "-.", "O": "---",
    "P": ".--.", "Q": "--.-", "R": ".-.", "S": "...", "T": "-",
    "U": "..-", "V": "...-", "W": ".--", "X": "-..-", "Y": "-.--",
    "Z": "--..", "/": " "
}

# Pre-create reference list in alphabetical order
morse_reference = [f"{k}: {v}" for k, v in sorted(MORSE_DICT.items())]

# Fullscreen window
cv2.namedWindow("Finger Morse Code", cv2.WINDOW_NORMAL)
cv2.setWindowProperty("Finger Morse Code", cv2.WND_PROP_FULLSCREEN, cv2.WINDOW_FULLSCREEN)

# Fonts
font_right = cv2.FONT_HERSHEY_SIMPLEX
font_top = cv2.FONT_HERSHEY_DUPLEX
font_scale_right = 0.5  # ~12px
font_scale_top = 0.8  # ~22px
thickness = 2

# Debounce timers
clear_one_timer = 0
space_timer = 0

while True:
    ret, frame = cap.read()
    frame = cv2.flip(frame, 1)
    h, w, _ = frame.shape

    rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
    result = hands.process(rgb)

    pinch_detected = False
    full_hand = False
    clear_one = False
    space_gesture = False

    if result.multi_hand_landmarks:
        for handLms in result.multi_hand_landmarks:
            mpDraw.draw_landmarks(frame, handLms, mpHands.HAND_CONNECTIONS)

            # Thumb tip = 4, Index tip = 8, Middle tip = 12, Ring =16, Pinky =20
            landmarks = [(int(lm.x*w), int(lm.y*h)) for lm in handLms.landmark]

            x_thumb, y_thumb = landmarks[4]
            x_index, y_index = landmarks[8]
            x_middle, y_middle = landmarks[12]
            x_ring, y_ring = landmarks[16]
            x_pinky, y_pinky = landmarks[20]

            # Adaptive pinch threshold based on hand size (distance wrist-thumb)
            wrist_x, wrist_y = landmarks[0]
            hand_size = np.hypot(x_thumb - wrist_x, y_thumb - wrist_y)
            pinch_distance = np.hypot(x_index - x_thumb, y_index - y_thumb)
            if pinch_distance < max(30, hand_size*0.25):
                pinch_detected = True

            # Fingers up detection with smoothing
            fingers_up = [
                y_thumb < landmarks[2][1],   # thumb
                y_index < landmarks[6][1],   # index
                y_middle < landmarks[10][1], # middle
                y_ring < landmarks[14][1],   # ring
                y_pinky < landmarks[18][1]   # pinky
            ]
            fingers_count = sum(fingers_up)

            # Full hand reset
            if fingers_count == 5:
                full_hand = True

            # Clear one letter (Thumb+Index+Middle)
            if fingers_up[0] and fingers_up[1] and fingers_up[2] and fingers_count <= 3:
                clear_one = True

            # Space gesture: Thumb+Index+Pinky
            if fingers_up[0] and fingers_up[1] and fingers_up[4] and not fingers_up[2] and not fingers_up[3]:
                space_gesture = True

    # Apply gestures
    current_time = time.time()
    if full_hand:
        morse_code = ""
        text_output = ""
    if clear_one and text_output and current_time - clear_one_timer > 0.5:
        text_output = text_output[:-1]
        clear_one_timer = current_time
    if space_gesture and current_time - space_timer > 0.5:
        if len(text_output) == 0 or text_output[-1] != " ":
            text_output += " "
        space_timer = current_time

    # Pinch logic
    if pinch_detected:
        if pinch_start is None:
            pinch_start = time.time()
    else:
        if pinch_start is not None:
            duration = time.time() - pinch_start
            pinch_start = None
            if duration < 0.3:
                morse_code += "."
            else:
                morse_code += "-"
            last_signal_time = time.time()

    # Convert Morse to text after pause
    if morse_code and time.time() - last_signal_time > 1.0:  # slightly faster recognition
        letter = next((k for k, v in MORSE_DICT.items() if v == morse_code), "?")
        text_output += letter
        morse_code = ""

    # --- TOP LEFT: Current text and Morse ---
    cv2.putText(frame, f"TEXT: {text_output}", (30, 50),
                font_top, font_scale_top, (255, 0, 0), thickness)
    cv2.putText(frame, f"MORSE: {morse_code}", (30, 100),
                font_top, font_scale_top, (0, 255, 0), thickness)

    # --- RIGHT SIDE: Full Morse reference panel ---
    panel_width = 220
    panel_x1 = w - panel_width - 10
    panel_y1 = 10
    panel_x2 = w - 10
    panel_y2 = h - 10

    # Transparent black rectangle
    overlay = frame.copy()
    cv2.rectangle(overlay, (panel_x1, panel_y1), (panel_x2, panel_y2), (0, 0, 0), -1)
    alpha = 0.6
    frame = cv2.addWeighted(overlay, alpha, frame, 1 - alpha, 0)

    # Draw reference text 2 per line
    line_height = 20
    start_y = panel_y1 + 30
    for i in range(0, len(morse_reference), 2):
        first = morse_reference[i]
        second = morse_reference[i + 1] if i + 1 < len(morse_reference) else ""
        text_line = f"{first} {second}"
        cv2.putText(frame, text_line, (panel_x1 + 10, start_y),
                    font_right, font_scale_right, (0, 255, 255), thickness)
        start_y += line_height

    cv2.imshow("Finger Morse Code", frame)

    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

cap.release()
cv2.destroyAllWindows()

Real-world uses

  • Assistive communication for people with speech or mobility impairments
  • Learning and practising Morse code
  • Touch-free text input
  • Gesture-recognition demos

Frequently asked questions

What is a AI Morse Code Converter Using Hand Gestures?

AI Morse Code Converter is a computer vision application that generates Morse code from hand gestures instead of a keyboard or Morse key. A webcam and MediaPipe hand tracking measure the distance and timing between thumb and index finger to create dots and dashes.

What components are needed for the AI Morse Code Converter Using Hand Gestures?

You need: Laptop / Computer (1), Webcam (built-in or external) (1). Software: Python 3.11.9, OpenCV, MediaPipe, NumPy, time (Python standard library), PyCharm IDE.

Is the AI Morse Code Converter Using Hand Gestures project good for beginners?

It is rated intermediate and takes about 2–3 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 Morse Code Converter Using Hand Gestures?

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

Do I need any hardware for the AI Morse Code Converter Using Hand Gestures?

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