报错1: ImportError: cannot import name 'TryExcept' from 'utils'
Traceback (most recent call last):
File "/usr/local/lib/python3.11/dist-packages/IPython/core/interactiveshell.py", line 3550, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-2-388ad7e07587>", line 1, in <module>
runfile('/media/pi/Lexar3/python_test/Visual_Test/models/yolo.py', wdir='/media/pi/Lexar3/python_test/Visual_Test/models')
File "/home/pi/Downloads/pycharm-2024.1/plugins/python/helpers/pydev/_pydev_bundle/pydev_umd.py", line 197, in runfile
pydev_imports.execfile(filename, global_vars, local_vars) # execute the script
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/pi/Downloads/pycharm-2024.1/plugins/python/helpers/pydev/_pydev_imps/_pydev_execfile.py", line 18, in execfile
exec(compile(contents+"\n", file, 'exec'), glob, loc)
File "/media/pi/Lexar3/python_test/Visual_Test/models/yolo.py", line 24, in <module>
from models.common import * # noqa
^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/pi/Downloads/pycharm-2024.1/plugins/python/helpers/pydev/_pydev_bundle/pydev_import_hook.py", line 21, in do_import
module = self._system_import(name, *args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/media/pi/Lexar3/python_test/Visual_Test/models/common.py", line 49, in <module>
from utils import TryExcept
ImportError: cannot import name 'TryExcept' from 'utils' (/media/pi/Lexar3/python_test/utils.py)
报错原因:因为打开目录不是yolov5的根目录,所以找不到yolov5文件夹下的utils模块
解决方案1:直接把文件夹名字加上
from utils import TryExcept, emojis
from utils.downloads import gsutil_getsize
from utils.metrics import box_iou, fitness
改成
from <.utils所在的文件夹名>.utils import TryExcept, emojis
from <.utils所在的文件夹名>.utils.downloads import gsutil_getsize
from <.utils所在的文件夹名>.utils.metrics import box_iou, fitness
解决方案2:直接打开源代码所在的文件夹
报错2:All git commands will error until this is rectified.
WARNING:tensorflow:From C:\Users\23665\AppData\Local\Programs\Python\Python311\Lib\site-packages\keras\src\losses.py:2976: The name tf.losses.sparse_softmax_cross_entropy is deprecated. Please use tf.compat.v1.losses.sparse_softmax_cross_entropy instead.
Traceback (most recent call last):
File "C:\Users\23665\AppData\Local\Programs\Python\Python311\Lib\site-packages\git\__init__.py", line 296, in <module>
refresh()
File "C:\Users\23665\AppData\Local\Programs\Python\Python311\Lib\site-packages\git\__init__.py", line 287, in refresh
if not Git.refresh(path=path):
^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\23665\AppData\Local\Programs\Python\Python311\Lib\site-packages\git\cmd.py", line 631, in refresh
raise ImportError(err)
ImportError: Bad git executable.
The git executable must be specified in one of the following ways:
- be included in your $PATH
- be set via $GIT_PYTHON_GIT_EXECUTABLE
- explicitly set via git.refresh(<full-path-to-git-executable>)
All git commands will error until this is rectified.
This initial message can be silenced or aggravated in the future by setting the
$GIT_PYTHON_REFRESH environment variable. Use one of the following values:
- quiet|q|silence|s|silent|none|n|0: for no message or exception
- warn|w|warning|log|l|1: for a warning message (logging level CRITICAL, displayed by default)
- error|e|exception|raise|r|2: for a raised exception
Example:
export GIT_PYTHON_REFRESH=quiet
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "E:\python_test\Visual_Test\train.py", line 72, in <module>
GIT_INFO = check_git_info()
^^^^^^^^^^^^^^^^
File "C:\Users\23665\AppData\Local\Programs\Python\Python311\Lib\contextlib.py", line 81, in inner
return func(*args, **kwds)
^^^^^^^^^^^^^^^^^^^
File "E:\python_test\Visual_Test\utils\general.py", line 370, in check_git_info
import git
File "C:\Users\23665\AppData\Local\Programs\Python\Python311\Lib\site-packages\git\__init__.py", line 298, in <module>
raise ImportError("Failed to initialize: {0}".format(_exc)) from _exc
ImportError: Failed to initialize: Bad git executable.
The git executable must be specified in one of the following ways:
- be included in your $PATH
- be set via $GIT_PYTHON_GIT_EXECUTABLE
- explicitly set via git.refresh(<full-path-to-git-executable>)
All git commands will error until this is rectified.
This initial message can be silenced or aggravated in the future by setting the
$GIT_PYTHON_REFRESH environment variable. Use one of the following values:
- quiet|q|silence|s|silent|none|n|0: for no message or exception
- warn|w|warning|log|l|1: for a warning message (logging level CRITICAL, displayed by default)
- error|e|exception|raise|r|2: for a raised exception
Example:
export GIT_PYTHON_REFRESH=quiet
保存原因:git环境变量设置问题
解决·方案:在导入包的上方增加以下代码
import os
os.environ["GIT_PYTHON_REFRESH"] = "quiet"
报错3:cv2.error: OpenCV(4.8.1) /io/opencv/modules/imgcodecs/src/loadsave.cpp:802: error: (-215:Assertion failed) !buf.empty() in function 'imdecode_'
(注:报错原因:我在使用绿联hdmi采集卡进行图像采集时所遇到的问题。)
M_predict_test: weights=yolov5s.pt, source=0, data=data/coco128.yaml, imgsz=[640, 640], conf_thres=0.25, iou_thres=0.45, max_det=1000, device=, view_img=False, save_txt=False, save_csv=False, save_conf=False, save_crop=False, nosave=False, classes=None, agnostic_nms=False, augment=False, visualize=False, update=False, project=runs/detect, name=exp, exist_ok=False, line_thickness=1, hide_labels=False, hide_conf=False, half=False, dnn=False, vid_stride=180
YOLOv5 🚀 f573f3b Python-3.11.2 torch-2.1.2 CPU
Fusing layers...
YOLOv5s summary: 213 layers, 7225885 parameters, 0 gradients, 16.4 GFLOPs
WARNING ⚠️ Environment does not support cv2.imshow() or PIL Image.show()
Traceback (most recent call last):
File "/usr/local/lib/python3.11/dist-packages/IPython/core/interactiveshell.py", line 3550, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-2-148bbbd5c4c3>", line 1, in <module>
runfile('/media/pi/Lexar3/python_test/Visual_Test/M_predict_test.py', wdir='/media/pi/Lexar3/python_test/Visual_Test')
File "/home/pi/Downloads/pycharm-2024.1/plugins/python/helpers/pydev/_pydev_bundle/pydev_umd.py", line 197, in runfile
pydev_imports.execfile(filename, global_vars, local_vars) # execute the script
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/pi/Downloads/pycharm-2024.1/plugins/python/helpers/pydev/_pydev_imps/_pydev_execfile.py", line 18, in execfile
exec(compile(contents+"\n", file, 'exec'), glob, loc)
File "/media/pi/Lexar3/python_test/Visual_Test/M_predict_test.py", line 428, in <module>
main(opt)
File "/media/pi/Lexar3/python_test/Visual_Test/M_predict_test.py", line 423, in main
run(**vars(opt))
File "/home/pi/.local/lib/python3.11/site-packages/torch/utils/_contextlib.py", line 115, in decorate_context
return func(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^
File "/media/pi/Lexar3/python_test/Visual_Test/M_predict_test.py", line 174, in run
dataset = LoadStreams(source, img_size=imgsz, stride=stride, auto=pt, vid_stride=vid_stride)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/media/pi/Lexar3/python_test/Visual_Test/utils/dataloaders.py", line 379, in __init__
_, self.imgs[i] = cap.read() # guarantee first frame
^^^^^^^^^^
cv2.error: OpenCV(4.8.1) /io/opencv/modules/imgcodecs/src/loadsave.cpp:802: error: (-215:Assertion failed) !buf.empty() in function 'imdecode_'
解决方案:hdmi采集卡与摄像头断开再重新连接