Skip to content

Commit

Permalink
Merge pull request ndleah#265 from kevin-291/object-detection
Browse files Browse the repository at this point in the history
Object detection
  • Loading branch information
ndleah authored Jun 2, 2024
2 parents a2baa17 + c2f1739 commit df9ad57
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 0 deletions.
31 changes: 31 additions & 0 deletions Object_Detection/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
![Star Badge](https://img.shields.io/static/v1?label=%F0%9F%8C%9F&message=If%20Useful&style=style=flat&color=BC4E99)
![Open Source Love](https://badges.frapsoft.com/os/v1/open-source.svg?v=103)

# Object Detection


## 🛠️ Description

This folder contains a Python script for a real-time object detection application using the YOLOv9 model from Ultralytics. The application uses the default camera (webcam) for video input and displays detected objects with bounding boxes, including labels of the detected object and their probability.

## ⚙️ Languages or Frameworks Used

Open Command Prompt and use the following command to install the required modules:
``` bash
pip install -r requirements.txt
```

## 🌟 How to run

You can run the program using the following command
``` bash
python object-detection.py
```

## 📺 Demo
<p align="center">
<img src="image.png" width=40% height=40%>

## 🤖 Author

[Kevin Cherian George](https://github.com/kevin-291)
Binary file added Object_Detection/image.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
65 changes: 65 additions & 0 deletions Object_Detection/object-detection.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import cv2
import torch
from ultralytics import YOLO

device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')

def main():

# Load YOLOv9 model
model = YOLO('yolov9c.pt')
model.to(device)

# Initialize video capture (0 for default camera)
cap = cv2.VideoCapture(0)

# Check if the video capture device is opened
if not cap.isOpened():
print("Error: Could not open video capture device")
return

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

if not ret:
print("Error: Could not read frame")
break

# Use YOLOv9 model to make predictions
results = model(frame)

# Process the results
for result in results:
# Loop through each detected object
for box in result.boxes:
# Get coordinates and class label
x1, y1, x2, y2 = box.xyxy[0]
label_id = int(box.cls[0].item())
confidence = box.conf[0].item()

# Get the class label from YOLO model
class_label = model.names[label_id]

# Create the label text
label_text = f"{class_label}: {confidence:.2f}"

# Draw bounding box on the frame
cv2.rectangle(frame, (int(x1), int(y1)), (int(x2), int(y2)), (0, 255, 0), 2)

# Draw the label text on the frame above the bounding box
cv2.putText(frame, label_text, (int(x1), int(y1) - 5), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 1)

# Display the frame with bounding boxes and labels
cv2.imshow('Real-Time Object Detection', frame)

# Exit the loop if the user presses 'q'
if cv2.waitKey(1) & 0xFF == ord('q'):
break

# Release the video capture device and close the window
cap.release()
cv2.destroyAllWindows()

if __name__ == "__main__":
main()
2 changes: 2 additions & 0 deletions Object_Detection/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ultralytics>=8.0.100
opencv-python>=4.5.5.62

0 comments on commit df9ad57

Please sign in to comment.