基于传统机器学习的水果识别的水果集处理python代码实现
时间: 2023-12-25 12:05:18 浏览: 46
以下是基于传统机器学习的水果识别的水果集处理Python代码实现。
首先,我们需要导入必要的库和模块:
```python
import os
import cv2
import numpy as np
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import LabelEncoder
from sklearn.svm import SVC
from sklearn.metrics import accuracy_score, confusion_matrix
```
然后,我们需要加载数据集和标签:
```python
data_path = 'fruit_dataset'
data = []
labels = []
for folder in os.listdir(data_path):
folder_path = os.path.join(data_path, folder)
for img_file in os.listdir(folder_path):
img_path = os.path.join(folder_path, img_file)
img = cv2.imread(img_path)
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
img = cv2.resize(img, (100, 100))
data.append(img)
labels.append(folder)
```
接下来,我们将数据集和标签进行编码:
```python
le = LabelEncoder()
labels = le.fit_transform(labels)
```
然后,我们将数据集和标签进行拆分:
```python
X_train, X_test, y_train, y_test = train_test_split(data, labels, test_size=0.2, random_state=42)
```
接着,我们将数据集进行特征提取:
```python
def extract_features(data):
features = []
for img in data:
hsv = cv2.cvtColor(img, cv2.COLOR_RGB2HSV)
h, s, v = cv2.split(hsv)
hist_h = cv2.calcHist([h], [0], None, [16], [0, 180])
hist_s = cv2.calcHist([s], [0], None, [16], [0, 256])
hist_v = cv2.calcHist([v], [0], None, [16], [0, 256])
hist = np.concatenate([hist_h, hist_s, hist_v]).flatten()
features.append(hist)
return features
X_train_features = extract_features(X_train)
X_test_features = extract_features(X_test)
```
最后,我们使用支持向量机(SVM)模型来训练和预测:
```python
svm = SVC(kernel='linear', C=1, random_state=42)
svm.fit(X_train_features, y_train)
y_pred = svm.predict(X_test_features)
acc = accuracy_score(y_test, y_pred) * 100
conf_matrix = confusion_matrix(y_test, y_pred)
print('Accuracy:', acc)
print('Confusion Matrix:', conf_matrix)
```
完整的代码如下:
```python
import os
import cv2
import numpy as np
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import LabelEncoder
from sklearn.svm import SVC
from sklearn.metrics import accuracy_score, confusion_matrix
data_path = 'fruit_dataset'
data = []
labels = []
for folder in os.listdir(data_path):
folder_path = os.path.join(data_path, folder)
for img_file in os.listdir(folder_path):
img_path = os.path.join(folder_path, img_file)
img = cv2.imread(img_path)
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
img = cv2.resize(img, (100, 100))
data.append(img)
labels.append(folder)
le = LabelEncoder()
labels = le.fit_transform(labels)
X_train, X_test, y_train, y_test = train_test_split(data, labels, test_size=0.2, random_state=42)
def extract_features(data):
features = []
for img in data:
hsv = cv2.cvtColor(img, cv2.COLOR_RGB2HSV)
h, s, v = cv2.split(hsv)
hist_h = cv2.calcHist([h], [0], None, [16], [0, 180])
hist_s = cv2.calcHist([s], [0], None, [16], [0, 256])
hist_v = cv2.calcHist([v], [0], None, [16], [0, 256])
hist = np.concatenate([hist_h, hist_s, hist_v]).flatten()
features.append(hist)
return features
X_train_features = extract_features(X_train)
X_test_features = extract_features(X_test)
svm = SVC(kernel='linear', C=1, random_state=42)
svm.fit(X_train_features, y_train)
y_pred = svm.predict(X_test_features)
acc = accuracy_score(y_test, y_pred) * 100
conf_matrix = confusion_matrix(y_test, y_pred)
print('Accuracy:', acc)
print('Confusion Matrix:', conf_matrix)
```