-
-
Notifications
You must be signed in to change notification settings - Fork 15.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to combine a yolov8 detector and a classifier #13078
Comments
👋 Hello @Joeyabuki99, thank you for your interest in YOLOv5 🚀! Please visit our ⭐️ Tutorials to get started, where you can find quickstart guides for simple tasks like Custom Data Training all the way to advanced concepts like Hyperparameter Evolution. If this is a 🐛 Bug Report, please provide a minimum reproducible example to help us debug it. If this is a custom training ❓ Question, please provide as much information as possible, including dataset image examples and training logs, and verify you are following our Tips for Best Training Results. RequirementsPython>=3.8.0 with all requirements.txt installed including PyTorch>=1.8. To get started: git clone https://github.com/ultralytics/yolov5 # clone
cd yolov5
pip install -r requirements.txt # install EnvironmentsYOLOv5 may be run in any of the following up-to-date verified environments (with all dependencies including CUDA/CUDNN, Python and PyTorch preinstalled):
StatusIf this badge is green, all YOLOv5 GitHub Actions Continuous Integration (CI) tests are currently passing. CI tests verify correct operation of YOLOv5 training, validation, inference, export and benchmarks on macOS, Windows, and Ubuntu every 24 hours and on every commit. Introducing YOLOv8 🚀We're excited to announce the launch of our latest state-of-the-art (SOTA) object detection model for 2023 - YOLOv8 🚀! Designed to be fast, accurate, and easy to use, YOLOv8 is an ideal choice for a wide range of object detection, image segmentation and image classification tasks. With YOLOv8, you'll be able to quickly and accurately detect objects in real-time, streamline your workflows, and achieve new levels of accuracy in your projects. Check out our YOLOv8 Docs for details and get started with: pip install ultralytics |
Hi there! Thanks for reaching out with your question. It looks like you're trying to combine a YOLOv8 detector with an EfficientNetB3 classifier, which is a great approach for leveraging the strengths of both models. Let's address the issue you're encountering. The error Here's a revised version of your code with some adjustments:
import torch
from tensorflow.keras.models import load_model
import cv2
# Load YOLOv8 model
detector = torch.load('/myPath/yolov8m_trained.pt')
# Load EfficientNetB3 classifier
classifier = load_model('/myPath/efficientnet_model_unfreeze.h5')
video_path = '/myPath/video_test.mp4'
cap = cv2.VideoCapture(video_path)
while cap.isOpened():
ret, frame = cap.read()
if not ret:
break
# Perform detection
results = detector(frame)
detections = results['pred'][0].cpu().numpy() # Adjust based on the actual output structure
for detection in detections:
x1, y1, x2, y2, conf, cls = detection
roi = frame[int(y1):int(y2), int(x1):int(x2)]
roi_resized = cv2.resize(roi, (224, 224))
roi_resized = roi_resized / 255.0
roi_resized = roi_resized.reshape(1, 224, 224, 3)
pred = classifier.predict(roi_resized)
class_id = pred.argmax(axis=1)[0]
label = f'Class: {class_id}, Conf: {conf:.2f}'
cv2.rectangle(frame, (int(x1), int(y1)), (int(x2), int(y2)), (0, 255, 0), 2)
cv2.putText(frame, label, (int(x1), int(y1) - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (0, 255, 0), 2)
cv2.imshow('Video', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
output_path = 'output_video.mp4'
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
out = cv2.VideoWriter(output_path, fourcc, 30.0, (int(cap.get(3)), int(cap.get(4))))
cap = cv2.VideoCapture(video_path)
while cap.isOpened():
ret, frame = cap.read()
if not ret:
break
results = detector(frame)
detections = results['pred'][0].cpu().numpy() # Adjust based on the actual output structure
for detection in detections:
x1, y1, x2, y2, conf, cls = detection
roi = frame[int(y1):int(y2), int(x1):int(x2)]
roi_resized = cv2.resize(roi, (224, 224))
roi_resized = roi_resized / 255.0
roi_resized = roi_resized.reshape(1, 224, 224, 3)
pred = classifier.predict(roi_resized)
class_id = pred.argmax(axis=1)[0]
label = f'Class: {class_id}, Conf: {conf:.2f}'
cv2.rectangle(frame, (int(x1), int(y1)), (int(x2), int(y2)), (0, 255, 0), 2)
cv2.putText(frame, label, (int(x1), int(y1) - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (0, 255, 0), 2)
out.write(frame)
cap.release()
out.release()
cv2.destroyAllWindows() Please ensure you have the latest versions of Feel free to reach out if you have any more questions or need further assistance. Happy coding! 🚀 |
Hi @glenn-jocher , thank you. I noticed that this problem can be resolved loading the yolo model with |
Hi @Joeyabuki99, Thank you for your question! It's great to hear that you've found a way to resolve the issue by loading the YOLO model with Using Here's a quick example to illustrate the correct usage: from yolov5 import YOLO
from tensorflow.keras.models import load_model
import cv2
# Load YOLOv5 model
detector = YOLO('/myPath/best.pt')
# Load EfficientNetB3 classifier
classifier = load_model('/myPath/efficientnet_model_unfreeze.h5')
video_path = '/myPath/video_test.mp4'
cap = cv2.VideoCapture(video_path)
while cap.isOpened():
ret, frame = cap.read()
if not ret:
break
# Perform detection
results = detector(frame)
detections = results.xyxy[0].cpu().numpy() # Adjust based on the actual output structure
for detection in detections:
x1, y1, x2, y2, conf, cls = detection
roi = frame[int(y1):int(y2), int(x1):int(x2)]
roi_resized = cv2.resize(roi, (224, 224))
roi_resized = roi_resized / 255.0
roi_resized = roi_resized.reshape(1, 224, 224, 3)
pred = classifier.predict(roi_resized)
class_id = pred.argmax(axis=1)[0]
label = f'Class: {class_id}, Conf: {conf:.2f}'
cv2.rectangle(frame, (int(x1), int(y1)), (int(x2), int(y2)), (0, 255, 0), 2)
cv2.putText(frame, label, (int(x1), int(y1) - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (0, 255, 0), 2)
cv2.imshow('Video', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows() This should work seamlessly for your use case. If you encounter any further issues or have additional questions, please don't hesitate to ask. The YOLO community and the Ultralytics team are here to help! 😊 |
Search before asking
Question
Hi guys, I want to combine togheter a detector trained with YOLOv8 and a classifier done with EfficientNetB3.
My detector is been saved with the
model.save(output_model_path)
and so has a.pt
extension, while the classifier is been saved with the same method but has a.h5
extension.Now how can I combine these two models and test the system on a custom video? I have tried this code but I'm receiving the error
TypeError: 'dict' object is not callable
on theresults = detector(frame)
(line 20).Thanks
` import torch
from tensorflow.keras.models import load_model
import cv2
The text was updated successfully, but these errors were encountered: