Esempio n. 1
0
def with_we_h(path, flag):
    # 拿到图像数据路径,方便后续读取
    imagePaths = sorted(list(utils_paths.list_images(path)))
    random.seed(42)
    random.shuffle(imagePaths)
    data2 = []
    labels = []

    for imagePath in imagePaths:
        # 读取图像数据
        image1 = cv2.imread(imagePath)
        # image = cv2.resize(image, (256, 256))
        image2 = cv2.cvtColor(image1, cv2.COLOR_BGR2HSV)
        h, s, v = cv2.split(image2)
        h_ = pywt.wavedec2(h, 'db4', level=2)
        s_ = pywt.wavedec2(v, 'db4', level=2)
        v_ = pywt.wavedec2(s, 'db4', level=2)
        h_ = WPEnergy(h_, fs=250, wavelet='db4', maxlevel=2)
        s_ = WPEnergy(s_, fs=250, wavelet='db4', maxlevel=2)
        v_ = WPEnergy(v_, fs=250, wavelet='db4', maxlevel=2)
        if h_ >= s_:
            k = h
        else:
            k = s
        if k >= v_:
            pass
        else:
            k = v
        data2.append(k)
        # 读取标签
        label = imagePath.split(os.path.sep)[-2]
        labels.append(label)
    # 对图像数据做scale操作
    data = np.array(data2, dtype="float") / 255.0
    labels = np.array(labels)

    # 数据集切分
    (trainX, testX, trainY, testY) = train_test_split(data,
                                                      labels,
                                                      test_size=0.25,
                                                      random_state=42)

    # 转换标签为one-hot encoding格式
    lb = LabelBinarizer()
    trainY = lb.fit_transform(trainY)
    testY = lb.transform(testY)

    # 数据增强处理
    aug = ImageDataGenerator(rotation_range=30,
                             width_shift_range=0.1,
                             height_shift_range=0.1,
                             shear_range=0.2,
                             zoom_range=0.2,
                             horizontal_flip=True,
                             fill_mode="nearest")
    if flag == 'train':
        return trainX, trainY
    else:
        return testX, testY
Esempio n. 2
0
def with_we_r(path, flag):
    # 拿到图像数据路径,方便后续读取
    imagePaths = sorted(list(utils_paths.list_images(path)))
    random.seed(42)
    random.shuffle(imagePaths)
    data1 = []
    labels = []

    for imagePath in imagePaths:
        # 读取图像数据
        image1 = cv2.imread(imagePath)
        # image = cv2.resize(image, (256, 256))
        b, g, r = cv2.split(image1)
        b_ = pywt.wavedec2(b, 'db4', level=2)
        g_ = pywt.wavedec2(g, 'db4', level=2)
        r_ = pywt.wavedec2(r, 'db4', level=2)
        b_ = WPEnergy(b_, fs=250, wavelet='db4', maxlevel=2)
        g_ = WPEnergy(g_, fs=250, wavelet='db4', maxlevel=2)
        r_ = WPEnergy(r_, fs=250, wavelet='db4', maxlevel=2)
        if b_ >= g_:
            k = b
        else:
            k = g
        if k >= r_:
            pass
        else:
            k = r
        data1.append(k)
        # 读取标签
        label = imagePath.split(os.path.sep)[-2]
        labels.append(label)
    # 对图像数据做scale操作
    data = np.array(data1, dtype="float") / 255.0
    labels = np.array(labels)

    # 数据集切分
    (trainX, testX, trainY, testY) = train_test_split(data,
                                                      labels,
                                                      test_size=0.25,
                                                      random_state=42)

    # 转换标签为one-hot encoding格式
    lb = LabelBinarizer()
    trainY = lb.fit_transform(trainY)
    testY = lb.transform(testY)

    # 数据增强处理
    aug = ImageDataGenerator(rotation_range=30,
                             width_shift_range=0.1,
                             height_shift_range=0.1,
                             shear_range=0.2,
                             zoom_range=0.2,
                             horizontal_flip=True,
                             fill_mode="nearest")
    if flag == 'train':
        return trainX, trainY
    else:
        return testX, testY
Esempio n. 3
0
def with_rgb(path, flag):
    # 拿到图像数据路径,方便后续读取
    imagePaths = sorted(list(utils_paths.list_images(path)))
    random.seed(42)
    random.shuffle(imagePaths)
    data4 = []
    data5 = []
    data6 = []
    labels = []
    for imagePath in imagePaths:
        # 读取图像数据
        image1 = cv2.imread(imagePath)
        # image = cv2.resize(image, (256, 256))
        b, g, r = cv2.split(image1)
        data4.append(b)
        data5.append(g)
        data6.append(r)
        label = imagePath.split(os.path.sep)[-2]
        labels.append(label)
    # 数据集切分
    (trainX_b, testX_b, trainY_b, testY_b) = train_test_split(data4,
                                                              labels,
                                                              test_size=0.25,
                                                              random_state=42)
    (trainX_g, testX_g, trainY_g, testY_g) = train_test_split(data5,
                                                              labels,
                                                              test_size=0.25,
                                                              random_state=42)
    (trainX_r, testX_r, trainY_r, testY_r) = train_test_split(data6,
                                                              labels,
                                                              test_size=0.25,
                                                              random_state=42)

    # 转换标签为one-hot encoding格式
    lb = LabelBinarizer()
    trainY = lb.fit_transform(trainY_b)
    testY = lb.transform(testY_b)

    # 数据增强处理
    aug = ImageDataGenerator(rotation_range=30,
                             width_shift_range=0.1,
                             height_shift_range=0.1,
                             shear_range=0.2,
                             zoom_range=0.2,
                             horizontal_flip=True,
                             fill_mode="nearest")
    if flag == 'train':
        return trainX_b, trainX_g, trainX_r, trainY
    else:
        return testX_b, testX_g, testX_r, testY
Esempio n. 4
0
def without_we(path, flag):
    # 拿到图像数据路径,方便后续读取
    imagePaths = sorted(list(utils_paths.list_images(path)))
    random.seed(42)
    random.shuffle(imagePaths)
    data3 = []
    labels = []
    for imagePath in imagePaths:
        # 读取图像数据
        image1 = cv2.imread(imagePath)
        # image = cv2.resize(image, (256, 256))
        image2 = cv2.cvtColor(image1, cv2.COLOR_BGR2HSV)
        b, g, r = cv2.split(image1)
        h, s, v = cv2.split(image2)
        data3.append(b)
        data3.append(g)
        data3.append(r)
        data3.append(h)
        data3.append(s)
        data3.append(v)
        label = imagePath.split(os.path.sep)[-2]
        for i in range(6):
            labels.append(label)
    # 数据集切分
    (trainX, testX, trainY, testY) = train_test_split(data3,
                                                      labels,
                                                      test_size=0.25,
                                                      random_state=42)

    # 转换标签为one-hot encoding格式
    lb = LabelBinarizer()
    trainY = lb.fit_transform(trainY)
    testY = lb.transform(testY)

    # 数据增强处理
    aug = ImageDataGenerator(rotation_range=30,
                             width_shift_range=0.1,
                             height_shift_range=0.1,
                             shear_range=0.2,
                             zoom_range=0.2,
                             horizontal_flip=True,
                             fill_mode="nearest")
    if flag == 'train':
        return trainX, trainY
    else:
        return testX, testY
Esempio n. 5
0
import matplotlib.pyplot as plt
import numpy as np
import argparse
import random
import pickle
import cv2
import os

#需先執行附錄檔案才可執行此檔案

print("------開始讀取數據------")
data = []
labels = []

# 拿到圖像數據路徑,方便後續讀取
imagePaths = sorted(list(utils_paths.list_images('圖庫路徑')))
random.seed(42)
random.shuffle(imagePaths)

# 讀取數據
for imagePath in imagePaths:
    # 讀取圖像數據,由於使用神經網路,需要给拉平成一维
    image = cv2.imread(imagePath)
    image = cv2.resize(image, (32, 32)).flatten()
    data.append(image)

    # 讀取標籤
    label = imagePath.split(os.path.sep)[-2]
    labels.append(label)

# 對圖像數據做scale操作
Esempio n. 6
0
import matplotlib.pyplot as plt
from cv2 import cv2
import numpy as np
import argparse
import random
import pickle

import os

# 读取数据和标签
print("------开始读取数据------")
data = []
labels = []

# 拿到图像数据路径,方便后续读取
imagePaths = sorted(list(utils_paths.list_images('./dataset')))
random.seed(42)
random.shuffle(imagePaths)

image_size = 256
# 遍历读取数据
for imagePath in imagePaths:
    # 读取图像数据
    image = cv2.imread(imagePath)
    image = cv2.resize(image, (image_size, image_size))
    data.append(image)
    # 读取标签
    label = imagePath.split(os.path.sep)[-2]
    labels.append(label)

data = np.array(data, dtype="float") / 255.0
Esempio n. 7
0
import matplotlib.pyplot as plt
import numpy as np
import argparse
import random
import pickle
import cv2
import os


# 读取数据和标签
print("------开始读取数据------")
data = []
labels = []

# 拿到图像数据路径,方便后续读取
imagePaths = sorted(list(utils_paths.list_images('./train')))
random.seed(42)
random.shuffle(imagePaths)

# 遍历读取数据
for imagePath in imagePaths:
    # 读取图像数据
    image = cv2.imread(imagePath)
    image = cv2.resize(image, (64, 64))
    data.append(image)
    # 读取标签
    label = imagePath.split(os.path.sep)[-2]
    labels.append(label)

# 对图像数据做scale操作
data = np.array(data, dtype="float") / 255.0
Esempio n. 8
0
# 导入工具包
import utils_paths
import numpy as np
import cv2

# 标签文件处理
rows = open("synset_words.txt").read().strip().split("\n")
classes = [r[r.find(" ") + 1:].split(",")[0] for r in rows]

# Caffe所需配置文件
net = cv2.dnn.readNetFromCaffe("bvlc_googlenet.prototxt",
                               "bvlc_googlenet.caffemodel")

# 图像路径
imagePaths = sorted(list(utils_paths.list_images("images/")))

# 图像数据预处理
image = cv2.imread(imagePaths[0])
resized = cv2.resize(image, (224, 224))
# 四个参数:image scalefactor size:图片h,w的大小 mean:RGB分别是多少,104,117,123是训练时得到的值 swapRB
blob = cv2.dnn.blobFromImage(resized, 1, (224, 224), (104, 117, 123))
print("First Blob: {}".format(blob.shape))

# 得到预测结果
net.setInput(blob)
preds = net.forward()  # 网络的前向传播

# 排序,取分类可能性最大的
idx = np.argsort(preds[0])[::-1][0]
text = "Label: {}, {:.2f}%".format(classes[idx], preds[0][idx] * 100)
cv2.putText(image, text, (5, 25), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 255),