コード例 #1
0
def extractimage(imageParam):
    data = []
    # ap = argparse.ArgumentParser()
    # ap.add_argument("-t", "--training", required=True,
    # 	help="path to the training images")
    # args = vars(ap.parse_args())
    face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_alt.xml')
    image_scale = 1

    # for imagePath in paths.list_images(args["training"]):
    img = cv2.imdecode(numpy.fromstring(imageParam.read(), numpy.uint8),
                       cv2.IMREAD_UNCHANGED)
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

    faces = face_cascade.detectMultiScale(gray, 1.3, 5)
    for (x, y, w, h) in faces:
        cv2.rectangle(gray, (x, y), (x + w, y + h), (255, 0, 0), 2)
        roi_gray = gray[y:y + h, x:x + w]
        roi_color = img[y:y + h, x:x + w]

    desc = LocalBinaryPatterns(24, 8)
    if faces == ():
        return False
    else:
        hist = desc.describe(roi_gray)
        data.append(hist)
        return data
    # print(hist)
    # for x in hist:
    #     with open('data.txt', 'a') as f:
    #         f.write(str(x)+',')

    # with open('dataset.csv','wb') as file:
    # 	for line in data:
    # 		file.write(line)
コード例 #2
0
        # Print each image to get the working status
        print(imagePath)
        print("-------------")
        # extract our unique image ID (i.e. the filename)
        k = imagePath[imagePath.rfind(os.sep) + 1:]
        # print(k)

        features = desc.describe(image)
        index[k] = features

elif option == "lbp":
    print("LBP Descriptor")
    # initialize the local binary patterns descriptor along with
    # the data and label lists
    desc = LocalBinaryPatterns(24, 8)
    # data = []
    # labels = []
    # size = len(glob.glob(args["dataset"] + os.sep + "*.png"))

    # Training
    # loop over the training images
    for imagePath in glob.glob(args["dataset"] + os.sep + "*.png"):
        # load the image, convert it to gray scale, and describe it
        image = cv2.imread(imagePath)
        print(imagePath)
        print("-------------")
        # extract our unique image ID (i.e. the filename)
        k = imagePath[imagePath.rfind(os.sep) + 1:]
        gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
        features = desc.describe(gray)
コード例 #3
0
import cv2

data = []
ap = argparse.ArgumentParser()
ap.add_argument("-t",
                "--training",
                required=True,
                help="path to the training images")
args = vars(ap.parse_args())
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_alt.xml')
image_scale = 1

for imagePath in paths.list_images(args["training"]):
    img = cv2.imread(imagePath)
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

    faces = face_cascade.detectMultiScale(gray, 1.3, 5)
    for (x, y, w, h) in faces:
        cv2.rectangle(gray, (x, y), (x + w, y + h), (255, 0, 0), 2)
        roi_gray = gray[y:y + h, x:x + w]
        roi_color = img[y:y + h, x:x + w]

    rezise_img = cv2.resize(roi_gray, (300, 300))
    desc = LocalBinaryPatterns(24, 8)
    hist = desc.describe(rezise_img)
    data.append(hist)
# print(faces)
for x in data:
    with open('data.txt', 'a') as f:
        f.write(str(x) + ',')
コード例 #4
0
def binary_function(type_rec):

    l = []
    # construct the argument parse and parse the arguments
    ap = argparse.ArgumentParser()
    ap.add_argument("-t",
                    "--training",
                    required=False,
                    default='images/training/',
                    help="path to the training images")
    ap.add_argument("-e",
                    "--testing",
                    required=False,
                    default='images/testing/',
                    help="path to the tesitng images")
    args = vars(ap.parse_args())
    # initialize the local binary patterns descriptor along with
    # the data and label lists
    if type_rec == 'symbol':
        desc = LocalBinaryPatterns(24, 8)
    else:
        desc = LocalBinaryPatterns(24, 3)
    data = []
    labels = []

    # loop over the training images
    for imagePath in paths.list_images(args["training"]):
        # for imagePath in os.listdir('images/training/'):
        # load the image, convert it to grayscale, and describe it
        image = cv2.imread(imagePath)
        gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
        hist = desc.describe(gray)
        # extract the label from the image path, then update the
        # label and data lists
        labels.append(imagePath.split(os.path.sep)[-2])
        data.append(hist)
    model = DecisionTreeClassifier(random_state=0)
    model.fit(data, labels)

    # loop over the testing images
    for imagePath in paths.list_images(args["testing"]):
        # for imagePath in os.listdir('images/testing/'):
        # load the image, convert it to grayscale, describe it,
        # and classify it
        image = cv2.imread(imagePath)
        gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
        hist = desc.describe(gray)
        prediction = model.predict(hist.reshape(1, -1))

        # display the image and the prediction
        # cv2.putText(image, prediction[0], (10, 30), cv2.FONT_HERSHEY_SIMPLEX,
        # 	1.0, (0, 0, 255), 3)
        # cv2.imshow("Image", image)
        # cv2.waitKey(0)
        l.append([prediction[0], imagePath])

    # print (l)
    if type_rec == 'symbol':
        result = symbol(l[0][0])
        string = "The symbol is " + str(
            l[0][0]
        ) + " Meaning of symbol: " + result[0] + " Reference: " + result[1]
    else:
        s_p = []
        for i in l:
            s_p.append(i[0])
        patt = ', '.join(s_p)
        string = "The symbols identified are " + patt

    return string
コード例 #5
0
import pickle
from imutils import paths
import argparse
import cv2

# construct the argument parse and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-t",
                "--training",
                required=True,
                help="path to the training images")
args = vars(ap.parse_args())

# initialize the local binary patterns descriptor along with
# the data and label lists
desc = LocalBinaryPatterns(24, 8)
data = []
labels = []

print("[INFO] Labeling Images...")
# loop over the training images
for imagePath in paths.list_images(args["training"]):
    # load the image, convert it to grayscale, and describe it
    image = cv2.imread(imagePath, 0)
    #gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    hist = desc.describe(image)
    # extract the label from the image path, then update the
    # label and data lists
    # label = imagePath.split("\\")[1]
    labels.append(imagePath.split("\\")[1])
    data.append(hist)
コード例 #6
0
# Load hue and saturation data (note we immediately convert to a normal python array)
# (probably these shouldn't be h5 dbs after all, cpickle would be better)
db_hs = h5py.File('model/hs-db.hdf5', mode='r')
hue_set = db_hs['hue'][::]
sat_set = db_hs['sat'][::]
sat_total_set = db_hs['sat_total'][::]

# The None category is a very dark magenta
category_colors = ((0, 0, 255), (255, 0, 0), (0, 255, 0), (0, 255, 255),
                   (10, 0, 10))

category_hue_backwards = {0: 0, 240: 1, 120: 2, 60: 3, 300: 4}

model_lbp4 = pickle.loads(open('output/model_lbp4.cpickle', "rb").read())
desc4 = LocalBinaryPatterns(24, 4)  # 0.58
desc8 = LocalBinaryPatterns(24, 8)  # 0.58

# === DEFINITIONS ===


def test_args_hs(sat_factor, hue_factor):
    score = 0
    for img_id, asset_name in enumerate(asset_names):
        for patch_id in range(annotations.shape[1]):
            prediction = probs_raw[img_id, patch_id]

            hist_hue = hue_histograms[img_id, patch_id]
            hist_sat = sat_histograms[img_id, patch_id]

            hue_diffs = np.zeros(5)
コード例 #7
0
for filename in os.listdir(directory):
    if filename.endswith(".png"):
        queryPath = os.path.join(directory, filename)
        print(queryPath)
        queryImage = cv2.imread(queryPath)
        queryImage = cv2.resize(queryImage, (450, 360))
        cv2.putText(queryImage, queryPath, (10, 30), cv2.FONT_HERSHEY_SIMPLEX,
                    1.0, (0, 0, 255), 3)
        cv2.imshow("Query", queryImage)
        print("query: %s" % queryPath)
        if search == 0:
            if args["descriptor"] == "rgb":
                desc = RGBHistogram([8, 8, 8])
                queryFeatures = desc.describe(queryImage)
            elif args["descriptor"] == "lbp":
                desc = LocalBinaryPatterns(24, 8)
                gray = cv2.cvtColor(queryImage, cv2.COLOR_BGR2GRAY)
                queryFeatures = desc.describe(gray)
            elif args["descriptor"] == "hog":
                winSize = (64, 64)
                blockSize = (16, 16)
                blockStride = (8, 8)
                cellSize = (8, 8)
                nbins = 9
                derivAperture = 1
                winSigma = 4.
                histogramNormType = 0
                L2HysThreshold = 2.0000000000000001e-01
                gammaCorrection = 0
                nlevels = 64
                winStride = (8, 8)
コード例 #8
0
ap = argparse.ArgumentParser()
ap.add_argument("-i",
                "--input",
                required=True,
                help="path to the training images")
args = vars(ap.parse_args())

category_ids = {'brick': 0, 'concrete': 1, 'metal': 2, 'wood': 3, 'z_none': 4}

# initialize the local binary patterns descriptor along with
# the data and label lists
#desc = LocalBinaryPatterns(24, 8) # 0.64
#desc = LocalBinaryPatterns(24, 16) # 0.55
#desc = LocalBinaryPatterns(48, 16) # 0.55
#desc = LocalBinaryPatterns(24, 32) # 0.58
desc4 = LocalBinaryPatterns(24, 4)  # 0.58
desc8 = LocalBinaryPatterns(24, 8)  # 0.58

data4 = []
data8 = []

labels = []

# loop over the training images
image_paths = list(paths.list_images(args["input"]))
random.shuffle(image_paths)

split_point = int(len(image_paths) / 5 * 3)

training_set = image_paths[:split_point]
testing_set = image_paths[split_point:]
コード例 #9
0
from pyimagesearch.localbinarypatterns import LocalBinaryPatterns
from sklearn.svm import LinearSVC
import cv2
import matplotlib.pyplot as plt
from os import listdir
from os.path import isfile, join
import glob
from sklearn.svm import LinearSVC

trainingPath = "/home/krzysztof/Dokumenty/SNR_grupa1/images/training"
keyboard = glob.glob(trainingPath + "/keyboard" + "/*.*")
carpet = glob.glob(trainingPath + "/carpet" + "/*.*")
area_rug = glob.glob(trainingPath + "/area_rug" + "/*.*")
wrapping_paper = glob.glob(trainingPath + "/wrapping_paper" + "/*.*")
desc = LocalBinaryPatterns(24, 8)
data = []
labels = []
paths = carpet + keyboard + area_rug + wrapping_paper
print("Ekstrakcja cech\n")
for imagePath in paths:
    print(imagePath + "\n")
    image = cv2.imread(imagePath)
    print(str(image.shape) + "\n")
    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)  # Skala szarości
    hist = desc.describe(gray)  # LBP na obrazie - zwraca histogram

    # extract the label from the image path, then update the
    # label and data lists
    labels.append(imagePath.split("/")[-2])
    data.append(hist)
コード例 #10
0
import skimage
from skimage import data, filters, io, exposure, feature
import numpy as np
import IPython
import PIL
import easydict

ap = argparse.ArgumentParser()
ap.add_argument("-t",
                "--training",
                required=True,
                help="path to the training images")
ap.add_argument("-e", "--testing", required=True, help="blah")
args = vars(ap.parse_args())

bob = LocalBinaryPatterns(24, 8)
data = []
labels = []

for imagePath in paths.list_images(args['training']):
    # image = io.imread(imagePath)
    # # stretch = exposure.rescale_intensity(image)
    # grey = skimage.color.rgb2gray(image)
    image = cv2.imread(imagePath)
    grey = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    hist = bob.describe(grey)
    labels.append(imagePath.split(os.path.sep)[-2])
    data.append(hist)

StandardScaler.transform(data)
model = LinearSVC(C=1000.0, random_state=42)
コード例 #11
0
# construct the argument parse and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-t",
                "--training",
                required=True,
                help="path to the training images")
ap.add_argument("-e",
                "--testing",
                required=True,
                help="path to the tesitng images")
args = vars(ap.parse_args())

# initialize the local binary patterns descriptor along with
# the data and label lists
desc = LocalBinaryPatterns(30, 20)
data = []
labels = []

# loop over the training images
for imagePath in paths.list_images(args["training"]):
    # load the image, convert it to grayscale, and describe it
    image = cv2.imread(imagePath)
    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    hist = desc.describe(gray)

    # extract the label from the image path, then update the
    # label and data lists
    labels.append(imagePath.split("/")[-2])
    data.append(hist)
コード例 #12
0
# construct the argument parse and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-t",
                "--training",
                required=True,
                help="path to the training images")
ap.add_argument("-e",
                "--testing",
                required=True,
                help="path to the tesitng images")
args = vars(ap.parse_args())

# initialize the local binary patterns descriptor along with
# the data and label lists
desc = LocalBinaryPatterns(10, 3)
colorFeatures = ColorFeatures()

data = []
labels = []

# loop over the training images
for imagePath in paths.list_images(args["training"]):
    # load the image, convert it to grayscale, and describe it
    image = cv2.imread(imagePath)
    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    hist = desc.describe(gray)
    hist = colorFeatures.addFeatures(image, hist)

    # extract the label from the image path, then update the
    # label and data lists
コード例 #13
0
from os import listdir
from os.path import isfile, join
import glob
from sklearn.svm import LinearSVC
from sklearn.svm import LinearSVC
import pickle
import metody

with open('LBPdata8_2.pckl', 'rb') as f:
    data, labels = pickle.load(f)
print("Zaimportowano dane\n")
# data = np.array(data, dtype = "float32")

model = LinearSVC(C=100.0, random_state=42)
model.fit(data, labels)
desc = LocalBinaryPatterns(8, 2)
trainingMainPath = "/home/krzysztof/Dokumenty/SNR_grupa1/Folio Leaf Dataset/Folio"
# paths - wszystkie (pełne) ścieżki do zdjęć liści
paths = metody.getListOfFiles(trainingMainPath)

# Koniec aktu 1: Mamy cechy obrazu. Następnie można użyć ich do rozpoznawania obrazów
# Akt 2
print("Uczenie SVC\n")
model = LinearSVC(C=100.0, random_state=42)
model.fit(data, labels)
print("Testowanie\n")
# Testing

for imagePath in paths:
    # load the image, convert it to grayscale, describe it,
    # and classify it
コード例 #14
0
import os
import regex as re
from sklearn.metrics import classification_report, confusion_matrix, roc_curve, auc, roc_auc_score, accuracy_score
import pickle
from matplotlib import pyplot

# construct the argument parse and parse the arguments
#ap = argparse.ArgumentParser()
#ap.add_argument("-t", "--training", required=True,
#	help="path to the training images")
#ap.add_argument("-e", "--testing", required=True,
#	help="path to the tesitng images")
#args = vars(ap.parse_args())
# initialize the local binary patterns descriptor along with
# the data and label lists
desc = LocalBinaryPatterns(24, 3)  #Era 24,3
data = []
labels = []
y_test = []
y_pred = []
y_pred_number = []
y_pred_proba = []
# loop over the training images
for imagePath in paths.list_images(os.getcwd() + "/images/training"):
    # load the image, convert it to grayscale, and describe it
    image = cv2.imread(imagePath)
    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    hist = desc.describe(gray)
    # extract the label from the image path, then update the
    # label and data lists
    labels.append(imagePath.split(os.path.sep)[-2])
コード例 #15
0
# construct the argument parse and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-t",
                "--training",
                required=True,
                help="path to the training images")
ap.add_argument("-e",
                "--testing",
                required=True,
                help="path to the tesitng images")
args = vars(ap.parse_args())

# initialize the local binary patterns descriptor along with
# the data and label lists
desc = LocalBinaryPatterns(24, 8)
data = []
labels = []

# loop over the training images
for imagePath in paths.list_images(args["training"]):
    # load the image, convert it to grayscale, and describe it
    image = cv2.imread(imagePath)
    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    hist = desc.describe(gray)

    # extract the label from the image path, then update the
    # label and data lists
    labels.append(imagePath.split("/")[-2])
    data.append(hist)
コード例 #16
0
vocab = pickle.loads(open(args["codebook"], "rb").read())
bovw = BagOfVisualWords(vocab)

# Load the bovw classifier
model = pickle.loads(open(args["model"], "rb").read())

# Load hue and saturation data
db_hs = h5py.File('model/hs-db.hdf5', mode='r')
hue_set = db_hs['hue'][::]
sat_set = db_hs['sat'][::]
sat_total_set = db_hs['sat_total'][::]

# Load lbp models
model_lbp4 = pickle.loads(open('model/model_lbp4.cpickle', "rb").read())
model_lbp8 = pickle.loads(open('model/model_lbp8.cpickle', "rb").read())
desc4 = LocalBinaryPatterns(24, 4)
desc8 = LocalBinaryPatterns(24, 8)

# The None category is a very dark magenta
#category_colors = ((0,0,255),(255,0,0),(0,255,0),(0,255,255),(10,0,10))
category_colors = ((0, 0, 255), (255, 0, 0), (0, 255, 0), (0, 255, 255), (0, 0,
                                                                          0))

category_names = ("brick", "concrete", "metal", "wood", "z_none")

# Whether to scale the input image down to 1024 width
flag_resize_image = True

# Whether to resize all patches to 364x364. Slows things down immensely, but may be necessary for accuracy?
flag_resize_patch = False
コード例 #17
0
# Check for model existence
if os.path.exists(modelName):
    with open(modelName, 'rb') as f:
        try:
            model = cPickle.load(f)
        except:
            print("Error reading in the model!!! exiting")
            sys.exit(1)
else:
    print("Error : {} does not exist".format(modelName))
    sys.exit(1)

# initialize the local binary patterns descriptor along with
# the data and label lists
desc = LocalBinaryPatterns(24, 8)

# load the image and define the window width and height
origImage = cv2.imread(args["image"])

# resize the image
image = cv2.resize(origImage,
                   None,
                   fx=.25,
                   fy=.25,
                   interpolation=cv2.INTER_AREA)

# window size for sliding window
(winW, winH) = (128, 128)
stepSize = 64
# (winW, winH) = (64, 64)  # too small
コード例 #18
0
ap = argparse.ArgumentParser()
ap.add_argument("-t",
                "--training",
                required=True,
                help="path to the training images")
ap.add_argument("-e",
                "--testing",
                required=True,
                help="path to the tesitng images")
args = vars(ap.parse_args())

lbpPointCount = 10

# initialize the local binary patterns descriptor along with
# the data and label lists
desc = LocalBinaryPatterns(lbpPointCount, 3)
colorFeatures = ColorFeatures()

data = numpy.empty((0, lbpPointCount + 5), float)
labels = []

# loop over the training images
for imagePath in paths.list_images(args["training"]):
    # load the image, convert it to grayscale, and describe it
    image = cv2.imread(imagePath)
    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    hist = desc.describe(gray)
    hist = colorFeatures.addFeatures(image, hist)

    # extract the label from the image path, then update the
    # label and data lists