我正在构建一个图像处理分类器,这段代码是一个API,用于预测整个代码运行的图像的图像类,除了这一行(pred = model.predict_classes(test_image))这个API是在Django框架中制作的,我正在使用python 2.7
如果我正常运行此代码(没有制作API)它运行完美,这是一个重点
def classify_image(request):
if request.method == 'POST' and request.FILES['test_image']:
fs = FileSystemStorage()
fs.save(request.FILES['test_image'].name, request.FILES['test_image'])
test_image = cv2.imread('media/'+request.FILES['test_image'].name)
if test_image is not None:
test_image = cv2.resize(test_image, (128, 128))
test_image = np.array(test_image)
test_image = test_image.astype('float32')
test_image /= 255
print(test_image.shape)
else:
print('image didnt load')
test_image = np.expand_dims(test_image, axis=0)
print(test_image)
print(test_image.shape)
pred = model.predict_classes(test_image)
print(pred)
return JsonResponse(pred, safe=False)
Run Code Online (Sandbox Code Playgroud) 更新
我正在研究机器学习文本分类,并且使用 svc 线性内核,整个代码都在工作,除了最后一行代码 (print (svm_model_linear.predict_proba(test)) 实际上是在构建一个分类器,其中有 3 个类别循环、足球和羽毛球,我有一些被标记为这些类别的人的 Facebook 状态,我也使用 train_test_split 训练了测试的分类器,之后我有一些未标记的状态,我想对它们进行分类,但最后一行代码给出我的错误
from sklearn.feature_extraction.text import CountVectorizer
cv = CountVectorizer(max_features = 700)
X = cv.fit_transform(corpus).toarray()
print X
y = dataset.iloc[:, 1].values
print y
# Splitting the dataset into the Training set and Test set
from sklearn.cross_validation import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size =
0.20, random_state = 0)
from sklearn.svm import SVC
svm_model_linear = SVC(kernel ='linear', C = 1,
probability=True).fit(X_train, y_train)
svm_predictions = svm_model_linear.predict(X_test)
# model accuracy …Run Code Online (Sandbox Code Playgroud) 我正在构建图像处理分类器,除了这一行以外的整个代码 -
input_img_resize=cv2.resize(input_img,(128,128))
这条线给我一个错误
('error: /io/opencv/modules/imgproc/src/imgwarp.cpp:3483: error: (-215) ssize.width > 0 && ssize.height > 0 in function resize')
Run Code Online (Sandbox Code Playgroud)
我的代码 -
PATH = os.getcwd()
# Define data path
data_path = PATH + '/data'
data_dir_list = os.listdir(data_path)
img_rows=128
img_cols=128
num_channel=3
num_epoch=30
num_classes = 67
img_data_list=[]
for dataset in data_dir_list:
img_list=os.listdir(data_path+'/'+ dataset)
print ('Loaded the images of dataset-'+'{}\n'.format(dataset))
for img in img_list:
input_img=cv2.imread(data_path + '/'+ dataset + '/'+ img )
input_img_resize=cv2.resize(input_img,(128,128))
img_data_list.append(input_img_resize)
Run Code Online (Sandbox Code Playgroud) python opencv machine-learning image-processing image-resizing