def load_data(path):
    print("[INFO] loading images...")
    data = []
    labels = []
    # grab the image paths and randomly shuffle them
    imagePaths = sorted(list(paths.list_images(path)))
    random.seed(42)
    random.shuffle(imagePaths)
    # loop over the input images
    for imagePath in imagePaths:
        # load the image, pre-process it, and store it in the data list
        image = cv2.imread(imagePath)
        image = cv2.resize(image, (norm_size, norm_size))
        image = img_to_array(image)
        data.append(image)

        # extract the class label from the image path and update the
        # labels list
        label = int(imagePath.split(os.path.sep)[-2])       
        labels.append(label)
    
    # scale the raw pixel intensities to the range [0, 1]
    data = np.array(data, dtype="float") / 255.0
    labels = np.array(labels)

    # convert the labels from integers to vectors
    labels = to_categorical(labels, num_classes=CLASS_NUM)                         
    return data,labels
Ejemplo n.º 2
0
def extract():

    imagepaths = sorted(paths.list_images('training_data/'))
    labels = []
    data = []

    for imagepath in imagepaths:
        label = imagepath[imagepath.rfind('/') + 1:].split('-')[0]
        image = cv2.imread(imagepath)

        features = describe_haralick_stats(image)
        labels.append(label)
        data.append(features)

    datafile = "models/features.pkl"
    td_file = open(datafile, 'wb')
    pickle.dump(data, td_file)

    td_file.close()

    labelfile = "models/labels.pkl"
    ld_file = open(labelfile, 'wb')
    pickle.dump(labels, ld_file)

    ld_file.close()

    return
Ejemplo n.º 3
0
def get_training_images(training_dir):
    training_images = dict()
    for image_path in paths.list_images(training_dir):
                leaf_sort = image_path.split("/")[-2]
                if leaf_sort in training_images:
                    training_images[leaf_sort].append(image_path)
                else:
                    training_images[leaf_sort] = [image_path]
    return training_images
Ejemplo n.º 4
0
def process_dir(dataset_path, output):
    files = paths.list_images(dataset_path)

    # for fname in files:
    f = open(output, 'a+')
    total = 0
    # f.write("{}\n".format(encodings.base64_encode_image(face)))
    total += 1

    print("[INFO] wrote {} frames to file".format(total))
    f.close()
Ejemplo n.º 5
0
def get_train_data(path,img_cols,img_rows,num_imgs=-1):
	print path
	i=1
	x_img=[]
	y_labels=[]
	if num_imgs!="-1":
		for imagePath in paths.list_images(path):
			i+=1
			print(imagePath,str(i))
			img=cv2.imread(imagePath)

			imagePath=imagePath.split("/")
			y_labels.append(int(imagePath[1].replace("c","")))#once we split, lists are like this
										#[train,c0,img3012341.jpg]
										#we want only integers for CNN model
			res = cv2.resize(img, (img_cols, img_rows))
			res=res.transpose(2,0,1)#Size is now 3x32x32
			x_img.append(res)
			if i==num_imgs:
				return x_img,y_labels
			print i


	else:
		print("HOLAAA")
		for imagePath in paths.list_images(path):
			i+=1
			print(imagePath,str(i))
			img=cv2.imread(imagePath)

			imagePath=imagePath.split("/")
			y_labels.append(int(imagePath[1].replace("c","")))#once we split, lists are like this
										#[train,c0,img3012341.jpg]
										#we want only integers for CNN model
			res = cv2.resize(img, (img_cols, img_rows))
			res=res.transpose(2,0,1)#Size is now 3x32x32
			x_img.append(res)

		return x_img,y_labels
Ejemplo n.º 6
0
def create(path):
    images = list(list_images(path))
    outfile = '/tmp/out.avi'
    fourcc = cv2.VideoWriter_fourcc(*'XVID')
    out = None
    fps = 3
    print(len(images))
    for img in sorted(images):
        image = cv2.imread(img)
        if out is None:
            h, w = image.shape[:2]
            print(h, w)
            out = cv2.VideoWriter(outfile, fourcc, fps, (w, h))

        out.write(image)
Ejemplo n.º 7
0
def create_gif(inputPath, outputPath, delay, finalDelay, loop):
    # grab all image paths in the input directory
    imagePaths = sorted(list(paths.list_images(inputPath)))

    # remove the last image path in the list
    lastPath = imagePaths[-1]
    imagePaths = imagePaths[:-1]

    # construct the image magick 'convert' command that will be used
    # generate our output GIF, giving a larger delay to the final
    # frame (if so desired)
    cmd = "convert -delay {} {} -delay {} {} -loop {} {}".format(
        delay, " ".join(imagePaths), finalDelay, lastPath, loop,
        outputPath)
    os.system(cmd)
Ejemplo n.º 8
0
def read_repre(repre_path):
    # f = open(repre_path, 'r')
    # repres = dict()
    # for l in f:
    #     elems = l.strip().split(' ')
    #     repres[elems[0]] = cv2.imread(elems[1])
    # f.close()
    # return repres
    files = paths.list_images(repre_path)
    repres = {}
    for f in files:
        name = f.split('/')[-1]
        name = name[:name.rfind('.')]
        img = cv2.imread(f)
        repres[name] = img
    return repres
Ejemplo n.º 9
0
def hyperparameters_tuning(img):
    scaleFactor_list = [1.05, 1.1, 1.2]
    minNeighbors_list = [5, 8, 11]
    minSize_list = [60, 80, 100]

    it = len(scaleFactor_list) * len(minNeighbors_list) * len(minSize_list)
    curr_it = 0
    res_file = open('/home/tomas/Dropbox/Data/celeb_dataset/hyperparameters_tuning.txt' 'w')
    for sf in scaleFactor_list:
        for mn in minNeighbors_list:
            for ms in minSize_list:
                missed = 0
                extra = 0
                ok = 0
                n_files = 0
                curr_it += 1
                files = paths.list_images(dataset_path)
                # for fname in tqdm(files):
                for fname in files:
                    n_files += 1
                    image = cv2.imread(fname)
                    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

                    # gray = imutils.resize(gray, width=width)

                    im_face, n_faces, bbox,  = detect_face(gray, fname, scaleFactor=sf, minNeighbors=mn, minSize=ms,
                                                           save_face=save_face)

                    if n_faces == 0:
                        missed += 1
                    elif n_faces > 1:
                        extra += n_faces - 1
                    else:
                        ok += 1

                # print 'total # of images: ', n_files
                print 'it #%i/%i  -----------------------------------------------' % (curr_it, it)
                print 'params: sf=%.2f, mn=%i, ms=%i' % (sf, mn, ms)
                print 'ok: %i/%i, missed: %i, extra: %i' % (ok, n_files, missed, extra)
                print '\n'

                res_file.write('it #%i/%i  -----------------------------------------------' % (curr_it, it))
                res_file.write('params: sf=%.2f, mn=%i, ms=%i' % (sf, mn, ms))
                res_file.write('ok: %i/%i, missed: %i, extra: %i' % (ok, n_files, missed, extra))
                res_file.write('\n')
Ejemplo n.º 10
0
def create_base64_files(dataset_path, base64_path, face_cascade):
    if not os.path.exists(base64_path):
        os.mkdir(base64_path)
    fd = FaceDetector(face_cascade)

    print("[INFO] Extracting faces from dataset...")
    files = paths.list_images(dataset_path)
    data_dict = {}
    for f in files:
        # name = f.split('/')[-2][:f.split('/')[-1].rfind('.')]
        # label = name[:name.rfind('_')]
        label = f.split('/')[-2]

        img = cv2.imread(f, 0)
        img = imutils.resize(img, width=500)

        # faceRects = fd.detect(img, scaleFactor=1.1, minNeighbors=9, minSize=(100, 100))
        faceRects = fd.detect(img, scaleFactor=1.1, minNeighbors=9, minSize=(40, 40))
        if len(faceRects) > 0:
            (x, y, w, h) = max(faceRects, key=lambda b: (b[2] * b[3]))
            face = img[y:y + h, x:x + w].copy(order="C")
        else:
            # cv2.imshow('No face detected', img)
            # cv2.waitKey(0)
            # raise ValueError('No face deteted.')
            continue

        if data_dict.has_key(label):
            data_dict[label].append(face)
        else:
            data_dict[label] = list((face,))

    # for fname in files:
    for k, v in data_dict.items():
        filename = k + '.txt'
        filepath = os.path.join(base64_path, filename)
        f = open(filepath, 'a+')
        total = 0
        for face in v:
            f.write("{}\n".format(encodings.base64_encode_image(face)))
            total += 1

        print("[INFO] wrote {} frames to file {}".format(total, filename))
        f.close()
Ejemplo n.º 11
0
def detect_batch(dataset_path, save_face=False):
    scaleFactor = 1.1
    minNeighbors = 8
    minSize = 60

    missed = 0
    extra = 0
    ok = 0
    n_files = 0
    files = paths.list_images(dataset_path)
    faces = []
    names = []

    # files = ['/home/tomas/Dropbox/Data/celeb_dataset/natalie_portman/natalie_portman_07.jpg', ]
    for fname in files:
        n_files += 1
        image = cv2.imread(fname)
        gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

        im_face, n_faces, bbox = detect_face(gray, fname, scaleFactor=scaleFactor, minNeighbors=minNeighbors,
                                             minSize=minSize, save_face=save_face)
        if n_faces == 0:
            missed += 1
            print 'missed:', fname.split('/')[-1]
        elif n_faces > 1:
            extra += n_faces - 1
            print 'extra:', fname.split('/')[-1]
        else:
            ok += 1
            # imgs.append(im_face)
            bbox = bbox[0]
            # bboxes.append(bbox)
            face = gray[bbox[1]:bbox[1] + bbox[3], bbox[0]:bbox[0] + bbox[2]]
            faces.append(face)
            name = fname.split('/')[-1].split('.')[0]
            names.append(name)


    # print 'total # of images: ', n_files
    print 'ok: %i/%i, missed: %i, extra: %i' % (ok, n_files, missed, extra)
    return faces, names
Ejemplo n.º 12
0
def load_images(if_grayscale=False, if_normalize_images=IF_NORMALIZE_IMAGES):
    print('Loading image data...')
    images = []
    image_labels = []

    # For every class, for every image in its images dataset, accumulate into list of features & labels
    for (i, clazz) in enumerate(CLASSES):
        image_paths = list(paths.list_images(clazz.get('image_dir_path')))
        image_paths = image_paths[0:IMAGE_COUNT_PER_CLASS] if IMAGE_COUNT_PER_CLASS else image_paths
        for image_path in image_paths:
            image = cv2.imread(
                image_path,
                cv2.IMREAD_GRAYSCALE if if_grayscale else cv2.IMREAD_COLOR,
            )
            if if_normalize_images:
                image = cv2.normalize(image, None, 0, 255, cv2.NORM_MINMAX)

            images.append(image)
            image_labels.append(i)

    return (images, image_labels)
Ejemplo n.º 13
0
def extract(conf):
    # grab the set of ground-truth images and select a percentage of them for training
    trnPaths = list(paths.list_images(conf["pos_raw_ph"]))
    assert len(trnPaths)!=0
    #
    if not os.path.isdir(conf['pos_feat_ph']):
        os.makedirs(conf['pos_feat_ph'])

    # setup the progress bar
    widgets = ["Extracting: ", progressbar.Percentage(), " ", progressbar.Bar(), " ", progressbar.ETA()]
    pbar = progressbar.ProgressBar(maxval=len(trnPaths), widgets=widgets).start()
    for (i, imgPath) in enumerate(trnPaths):
        # img = imread(imgPath)
        # resize to the training resolution (wid,height)
        img = auto_resized(imread(imgPath, as_grey=True),conf['sliding_size'])
        fd=hog(img, conf['orientations'], conf['pixels_per_cell'], 
                    conf['cells_per_block'], conf['visualize'], conf['normalize'])
        fd_name = os.path.split(imgPath)[1].split(".")[0] + ".feat"
        fd_path = os.path.join(conf['pos_feat_ph'], fd_name)
        joblib.dump(fd, fd_path)
        pbar.update(i)
    pbar.finish()
    print '[*] Finished Pos.'
Ejemplo n.º 14
0
def data2files(data_dir, face_cascade, res_file_path, label_dict_path):
    # initializing face detector
    fd = FaceDetector(face_cascade)

    dataset = []
    files = paths.list_images(data_dir)
    file_dict = process_files(files)

    for key in file_dict.keys():
        print 'Processing %s ...' % key,
        celeb_files = file_dict[key]
        n_files = len(celeb_files)
        ok = 0
        for f in celeb_files:
            item = create_data_item(f, fd)
            if item is not None:
                dataset.append(item)
                ok += 1
        print 'done, face found: %i/%i (%i%%)' % (ok, n_files, int(100 * ok / n_files))

    print 'Saving data in pickle format...',
    # data_fname = os.path.join(res_dir, 'data.npz')
    data_file = gzip.open(res_file_path, 'wb', compresslevel=1)
    pickle.dump(dataset, data_file)
    # np.save(data_file, np.array(data_cubes_resh))
    data_file.close()
    print 'done'

    print 'Saving label dictionary ...'
    label_dict = {}
    names = file_dict.keys()
    for i, n in enumerate(names):
        label_dict[n] = i
    labels_file = gzip.open(label_dict_path, 'wb', compresslevel=1)
    pickle.dump(label_dict, labels_file)
    labels_file.close()
    print 'done'
Ejemplo n.º 15
0
ap.add_argument("-d", "--dataset", required=True, help="path to input dataset")
#ap.add_argument("-a", "--augment", type=int, default=-1,
#	help="whether or not 'on the fly' data augmentation should be used")
ap.add_argument("-p",
                "--plot",
                type=str,
                default="plot.png",
                help="path to output loss/accuracy plot")
args = vars(ap.parse_args())
# # Using Resnet50 and initialised with weights of imagenet
# ## images in smear 2005 are resized to 224 x224

# grab the list of images in our dataset directory, then initialize
# the list of data (i.e., images) and class images
print("[INFO] loading images...")
imagePaths = list(paths.list_images(args["dataset"]))
data = []
labels = []

# loop over the image paths
for imagePath in imagePaths:
    # extract the class label from the filename, load the image, and
    # resize it to be a fixed 64x64 pixels, ignoring aspect ratio
    label = imagePath.split(os.path.sep)[-2]
    image = cv2.imread(imagePath, 1)
    image = cv2.resize(image, (224, 224))

    # update the data and labels lists, respectively
    data.append(image)
    labels.append(label)
Ejemplo n.º 16
0
                "--images",
                required=True,
                help="path to input directory of images")
ap.add_argument(
    "-t",
    "--threshold",
    type=float,
    default=100.0,
    help="focus measures that fall below this value will be considered 'blurry'"
)
args = vars(ap.parse_args())

outputfolder_path = "/content/drive/MyDrive/checking-blur/sharpimages/"
i = 0
# loop over the input images
for imagePath in paths.list_images(args["images"]):
    # load the image, convert it to grayscale, and compute the
    # focus measure of the image using the Variance of Laplacian
    # method
    name = path.splitext(os.path.basename(imagePath))[0]
    image = cv2.imread(imagePath)
    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    fm = variance_of_laplacian(gray)
    text = "Not Blurry"
    # if the focus measure is less than the supplied threshold,
    # then the image should be considered "blurry"
    if fm < args["threshold"]:
        text = "Blurry"
    # show the image
    cv2.putText(image, "{}: {:.2f}".format(text, fm), (10, 30),
                cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0, 0, 255), 3)
Ejemplo n.º 17
0
# USAGE
# python build_dogs_vs_cats.py
from config import dogs_and_cats_config as config
from sklearn.preprocessing import LabelEncoder
from sklearn.model_selection import train_test_split
from pyimagesearch.preprocessing import AspectAwarePreprocessor
from pyimagesearch.io import HDF5DatasetWriter
from imutils import paths
import numpy as np
import progressbar
import json
import cv2
import os

# grab the paths to the images
train_paths = list(paths.list_images(config.IMAGES_PATH))
train_labels = [p.split(os.path.sep)[-1].split('.')[0] for p in train_paths]
le = LabelEncoder()
train_labels = le.fit_transform(train_labels)

# perform stratified sampling from the training set to build the testing split from the training data
train_paths, test_paths, train_labels, test_labels = train_test_split(
    train_paths,
    train_labels,
    test_size=config.NUM_TEST_IMAGES,
    stratify=train_labels,
    random_state=42)
train_paths, val_paths, train_labels, val_labels = train_test_split(
    train_paths,
    train_labels,
    test_size=config.NUM_VAL_IMAGES,
TARGET_IMAGE_WIDTH = 28
TARGET_IMAGE_HEIGHT = 28
LR = 0.001  # 学习率
BATCH_SIZE = 64
EPOCHS = 40

################################################
# 第一部分:数据预处理

# initialize the image preprocessor and datasetloader
sp = SimplePreprocessor(TARGET_IMAGE_WIDTH, TARGET_IMAGE_HEIGHT)
sdl = SimpleDatasetLoader(preprocessors=[sp])

# Load images
print("[INFO] 导入图像...")
image_paths = list(paths.list_images(dataset_path))  # path includedd
print(paths.list_images(dataset_path))
(X, y) = sdl.load(image_paths, verbose=500, grayscale=True)

# Flatten (reshape the data matrix)
# convert from (13164,TARGET_IMAGE_WIDTH,TARGET_IMAGE_HEIGHT)
#into (13164,TARGET_IMAGE_WIDTH*TARGET_IMAGE_HEIGHT)
X = X.reshape((X.shape[0], TARGET_IMAGE_WIDTH * TARGET_IMAGE_HEIGHT))
X = X.astype("float") / 255.0  # 特征缩放,是非常重要的步骤

# Show some information on memory consumption of the images
print("[INFO] features matrix: {:.1f}MB".format(X.nbytes / (1024 * 1024.0)))

# Label encoder
le = LabelEncoder()
y = to_categorical(le.fit_transform(y), 2)
Ejemplo n.º 19
0
from tensorflow.keras.models import load_model
import config
from imutils import paths
import numpy as np
import imutils
import random
import cv2
import os

# load the trained model from disk
print("[INFO] loading model...")
model = load_model(config.MODEL_PATH)

# grab the paths to the fire and non-fire images, respectively
print("[INFO] predicting...")
firePaths = list(paths.list_images(config.FIRE_PATH))
nonFirePaths = list(paths.list_images(config.NON_FIRE_PATH))

# combine the two image path lists, randomly shuffle them, and sample
# them
imagePaths = firePaths + nonFirePaths
random.shuffle(imagePaths)
imagePaths = imagePaths[:config.SAMPLE_SIZE]

# loop over the sampled image paths
for (i, imagePath) in enumerate(imagePaths):
    # load the image and clone it
    image = cv2.imread(imagePath)
    output = image.copy()

    # resize the input image to be a fixed 128x128 pixels, ignoring
Ejemplo n.º 20
0
def predictImages(modelArg, labelsArg, imagePathArg, num_classesArg,
                  min_confidenceArg, image_displayArg, pred_stagesArg):

    # initialize the model
    model = tf.Graph()

    # create a context manager that makes this model the default one for
    # execution
    with model.as_default():
        # initialize the graph definition
        graphDef = tf.GraphDef()

        # load the graph from disk
        with tf.gfile.GFile(modelArg, "rb") as f:
            serializedGraph = f.read()
            graphDef.ParseFromString(serializedGraph)
            tf.import_graph_def(graphDef, name="")

    # load the class labels from disk
    labelMap = label_map_util.load_labelmap(labelsArg)
    categories = label_map_util.convert_label_map_to_categories(
        labelMap, max_num_classes=num_classesArg, use_display_name=True)
    categoryIdx = label_map_util.create_category_index(categories)

    # create a plateFinder
    plateFinder = PlateFinder(min_confidenceArg,
                              categoryIdx,
                              rejectPlates=False,
                              charIOUMax=0.3)

    # create plate displayer
    plateDisplay = PlateDisplay()

    # create a session to perform inference
    with model.as_default():
        with tf.Session(graph=model) as sess:
            # create a predicter, used to predict plates and chars
            predicter = Predicter(model, sess, categoryIdx)

            imagePaths = paths.list_images(imagePathArg)
            frameCnt = 0
            start_time = time.time()
            platesReturn = []
            numPlates = 0
            # Loop over all the images
            for imagePath in imagePaths:
                frameCnt += 1

                # load the image from disk
                print("[INFO] Loading image \"{}\"".format(imagePath))
                image = cv2.imread(imagePath)
                (H, W) = image.shape[:2]

                # If prediction stages == 2, then perform prediction on full image, find the plates, crop the plates from the image,
                # and then perform prediction on the plate images
                if pred_stagesArg == 2:
                    # Perform inference on the full image, and then select only the plate boxes
                    boxes, scores, labels = predicter.predictPlates(
                        image, preprocess=True)
                    licensePlateFound_pred, plateBoxes_pred, plateScores_pred = plateFinder.findPlatesOnly(
                        boxes, scores, labels)
                    # loop over the plate boxes, find the chars inside the plate boxes,
                    # and then scrub the chars with 'processPlates', resulting in a list of final plateBoxes, char texts, char boxes, char scores and complete plate scores
                    plates = []
                    for plateBox in plateBoxes_pred:
                        boxes, scores, labels = predicter.predictChars(
                            image, plateBox)
                        chars = plateFinder.findCharsOnly(
                            boxes, scores, labels, plateBox, image.shape[0],
                            image.shape[1])
                        if len(chars) > 0:
                            plates.append(chars)
                        else:
                            plates.append(None)
                    plateBoxes_pred, charTexts_pred, charBoxes_pred, charScores_pred, plateAverageScores_pred = plateFinder.processPlates(
                        plates, plateBoxes_pred, plateScores_pred)

                # If prediction stages == 1, then predict the plates and characters in one pass
                elif pred_stagesArg == 1:
                    # Perform inference on the full image, and then find the plate text associated with each plate
                    boxes, scores, labels = predicter.predictPlates(
                        image, preprocess=False)
                    licensePlateFound_pred, plateBoxes_pred, charTexts_pred, charBoxes_pred, charScores_pred, plateScores_pred = plateFinder.findPlates(
                        boxes, scores, labels)
                else:
                    print(
                        "[ERROR] --pred_stages {}. The number of prediction stages must be either 1 or 2"
                        .format(pred_stagesArg))
                    quit()

                # Print plate text
                for charText in charTexts_pred:
                    print("    Found: ", charText)

                # Display the full image with predicted plates and chars
                if image_displayArg == True:
                    imageLabelled = plateDisplay.labelImage(
                        image, plateBoxes_pred, charBoxes_pred, charTexts_pred)
                    cv2.imshow("Labelled Image", imageLabelled)
                    cv2.waitKey(0)

                imageResults = []
                for i, plateBox in enumerate(plateBoxes_pred):
                    imageResults.append({
                        'plateText':
                        charTexts_pred[i],
                        'plateBoxLoc':
                        list(plateBox),
                        'charBoxLocs':
                        list([list(x) for x in charBoxes_pred[i]])
                    })
                    numPlates += 1

                platesReturn.append({
                    'imagePath': imagePath.split("/")[-1],
                    'imageResults': imageResults
                })

            # print some performance statistics
            curTime = time.time()
            processingTime = curTime - start_time
            fps = frameCnt / processingTime
            print(
                "[INFO] Processed {} frames in {:.2f} seconds. Frame rate: {:.2f} Hz"
                .format(frameCnt, processingTime, fps))

            #results = results.encode('utf-8')
            return {
                "processingTime": processingTime,
                "numPlates": numPlates,
                "numImages": len(platesReturn),
                "images": platesReturn
            }
Ejemplo n.º 21
0
def train_network(num_epochs=30, LETTER_IMAGES_FOLDER="images"):
    """
    Trains a convolutional neural network to our training set and outputs the 
    model to a local file to be read and used
    """
    MODEL_FILENAME = "model.hdf5"
    MODEL_LABELS_FILENAME = "model_labels.dat"

    # initialize the data and labels
    data = []
    labels = []

    # loop over the input images
    for image_file in paths.list_images(LETTER_IMAGES_FOLDER):
        # Load the image and convert it to grayscale
        image = cv2.imread(image_file, 0)

        if image is None:
            # the filename was probably too long
            print(
                "WARNING: We couldn't open this image, its filename is probably too long:"
            )
            print(image_file)
            continue

        # threshold
        _, image = cv2.threshold(image, 0, 255,
                                 cv2.THRESH_BINARY + cv2.THRESH_OTSU)
        image = crop_letter(image)

        # Resize the letter so it fits in a 20x20 pixel box
        image = resize_to_fit(image, 20, 20)

        # Add a third channel dimension to the image to make Keras happy
        image = np.expand_dims(image, axis=2)

        # Grab the name of the letter based on the folder it was in
        label = image_file.split(os.path.sep)[-1].replace('.png', '')

        # Add the letter image and it's label to our training data
        data.append(image)
        labels.append(label)

    # scale the raw pixel intensities to the range [0, 1] (this improves training)
    data = np.array(data, dtype="float") / 255.0
    labels = np.array(labels)

    # Split the training data into separate train and test sets
    (X_train, X_test, Y_train, Y_test) = train_test_split(data,
                                                          labels,
                                                          test_size=0.25,
                                                          random_state=0)

    # Convert the labels (letters) into one-hot encodings that Keras can work with
    lb = LabelBinarizer().fit(Y_train)
    Y_train = lb.transform(Y_train)
    Y_test = lb.transform(Y_test)

    # Save the mapping from labels to one-hot encodings.
    # We'll need this later when we use the model to decode what it's predictions mean
    with open(MODEL_LABELS_FILENAME, "wb") as f:
        pickle.dump(lb, f)

    # Build the neural network!
    model = Sequential()

    # First convolutional layer with max pooling
    model.add(
        Conv2D(20, (5, 5),
               padding="same",
               input_shape=(20, 20, 1),
               activation="relu"))
    model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2)))

    # Second convolutional layer with max pooling
    model.add(Conv2D(50, (5, 5), padding="same", activation="relu"))
    model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2)))

    # Hidden layer with 500 nodes
    model.add(Flatten())
    model.add(Dense(500, activation="relu"))

    # Output layer with 70 nodes (one for each possible letter/number we predict)
    model.add(Dense(97, activation="softmax"))

    # Ask Keras to build the TensorFlow model behind the scenes
    model.compile(loss="categorical_crossentropy",
                  optimizer="adam",
                  metrics=["accuracy"])

    # Train the neural network
    model.fit(X_train,
              Y_train,
              validation_data=(X_test, Y_test),
              batch_size=32,
              epochs=num_epochs,
              verbose=1)

    # Save the trained model to disk
    model.save(MODEL_FILENAME)
Ejemplo n.º 22
0
args = vars(ap.parse_args())

model = SeparableConv.build_model(width=48, height=48, depth=3, classes=2)
opt = Adagrad(lr=config.INIT_LR, decay=config.INIT_LR / config.NUM_EPOCHS)
model.compile(loss="binary_crossentropy", optimizer=opt, metrics=["accuracy"])
print(model.summary())
from keras.utils import plot_model

plot_model(model,
           to_file='model_plot.png',
           show_shapes=True,
           show_layer_names=True)

# determine the total number of image paths in training, validation,
# and testing directories
trainPaths = list(paths.list_images(config.TRAIN_PATH))[:5000]
totalTrain = len(trainPaths)
totalVal = len(list(paths.list_images(config.VAL_PATH)))
totalTest = len(list(paths.list_images(config.TEST_PATH)))

print("totalTrain : % 2d, totalVal : % 2d, totalTest: % 2d" %
      (totalTrain, totalVal, totalTest))

# account for skew in the labeled data
trainLabels = [int(p.split(os.path.sep)[-2]) for p in trainPaths]
trainLabels = np_utils.to_categorical(trainLabels)
classTotals = trainLabels.sum(axis=0)
classWeight = classTotals.max() / classTotals

# initialize the training training data augmentation object
trainAug = ImageDataGenerator(rescale=1 / 255.0,
# USAGE run in cmd: python3 encode_faces.py or python encode_faces.py
# import the necessary packages
from imutils import paths
import face_recognition
import pickle
import cv2
import os

# construct the argument parser and parse the arguments

# grab the paths to the input images in our dataset
print("[INFO] quantifying faces...")
imagePaths = list(paths.list_images("dataset"))

# initialize the list of known encodings and known names
knownEncodings = []
knownNames = []

# loop over the image paths
for (i, imagePath) in enumerate(imagePaths):
    # extract the person name from the image path
    print("[INFO] processing image {}/{}".format(i + 1,
        len(imagePaths)))
    name = imagePath.split(os.path.sep)[-2]
    print(name)

    # load the input image and convert it from RGB (OpenCV ordering)
    # to dlib ordering (RGB)
    image = cv2.imread(imagePath)
    rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
def empty_download_folder():
    imagePaths = list(paths.list_images(FTP_DIRECTORY))
    for image in imagePaths:
        os.remove(image)
Ejemplo n.º 25
0
def ListarImagenesDirectorio(dir):
    return sorted(list(paths.list_images(dir + "/")))
Ejemplo n.º 26
0
# -*- coding: utf-8 -*-
"""
Created on Fri May 3 15:20:00 2018

@author: jercas
"""
from imutils import paths
import pandas as pd
import numpy as np
from icecream.icecream import ic
import shutil
import os

pathDirs = list(paths.list_images("./dataset/train_origin"))
data = np.loadtxt("train.txt", dtype=np.str_, delimiter=' ')
pathTxts = data[:, 0]
pathLabel = data[:, 1]
count = 1

for i, pathTxt in enumerate(pathTxts):
    for j, pathDir in enumerate(pathDirs):
        purePathDir = pathDir.split(os.path.sep)[-1]
        if pathTxt == purePathDir:
            ic(pathDir)
            ic(pathTxt)
            label = pathLabel[i]
            if not os.path.exists("./dataset/train_split/{}".format(label)):
                os.mkdir("./dataset/train_split/{}".format(label))
            shutil.copyfile(
                pathDir, "./dataset/train_split/{}/{}".format(label, pathTxt))
            ic(count)
Ejemplo n.º 27
0
		p = os.path.sep.join([args["output"], "{}.jpg".format(
			str(total).zfill(8))])
		f = open(p, "wb")
		f.write(r.content)
		f.close()

		# update the counter
		print("[INFO] downloaded: {}".format(p))
		total += 1

	# handle if any exceptions are thrown during the download process
	except:
		print("[INFO] error downloading {}...skipping".format(p))

# loop over the image paths we just downloaded
for imagePath in paths.list_images(args["output"]):
	# initialize if the image should be deleted or not
	delete = False

	# try to load the image
	try:
		image = cv2.imread(imagePath)

		# if the image is `None` then we could not properly load it
		# from disk, so delete it
		if image is None:
			delete = True

	# if OpenCV cannot load the image then the image is likely
	# corrupt so we should delete it
	except:
Ejemplo n.º 28
0
# initialize the HOG descriptor/person detector
hog = cv2.HOGDescriptor()
hog.setSVMDetector(cv2.HOGDescriptor_getDefaultPeopleDetector())
#video capture
vidcap = cv2.VideoCapture(r'C:\ADITYA\Computervision\UCF50_videos\UCF50\BaseballPitch\v_BaseballPitch_g01_c01.avi')
success,image = vidcap.read()
count = 0
while success:
  cv2.imwrite(r"C:\ADITYA\Computervision\UCF50_videos\BaseballPitch_frames\frame%d.jpg" % count, image)     # save frame as JPEG file      
  success,image = vidcap.read()
  print('Read a new frame: ', success)
  count += 1

# loop over the image paths
imagePaths = list(paths.list_images(args["images"]))
d=0

for imagePath in imagePaths:
	# load the image and resize it to (1) reduce detection time
	# and (2) improve detection accuracy
    image = cv2.imread(imagePath)
    image = imutils.resize(image, width=min(400, image.shape[1]))
    orig = image.copy()

	# detect people in the image
    (rects, weights) = hog.detectMultiScale(image, winStride=(4, 4),
     padding=(8, 8), scale=1.05)
    
	# draw the original bounding boxes
    for (x, y, w, h) in rects:
Ejemplo n.º 29
0
def prepare_cifar_data_for_ae2_svdd(train_path, test_path, modelpath):

    NB_IV3_LAYERS_TO_FREEZE = 4
    side = 32
    side = 32
    channel = 3

    ## Declare the scoring functions
    g = lambda x: 1 / (1 + tf.exp(-x))

    # g  = lambda x : x # Linear
    def nnScore(X, w, V, g):

        # print "X",X.shape
        # print "w",w[0].shape
        # print "v",V[0].shape
        return tf.matmul(g((tf.matmul(X, w))), V)

    def relu(x):
        y = x
        y[y < 0] = 0
        return y

    def add_new_last_layer(base_model_output, base_model_input):
        """Add last layer to the convnet
        Args:
          base_model: keras model excluding top
          nb_classes: # of classes
        Returns:
          new keras model with last layer
        """
        x = base_model_output
        print("base_model.output", x.shape)
        inp = base_model_input
        print("base_model.input", inp.shape)
        dense1 = Dense(128, name="dense_output1")(x)  # new sigmoid layer
        dense1out = Activation("relu", name="output_activation1")(dense1)
        dense2 = Dense(1, name="dense_output2")(dense1out)  # new sigmoid layer
        dense2out = Activation("relu", name="output_activation2")(
            dense2)  # new sigmoid layer
        model = Model(inputs=inp, outputs=dense2out)
        return model

    # load the trained convolutional neural network freeze all the weights except for last four layers
    print("[INFO] loading network...")
    model = load_model(modelpath)
    model = add_new_last_layer(model.output, model.input)
    print(model.summary())
    print("Length of model layers...", len(model.layers))

    # Freeze the weights of untill the last four layers
    for layer in model.layers[:NB_IV3_LAYERS_TO_FREEZE]:
        layer.trainable = False
    for layer in model.layers[NB_IV3_LAYERS_TO_FREEZE:]:
        layer.trainable = True

    print("[INFO] loading images for training...")
    data = []
    data_test = []
    labels = []
    labels_test = []
    # grab the image paths and randomly shuffle them
    imagePaths = sorted(list(paths.list_images(train_path)))
    # loop over the input training images
    image_dict = {}
    test_image_dict = {}
    i = 0
    for imagePath in imagePaths:
        # load the image, pre-process it, and store it in the data list
        image = cv2.imread(imagePath)
        image = cv2.resize(image, (side, side))
        image = img_to_array(image)
        data.append(image)
        image_dict.update({i: imagePath})
        i = i + 1

        # extract the class label from the image path and update the
        # labels list
        label = imagePath.split(os.path.sep)[-2]
        label = 1 if label == "dogs" else 0
        labels.append(label)
    # scale the raw pixel intensities to the range [0, 1]
    data = np.array(data)

    y = keras.utils.to_categorical(labels, num_classes=2)
    # print image_dict

    print("[INFO] preparing test data (anomalous )...")
    testimagePaths = sorted(list(paths.list_images(test_path)))
    # loop over the test images
    j = 0
    for imagePath in testimagePaths:
        # load the image, pre-process it, and store it in the data list
        image = cv2.imread(imagePath)
        image = cv2.resize(image, (side, side))
        image = img_to_array(image)
        data_test.append(image)
        test_image_dict.update({j: imagePath})
        j = j + 1
        # extract the class label from the image path and update the
        # labels list
        label = imagePath.split(os.path.sep)[-2]
        label = 0 if label == "cats" else 1
        labels_test.append(label)
    # scale the raw pixel intensities to the range [0, 1]
    data_test = np.array(data_test)

    ## Normalise the data
    data = np.array(data) / 255.0
    data_test = np.array(data_test) / 255.0

    # Preprocess the inputs
    x_train = data
    x_test = data_test

    ## Obtain the intermediate output
    layer_name = 'dense_1'
    intermediate_layer_model = Model(
        inputs=model.input, outputs=model.get_layer(layer_name).output)

    layer1 = model.get_layer("dense_output1")
    layer2 = model.get_layer("dense_output2")
    intermediate_output = intermediate_layer_model.predict(x_train)
    X = intermediate_output
    intermediate_output = intermediate_layer_model.predict(x_test)
    X_test = intermediate_output

    K.clear_session()

    return [X, X_test]
		hist = hist.astype("float")
		hist /= (hist.sum() + eps)

		# return the histogram of Local Binary Patterns
		return hist
#%%

# initialize the local binary patterns descriptor along with
# the data and label lists
desc = LocalBinaryPatterns(24, 8)
data = []
labels = []
#%%

# loop over the training images
for imagePath in paths.list_images('/home/alfredo/Desktop/PROJECTS/Master Thesis/opencv/images/local_binary_patterns/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)
#%%
 
# train a Linear SVM on the data
model = LinearSVC(C=100.0, random_state=42)
model.fit(data, labels)
Ejemplo n.º 31
0
ap.add_argument("-p",
                "--plot",
                type=str,
                default="plot.png",
                help="path to output loss/accuracy plot")
args = vars(ap.parse_args())

EPOCHS = 25
INIT_LR = 1e-3
BS = 32

print("[INFO] loading images...")
data = []
labels = []

imagePaths = sorted(list(paths.list_images(args["dataset"])))
random.seed(42)
random.shuffle(imagePaths)

for imagePath in imagePaths:
    image = cv2.imread(imagePath)
    image = cv2.resize(image, (28, 28))
    image = img_to_array(image)
    data.append(image)

    label = imagePath.split(os.path.sep)[-2]
    label = 1 if label == "fire" else 0
    labels.append(label)

data = np.array(data, dtype="float") / 255.0
labels = np.array(labels)
Ejemplo n.º 32
0
import imutils
import cv2
import random
import pickle

# Construct the argument parser and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-m", "--model", required=True, help="path to trained model")
ap.add_argument("-t", "--testset", required=True, help="path to test images")
args = vars(ap.parse_args())

# Resize parmeters (RESIZE should be the same as used in training)
RESIZE = 96

# Grab the image paths and randomly shuffle them
image_paths = sorted(list(paths.list_images(args["testset"])))

random.seed()
random.shuffle(image_paths)

# Read labels for classes to recognize
mlb = pickle.loads(open(args["model"] + ".lbl", "rb").read())
labels = mlb.classes_

# Load the trained network
model = load_model(args["model"] + ".h5")
print(model.summary())
for image_path in image_paths:
    # Load the image
    image = cv2.imread(image_path)
    output = imutils.resize(image, width=400)
Ejemplo n.º 33
0
from imutils import paths

# Config
crop_start = (50, 285)
crop_size = (224, 224)
dark_factor = 0.3

# Prepare crop area
crop_area = numpy.index_exp[
    crop_start[0]:crop_start[0]+crop_size[0],
    crop_start[1]:crop_start[1]+crop_size[1]
]

# find the image paths and randomly shuffle them
print("# loading images...")
imagePaths = sorted(list(paths.list_images(sys.argv[1], contains="capture")))
print("# loaded %d images." % (len(imagePaths)))

for imagePath in imagePaths:
    image = cv2.imread(imagePath)
    orig = image[crop_area]
    image = (image * dark_factor).astype(numpy.uint8)
    image[crop_area] = orig
    cv2.imshow("Output", image)
    key_raw = cv2.waitKey(0)
    try:
        key = chr(key_raw)
        if key in ['d', 'D']:
            print("delete:%s" % imagePath)
            os.unlink(imagePath)
        elif key in ['p', 'P', 'y', 'Y']:
Ejemplo n.º 34
0
from keras.layers.core import Flatten, Dense
from helpers import resize_to_fit
from keras.callbacks import ModelCheckpoint, TensorBoard
from keras.utils.vis_utils import plot_model

# LETTER_IMAGES_FOLDER = "extracted_letter_images"
LETTER_IMAGES_FOLDER = "extracted_letter_images"
MODEL_FILENAME = "captcha_model.hdf5"
MODEL_LABELS_FILENAME = "model_labels.dat"

# initialize the data and labels
data = []
labels = []

# loop over the input images
for image_file in paths.list_images(LETTER_IMAGES_FOLDER):
    # Load the image and convert it to grayscale
    image = cv2.imread(image_file)
    try:
        image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    except Exception:
        print(image)

    # Resize the letter so it fits in a 20x20 pixel box
    image = resize_to_fit(image, 20, 20)

    # Add a third channel dimension to the image to make Keras happy
    image = np.expand_dims(image, axis=2)

    # Grab the name of the letter based on the folder it was in
    label = image_file.split(os.path.sep)[-2]
def train(number_neighbors, focus, hidden_layer_sizes, max_iter_bpnn,
          max_iter_svm):
    respond = {}
    __init_log(focus)
    respond['focus'] = focus
    imagePaths = list(paths.list_images(FOLDER_DATASET))

    rawImages = []
    features = []
    labels = []

    for (i, imagePath) in enumerate(imagePaths):
        image = cv2.imread(imagePath)
        label = imagePath.split(os.path.sep)[-1].split(".")[0]
        pixels = __image_to_feature_vector(image)
        hist = __extract_color_histogram(image)

        rawImages.append(pixels)
        features.append(hist)
        labels.append(label)

        if i > 0 and ((i + 1) % 200 == 0 or i == len(imagePaths) - 1):
            size_images = len(imagePaths)
            line = "[INFO] processed {}/{}".format(i + 1, size_images)
            respond['length_images'] = size_images
            __write_log(line)

    labels = np.array(labels)
    if focus == 'histogram':
        features = np.array(features)
        size_package = '{:.2f}MB'.format(features.nbytes / (1024 * 1000.0))
        line = '[INFO] features matrix: ' + size_package
        respond['size_package'] = size_package
        __write_log(line)
        (trainFeat, testFeat, trainLabels,
         testLabels) = train_test_split(features,
                                        labels,
                                        test_size=0.15,
                                        random_state=42)

        # k-NN
        line = "[INFO] evaluating histogram accuracy..."
        __write_log(line)
        model = KNeighborsClassifier(n_neighbors=number_neighbors)
        model.fit(trainFeat, trainLabels)
        acc = model.score(testFeat, testLabels)
        line = "[INFO] k-NN classifier: k=%d" % number_neighbors
        __write_log(line)
        accuracy_knn = '{:.2f}%'.format(acc * 100)
        line = "[INFO] histogram accuracy: " + accuracy_knn
        respond['accuracy_knn'] = accuracy_knn
        __write_log(line)
        joblib.dump(model, FILE_MODELS_HISTOGRAM_KNN)

        # neural network
        line = "[INFO] evaluating histogram accuracy..."
        __write_log(line)
        model = MLPClassifier(hidden_layer_sizes=(50, ),
                              max_iter=1000,
                              alpha=1e-4,
                              solver='sgd',
                              tol=1e-4,
                              random_state=1,
                              learning_rate_init=.1)
        model.fit(trainFeat, trainLabels)
        acc = model.score(testFeat, testLabels)
        accuracy_bpnn = "{:.2f}%".format(acc * 100)
        line = "[INFO] neural network histogram accuracy: " + accuracy_bpnn
        respond['accuracy_bpnn'] = accuracy_bpnn
        __write_log(line)
        joblib.dump(model, FILE_MODELS_HISTOGRAM_BPNN)

        # SVC
        line = "[INFO] evaluating histogram accuracy..."
        __write_log(line)
        model = SVC(max_iter=1000, class_weight='balanced')
        model.fit(trainFeat, trainLabels)
        acc = model.score(testFeat, testLabels)
        accuracy_svm = '{:.2f}%'.format(acc * 100)
        line = "[INFO] SVM-SVC histogram accuracy: " + accuracy_svm
        respond['accuracy_svm'] = accuracy_svm
        __write_log(line)
        joblib.dump(model, FILE_MODELS_HISTOGRAM_SVM)
        respond['success'] = True
        return respond
    elif focus == 'pixel':
        rawImages = np.array(rawImages)
        size_package = "{:.2f}MB".format(rawImages.nbytes / (1024 * 1000.0))
        line = "[INFO] pixels matrix: " + size_package
        respond['size_package'] = size_package
        __write_log(line)
        (trainRI, testRI, trainRL, testRL) = train_test_split(rawImages,
                                                              labels,
                                                              test_size=0.15,
                                                              random_state=42)

        # k-NN
        line = "[INFO] evaluating raw pixel accuracy..."
        __write_log(line)
        model = KNeighborsClassifier(n_neighbors=number_neighbors)
        model.fit(trainRI, trainRL)
        acc = model.score(testRI, testRL)
        line = "[INFO] k-NN classifier: k=%d" % number_neighbors
        __write_log(line)
        accuracy_knn = "{:.2f}%".format(acc * 100)
        line = "[INFO] raw pixel accuracy: " + accuracy_knn
        respond['accuracy_knn'] = accuracy_knn
        __write_log(line)
        joblib.dump(model, FILE_MODELS_PIXEL_KNN)

        # neural network
        line = "[INFO] evaluating raw pixel accuracy..."
        __write_log(line)
        model = MLPClassifier(hidden_layer_sizes=(hidden_layer_sizes, ),
                              max_iter=max_iter_bpnn,
                              alpha=1e-4,
                              solver='sgd',
                              tol=1e-4,
                              random_state=1,
                              learning_rate_init=.1)
        model.fit(trainRI, trainRL)
        acc = model.score(testRI, testRL)
        accuracy_bpnn = "{:.2f}%".format(acc * 100)
        line = "[INFO] neural network raw pixel accuracy: " + accuracy_bpnn
        respond['accuracy_bpnn'] = accuracy_bpnn
        __write_log(line)
        joblib.dump(model, FILE_MODELS_PIXEL_BPNN)

        # SVC
        line = "[INFO] evaluating raw pixel accuracy..."
        __write_log(line)
        model = SVC(max_iter=max_iter_svm, class_weight='balanced')
        model.fit(trainRI, trainRL)
        acc = model.score(testRI, testRL)
        accuracy_svm = "{:.2f}%".format(acc * 100)
        line = "[INFO] SVM-SVC raw pixel accuracy: " + accuracy_svm
        respond['accuracy_svm'] = accuracy_svm
        __write_log(line)
        joblib.dump(model, FILE_MODELS_PIXEL_SVM)
        respond['success'] = True
        return respond
    else:
        respond['success'] = False
        respond['error'] = 'focus no esperado'
        return respond
Ejemplo n.º 36
0
# -*- coding: utf-8 -*-
"""
Created on Thu Jul  9 04:16:07 2020

@author: Devdarshan
"""
import os
from imutils import paths

path = "Fire/"
path2 = "images/"

imagePaths = list(paths.list_images(path))

import cv2

i = 0
for imagePath in imagePaths:
    # load the image and resize it to be a fixed 128x128 pixels,
    # ignoring aspect ratio
    print(imagePath)
    image = cv2.imread(imagePath)
    image = cv2.resize(image, (608, 608))
    cv2.imwrite(os.path.join(path2, "{}.jpg").format(i), image)
    i += 1
Ejemplo n.º 37
0
        f_ishift = numpy.fft.ifftshift(fshift)
        img_fft = numpy.fft.ifft2(f_ishift)
        img_fft = 20*numpy.log(numpy.abs(img_fft))
        result = numpy.mean(img_fft)
        return result


# construct the argument parse and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-i", "--images", required=True,
	help="path to input directory of images")

args = vars(ap.parse_args())

# loop over the input images
for i, imagePath in enumerate(sorted(paths.list_images(args["images"]))):
	# load the image, convert it to grayscale, and compute the
	# focus measure of the image using the Variance of Laplacian
	# method
	image = cv2.imread(imagePath)
        image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
	gray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
        eqhist = cv2.equalizeHist(gray)
        metrics = {}

        width, height, _ = image.shape

        cropped = image[50:height-50,50:width-50,:]
        grayc = cv2.cvtColor(cropped, cv2.COLOR_RGB2GRAY)

Ejemplo n.º 38
0
# import the necessary packages
from imutils import paths
import argparse
import dlib
import cv2

# construct the argument parse and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-d", "--detector", required=True, help="Path to trained object detector")
ap.add_argument("-t", "--testing", required=True, help="Path to directory of testing images")
args = vars(ap.parse_args())

# load the detector
detector = dlib.simple_object_detector(args["detector"])

# loop over the testing images
for testingPath in paths.list_images(args["testing"]):
	# load the image and make predictions
	image = cv2.imread(testingPath)
	boxes = detector(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))

	# loop over the bounding boxes and draw them
	for b in boxes:
		(x, y, w, h) = (b.left(), b.top(), b.right(), b.bottom())
		cv2.rectangle(image, (x, y), (w, h), (0, 255, 0), 2)
		print (x,y)
	# show the image
	cv2.imshow("Image", image)
	cv2.waitKey(0)
Ejemplo n.º 39
0
import cv2
import numpy as np
from imutils import paths  
import os 
from sklearn.metrics import accuracy_score
from matplotlib import pyplot as plt

classes = {1: 'Bike',
           2: 'Horse'} 
labels = []
dictionarySize = 12

BOW = cv2.BOWKMeansTrainer(dictionarySize)
sift = cv2.xfeatures2d.SIFT_create()

imagePaths = list(paths.list_images("train"))
for image in imagePaths:
	label = image.split('/')[1]
	if label == 'Bike':
		labels.append(1)
	elif label == 'Horse':
		labels.append(2)

	image = image.replace("\\","")
	img = cv2.imread(image,0)
	kp, des = sift.detectAndCompute(img,None)
	BOW.add(des)

dictionary = BOW.cluster()
print("Created Bag of Words")
Ejemplo n.º 40
0
ap.add_argument("-l", "--labelbin", required=True,
	help="path to output label binarizer")
ap.add_argument("-p", "--plot", type=str, default="plot.png",
	help="path to output accuracy/loss plot")
args = vars(ap.parse_args())

# initialize the number of epochs to train for, initial learning rate,
# batch size, and image dimensions
EPOCHS = 75
INIT_LR = 1e-3
BS = 32
IMAGE_DIMS = (96, 96, 3)

# grab the image paths and randomly shuffle them
print("[INFO] loading images...")
imagePaths = sorted(list(paths.list_images(args["dataset"])))
random.seed(42)
random.shuffle(imagePaths)

# initialize the data and labels
data = []
labels = []

# loop over the input images
for imagePath in imagePaths:
	# load the image, pre-process it, and store it in the data list
	image = cv2.imread(imagePath)
	image = cv2.resize(image, (IMAGE_DIMS[1], IMAGE_DIMS[0]))
	image = img_to_array(image)
	data.append(image)
Ejemplo n.º 41
0
                help="Path to the CALTECH-101 class annotations")
ap.add_argument("-o",
                "--output",
                required=True,
                help="Path to the output detector")
args = vars(ap.parse_args())

# grab the default training options for our HOG + Linear SVM detector initialize the
# list of images and bounding boxes used to train the classifier
print("[INFO] gathering images and bounding boxes...")
options = dlib.simple_object_detector_training_options()
images = []
boxes = []

# loop over the image paths
for imagePath in paths.list_images(args["class"]):
    # extract the image ID from the image path and load the annotations file
    imageID = imagePath[imagePath.rfind("/") + 1:].split("_")[1]
    imageID = imageID.replace(".jpg", "")
    p = "{}/annotation_{}.mat".format(args["annotations"], imageID)
    annotations = loadmat(p)["box_coord"]

    # loop over the annotations and add each annotation to the list of bounding
    # boxes
    bb = [
        dlib.rectangle(left=long(x),
                       top=long(y),
                       right=long(w),
                       bottom=long(h)) for (y, h, x, w) in annotations
    ]
    boxes.append(bb)
Ejemplo n.º 42
0
import numpy as np
import argparse
import imutils
import cv2
 
# construct the argument parse and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-i", "--images", required=True, help="path to images directory")
args = vars(ap.parse_args())
 
# initialize the HOG descriptor/person detector
hog = cv2.HOGDescriptor()
hog.setSVMDetector(cv2.HOGDescriptor_getDefaultPeopleDetector())

# loop over the image paths
for imagePath in paths.list_images(args["images"]):
    # load the image and resize it to (1) reduce detection time
    # and (2) improve detection accuracy
    image = cv2.imread(imagePath)
    image = imutils.resize(image, width=min(400, image.shape[1]))
    orig = image.copy()
 
    # detect people in the image
    (rects, weights) = hog.detectMultiScale(image, winStride=(4, 4),
        padding=(8, 8), scale=1.05)
 
    # draw the original bounding boxes
    for (x, y, w, h) in rects:
        cv2.rectangle(orig, (x, y), (x + w, y + h), (0, 0, 255), 2)
 
    # apply non-maxima suppression to the bounding boxes using a
Ejemplo n.º 43
0
ap = argparse.ArgumentParser()
ap.add_argument("-b", "--base-model", required=True,
	help="base model path")
ap.add_argument("-i", "--images", required=True,
	help="base path to input directory of images")
ap.add_argument("-o", "--output", required=True,
	help="base path to output directory")
ap.add_argument("-l", "--layers", nargs='+', default=["conv2/3x3", "inception_3b/5x5_reduce", "inception_4c/output"],
	help="layer or layers to use")
args = ap.parse_args()

# buy the ticket, take the ride
bc = BatCountry(args.base_model)

# loop over the input directory of images
for imagePath in paths.list_images(args.images):
	# loop over the layers
	for layer in args.layers:
		# we can't stop here...
		print("[INFO] processing `{}`".format(imagePath))
		image = bc.dream(np.float32(Image.open(imagePath)), end=layer)

		# write the output image to file
		filename = imagePath[imagePath.rfind("/") + 1:]
		outputPath = "{}/{}_{}".format(args.output, layer.replace("/", "_"), filename)
		result = Image.fromarray(np.uint8(image))
		result.save(outputPath)

# do some cleanup
bc.cleanup()
Ejemplo n.º 44
0
import cv2


ap = argparse.ArgumentParser()
ap.add_argument("-d", "--dataset", required=True)
ap.add_argument("-k", "--clusters", type=int, default=2,
                help="# of clusters to generate")

args = vars(ap.parse_args())

# initialize the image descriptor along with the image matrix
desc = LabHistogram([8, 8, 8])
data = []

# grab image paths from dataset directory
image_paths = list(paths.list_images(args["dataset"]))
image_paths = np.array(sorted(image_paths))

# loop over the input dataset of images
for im_path in image_paths:
    # load the image, describe the image, then update the list of data
    image = cv2.imread(im_path)
    hist = desc.describe(image)
    data.append(hist)

# cluster the color histograms
clt = KMeans(n_clusters=args["clusters"])
labels = clt.fit_predict(data)


from IPython import embed
Ejemplo n.º 45
0
def move(path):
    for pf in list_images(path):
        f = pf.split('/')[-1]
        new_f = f[0] + ("%05d" % int(f[1:-4])) + f[-4:]
        shutil.move(pf, os.path.join(path, new_f))
Ejemplo n.º 46
0
# Freeze the weights of untill the last four layers
for layer in model.layers[:NB_IV3_LAYERS_TO_FREEZE]:
    layer.trainable = False
for layer in model.layers[NB_IV3_LAYERS_TO_FREEZE:]:
    layer.trainable = True

# declare the global variable

print("[INFO] loading images for training...")
data = []
data_test = []
labels = []
labels_test = []
# grab the image paths and randomly shuffle them
imagePaths = sorted(list(paths.list_images(train_path)))
import random
random.seed(42)
random.shuffle(imagePaths)
# loop over the input training images
image_dict = {}
i = 0
for imagePath in imagePaths:
    # load the image, pre-process it, and store it in the data list
    image = cv2.imread(imagePath)
    image = cv2.resize(image, (28, 28))
    image = img_to_array(image)
    data.append(image)
    image_dict.update({i: imagePath})
    i = i + 1
Ejemplo n.º 47
0
img_rows, img_cols = 28, 28
# number of convolutional filters to use
nb_filters = 32
# size of pooling area for max pooling
pool_size = (2, 2)
# convolution kernel size
kernel_size = (3, 3)

# the data, shuffled and split between train and test sets
# (X_train, y_train), (X_test, y_test) = mnist.load_data()
X_train = np.empty((0, 1, img_rows, img_cols))
y_train = np.empty((0))
X_test = np.empty((0, 1, img_rows, img_cols))
y_test = np.empty((0))
for i in range(0, 10):
	(X, Y) = build_batch(sorted(list(paths.list_images("dataset/{}".format(i)))), img_rows, img_cols)
	X_train = np.concatenate((np.asarray(X[0:75]), X_train))
	y_train = np.concatenate((np.asarray(Y[0:75]), y_train))
	X_test = np.concatenate((np.asarray(X[75:]), X_test))
	y_test = np.concatenate((np.asarray(Y[75:]), y_test))

print(X_train.shape)
print(y_train.shape)
print(X_test.shape)
print(y_test.shape)

if K.image_dim_ordering() == 'th':
	X_train = X_train.reshape(X_train.shape[0], 1, img_rows, img_cols)
	X_test = X_test.reshape(X_test.shape[0], 1, img_rows, img_cols)
	input_shape = (1, img_rows, img_cols)
else:
Ejemplo n.º 48
0
from localbinarypatterns import LocalBinaryPatterns
from imutils import paths
import cv2
import pandas as pd
import numpy as np
from skimage import feature
import matplotlib.pyplot as plt

db = 'yale'
data_training = 'db/%s' % db

P = 8  # número de pontos
R = 8  # raio

dataset = [file for file in paths.list_images(data_training)]
desc = LocalBinaryPatterns(8, 8)

gbr = paths.list_images(data_training)
nama_gbr = gbr.__next__()
print(nama_gbr)
image = cv2.imread(nama_gbr)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
lbp = feature.local_binary_pattern(gray, P, R, method='uniform')
hist = np.histogram(lbp.ravel(), bins=range(0, P + 3))
h = hist[0].astype('float')
h /= (h.sum())
print(h, h.sum())
plt.bar(np.array(range(0, P + 2)), h)
plt.xticks(np.array(range(0, P + 2)))
plt.ylabel('Percentage')
plt.show()
Ejemplo n.º 49
0
            
            if self.preprocessors is not None:
                for p in self.preprocessors:
                    image = p.preprocess(image)
                    #print(image.shape)
            data.append(image)
            labels.append(label)
            
            if verbose > 0 and i>0 and (i+1)%verbose==0:
                print("[INFO] processed {}/{}".format(i+1, len(imagePaths)))
        return(np.array(data), np.array(labels))


# In[57]:

image_paths = list(paths.list_images('/home/padmach/data/DogVsCats/train'))


# In[58]:

sp = SimplePreprocessor(32, 32)
sdl = SimpleDatasetLoader(preprocessors=[sp])
(data, labels) = sdl.load(image_paths, verbose=500)


# In[60]:

data = data.reshape((data.shape[0], 3072))


# In[63]:
from imutils import paths
import numpy as np
import imutils
import cv2

crop = 1

print("[INFO] loading images...")
imagePaths = sorted(list(paths.list_images('images')))
print(imagePaths)
images = []

for imagePath in imagePaths:
    image = cv2.imread(imagePath)
    images.append(image)

print("[INFO] stitching images...")
stitcher = cv2.createStitcher() if imutils.is_cv3() else cv2.Stitcher_create()
(status, stitched) = stitcher.stitch(images)

if status == 0:

    if crop > 0:

        print("[INFO] cropping...")
        stitched = cv2.copyMakeBorder(stitched, 10, 10, 10, 10,
                                      cv2.BORDER_CONSTANT, (0, 0, 0))

        gray = cv2.cvtColor(stitched, cv2.COLOR_BGR2GRAY)
        thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY)[1]
Ejemplo n.º 51
0
		f = open(p, "wb")
		f.write(r.content)
		f.close()

		# update the counter
		print("[INFO] downloaded: {}".format(p))
		total += 1

	# handle if any exceptions are thrown during the download process
	except:
		print("[INFO] error downloading {}...skipping".format(p))


		# loop over the image paths we just downloaded

        for imagePath in paths.list_images(args["output"]):
                # initialize if the image should be deleted or not
                delete = False
         
                # try to load the image
                try:
                        image = cv2.imread(imagePath)
         
                        # if the image is `None` then we could not properly load it
                        # from disk, so delete it
                        if image is None:
                                delete = True
         
                # if OpenCV cannot load the image then the image is likely
                # corrupt so we should delete it
                except:
        cv2.namedWindow("quadro")
        cv2.setMouseCallback("quadro",alteraEstado,param=(lista,img))
        cv2.imshow("quadro",img)

        print conta,'Arq:',lista[conta]["filename"],' Sel Status:',lista[conta]["isok"]


if __name__ == '__main__':
        cascade = cv2.CascadeClassifier(args.cascade)
        largeimg = np.zeros((800,1280,3),np.uint8)
        xpos = 0
        ypos = 0
        listaInfoJ = []
        conta = 0
        for pimagem in paths.list_images(mypath):
            print pimagem
            rgbIn = cv2.imread(pimagem)
            [heb,wib,pb] = rgbIn.shape
            rects,bdisp=detect(rgbIn, cascade)
            for x1, y1, x2, y2 in rects:
                x1,y1,x2,y2 = RepUtil.novoEquad(x1,y1,x2,y2,wib,heb)
                vis_roi = rgbIn[y1:y2, x1:x2]
                [he,wi,pro] = vis_roi.shape
                if wi > 640:
                    fato = 640.0/float(wi)
                    rgbFrame  = cv2.resize(vis_roi,(0,0),fx=fato,fy=fato)
                else:
                    rgbFrame = vis_roi.copy()
                bb = align.getLargestFaceBoundingBox(rgbFrame)
                bbs = [bb] if bb is not None else []
Ejemplo n.º 53
0
    model.compile(optimizer='adam',
                  loss='categorical_crossentropy',
                  metrics=['accuracy'])
    print("[INFO] Model Compiled!")
    return model


#%%

print("[INFO] loading images from private data...")
data = []
labels = []

# grab the image paths and randomly shuffle them
imagePaths = sorted(
    list(paths.list_images('C:\\Users\\User\\spn_myone\\spn_pr'))
)  # data folder with 2 categorical folders
random.seed(SEED)
random.shuffle(imagePaths)

# loop over the input images
for imagePath in imagePaths:
    # load the image, resize the image to be 32x32 pixels (ignoring aspect ratio),
    # flatten the 32x32x3=3072 pixel image into a list, and store the image in the data list
    image = cv2.imread(imagePath)
    #image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    image = cv2.resize(image, (32, 32)) / 255
    data.append(image)

    # extract the class label from the image path and update the labels list
    label = imagePath.split(os.path.sep)[-2]
Ejemplo n.º 54
0
        print "{}".format(rect)
        x1,y1,x2,y2 = novoEquadR(rect,w,h)
        print "{}; {}; {}; {}".format(x1,y1,x2,y2)
        newim = img[y1:y2,x1:x2].copy()
        bb = align.getLargestFaceBoundingBox(newim)
        if bb is None:
            continue
        hr,wr,c = newim.shape
        if wr > 600:
            newim = cv2.resize(newim,(0,0),fx=0.5,fy=0.5)
        return newim
    return None

if __name__ == '__main__':
    cascade = cv2.CascadeClassifier(join("..","..","data","haarcascades","haarcascade_frontalface_alt2.xml"))
    for pimagem in paths.list_images(pathimg):
        print pimagem
        im = cv2.imread(pimagem)
        if im is None:
            continue
        
        im=detect(im, cascade)
        if im is None:
            continue
        
        h,w,c = im.shape
        if h < 100 or w < 100:
            print "Imagem pequena??? -> {} -> {}x{}".format(pimagem,w,h)
            continue
        
        if im is None:
Ejemplo n.º 55
0
import numpy as np
import argparse
import imutils
import cv2

# construct the argument parse and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-i", "--images", required=True, help="path to images directory")
args = vars(ap.parse_args())

# initialize the HOG descriptor/person detector
hog = cv2.HOGDescriptor()
hog.setSVMDetector(cv2.HOGDescriptor_getDefaultPeopleDetector())

# loop over the image paths
imagePaths = list(paths.list_images(args["images"]))

for imagePath in imagePaths:
	# load the image and resize it to (1) reduce detection time
	# and (2) improve detection accuracy
	image = cv2.imread(imagePath)
	image = imutils.resize(image, width=min(400, image.shape[1]))
	orig = image.copy()

	# detect people in the image
	(rects, weights) = hog.detectMultiScale(image, winStride=(4, 4),
		padding=(8, 8), scale=1.05)

	# draw the original bounding boxes
	for (x, y, w, h) in rects:
		cv2.rectangle(orig, (x, y), (x + w, y + h), (0, 0, 255), 2)
# author:    Adrian Rosebrock
# website:   http://www.pyimagesearch.com

# import the necessary packages
from __future__ import print_function
from imutils import paths

# loop over the image paths in the previous 'demo_images'
# directory and print the paths to the terminal
for imagePath in paths.list_images("../demo_images"):
    print(imagePath)
MODEL_FILENAME = "captcha_model.hdf5"
MODEL_LABELS_FILENAME = "model_labels.dat"
CAPTCHA_IMAGE_FOLDER = "generated_captcha_images"


# Load up the model labels (so we can translate model predictions to actual letters)
with open(MODEL_LABELS_FILENAME, "rb") as f:
    lb = pickle.load(f)

# Load the trained neural network
model = load_model(MODEL_FILENAME)

# Grab some random CAPTCHA images to test against.
# In the real world, you'd replace this section with code to grab a real
# CAPTCHA image from a live website.
captcha_image_files = list(paths.list_images(CAPTCHA_IMAGE_FOLDER))
captcha_image_files = np.random.choice(captcha_image_files, size=(100,), replace=False)

# loop over the image paths
for image_file in captcha_image_files:
    # Load the image and convert it to grayscale
    image = cv2.imread(image_file)
    image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

    # Add some extra padding around the image
    image = cv2.copyMakeBorder(image, 20, 20, 20, 20, cv2.BORDER_REPLICATE)

    # threshold the image (convert it to pure black and white)
    thresh = cv2.threshold(image, 0, 255, cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU)[1]

    # find the contours (continuous blobs of pixels) the image
 def readImagesAndAnnotations(self):
     self.imagePaths = list(paths.list_images(self.inputPath))
Ejemplo n.º 59
0
import config
from imutils import paths
import random
import shutil
import os

# Grab the paths to all input images in the original input directory and shuffle them
imagePaths = list(paths.list_images(config.ORIG_INPUT_DATASET))
random.seed(100)
random.shuffle(imagePaths)

# Compute the training and testing split
i = int(len(imagePaths) * config.TRAIN_SPLIT)
trainPaths = imagePaths[:i]
testPaths = imagePaths[i:]

# Using part of the training dataa for validation
val_num = int(len(trainPaths) * config.VAL_SPLIT)
valPaths = trainPaths[:i]
trainPaths = trainPaths[i:]

# Define the datasets that we'll be building
datasets = [("training", trainPaths, config.TRAIN_PATH),
            ("validation", valPaths, config.VALIDATION_PATH),
            ("testing", testPaths, config.TEST_PATH)]


# Loop over the datasets
for (dataType, imagePaths, baseOutput) in datasets:
    # Show which dataset we are creating
    print("[INFO] Buildint {} split.".format(dataType))
Ejemplo n.º 60
-1
def updata(identity,path,base,oldFname,separator="\\"):
    listIm = paths.list_images(path)
    contai = 0
    for fileim in listIm:
        vf = fileim.split(separator)
        #print vf[len(vf)-1]
        if oldFname is not None:
            if fileim in oldFname:
                print "O arquivo {} tem duplicacao".format(fileim)
                continue
        #print "Processa {}".format(fileim)
        img = cv2.imread(fileim)
        mdb.gravaIm(img, base, contai, identity,vf[len(vf)-1], ".png")
        contai += 1
    return contai