anaconda3로 visual studio code를 실행하고 거기서 python을 다운받아서 실행하였고 YOLOv5 2024-5-24 Python-3.11.7 torch-2.3.0+cpu CPU
import torch
import cv2
from numpy import random
YOLOv5 모델 정의
model = torch.hub.load('ultralytics/yolov5', 'custom', path='best.pt')
cap = cv2.VideoCapture(0)
while cap.isOpened():
ret, frame = cap.read()
if ret:
# 프레임 크기 조정
frame = cv2.resize(frame, (640, 480))
# 이미지를 모델에 입력
results = model(frame)
# 객체 감지 결과 얻기
detections = results.pandas().xyxy[0]
if not detections.empty:
# 결과를 반복하며 객체 표시
for _, detection in detections.iterrows():
x1, y1, x2, y2 = detection[['xmin', 'ymin', 'xmax', 'ymax']].astype(int).values
label = detection['name']
conf = detection['confidence']
# 박스와 라벨 표시
color = [int(c) for c in random.choice(range(256), size=3)]
cv2.rectangle(frame, (x1, y1), (x2, y2), color, 2)
cv2.putText(frame, f'{label} {conf:.2f}', (x1, y1 - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, color, 2)
# 프레임 표시
cv2.imshow('frame', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
작동시키면
PS C:\Users\dlgus\OneDrive\Desktop\python_code> & C:/Users/dlgus/anaconda3/python.exe c:/Users/dlgus/OneDrive/Desktop/python_code/main1-2.py
Using cache found in C:\Users\dlgus/.cache\torch\hub\ultralytics_yolov5_master
YOLOv5 2024-5-24 Python-3.11.7 torch-2.3.0+cpu CPU
Traceback (most recent call last):
File "C:\Users\dlgus/.cache\torch\hub\ultralytics_yolov5_master\hubconf.py", line 50, in _create
model = DetectMultiBackend(path, device=device, fuse=autoshape) # detection model
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\dlgus/.cache\torch\hub\ultralytics_yolov5_master\models\common.py", line 467, in init
model = attempt_load(weights if isinstance(weights, list) else w, device=device, inplace=True, fuse=fuse)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\dlgus/.cache\torch\hub\ultralytics_yolov5_master\models\experimental.py", line 98, in attempt_load
ckpt = torch.load(attempt_download(w), map_location="cpu") # load
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\dlgus\anaconda3\Lib\site-packages\torch\serialization.py", line 1025, in load
return _load(opened_zipfile,
^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\dlgus\anaconda3\Lib\site-packages\torch\serialization.py", line 1446, in _load
result = unpickler.load()
^^^^^^^^^^^^^^^^
File "C:\Users\dlgus\anaconda3\Lib\pathlib.py", line 873, in new
raise NotImplementedError("cannot instantiate %r on your system"
NotImplementedError: cannot instantiate 'PosixPath' on your system
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\dlgus/.cache\torch\hub\ultralytics_yolov5_master\hubconf.py", line 65, in _create
model = attempt_load(path, device=device, fuse=False) # arbitrary model
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\dlgus/.cache\torch\hub\ultralytics_yolov5_master\models\experimental.py", line 98, in attempt_load
ckpt = torch.load(attempt_download(w), map_location="cpu") # load
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\dlgus\anaconda3\Lib\site-packages\torch\serialization.py", line 1025, in load
return _load(opened_zipfile,
^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\dlgus\anaconda3\Lib\site-packages\torch\serialization.py", line 1446, in _load
result = unpickler.load()
^^^^^^^^^^^^^^^^
File "C:\Users\dlgus\anaconda3\Lib\pathlib.py", line 873, in new
raise NotImplementedError("cannot instantiate %r on your system"
NotImplementedError: cannot instantiate 'PosixPath' on your system
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "c:\Users\dlgus\OneDrive\Desktop\python_code\main1-2.py", line 6, in
model = torch.hub.load('ultralytics/yolov5', 'custom', path='best.pt')
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\dlgus\anaconda3\Lib\site-packages\torch\hub.py", line 568, in load
model = _load_local(repo_or_dir, model, *args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\dlgus\anaconda3\Lib\site-packages\torch\hub.py", line 597, in _load_local
model = entry(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\dlgus/.cache\torch\hub\ultralytics_yolov5_master\hubconf.py", line 88, in custom
return _create(path, autoshape=autoshape, verbose=_verbose, device=device)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\dlgus/.cache\torch\hub\ultralytics_yolov5_master\hubconf.py", line 83, in _create
raise Exception(s) from e
Exception: cannot instantiate 'PosixPath' on your system. Cache may be out of date, try force_reload=True
or see PyTorch Hub - Ultralytics YOLO Docs for help.
이런 형식으로 에러가 발생하는대 도움을 받을수 있을까요?