Esempio n. 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)
# 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
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
# 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)
Esempio n. 5
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)
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
Esempio n. 7
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)
Esempio n. 8
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)
Esempio n. 9
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
Esempio n. 10
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
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])
Esempio n. 12
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