Ejemplo n.º 1
0
    def test(self):
        """ Testing images with known mat files """
        if os.path.exists("./data/train_sdm.mat"):
            mat = io.loadmat("./data/train_sdm.mat")
            R = mat['R']
            b = mat['b']
            shape_x = mat['i']

            # accuracy = 0
            for idx, img in enumerate(self.grays):
                img = copy.deepcopy(self.grays[idx])
                shape_x = mat['i']
                faces = self.__get_dlib_rect(img)[0]
                rect = [
                    faces.left(),
                    faces.top(),
                    faces.right(),
                    faces.bottom()
                ]
                # get extend ground truth bounding box
                new_rect = self.__crop(img, rect, self.extend_rate)
                # crop image
                cropped = img[int(new_rect[1]):int(new_rect[3]),
                              int(new_rect[0]):int(new_rect[2])]
                # resize image
                img = self.__resize(cropped, self.new_size)
                # self.__recompute_shape(cropped, self.shapes[idx], rect)

                for i in range(1):
                    shape = Shape.turn_back_to_point(shape_x)
                    hog_descriptor = HOG(img, shape)
                    hog_x = hog_descriptor.extract()
                    shape_x = shape_x + np.matmul(hog_x, R[i, :]) + b[i, :]

                # accuracy += self.evaluate(shape_x, self.shapes[idx].get_vector())
                # print("Already completed", idx + 1, "images.")

                height, width = cropped.shape[:2]
                scale_x = self.new_size[0] / width
                scale_y = self.new_size[1] / height

                shape = Shape.turn_back_to_point(shape_x)
                for pt in shape.pts:
                    pt.x = pt.x / scale_x - (
                        rect[2] - rect[0]) / self.extend_rate + rect[0]
                    pt.y = pt.y / scale_y - (
                        rect[3] - rect[1]) / self.extend_rate + rect[1]

            # print("Accuracy:", accuracy/len(self.shapes))
                return shape
        return
Ejemplo n.º 2
0
def main():
    root = os.path.dirname(os.path.abspath(__file__))
    cifar_path = os.path.join('datasets', 'cifar-10')
    texture_path = os.path.join('datasets', 'texture')
    faces_path = os.path.join('datasets', 'faces')
    cifar10 = Cifar10(root, cifar_path)
    cifar10.read_data()
    cifar10.rotate_images()
    desc_texture = DescribableTexture(root, texture_path)
    desc_texture.split_data()
    faces = Faces(root, faces_path)
    faces.split_data()
    hog = HOG(show_images=False)
    lbp = LBP(show_images=False)

    datasets = [cifar10, desc_texture, faces]
    descriptors = [lbp, hog]

    for dataset in datasets:
        for descriptor in descriptors:
            print('--------------------------------------------------------')
            print('Results for {} on {}:'.format(descriptor.__class__.__name__,
                                                 dataset.__class__.__name__))
            descriptor.apply(dataset.train_data)
            clf = LinearSVC()
            clf = clf.fit(descriptor.descriptor, dataset.train_labels)
            descriptor.apply(dataset.test_data)
            predictions = clf.predict(descriptor.descriptor)
            score = clf.score(descriptor.descriptor, dataset.test_labels)
            print('Score = {}'.format(score))
            print(
                classification_report(dataset.test_labels,
                                      predictions,
                                      target_names=dataset.labels_names))
            cm = confusion_matrix(dataset.test_labels, predictions)
            ax = plt.subplot()
            sns.heatmap(cm, annot=True)
            ax.set_xlabel('Predicted labels')
            ax.set_ylabel('True labels')
            ax.set_title('Confusion Matrix for {}'.format(
                descriptor.__class__.__name__))
            ax.xaxis.set_ticklabels(dataset.labels_names)
            ax.yaxis.set_ticklabels(dataset.labels_names)
            plt.show()
            print('--------------------------------------------------------')
            print('\n\n')
Ejemplo n.º 3
0
from sklearn.svm import LinearSVC
from hog import HOG
import dataset
import argparse

ap = argparse.ArgumentParser()
ap.add_argument("-d", "--dataset", required=True,
                help="path to the dataset file")
ap.add_argument("-m", "--model", required=True,
                help="path to where the model will be stored")
args = vars(ap.parse_args())

(digits, target) = dataset.load_digits(args["dataset"])
data = []

hog = HOG(orientations=18, pixelspercell=(10, 10),
          cellsperblock=(1, 1), transform=True)

for image in digits:
    image = dataset.deskew(image, 20)
    image = dataset.center_extent(image, (20, 20))

    hist = hog.describe(image)
    data.append(hist)

# instantiate random for reproducible results
model = LinearSVC(random_state=42)
model.fit(data, target)

joblib.dump(model, args["model"])
ap = argparse.ArgumentParser()
ap.add_argument("-c",
                "--conf",
                required=True,
                help="path to the configuration file")
args = vars(ap.parse_args())

# load the configuration file and initialize the data list
conf = Conf(args["conf"])
data = []

# load the classifier, then initialize the Histogram of Oriented Gradients descriptor
# and the object detector
model = cPickle.loads(open(conf["classifier_path"]).read())
hog = HOG(orientations=conf["orientations"],
          pixelsPerCell=tuple(conf["pixels_per_cell"]),
          cellsPerBlock=tuple(conf["cells_per_block"]),
          normalize=conf["normalize"])
od = ObjectDetector(model, hog)

# grab the set of distraction paths and randomly sample them
dstPaths = list(paths.list_images(conf["image_distractions"]))
dstPaths = random.sample(dstPaths, conf["hn_num_distraction_images"])

# setup the progress bar
widgets = [
    "Mining: ",
    progressbar.Percentage(), " ",
    progressbar.Bar(), " ",
    progressbar.ETA()
]
pbar = progressbar.ProgressBar(maxval=len(dstPaths), widgets=widgets).start()
Ejemplo n.º 5
0
    def train_landmarks(self):
        """ Training face landmarks with Lasso function """
        # crop and resize training images
        orders, detect_box = self.__get_detection()
        self.__reload(orders)
        self.__crop_and_resize(detect_box)

        hog = []
        shapes = []
        for idx in range(len(self.grays)):
            print("Calculating ", idx, "th HOG features of training images...")
            # get hog features
            hog_descriptor = HOG(self.grays[idx], self.shapes[idx])
            h = hog_descriptor.extract()
            hog.append(h)
            # get shape vector list
            s = Shape.get_vector(self.shapes[idx])
            shapes.append(s)

        # true hog features and true shapes
        hog_star = np.array(hog)
        shapes_star = np.array(shapes)

        # get mean shape as x0
        #pdm = PointDistributionModel(self.shapes)
        #x0 = pdm.mean
        x0 = self.__get_mean_shape().get_vector()
        shape_x = np.array([x0.tolist()] * len(self.grays))

        # parameters we need
        R = []
        b = []
        # training
        for i in range(self.iterates):
            # delta shape vector
            delta_x = shapes_star - shape_x
            # hog features of computed shapes
            hog_x = np.zeros_like(hog_star)
            for j in range(len(self.grays)):
                # get hog features
                hog_descriptor = HOG(self.grays[j],
                                     Shape.turn_back_to_point(shape_x[j]))
                h = hog_descriptor.extract()
                hog_x[j, :] = h

            # linear regression
            if self.alpha == 0:
                reg = LinearRegression(fit_intercept=False)
            else:
                #reg = LinearRegression()
                #reg = SVR()
                #reg = Ridge(alpha=self.alpha)
                reg = Lasso(alpha=self.alpha)
            print("Calculating with Linear Regression...")
            reg.fit(hog_x, delta_x)
            R.append(reg.coef_.T)
            b.append(reg.intercept_.T)

            shape_x = shape_x + np.matmul(hog_x, R[i]) + b[i]

        # x0 = x0.tolist()
        io.savemat("./data/train_sdm", {"R": R, "b": b, "i": x0})
Ejemplo n.º 6
0
# construct the argument parse and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-m",
                "--model",
                required=True,
                help="path to where the model will be stored")
ap.add_argument("-i", "--image", required=True, help="path to the image file")
args = vars(ap.parse_args())

# load the model
model = joblib.load(args["model"])

# initialize the HOG descriptor
hog = HOG(orientations=9,
          pixelsPerCell=(4, 4),
          cellsPerBlock=(2, 2),
          normalize=True)

# load the image and convert it to grayscale
image = cv2.imread(args["image"])
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

# blur the image, find edges, and then find contours along the edged regions
blurred = cv2.GaussianBlur(gray, (5, 5), 0)
edged = cv2.Canny(blurred, 30, 150)
(cnts, _) = cv2.findContours(edged.copy(), cv2.RETR_EXTERNAL,
                             cv2.CHAIN_APPROX_SIMPLE)

# sort the contours by their x-axis position, ensuring that we read the numbers from
# left to right
cnts = sorted([(c, cv2.boundingRect(c)[0]) for c in cnts], key=lambda x: x[1])
from hog import HOG
import cv2

img = cv2.imread("tes.pgm",cv2.IMREAD_GRAYSCALE)
cobaa = HOG(img)

x,y = cobaa.gradient()
# print "X =",x
# print "y =",y
Ejemplo n.º 8
0
            "Error: command line should look like: \"python <fileName.py> <target image> <video file>\""
        )
        sys.exit()

    # read video
    video = cv2.VideoCapture(sys.argv[2])

    # exit if video not opened
    if not video.isOpened():
        print("Error: Could not open video")
        sys.exit()

    # create HOG and feature descriptor
    hogImage = cv2.imread(sys.argv[1])
    hogImage = hogify(hogImage, 128, 128)
    targetFD = HOG(hogImage)

    # while we do not have 10 consecutive frames that have changed
    print("Processing video... ")
    while not contiguousPassed:
        while imageIsBlack:
            # get first frame and resize if ok
            ok, frame = video.read()
            frameCheck(ok)
            frame = imutils.resize(frame, width=800)

            # get second frame and resize if ok
            ok2, frame2 = video.read()
            frameCheck(ok2)
            frame2 = imutils.resize(frame2, width=800)
Ejemplo n.º 9
0
argument_parser = argparse.ArgumentParser()
argument_parser.add_argument('-d',
                             '--dataset',
                             required=True,
                             help='Path to the dataset file.')
argument_parser.add_argument('-m',
                             '--model',
                             required=True,
                             help='Path to where the model will be stored.')
arguments = vars(argument_parser.parse_args())

(digits, target) = dataset.load_digits(arguments['dataset'])
data = []

hog = HOG(orientations=18,
          pixels_per_cell=(10, 10),
          cells_per_block=(1, 1),
          transform=True)

for image in digits:
    image = dataset.deskew(image, 20)
    image = dataset.center_extent(image, (20, 20))

    histogram = hog.describe(image)
    data.append(histogram)

model = LinearSVC(random_state=42)
model.fit(data, target)

joblib.dump(model, arguments['model'])
class Controller:
    def __init__(self):
        from hog import HOG
        self.hog_generator = HOG()

    def load_batch(self,path):
        '''
        Loading a image batch
        '''
        batch_loader = ImageLoader()
        
        return batch_loader.load_image(path)

    def grascale(self,image_batch):
        '''
        images are loaded bgr
        convert from rgb to gray
        '''
        new_batch = []
        for image in image_batch:
            frame = 0.114 * image[:,:,0] + 0.587 * image[:,:,1] + 0.299 * image[:,:,2]
            new_batch.append(frame)

        return new_batch

    def genearte_hog(self,image_batch):
        '''
        Hook to generate hog, written to handle batches
        '''

        batch = []
        
        for image in image_batch:
            descriptor = self.hog_generator.generate_hog_features(image,(8,8),2)
            batch.append(descriptor)
        
        return batch

    def randomize_input_batch(self,batch1,batch2):
        '''
        Randomize dataset for training
        '''
        import random
        batch = []
        label = []
        for _ in range(len(batch1 + batch2)):
            # randomly picking a positive or a negative sample
            r = random.randint(0,1)
            if (r == 1):
                try:
                    batch.append(batch1[0])
                    label.append(1)
                    batch1 = batch1[1:]
                except:
                    batch.append(batch2[0])
                    label.append(0)
                    batch2 = batch2[1:]
            if (r == 0):
                try:
                    batch.append(batch2[0])
                    label.append(0)
                    batch2 = batch2[1:]
                except:
                    batch.append(batch1[0])
                    label.append(1)
                    batch1 = batch1[1:]
        return batch,label



    def run(self):
        '''
        Main running function

        '''
        # loading image batch
        print("Loading image")
        train_positive = self.load_batch("./Train_positive/")
        train_negative = self.load_batch("./Train_negative/")
        test_positive = self.load_batch("./Test_positive/")
        test_negative = self.load_batch("./Test_negative/")
        print(len(test_positive))
        print("Image Batch Loaded")

        # Generating grascale images
        print("Converting to grayscale")
        train_positive_gray = self.grascale(train_positive)
        train_negative_gray = self.grascale(train_negative)
        test_positive_gray = self.grascale(test_positive)
        test_negative_gray = self.grascale(test_negative)
        print("Grayscale computed")

        print("Extracting HOG descriptor")
        # Extracting Hog features for training images 
        train_positive_descriptor = self.genearte_hog(train_positive_gray)
        train_negative_descriptor = self.genearte_hog(train_negative_gray)
        
        test_positive_descriptor = self.genearte_hog(test_positive_gray)
        test_negative_descriptor = self.genearte_hog(test_negative_gray)
        print("Extracted descriptor")
        # training a neural net
        for num_neuron in [250,500,1000]:
            nn = NeuralNetwork(1, [num_neuron], 0.05)
            
            X_, Y_ = self.randomize_input_batch(train_positive_descriptor,train_negative_descriptor)
            X_train = np.array(X_, dtype=np.float32)
            
            y_train = np.array(Y_, dtype=np.int32)
            nn.train(X_train,y_train)

            # testing the network
            x_, y_ = self.randomize_input_batch(test_positive_descriptor, test_negative_descriptor)
            X_test = np.array(test_positive_descriptor + test_negative_descriptor, dtype=np.float32)
            Y_test = np.array(len(test_positive_descriptor) * [1] + len(test_negative_descriptor) * [0], dtype=np.int32)
            y_pred = nn.predict(X_test)

            """## Results and final comment"""

            print(nn.accuracy(y_pred,Y_test))
            print("->>>>>>>>>>>>>>>>>>>")
            print(y_pred)
            print("-..................")
            print(Y_test)
 def __init__(self):
     from hog import HOG
     self.hog_generator = HOG()
Ejemplo n.º 12
0
import dataset
import argparse
import mahotas
import cv2

ap = argparse.ArgumentParser()
ap.add_argument('-m','--model', required=True,
	help='path to where the model is stored')

ap.add_argument('-i','--image', required=True, 
	help = 'path to images')
args = vars(ap.parse_args())

model = joblib.load(args['model'])

hog = HOG(orientations = 18, pixelsPerCell=(10,10),
	cellsPerBlock=(1,1), normalize=True)

image = cv2.imread(args['image'])
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

blurred = cv2.GaussianBlur(gray, (5,5), 0)
edged = cv2.Canny(blurred, 30, 150)
(_, cnts, _) = cv2.findContours(edged.copy(), cv2.RETR_EXTERNAL,
	cv2.CHAIN_APPROX_SIMPLE)

cnts = sorted([(c, cv2.boundingRect(c)[0]) for c in cnts],
	key = lambda x: x[1])

for (c, _) in cnts:
	(x, y, w, h) = cv2.boundingRect(c)
Ejemplo n.º 13
0
from __future__ import print_function
import joblib
from hog import HOG
import argparse
import mahotas
import cv2
ap = argparse.ArgumentParser()
ap.add_argument("-m",
                "--model",
                required=True,
                help="path to where model will be stored")
ap.add_argument("-i", "--image", required=True, help="path to the image file")
args = vars(ap.parse_args())
model = joblib.load(args["model"])
hog = HOG(orientations=18,
          pixelsPerCell=(10, 10),
          cellsPerBlock=(1, 1),
          transform=True)
image = cv2.imread(args["image"])
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
blurred = cv2.GaussianBlur(gray, (5, 5), 0)
edged = cv2.Canny(blurred, 30, 150)
cnts, _ = cv2.findContours(edged.copy(), cv2.RETR_EXTERNAL,
                           cv2.CHAIN_APPROX_SIMPLE)
cnts = sorted([(c, cv2.boundingRect(c)[0]) for c in cnts], key=lambda x: x[1])

for (c, _) in cnts:
    (x, y, w, h) = cv2.boundingRect(c)
    if w >= 7 and h >= 20:
        roi = gray[y:y + h, x:x + w]
        thresh = roi.copy()
        T = mahotas.thresholding.otsu(roi)
Ejemplo n.º 14
0
ap = argparse.ArgumentParser()
ap.add_argument("-m",
                "--model",
                required=True,
                help="path to where the model will be stored")
ap.add_argument("-i", "--image", required=True, help="path to the image file")
ap.add_argument("-f", "--face", required=True, help="path to face cascade")
args = vars(ap.parse_args())

# load the model
model = open(args["model"]).read()
model = cPickle.loads(model)

# initialize the HOG descriptor
hog = HOG(orientations=40,
          pixelsPerCell=(10, 10),
          cellsPerBlock=(1, 1),
          normalize=True)

# load the image and convert it to grayscale
image = cv2.imread(args["image"])
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
fd = Facedetector(args["face"])
faces = fd.detect(gray, scaleFactor=1.2, minNeighbors=3, minSize=(30, 30))
#cv2.imshow('img',gray)
if (len(faces) == 0):
    image = cv2.resize(image, (47, 62), interpolation=cv2.INTER_AREA)
else:
    (x, y, w, h) = faces[0]
    roi_color = image[y:y + h, x:x + w]
    image = cv2.resize(roi_color, (47, 62), interpolation=cv2.INTER_AREA)
Ejemplo n.º 15
0
ap.add_argument("-m",
                "--model",
                default="svm.pickle",
                help="path to where the model will be stored")
args = vars(ap.parse_args())
print("Collecting annotations ...")

#CHANGE 'inflammed aorta' to the disease which you are working to diagnose
d = Dataset(myDirectory, myDirectory, ['inflamed aorta'])
labels, images = d.load_data()
print("Gathered {} image slices".format(len(images)))
data = []
labels_new = []

hog = HOG(orientations=19,
          pixelsPerCell=(8, 8),
          cellsPerBlock=(3, 3),
          transform=True)

for i, image in enumerate(images):
    if i % 100 == 0:
        print("Gathering features, {} of {}".format(i, len(images)))
    if 0 not in image.shape:
        image_resized = resize(image, (291, 218), anti_aliasing=True)
        hist = hog.describe(rgb2gray(image_resized))
        data.append(hist)
        labels_new.append(labels[i])

X_train, X_test, y_train, y_test = train_test_split(data,
                                                    labels_new,
                                                    random_state=0)
print("Training on {} images".format(len(X_train)))
Ejemplo n.º 16
0
import pickle
import dataset
import argparse
from hog import HOG
from sklearn.svm import LinearSVC

ap = argparse.ArgumentParser()
ap.add_argument("-t", "--train", required=True, help="train.csv path")
ap.add_argument("-m",
                "--model",
                required=True,
                help="path of where model will be saved")
args = vars(ap.parse_args())

digits, labels = dataset.load_digits(args["train"])
hog = HOG(orientations=18,
          pixels_per_cell=(6, 6),
          cells_per_block=(1, 1),
          transform=True)

data = []
for digit in digits:
    hist = hog.describe(digit)
    data.append(hist)

model = LinearSVC()
model.fit(data, labels)
pickle.dump(model, open(args["model"], 'wb'))
Ejemplo n.º 17
0
negative = Controller().load_batch("./Test_negative/")
print(len(positive))
positive = Controller().grascale(positive)
negative = Controller().grascale(negative)
print(len(positive))
i = 0
for image in positive:
    i += 1
    grad_x, grad_y = Canny().gradient_generation(image)
    egde_mag = Canny().edge_magnitude(grad_x, grad_y)
    cv2.imwrite("./results/Postive" + str(i) + ".jpg", egde_mag)

i = 0
for image in negative:
    i += 1
    grad_x, grad_y = Canny().gradient_generation(image)
    egde_mag = Canny().edge_magnitude(grad_x, grad_y)
    cv2.imwrite("./results/Negative" + str(i) + ".jpg", egde_mag)

image = cv2.imread("./Train_positive/crop001278a.bmp")
image = 0.114 * image[:, :, 0] + 0.587 * image[:, :, 1] + 0.299 * image[:, :,
                                                                        2]
descriptor = HOG().generate_hog_features(image, (8, 8), 2)
np.savetxt("crop001278a.csv", descriptor.flatten(), delimiter="\n")

image = cv2.imread("./Test_positive/crop001045b.bmp")
image = 0.114 * image[:, :, 0] + 0.587 * image[:, :, 1] + 0.299 * image[:, :,
                                                                        2]
descriptor = HOG().generate_hog_features(image, (8, 8), 2)
np.savetxt("crop001045b.csv", descriptor.flatten(), delimiter="\n")
Ejemplo n.º 18
0
from sklearn.svm import LinearSVC
from hog import HOG
import dataset
import argparse

ap = argparse.ArgumentParser()
ap.add_argument('-d', '--dataset', required=True,
	help = 'Path to the dataset file')
ap.add_argument('-m', '--model', required = True,
	help='path to where the model will be stored')
args = vars(ap.parse_args())

(digits, target) = dataset.load_digits(args['dataset'])
data = []

hog = HOG(orientations = 18, pixelsPerCell=(10, 10),
 cellsPerBlock=(1,1), normalize=True)

for image in digits:
	image = dataset.deskew(image, 20)
	image = dataset.center_extent(image, (20, 20))

	hist = hog.describe(image)
	data.append(hist)

model = LinearSVC(random_state = 42)
model.fit(data, target)

joblib.dump(model, args['model'])
# print 'I don\'t give a shit'

Ejemplo n.º 19
0
def main():

    #-model=./modelo -image=./imagen.jpg

    #Para pasarle los argumentos
    argumento = argparse.ArgumentParser()

    argumento.add_argument('-model',
                           '--model',
                           required=True,
                           help="path where the training model is saved")
    argumento.add_argument(
        "-image",
        "--image",
        required=True,
        help="path where the image, which is going to be clasified, is saved")

    #Se le asigna el primer argumento a la ruta del modelo de entrenamiento
    args = vars(argumento.parse_args())
    model_path = args['model']

    #Se le asigna el segundo argumento ala ruta de la imagen que se va a clasificar
    image_path = args['image']

    #Se carga el entrenamiento
    model = cPickle.load(file(model_path))

    #Se carga la imagen
    image = cv2.imread(image_path)

    #escalo la imagen porque me sale muy grande
    h, w = image.shape[:2]
    image = cv2.resize(image, (w / 4, h / 4), interpolation=cv2.INTER_AREA)

    #A escala de grises
    gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

    #Filtro gausiano
    blur = cv2.GaussianBlur(gray_image, (5, 5), 0)

    #Bordes canny
    edges = cv2.Canny(blur, 150, 100)

    #Extraer contornos
    ima, ctrs, hier = cv2.findContours(edges, cv2.CHAIN_APPROX_SIMPLE,
                                       cv2.RETR_TREE)

    #Obtenemos los recangulos de cada contorno
    rects = [cv2.boundingRect(ctr) for ctr in ctrs]

    #Definimos los parametros de la Transformada de hog
    hog = HOG(orientations=18,
              pixelsPerCell=(10, 10),
              cellsPerBlock=(1, 1),
              normalize=True)

    for rect in rects:
        #Draw the rectangles
        cv2.rectangle(image, (rect[0], rect[1]),
                      (rect[0] + rect[2], rect[1] + rect[3]), (0, 255, 0), 3)

        # Make the rectangular region around the digit
        leng = int(rect[3] * 1.6)
        pt1 = int(rect[1] + rect[3] // 2 - leng // 2)
        pt2 = int(rect[0] + rect[2] // 2 - leng // 2)

        #Se recorta la region
        crop_region = gray_image[pt1:pt1 + leng, pt2:pt2 + leng]

        #Umbralizado de otsu e invertimos la imaagen
        ret, threshold = cv2.threshold(crop_region, 177, 255,
                                       cv2.THRESH_OTSU + cv2.THRESH_BINARY_INV)

        threshold = dataset.deskew(threshold, 20)
        threshold = dataset.center_extent(threshold, (20, 20))

        #Se obtiene el digito a partir del modelo de gradiente orientado y del modelo de entramiento
        hist = hog.describe(threshold)
        digit = model.predict(hist)[0]

        #Se dibuja el digito correspondiente
        cv2.putText(image, str(int(digit)), (rect[0], rect[1]),
                    cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 0, 0), 2)

    cv2.imshow('ventana', image)
    cv2.waitKey()
Ejemplo n.º 20
0
from __future__ import print_function
from sklearn.externals import joblib
from hog import HOG
import dataset
import argparse
import mahotas
import cv2

ap=argparse.ArgumentParser()
ap.add_argument('-m','--model',required=True,help='Path to where the model will be stored')
ap.add_argument('-i','--image',required=True,help='Path to the image file')
args=vars(ap.parse_args())

model=joblib.load(args['model'])

hog=HOG(orientations=18,pixelsPerCell=(10,10),cellsPerBlock=(1,1),transform=True)

image=cv2.imread(args['image'])
gray=cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)

blurred=cv2.GaussianBlur(gray,(5,5),0)
edged=cv2.Canny(blurred,30,150)
(_,cnts,_)=cv2.findContours(edged.copy(),cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
cnts=sorted([(c,cv2.boundingRect(c)[0]) for c in cnts], key = lambda x: x[1])

for (c,_) in cnts:
    (x,y,w,h)=cv2.boundingRect(c)

    if w>=7 and h>=20:
        roi=gray[y:y+h,x:x+w]
        thresh=roi.copy()
Ejemplo n.º 21
0
                required=True,
                help="Path to the configuration file")
ap.add_argument("-i",
                "--image",
                required=True,
                help="Path to the image to be classified")
args = vars(ap.parse_args())

# load the configuration file
conf = Conf(args["conf"])

#load the classifier, then initialize the HOG descriptors
# and the object detector
model = cPickle.loads(open(conf["classifier_path"]).read())
hog = HOG(orientations=conf["orientations"],
          pixelsPerCell=tuple(conf["pixels_per_cell"]),
          cellsPerBlock=tuple(conf["cells_per_block"]),
          normalize=conf["normalize"])
od = ObjectDetector(model, hog)

# load the image and convert it to grayscale
image = cv2.imread(args["image"])
image = imutils.resize(image, width=min(128, image.shape[1]))
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
blur = cv2.GaussianBlur(gray, (5, 5), 0)
ret, thresh = cv2.threshold(blur, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (3, 3))
opening = cv2.morphologyEx(gray, cv2.MORPH_OPEN, kernel)
edges = cv2.Canny(opening, ret, ret * 0.95)

# detect objects in the image and apply non-maxima supression to the bounding boxes
(boxes, probs) = od.detect(edges,