加载数据,这我们获取图片是从微软提供资源进行机器学习
https://www.microsoft.com/en-us/download/confirmation.aspx?id=54765
import numpy as np
import matplotlib.pyplot as plt
import os
import cv2
cv 是对图片进行处理,os 操作文件路径,而 matplotlib 用于将图片可视化。加载后将图片解压到 images_data/PetImages

DATA_DIR = "images_data/PetImages"
CATEGORIES = ["Dog","Cat"]
for category in CATEGORIES:
path = os.path.join(DATA_DIR,category)
# print(path)
for img in os.listdir(path):
print(img)
img_array = cv2.imread(os.path.join(path,img),cv2.IMREAD_GRAYSCALE)
plt.imshow(img_array,cmap="gray")
plt.show()
break
break
显示第一张图片,这是一张 Dog 图片 cv2.IMREAD_GRAYSCALE 将图片转换为黑白照片,因为识别 Dog ,颜色对识别作用并不大,所以这里所以忽略图片

print(img_array.shape)
(500, 333)
从图片上来看,图片大小和方向都不一致,有图图片是长方形,有的图片是正方形。
IMG_SIZE = 50
new_array = cv2.resize(img_array, (IMG_SIZE, IMG_SIZE))
plt.imshow(new_array,cmap='gray')
plt.show()
我们对图片进行一些处理,图片尺寸缩减为 50 ,调整一个图片大小尽量小,而且可从中判断出 Dog。

training_data = []
def create_training_data():
for category in CATEGORIES:
print(category)
path = os.path.join(DATA_DIR,category)
class_num = CATEGORIES.index(category)
# print(path)
for img in os.listdir(path):
try:
img_array = cv2.imread(os.path.join(path,img),cv2.IMREAD_GRAYSCALE)
new_array = cv2.resize(img_array, (IMG_SIZE, IMG_SIZE))
training_data.append([new_array,class_num])
except Exception as e:
pass
create_training_data()
print(len(training_data))
12476
import random
random.shuffle(training_data)
for sample in training_data[:10]:
print(sample[1])
X = []
y = []
for features, label in training_data:
X.append(features)
y.append(label)
X = np.array(X).reshape(-1,IMG_SIZE,IMG_SIZE,1)
pickle_out = open("X.pickle","wb")
pickle.dump(X, pickle_out)
pickle_out.close()
pickle_out = open("y.pickle","wb")
pickle.dump(y, pickle_out)
pickle_out.close()
网友评论