0% found this document useful (0 votes)
259 views3 pages

AI Robot Control Code Overview

The document outlines a Python program for an AI robot that integrates voice recognition, vision detection, and motor movement. It includes functions for moving the robot in various directions, recognizing voice commands, and detecting red objects using a camera. The main loop allows the robot to respond to commands such as moving forward, backward, turning, and scanning for red objects.

Uploaded by

Shashi Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
259 views3 pages

AI Robot Control Code Overview

The document outlines a Python program for an AI robot that integrates voice recognition, vision detection, and motor movement. It includes functions for moving the robot in various directions, recognizing voice commands, and detecting red objects using a camera. The main loop allows the robot to respond to commands such as moving forward, backward, turning, and scanning for red objects.

Uploaded by

Shashi Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

AI Robot Project: Voice + Vision + Movement + Speech

import cv2
import [Link] as GPIO
import time
import os
import speech_recognition as sr

# ========== GPIO Setup ==========


motor_pins = {'in1': 17, 'in2': 27, 'in3': 22, 'in4': 23}
[Link]([Link])
for pin in motor_pins.values():
[Link](pin, [Link])

def move_forward():
[Link](motor_pins['in1'], True)
[Link](motor_pins['in2'], False)
[Link](motor_pins['in3'], True)
[Link](motor_pins['in4'], False)

def move_backward():
[Link](motor_pins['in1'], False)
[Link](motor_pins['in2'], True)
[Link](motor_pins['in3'], False)
[Link](motor_pins['in4'], True)

def turn_left():
[Link](motor_pins['in1'], False)
[Link](motor_pins['in2'], True)
[Link](motor_pins['in3'], True)
[Link](motor_pins['in4'], False)

def turn_right():
[Link](motor_pins['in1'], True)
[Link](motor_pins['in2'], False)
[Link](motor_pins['in3'], False)
[Link](motor_pins['in4'], True)

def stop():
for pin in motor_pins.values():
[Link](pin, False)

# ========== Voice Recognition ==========


def speak(text):
[Link](f'espeak "{text}"')

def get_voice_command():
r = [Link]()
with [Link]() as source:
speak("I'm listening.")
print("Listening...")
audio = [Link](source, timeout=5)
try:
command = r.recognize_sphinx(audio)
print("You said:", command)
return [Link]()
except [Link]:
speak("Sorry, I didn't catch that.")
return ""
except [Link]:
speak("Speech system error.")
return ""

# ========== Vision Detection ==========


def detect_red_object():
cam = [Link](0)
ret, frame = [Link]()
[Link]()
if not ret:
return False
hsv = [Link](frame, cv2.COLOR_BGR2HSV)
lower_red = (0, 120, 70)
upper_red = (10, 255, 255)
mask = [Link](hsv, lower_red, upper_red)
red_pixels = [Link](mask)
print("Red pixels detected:", red_pixels)
return red_pixels > 500

# ========== Main Loop ==========


try:
speak("Robot is ready.")
while True:
cmd = get_voice_command()

if "forward" in cmd:
speak("Moving forward.")
move_forward()
[Link](2)
stop()

elif "backward" in cmd:


speak("Moving backward.")
move_backward()
[Link](2)
stop()

elif "left" in cmd:


speak("Turning left.")
turn_left()
[Link](1)
stop()

elif "right" in cmd:


speak("Turning right.")
turn_right()
[Link](1)
stop()

elif "stop" in cmd:


speak("Stopping.")
stop()

elif "follow red" in cmd or "find red" in cmd:


speak("Scanning for red object.")
if detect_red_object():
speak("Red object detected. Approaching.")
move_forward()
[Link](2)
stop()
else:
speak("No red object found.")

elif "exit" in cmd or "quit" in cmd:


speak("Shutting down.")
break

except KeyboardInterrupt:
speak("Program stopped manually.")
finally:
stop()
[Link]()

You might also like