Пример #1
0
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)
        thresh[thresh>T]=255
        thresh=cv2.bitwise_not(thresh)

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

        cv2.imshow('thresh',thresh)
        
        hist=hog.describe(thresh)
        digit=model.predict([hist])[0]
        print('I think that number is: {}'.format(digit))

        cv2.rectangle(image,(x,y),(x+w,y+h),(0,255,0),1)
        cv2.putText(image,str(digit),(x-10,y-10),cv2.FONT_HERSHEY_SIMPLEX,1.2,(0,255,0),2)
        cv2.imshow('image',image)
        cv2.waitKey(0)
Пример #2
0
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'


Пример #3
0
# -*- coding: utf-8 -*-
"""
Created on Tue Jun  4 18:04:45 2019

@author: David
"""

from sklearn.externals import joblib
from sklearn.svm import LinearSVC
from hog import HOG
import dataset

(digits, target) = dataset.load_digits("train.csv")
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)

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

joblib.dump(model, "svm.cpickle")
    # detect objects in the image
    (boxes, probs) = od.detect(gray,
                               conf["window_dim"],
                               winstep=conf["hn_window_step"],
                               pyramidscale=conf["hn_pyramid_scale"],
                               minprob=conf["hn_min_probability"])

    # loop over the bounding boxes
    for (prob, (startX, startY, endX, endY)) in zip(probs, boxes):
        # extract the ROI from the image, resize it to a known, canonical size, extract
        # HOG features from teh ROI, and finally update the data
        roi = cv2.resize(gray[startY:endY, startX:endX],
                         tuple(conf["window_dim"]),
                         interpolation=cv2.INTER_AREA)
        features = hog.describe(roi)
        data.append(np.hstack([[prob], features]))

    # update the progress bar
    pbar.update(i)

# sort the data points by confidence
pbar.finish()
print("[INFO] sorting by probability...")
data = np.array(data)
data = data[data[:, 0].argsort()[::-1]]

# dump the dataset to file
print("[INFO] dumping hard negatives to file...")
dataset_alphabets.dump_dataset(data[:, 1:], [-1] * len(data),
                               conf["features_path"],
Пример #5
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'))
Пример #6
0
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)))
print("Testing on  {} images".format(len(X_test)))

clf = LinearSVC()
clf.fit(X_train, y_train)
y_pred = clf.predict(X_test)

# Compute confusion matrix
cnf_matrix = confusion_matrix(y_test, y_pred)
Пример #7
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()
Пример #8
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'])
Пример #9
0
          pixels_per_cell=(6, 6),
          cells_per_block=(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)

contours, _ = cv2.findContours(edged.copy(), cv2.RETR_EXTERNAL,
                               cv2.CHAIN_APPROX_SIMPLE)
contours = sorted([(c, cv2.boundingRect(c)[0]) for c in contours],
                  key=lambda x: x[1])

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

    if w >= 4 and h >= 15:
        roi = gray[y:y + h, x:x + w]
        resized = cv2.resize(roi, (28, 28))
        hist = hog.describe(resized)
        digit = model.predict([hist])[0]

        cv2.rectangle(image, (x, y), (x + w, y + h), (0, 255, 0), 1)
        cv2.putText(image, str(digit), (x - 10, y - 10),
                    cv2.FONT_HERSHEY_SIMPLEX, 1.2, (0, 255, 0), 2)
        cv2.imshow("final", image)
        cv2.waitKey(0)

# with some image transformations on image during training and parameter tuning, result should be better
Пример #10
0
(contours, _) = cv2.findContours(edged.copy(), cv2.RETR_EXTERNAL,
                                 cv2.CHAIN_APPROX_SIMPLE)

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

for (c, _) in contours:
    (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)
        thresh[thresh > T] = 255
        thresh = cv2.bitwise_not(thresh)

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

        cv2.imshow('thresh', thresh)

        histogram = hog.describe(thresh)
        digit = model.predict([histogram])[0]
        print('I think that number is: {}'.format(digit))

        cv2.rectangle(image, (x, y), (x + w, y + h), (128, 0, 128), 1)
        cv2.putText(image, str(digit), (x - 10, y - 10),
                    cv2.FONT_HERSHEY_SIMPLEX, 1.2, (128, 0, 128), 2)
        cv2.imshow('image', image)
        cv2.waitKey(0)
Пример #11
0
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)
		thresh[thresh>T] = 255
		thresh = cv2.bitwise_not(thresh)

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

		cv2.imshow('thresh', thresh)

		hist = hog.describe(thresh)
		digit = model.predict(hist)[0]
		print('The number is {}'.format(digit))

		cv2.rectangle(image, (x,y), (x+w, y+h),
			(0, 255, 0), 1)
		cv2.putText(image, str(digit), (x-10, y-10), 
			cv2.FONT_HERSHEY_SIMPLEX, 1.2, (0, 255,0), 2)
		cv2.imshow('image', image)
		cv2.waitKey(0)




#loop over the training paths
for (i, trnpath) in enumerate(trnpaths):
    image = cv2.imread(trnpath)
    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    [x, y, w, h] = img_proc_roi(trnpath)
    if (x > 0 and y > 0 and w > 0 and h > 0):
        bb = [x, y, w, h]
        # print(trnpath)
        roi = crop_roi(gray, bb, 10, dstSize=(32, 32))
        # define the list of ROI's that will be described,based on wheather or not the horizontal flip of the image should
        # be used
        rois = (roi, cv2.flip(roi, 1)) if conf["use_flip"] else (roi, )
        #loop over the rois
        for roi in rois:
            # extract features from the ROI and update the list of features and labels
            features = hog.describe(roi)
            data.append(features)
            labels.append(1)

            # update the progress bar
            pbar.update(i)

# grab the distraction image paths and reset the progress bar
pbar.finish()

# grab the distraction image paths and reset the progress bar
dstPaths = list(paths.list_images(conf["image_distractions"]))
pbar = progressbar.ProgressBar(maxval=conf["num_distraction_images"],
                               widgets=widgets).start()
print("[INFO] describing distraction ROIs...")