Module 7: AI Applications
20 Slides with Example Programs &
Outputs
OpenCV Basics
• Load and display image using [Link] & [Link]
• Example Code:
import cv2
img = [Link]('[Link]')
[Link]('Image', img)
[Link](0)
• Output: Displays the image window
Image Transformations
• Convert to grayscale, resize, blur
• Code:
gray = [Link](img, cv2.COLOR_BGR2GRAY)
blur = [Link](img,(5,5),0)
• Output: Gray + Blurred image
Face Detection with Haarcascade
• Code:
face_cascade = [Link]('haarcascade_frontalface_default.xml')
faces = face_cascade.detectMultiScale(gray,1.1,4)
• Draw rectangle around faces
• Output: Detected face bounding boxes
Edge Detection
• Canny Edge Detection
• Code:
edges = [Link](gray,100,200)
• Output: Edge-highlighted image
Real-time Webcam Face Detection
• Use [Link](0) to access webcam
• Detect and draw rectangles in real-time
• Output: Live detection
Speech Recognition Basics
• Library: speech_recognition
• Code:
import speech_recognition as sr
r = [Link]()
with [Link]() as source:
audio = [Link](source)
print(r.recognize_google(audio))
• Output: Text transcript of speech
Text-to-Speech with gTTS
• Convert text to mp3
• Code:
from gtts import gTTS
tts = gTTS('Hello World')
[Link]('hello.mp3')
• Output: hello.mp3 audio file
Offline TTS with pyttsx3
• Code:
import pyttsx3
engine = [Link]()
[Link]('Hello AI')
[Link]()
• Output: Audio spoken by system voice
Voice Assistant Use Case
• Combine STT + TTS
• Recognize user command & reply with TTS
• Output: Interactive Q&A voice assistant
RL Basics
• Key Components: Agent, Environment, Reward
• Agent takes action → Environment responds → Reward given
• Used in robotics, gaming, optimization
Q-Learning Algorithm
• Q(state, action) = Q(state, action) + α [reward + γ * max(Q(next_state)) - Q(state,
action)]
• Exploration vs Exploitation: ε-greedy strategy
• Helps agent learn best actions
Q-Learning Example
• Environment: GridWorld/Taxi
• Code snippet to update Q-table iteratively
• Output: Learned policy with Q-values
OpenAI Gym Example
• CartPole balancing game
• Code:
import gym
env = [Link]('CartPole-v1')
• Agent learns to keep pole balanced
• Output: Rewards increase over episodes
Game Bot with RL
• Train bot using Gym + Deep Q-Learning
• Can play Atari games autonomously
• Output: Bot gameplay video/screenshots
GANs Introduction
• GAN = Generator + Discriminator
• Generator creates fake data, Discriminator checks real vs fake
• Used for image, video, audio generation
GAN Example Code
• TensorFlow/PyTorch implementation
• Train on MNIST dataset to generate digits
• Output: Synthetic handwritten digits
Stable Diffusion
• Text-to-Image generation
• Code:
from diffusers import StableDiffusionPipeline
• [Link]('cuda')
image = pipe('A fantasy landscape').images[0]
• Output: AI-generated image
Generative AI Ethics
• Applications: Art, content generation, design
• Concerns: Deepfakes, misuse, copyright
• Balance: Creativity vs Ethics
Project 1: Face Detection System
• End-to-end implementation using OpenCV
• Real-time face detection via webcam
• Final Output: Live detection system
Project 2: Game Bot with RL
• Agent trained in OpenAI Gym
• Applies Q-learning/Deep Q-learning
• Final Output: Bot plays and wins game