Machine Learning Program 6 (SHANKAR)
Machine Learning Program 6 (SHANKAR)
AIM:
The Aim of the program is to perform object detection in both online and
offline mode and implement a simple object detection model using a pre-trained
model such as YOLO (You Look Only Once) in Python.
HARDWARE SPECIFICATION:
Processor : Apple M1
Installed RAM : 8.00 GB
SOFTWARE SPECIFICATION:
LIBRARIES:
ALGORITHM:
The lab will cover the following steps and aspects of object detection:
1. Introduction to Object Detection:
• Brief explanation of object detection in the context of computer vision.
• Overview of popular object detection algorithms.
2. Data Loading:
• Import necessary libraries (e.g., OpenCV, NumPy).
• Download a pre-labeled dataset for object detection (e.g., COCO dataset).
3. Data Exploration:
• Understand the structure of the dataset, including labeled bounding boxes and
class labels.
4. Data Preprocessing:
• Load and preprocess images from the dataset.
• Normalize or standardize pixel values.
SUDARSAN R-21EE113
5. Model Implementation:
• Implement object detection using a pre-trained model (e.g., YOLO).
• Discuss the architecture of the chosen model and its components.
6. Model Inference:
• Apply the trained model to detect objects in new images.
• Visualize the bounding boxes and class labels.
7. Evaluation and Fine-Tuning:
• Evaluate the performance of the model on the test set.
• Discuss strategies for fine-tuning the model or training on a custom dataset.
SUDARSAN R-21EE113
PROGRAM:
OFFLINE OBJECT DETECTION:
import cv2
import requests
import numpy as np
from io import BytesIO
from PIL import Image
import matplotlib.pyplot as plt
from requests.adapters import HTTPAdapter
from requests.packages.urllib3.util.retry import Retry
try:
response = session.get(url)
response.raise_for_status()
img = Image.open(BytesIO(response.content))
img = np.array(img)
return img
except requests.exceptions.RequestException as e:
print(f"Error loading image from URL: {e}")
return None
SUDARSAN R-21EE113
# Load the image from URL
image = load_image_from_url(image_url)
import cv2
from random import randint
dnn = cv2.dnn.readNet('yolov4-tiny.weights', 'yolov4-tiny.cfg')
model = cv2.dnn_DetectionModel(dnn)
SUDARSAN R-21EE113
model.setInputParams(size=(416, 416), scale=1/255, swapRB=True)
with open('classes.txt') as f:
classes = f.read().strip().splitlines()
capture = cv2.VideoCapture(0)
capture.set(cv2.CAP_PROP_FRAME_WIDTH, 1280)
capture.set(cv2.CAP_PROP_FRAME_HEIGHT, 720)
color_map = {}
while True:
# frame capture
_, frame = capture.read()
frame = cv2.flip(frame,1)
# object detection
class_ids, confidences, boxes = model.detect(frame)
for id, confidence, box in zip(class_ids, confidences, boxes):
x, y, w, h = box
obj_class = classes[id]
if obj_class not in color_map:
color = (randint(0, 255), randint(0, 255), randint(0, 255))
color_map[obj_class] = color
else:
color = color_map[obj_class]
cv2.putText(frame, f'{obj_class.title()} {format(confidence, ".2f")}', (x, y-10),
cv2.FONT_HERSHEY_DUPLEX, 1, color, 2)
cv2.rectangle(frame, (x, y), (x + w, y + h), color, 2)
cv2.imshow('Video Capture', frame)
key = cv2.waitKey(1) # freezes frame for 1ms
match(key):
case 27: # esc key to exit
capture.release()
cv2.destroyAllWindows()
case 13: # enter key to reset colors
color_map = {}
SUDARSAN R-21EE113
OUTPUT:
SUDARSAN R-21EE113
ONLINE OBJECT DETECTION:
SUDARSAN R-21EE113
INFERENCE:
However, the provided output section is empty, possibly due to the absence of
executed code or missing output statements.
RUBRICS:
Outcome
Exemplary Proficient Apprentice Novice
Parameter Score
(4) (3) (2) (1)
(1 - 4)
Identifying clear goals for
the experiment
Choosing the appropriate
experimental test bed
(Hardware, Software,
Emulation, Simulation, or
hybrid) to achieve the
identified objectives of the
experiment
Designing and conducting
the experiment
RESULT:
SUDARSAN R-21EE113