Skip to content
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

No module named 'models' #13021

Open
1 task done
zyad630 opened this issue May 17, 2024 · 2 comments
Open
1 task done

No module named 'models' #13021

zyad630 opened this issue May 17, 2024 · 2 comments
Labels
question Further information is requested

Comments

@zyad630
Copy link

zyad630 commented May 17, 2024

Search before asking

Question

this is code :
import cv2
import math
import torch
import pygame
from models.experimental import attempt_load
from utils.general import non_max_suppression, scale_coords
from utils.torch_utils import select_device

Initialize Pygame and Pygame Mixer

pygame.init()
pygame.mixer.init()
Sound = pygame.mixer.Sound(r"C:\Users\ITC\Downloads\mixkit-alert-alarm-1005.wav")

Initialize YOLOv5

device = select_device('')
model = torch.hub.load('ultralytics/yolov5', 'yolov5s', pretrained=True, force_reload=True)
model = attempt_load(r'C:\Users\ITC\Downloads\best.pt', map_location=device)
stride = int(model.stride.max()) # model stride
names = model.module.names if hasattr(model, 'module') else model.names

cap = cv2.VideoCapture(0)

while True:
ret, frame = cap.read()
img = frame.copy()
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)

# Inference
img = torch.from_numpy(img).to(device)
img = img.float()  # uint8 to fp16/32
img /= 255.0  # 0 - 255 to 0.0 - 1.0
if img.ndimension() == 3:
    img = img.unsqueeze(0)

# Predict
pred = model(img)[0]
pred = non_max_suppression(pred, 0.5, 0.4)

for i, det in enumerate(pred):
    if len(det):
        det[:, :4] = scale_coords(img.shape[2:], det[:, :4], frame.shape).round()

        for *xyxy, conf, cls in reversed(det):
            c = int(cls)
            confidence = conf.item() * 100
            if confidence > 50 and names[c] == 'person':  # Change to 'human' if that's your class name
                x1, y1, x2, y2 = [int(i) for i in xyxy]
                cv2.rectangle(frame, (x1, y1), (x2, y2), (0, 0, 255), 5)
                cv2.putText(frame, f'{names[c]} {confidence:.2f}%', (x1 + 8, y1 + 100), cv2.FONT_HERSHEY_SIMPLEX,
                            1, (255, 0, 0), 2)

                # Add your Pygame sound logic here
                pygame.mixer.Sound.play(Sound)
                # You might need to add logic to stop sound when there's no detection

cv2.imshow("command", frame)
if cv2.waitKey(1) == ord('a'):
    break

cap.release()
cv2.destroyAllWindows()
and this is the error in juputer


ModuleNotFoundError Traceback (most recent call last)
Cell In[7], line 5
3 import torch
4 import pygame
----> 5 from models.experimental import attempt_load
6 from utils.general import non_max_suppression, scale_coords
7 from utils.torch_utils import select_device

ModuleNotFoundError: No module named 'models'

Additional

No response

@zyad630 zyad630 added the question Further information is requested label May 17, 2024
Copy link
Contributor

👋 Hello @zyad630, 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.

Requirements

Python>=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

Environments

YOLOv5 may be run in any of the following up-to-date verified environments (with all dependencies including CUDA/CUDNN, Python and PyTorch preinstalled):

Status

YOLOv5 CI

If 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

@glenn-jocher
Copy link
Member

Hey there! 🚀 It looks like your script is unable to locate the models module which is typically part of the directory structure. One common cause is not being in the correct directory when running your Jupyter notebook or your environment not being set up properly.

Ensure that you're running the Jupyter Notebook in the root directory of your cloned yolov5 repository. Alternatively, you can adjust your sys path at the beginning of your script to include the root directory of YOLOv5:

import sys
sys.path.append('path_to_yolov5')

from models.experimental import attempt_load
# Your other import statements and code follow here

Make sure to replace 'path_to_yolov5' with the path to your YOLOv5 directory. This adjustment tells Python to include your specified directory in its search for modules.

Hope this helps! Let me know if there's anything else you need. 😊

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants