0% found this document useful (0 votes)
3 views4 pages

Untitled Document

This document outlines a project for creating a real-time object detection system using OpenCV and pre-trained models like YOLO. It provides a step-by-step guide including installation of necessary libraries, loading the model, writing the code, and testing the system. Additionally, it suggests optional enhancements such as multiple camera support and real-time alerts.

Uploaded by

DHRUV SINGH
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)
3 views4 pages

Untitled Document

This document outlines a project for creating a real-time object detection system using OpenCV and pre-trained models like YOLO. It provides a step-by-step guide including installation of necessary libraries, loading the model, writing the code, and testing the system. Additionally, it suggests optional enhancements such as multiple camera support and real-time alerts.

Uploaded by

DHRUV SINGH
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
You are on page 1/ 4

Creating a project on computer vision can be both exciting and highly rewarding.

Below is a
simple yet effective project idea along with the steps to build it.

### Project: **Real-Time Object Detection Using OpenCV and Pre-trained Models**

#### Overview:
In this project, we will implement a real-time object detection system using OpenCV, a
powerful computer vision library, and a pre-trained deep learning model (e.g., YOLO,
MobileNet, or Faster R-CNN). The system will be able to detect various objects (such as
people, cars, animals, etc.) in live video feed from a camera.

#### Requirements:
1. **Python** (version 3.7+)
2. **OpenCV** (for image processing and video capturing)
3. **TensorFlow/Keras** (for deep learning models)
4. **Pre-trained Object Detection Model** (YOLOv3 or MobileNet)
5. **Numpy** (for mathematical operations)
6. **Matplotlib** (optional, for displaying results)

#### Steps:

### Step 1: Install Required Libraries


First, install the necessary libraries:

```bash
pip install opencv-python opencv-python-headless tensorflow numpy matplotlib
```

### Step 2: Load the Pre-trained Model (YOLO, MobileNet, etc.)


For simplicity, we’ll use a YOLOv3 (You Only Look Once) model, which is very popular for
object detection.

You can download the pre-trained YOLOv3 weights and configuration files from:

- **YOLOv3 Weights**: [Download link](https://pjreddie.com/media/files/yolov3.weights)


- **YOLOv3 Config File**: [Download
link](https://github.com/pjreddie/darknet/blob/master/cfg/yolov3.cfg)

Also, you'll need the **coco.names** file, which contains the names of objects the model can
recognize (e.g., "person", "car", "dog").

### Step 3: Write the Code

```python
import cv2
import numpy as np

# Load YOLO
net = cv2.dnn.readNet("yolov3.weights", "yolov3.cfg")
layer_names = net.getLayerNames()
output_layers = [layer_names[i - 1] for i in net.getUnconnectedOutLayers()]

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

# Initialize webcam
cap = cv2.VideoCapture(0)

while True:
# Capture frame-by-frame
ret, frame = cap.read()
height, width, channels = frame.shape

# Prepare the image for YOLO model


blob = cv2.dnn.blobFromImage(frame, 0.00392, (416, 416), (0, 0, 0), True, crop=False)
net.setInput(blob)
outs = net.forward(output_layers)

# Process the detections


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:
# Object detected
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)

# Save object details


boxes.append([x, y, w, h])
confidences.append(float(confidence))
class_ids.append(class_id)

# Apply non-maxima suppression to remove duplicates


indices = cv2.dnn.NMSBoxes(boxes, confidences, 0.5, 0.4)

# Draw bounding boxes and labels


if len(indices) > 0:
for i in indices.flatten():
x, y, w, h = boxes[i]
label = str(classes[class_ids[i]])
confidence = str(round(confidences[i], 2))

cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)


cv2.putText(frame, f"{label} {confidence}", (x, y - 10),
cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)

# Display the resulting frame


cv2.imshow("Real-Time Object Detection", frame)

# Exit condition (press 'q' to quit)


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

# Release the capture and close windows


cap.release()
cv2.destroyAllWindows()
```

### Explanation:
- **YOLO Network:** The pre-trained YOLOv3 model is loaded using OpenCV’s
`cv2.dnn.readNet()` function. It’s designed to detect objects in images and video.
- **Video Capture:** OpenCV’s `cv2.VideoCapture(0)` captures real-time video from the
webcam.
- **Blob Preparation:** The image frame is resized and normalized before passing it to the
YOLO network using `cv2.dnn.blobFromImage()`.
- **Bounding Boxes:** Detected objects are drawn as bounding boxes, and their class labels
and confidence scores are displayed on the video feed.
- **NMS (Non-Maximum Suppression):** This step is used to remove duplicate detections for
the same object.

### Step 4: Test the System


Once the code is running, you can point your camera towards objects, and the system will
display bounding boxes around detected objects. The labels will correspond to the names of
the detected objects (e.g., "person", "car", "dog").

### Step 5: Optional Enhancements:


1. **Multiple Camera Support:** Extend the system to handle multiple camera inputs
simultaneously.
2. **Real-Time Alerts:** Implement a system to send alerts when certain objects are
detected (e.g., "person" or "car").
3. **Accuracy Improvement:** Fine-tune the model with a custom dataset if required.
4. **Cloud Processing:** Push the results to a cloud server for further analysis or storage.

#### Conclusion:
This is a basic real-time object detection system, but you can expand it further by
incorporating more advanced techniques such as image segmentation, tracking, or even
integrating it into larger systems like security cameras or autonomous robots.

You might also like