Akhilesh R Mohan
Published

VisionAid: Assistive Tech for the Visually Impaired

VisionAid is a smart device that provides real-time audio descriptions of the surrounding environment, helping visually impaired users navig

BeginnerWork in progress86
VisionAid: Assistive Tech for the Visually Impaired

Things used in this project

Story

Read more

Code

VisionAid is a smart device that provides real-time audio descriptions of the surrounding environmen

Python
import cv2
import numpy as np
import pyttsx3
from time import sleep

# Initialize camera, TTS engine, and model
camera = cv2.VideoCapture(0)
engine = pyttsx3.init()
engine.setProperty('rate', 150)  # Speed of speech

# Load YOLO model and COCO labels
net = cv2.dnn.readNet('yolov3.weights', 'yolov3.cfg')
layer_names = net.getLayerNames()
output_layers = [layer_names[i[0] - 1] for i in net.getUnconnectedOutLayers()]

with open("coco.names", "r") as f:
    classes = [line.strip() for line in f.readlines()]

def detect_objects():
    ret, frame = camera.read()
    height, width, channels = frame.shape
    
    # Preparing the image for YOLO
    blob = cv2.dnn.blobFromImage(frame, 0.00392, (416, 416), (0, 0, 0), True, crop=False)
    net.setInput(blob)
    outs = net.forward(output_layers)

    # Process detection results
    class_ids = []
    confidences = []
    boxes = []
    for out in outs:
        for detection in out:
            scores = detection[5:]
            class_id = np.argmax(scores)
            confidence = scores[class_id]
            if confidence > 0.5:
                center_x = int(detection[0] * width)
                center_y = int(detection[1] * height)
                w = int(detection[2] * width)
                h = int(detection[3] * height)
                x = int(center_x - w / 2)
                y = int(center_y - h / 2)
                boxes.append([x, y, w, h])
                confidences.append(float(confidence))
                class_ids.append(class_id)
    
    # Draw bounding boxes and generate audio descriptions
    for i in range(len(boxes)):
        x, y, w, h = boxes[i]
        label = str(classes[class_ids[i]])
        confidence = confidences[i]
        print(f"Detected {label} with {confidence:.2f} confidence")
        engine.say(f"Detected {label}")
    
    engine.runAndWait()

while True:
    detect_objects()
    sleep(2)  # Pause before next detection

Credits

Akhilesh R Mohan
2 projects • 1 follower
Contact

Comments

Please log in or sign up to comment.