skip to Main Content

I have trained a certain model and have the weights (weight.pt) and am able to detect objects from images and videos, but the problem is the webcam is not opening.

In google collab to train my model

!git clone https://github.com/ultralytics/yolov5 
%cd yolov5
%pip install -qr requirements.txt
%pip install -q roboflow
%pip install torch==1.8.2 torchvision==0.9.2 torchaudio===0.8.2 --extra-index-url https://download.pytorch.org/whl/lts/1.8/cu111


from roboflow import Roboflow
import torch
import os
import numpy as np
from matplotlib import pyplot as plt
from IPython.display import Image, clear_output #to display image
import glob
from IPython.display import Image, display



rf_model = Roboflow(model_format="yolov5",notebook="ultralytics")

os.environ['DATASET_DIRECTORY'] = "/content/dataset" #setting environment

//data set
dataset = project.version(5).download("yolov5")

!python train.py --img 640 --batch 16 --epochs 1 --data {dataset.location}/data.yaml --weight yolov5x.pt --cache --worker 2

!python detect.py  --img 640 --source 0 --weigths {path/to custom weights}

getting the error

 File "c:UsersAdminDesktopairTool_Object_Detectionyolov5detect.py", line 114, in run
    for path, im, im0s, vid_cap, s in dataset:
  File "c:UsersAdminDesktopairTool_Object_Detectionyolov5utilsdataloaders.py", line 406, in __next__
    if not all(x.is_alive() for x in self.threads) or cv2.waitKey(1) == ord('q'):  # q to quit
cv2.error: OpenCV(4.6.0) D:aopencv-pythonopencv-pythonopencvmoduleshighguisrcwindow.cpp:1333: error: (-2:Unspecified error) The function is not implemented. Rebuild the library with Windows, GTK+ 2.x or Cocoa support. If you are on Ubuntu or Debian, install libgtk2.0-dev and pkg-config, then re-run cmake or configure script in function 'cvWaitKey'

Then I tried ..

In the cam.py file

import cv2
import torch
import numpy as np

model = torch.hub.load('ultralytics/yolov5','custom','odec\best_v5s_640_500_21_9.pt')
cap = cv2.VideoCapture(0)

while cap.isOpened():
    ret,frame = cap.read()

    result = model(frame)
    cv2.imshow('YOLO',np.squeeze(result.render()))
    if cv2.waitKey(10) == ord('q'):
        break

cap.release()
cv2.destroyAllWindows()

The above is the code that I tried

2

Answers


  1. Chosen as BEST ANSWER

    There is no issue with the code the problem lies with the package opencv-python==4.6.0.66, changing the function code works, install lower version


  2. pip install --upgrade pip
    pip install opencv-contrib-python
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search