Traceback (most recent call last): File "i:\18Breakageratecalculation\SVM run\HOG_SVM-master\hog_color_self", line 16, in <module> from sklearn.externals import joblib ImportError: cannot import name 'joblib' from 'sklearn.externals' (C:\Users\win10\AppData\Local\Programs\Python\Python311\Lib\site-packages\sklearn\externals\__init__.py)
时间: 2023-07-08 14:48:29 浏览: 169
这个错误提示是因为你使用的是较新版本的scikit-learn(sklearn)库,而该库已经将`joblib`模块从`sklearn.externals`移动到了顶层,所以旧的导入方法不再适用。解决方法有两种:
1. 直接使用`joblib`模块:
```python
import joblib
```
2. 使用`sklearn`顶层导入:
```python
from sklearn import joblib
```
你可以根据自己的需要选择其中一种方法进行修改。
相关问题
Traceback (most recent call last): File "i:\18Breakageratecalculation\SVM run\HOG_SVM-master\hog_color_self", line 16, in <module> from sklearn import joblib ImportError: cannot import name 'joblib' from 'sklearn' (C:\Users\win10\AppData\Local\Programs\Python\Python311\Lib\site-packages\sklearn\__init__.py)
这个错误提示是因为你的scikit-learn(sklearn)库中没有`joblib`模块,可能是由于你的scikit-learn版本过低,或者没有正确安装。建议你先升级一下scikit-learn库,可以使用以下命令:
```python
pip install -U scikit-learn
```
如果升级后仍然无法导入`joblib`模块,可以尝试重新安装scikit-learn库,使用以下命令:
```python
pip uninstall scikit-learn
pip install scikit-learn
```
如果你使用的是conda环境,可以使用以下命令升级或重新安装:
```python
conda update scikit-learn
conda install scikit-learn
```
如果以上方法仍然无法解决问题,建议你检查一下Python环境和依赖库是否正确安装配置。
Traceback (most recent call last): File "i:\18Breakageratecalculation\SVM run\hog-color-svm", line 11, in <module> for i in range(num_samples = 681): ^^^^^^^^^^^^^^^^^^^^^^^^ TypeError: range() takes no keyword arguments
在`range()`函数中不能使用关键字参数来指定循环次数,因此抛出了`TypeError: range() takes no keyword arguments`异常。你需要将`num_samples = 681`这个赋值语句移到循环外面,然后使用`range(num_samples)`来指定循环次数。例如,你可以这样修改代码:
```
import cv2
import numpy as np
# 提取图像的HOG特征
def get_hog_features(image):
hog = cv2.HOGDescriptor()
hog_features = hog.compute(image)
return hog_features
# 加载训练数据集
train_data = [r"I:\18Breakageratecalculation\SVM run\detection_cut\whole\train128"]
train_labels = [r"I:\18Breakageratecalculation\SVM run\detection_cut\whole\train128\labels.txt"]
num_samples = 681 # 定义循环次数
for i in range(num_samples):
image = cv2.imread('image_'+str(i)+'.jpg', 0)
hog_features = get_hog_features(image)
hsv_image = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
color_hist = cv2.calcHist([hsv_image], [0, 1], None, [180, 256], [0, 180, 0, 256])
color_features = cv2.normalize(color_hist, color_hist).flatten()
train_data.append(hog_features)
train_labels.append(labels[i])
# 训练SVM模型
svm = cv2.ml.SVM_create()
svm.setType(cv2.ml.SVM_C_SVC)
svm.setKernel(cv2.ml.SVM_LINEAR)
svm.train(np.array(train_data), cv2.ml.ROW_SAMPLE, np.array(train_labels))
# 对测试图像进行分类
test_image = cv2.imread('I:\18Breakageratecalculation\mask-slic use\maskSLIC-master\result\split\result2\maskslic2_roi.png', 0)
test_features = get_hog_features(test_image)
result = svm.predict(test_features.reshape(1,-1))
# 显示分割结果
result_image = np.zeros(test_image.shape, np.uint8)
for i in range(test_image.shape[0]):
for j in range(test_image.shape[1]):
if result[i,j] == 1:
result_image[i,j] = 255
cv2.imshow('I:\18Breakageratecalculation\mask-slic use\maskSLIC-master\result\split\result2\Result.png', result_image)
cv2.waitKey(0)
cv2.destroyAllWindows()
```
阅读全文