-1

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:\Users\Admin\Desktop\air\Tool_Object_Detection\yolov5\detect.py", line 114, in run
    for path, im, im0s, vid_cap, s in dataset:
  File "c:\Users\Admin\Desktop\air\Tool_Object_Detection\yolov5\utils\dataloaders.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:\a\opencv-python\opencv-python\opencv\modules\highgui\src\window.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

Christoph Rackwitz
  • 11,317
  • 4
  • 27
  • 36
Rajesh Khan
  • 59
  • 1
  • 4
  • Does this answer your question? [error: (-2:Unspecified error) The function is not implemented. Rebuild the library with Windows, GTK+ 2.x or Cocoa support](https://stackoverflow.com/questions/67120450/error-2unspecified-error-the-function-is-not-implemented-rebuild-the-libra) – Christoph Rackwitz Oct 12 '22 at 19:30

2 Answers2

0

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

Rajesh Khan
  • 59
  • 1
  • 4
  • no... you installed a headless variant, or it was installed as the dependency of another package. check `pip list` -- `Rebuild the library with Windows, GTK+ 2.x or Cocoa support.` indicates this -- prebuilt packages of `opencv-python`, be it 4.6.0 or any other version, always come with gui support. `opencv-python-headless` does *not*. – Christoph Rackwitz Oct 12 '22 at 19:29
0
pip install --upgrade pip
pip install opencv-contrib-python
Moritz Ringler
  • 9,772
  • 9
  • 21
  • 34
亮琦陈
  • 1
  • 1