#오류 0
import cv2
import os
import glob
from sklearn.model_selection import train_test_split
from sklearn import datasets, metrics
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import accuracy_score
import joblib
# 이미지 학습 크기와 경로 지정하기
image_size = (20, 10)
#filename = inspect.getframeinfo(inspect.currentframe()).filename
#path = os.path.dirname(os.path.abspath(filename))
#교재 코드
path = os.path.dirname(os.path.abspath('__file__'))
#path_fish = path + 'C:/Users/USER/장석희/fish'
#path_nofish = path + 'C:/Users/USER/장석희/nofish'
#교재 코드
path_fish = path + '/fish'
path_nofish = path + '/nofish'
x = [] # 이미지 데이터 images
y = [] # 레이블 데이터 labes
# 이미지 데이터를 읽어 들이고 배열에 넣기 --- (*1)
def read_dir(path, label):
files = glob.glob(path + "/*.jpg")
for f in files:
img = cv2.imread(f)
img = cv2.resize(img, image_size) #이미지 크기 통일
img_data = img.reshape(-1) # 1차원으로 전개하기
x.append(img_data)
y.append(label)
# 이미지 데이터 읽어 들이기
read_dir(path_nofish, 0)
read_dir(path_fish, 1)
# 데이터를 학습 전용과 테스트 전용으로 분리하기 --- (*2)
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.2)
# 데이터 학습하기 --- (*3)
clf = RandomForestClassifier()
clf.fit(x_train, y_train)
# 정답률 확인하기 --- (*4)
y_pred = clf.predict(x_test)
print(accuracy_score(y_test, y_pred))
# 데이터 저장하기 --- (*5)
joblib.dump(clf, 'fish.pkl')
error Traceback (most recent call last)
<ipython-input-69-5366138763c3> in <module>
43
44 # 이미지 데이터 읽어 들이기
---> 45 read_dir(path_nofish, 0)
46 read_dir(path_fish, 1)
47
<ipython-input-69-5366138763c3> in read_dir(path, label)
36 for f in files:
37 img = cv2.imread(f)
---> 38 img = cv2.resize(img, image_size) #이미지 크기 통일
39 img_data = img.reshape(-1) # 1차원으로 전개하기
40 x.append(img_data)
error: OpenCV(4.5.5) D:\a\opencv-python\opencv-python\opencv\modules\imgproc\src\resize.cpp:4052: error: (-215:Assertion failed) !ssize.empty() in function 'cv::resize'
댓글 입력