示例#1
0
from numpy import mean, std
from sklearn.model_selection import cross_val_score

from classification.pipe import get_pipe
from datagen.DatasetGenerator import DatasetGenerator
from datagen.FileProviderAsl import FileProviderAsl
from preprocessing.PreProcessorAsl import PreProcessorAsl
from preprocessing.representation.BagOfHogs import BagOfHogs

g = DatasetGenerator(
    FileProviderAsl("../../../resource/dataset/fingerspelling5/dataset5/"),
    preprocessor=PreProcessorAsl(descriptor=BagOfHogs(
        "../../../resource/models/codebook_total_new.pkl")))
data, labels = g.generate()

model = get_pipe()
results = cross_val_score(model, data, labels.ravel(), cv=6)
print("Accuracy: " + str(mean(results)) + "(+/- " + str(std(results)))
def colourbased_skin_segmentation(image,
                                  lower_thresh=np.array([0, 80, 80],
                                                        dtype="uint8"),
                                  upper_thresh=np.array([20, 240, 240],
                                                        dtype="uint8")):
    converted = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
    skin_mask = cv2.inRange(converted, lower_thresh, upper_thresh)
    skin_mask = cv2.GaussianBlur(skin_mask, (3, 3), 2)

    # apply a series of erosions and dilations to the mask
    # using an elliptical kernel
    kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (3, 3))
    skin_mask = cv2.erode(skin_mask, kernel, iterations=1)
    skin_mask = cv2.dilate(skin_mask, kernel, iterations=1)

    # blur the mask to help remove noise, then apply the
    # mask to the frame
    _, likelihood = cv2.threshold(skin_mask, 10, 255, cv2.NORM_MINMAX)
    return likelihood


example_image_file = "../../../resource/examples/color_0_0028.png"
# read image
img, _ = FileProviderAsl.read_img(example_image_file)

cv2.imshow('image', img)
img = colourbased_skin_segmentation(img)
cv2.imshow("Likelihood", img.astype(np.uint8))
cv2.waitKey(0)
import random

from datagen.FileProviderAsl import FileProviderAsl
from preprocessing.PreProcessorAsl import PreProcessorAsl
from preprocessing.representation.BagOfHogs import BagOfHogs
from preprocessing.segmentation.MRFAsl import MRFAsl

codebook_file = '../../resource/models/codebook.pkl'

paths = FileProviderAsl(
    "../../resource/dataset/fingerspelling5/dataset5/").img_file_paths

paths = random.sample(paths[random.choice(list(paths.keys()))], k=100)

img = FileProviderAsl.read_img(random.choice(paths))
pp = PreProcessorAsl(segmenter=MRFAsl(), descriptor=BagOfHogs(codebook_file))
img = pp.preprocess(img)

descriptor = pp.get_descr(img)

print(str(descriptor))
示例#4
0
from sklearn.externals import joblib

from datagen.FileProviderAsl import FileProviderAsl
from preprocessing.PreProcessorAsl import PreProcessorAsl
from preprocessing.representation.BagOfHogs import BagOfHogs

paths_dict = FileProviderAsl(
    "../../resource/dataset/fingerspelling5/dataset5/").img_file_paths
codebook_file = '../../resource/models/codebook_total.pkl'

images = []
for letter in paths_dict:
    for path in paths_dict[letter]:
        images.append(FileProviderAsl.read_img(path))
    break

codebook = BagOfHogs(n_bins=16, winsize=6).get_codebook(
    PreProcessorAsl(img_size=(60, 60)).preprocess_all(images), k_words=512)

joblib.dump(codebook, codebook_file)
示例#5
0
import cv2
import numpy as np

from datagen.FileProviderAsl import FileProviderAsl
from preprocessing.segmentation.MRFAsl import MRFAsl

example_image_file = "../../../resource/examples/color_0_0028.png"
# read image
img, img_depth = FileProviderAsl.read_img(example_image_file)

cv2.imshow('image', img)

img_segment = cv2.GaussianBlur(img, (11, 11), 2)

threshold = img_depth[int(img_depth.shape[0] / 2), int(img_depth.shape[1] / 2)]

img_gray = cv2.cvtColor(img_segment, cv2.COLOR_RGB2GRAY)

background_label = img_gray.copy()
background_label[img_depth != threshold] = 0
background_label[img_gray > 235] = 0
background_label[img_gray < 25] = 0

area_foreground = background_label.copy()
area_foreground[0:int(img_segment.shape[0] / 2 - 30),
                0:int(img_segment.shape[1] / 2 - 30)] = 0
area_foreground[int(img_segment.shape[0] / 2) + 30:img_segment.shape[0],
                int(img_segment.shape[1] / 2) + 30:img_segment.shape[0]] = 0

pixels_fg = img_segment[area_foreground != 0].reshape(-1, 3)
pixels_bg = img_segment[background_label == 0].reshape(-1, 3)
import cv2
import numpy as np
from numpy.linalg import norm
from numpy.ma import floor

from datagen.FileProviderAsl import FileProviderAsl
from preprocessing.segmentation.ColourHistogram import ColourHistogram

paths_dict = FileProviderAsl(sets=["E"], alphabet=["a"]).img_file_paths
winsize = 3
samples = []
for paths in paths_dict.values():
    for path in paths:
        img = read_image(path)
        img_filtered = cv2.pyrMeanShiftFiltering(img, 5, 50)

        y = int(floor(img.shape[0] / 2) - floor(winsize / 2))
        x = int(floor(img.shape[1] / 2) - floor(winsize / 2))
        # sample = img.copy()
        # cv2.rectangle(sample, (x, y), (x+winsize, y+winsize), (0, 255, 0))
        # cv2.imshow("Box", sample)

        window = img[y:y + winsize, x:x + winsize]
        samples.append(window.reshape(-1, 3))

n_bins = 32
bin_range = 255 / n_bins
hist = np.zeros(shape=(1, 3 * n_bins))
for sample in samples:
    hist += ColourHistogram.colour_hist(sample)
示例#7
0
from datagen.DatasetGenerator import DatasetGenerator
from datagen.FileProviderAsl import FileProviderAsl
from preprocessing.PreProcessorAsl import PreProcessorAsl
from preprocessing.representation.HistogramOfGradients import HistogramOfGradients
from preprocessing.segmentation.MRFAsl import MRFAsl

g = DatasetGenerator(file_provider=FileProviderAsl(
    "../../resource/dataset/fingerspelling5/dataset5/"),
                     preprocessor=PreProcessorAsl(
                         segmenter=MRFAsl(),
                         descriptor=HistogramOfGradients(window_size=6,
                                                         n_bins=8)))

data, labels = g.generate(sample_size=2500)

g.save(data, labels, '../../resource/models/', 'hog')