我已经训练了一个特定的模型,并有权重(weightiderpt),能够从图像和视频中检测物体,但问题是网络摄像头没有打开。
在google collab训练我的模特
!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}
获取错误
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'
然后我试着..
在cam.py文件中
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()
以上是我尝试的代码
1条答案
按热度按时间0ve6wy6x1#
代码没有问题,问题在于包opencv-python==4.6.0.66,更改功能代码可以工作,安装较低版本