forked from ndleah/python-mini-project
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request ndleah#265 from kevin-291/object-detection
Object detection
- Loading branch information
Showing
4 changed files
with
98 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
ultralytics>=8.0.100 | ||
opencv-python>=4.5.5.62 |