# -*- 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")
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)
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'
import numpy as np import cv2 import dataset import imutils p = "/home/hrushikesh/book_opencv/Adrian Rosebrock - Practical Python and OpenCV, 3rd Edition + Case studies (2016)/Books/Case Studies, 3rd Edition/code/digits/data/digits.csv" data = np.genfromtxt(p, delimiter=',',dtype="uint8") target = data[:, 0] data = data[:, 1:].reshape(data.shape[0], 28, 28) print(data.shape) images = data[:30,:,:] print(images.shape) for i in range(10, 20): image = imutils.resize(images[i,:,:],width=200) # cv2.imshow("image{}".format(i), image) image_deskew = dataset.deskew(image,200) stack = np.hstack([image, image_deskew]) cv2.imshow("Image Deskewed",stack) cv2.waitKey(0) cv2.destroyAllWindows()
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()
''' Set up the argument parser which will get the CSV file and location where model is to be stored''' argparser = argparse.ArgumentParser() argparser.add_argument("-d", "--dataset", required = True, help = "path to the dataset file") argparser.add_argument("-m", "--model", required = True, help = "path to where the model will be stored") args = vars(argparser.parse_args()) (digits, labels) = dataset.load_data(args["dataset"]) hog = HOG(orientations = 18, pixelsPerCell = (10, 10), cellsPerBlock = (1, 1), normalise = True) data = [] # Add histogram for each digit in a list for digit in digits: digit = dataset.deskew(digit) hist = hog.describe(digit.reshape((28,28))) data.append(hist) # Set up and train the model SVC_model = LinearSVC() SVC_model.fit(data, labels) # Save the model to file joblib.dump(SVC_model, args["model"], compress = 3)
# Find all the bounding rectangles of the contours rects = [cv2.boundingRect(contour) for contour in contours] # Loop over all the rectangles for rect in rects: (x, y, w, h) = rect cv2.rectangle(image, (x, y), (x + w, y + h), (0, 255, 0), 2) # Find the region of interest, and resize it roi = gray[y: y + h, x: x + w] roi = cv2.resize(roi, (28, 28), interpolation = cv2.INTER_AREA) # Threshold the region of interest thresh = roi.copy() _, thresh = cv2.threshold(thresh, 90, 255, cv2.THRESH_BINARY_INV) thresh = dataset.deskew(thresh) # Get the histogram and predict the number hist = hog.describe(thresh) predict = SVC_model.predict(np.array([hist], 'float64')) cv2.putText(image, str(predict), (x-10, y-10), cv2.FONT_HERSHEY_SIMPLEX, 1.2, (0, 255, 0) , 2) # Display the result cv2.imshow("Image", image) cv2.waitKey(0)