コード例 #1
0
class FaceDetectionArgs:

    CASCADE_FILE = "lbpcascade_animeface.xml"

    PPR_IMG_FUNC_LIST = [
        to_gray, lambda img: _apply_clahe(to_gray(img)),
        lambda img: eqlHist(to_gray(img)),
        lambda img: eqlHist(_apply_clahe(to_gray(img))),
        lambda img: _apply_clahe(eqlHist(to_gray(img)))
    ]  # image-preprocessing function list for face detection.

    CLAHE = {"clipLimit": 2.0, "tileGridSize": (8, 8)}

    MIN_FACE_PER_SL_RATIO = 1.0 / 16  #minium of face per side length ratio.

    dMultiScale = {
        #"scaleFactor" : ,
        #"minNeighbors" : ,
        #"flags" : ,
        #"minSize" : (8,8),
        #"maxSize" :
    }

    class Elbow:
        THR_IS_ELBOW = 1  # use initial threshold cost ratio to identify elbow.
        ARGMIN_IS_ELBOW = 2  # use argmin of log decay rate to identify elbow.

        INI_THR_COST_RATIO = 1.0  #initial threshold cost ratio when num of faces = 2
        #THR_COST_V_RATIO = 0.25 #threshold cost decay velocity ratio.
        ELBOW_SEARCH_METHOD = 1  # which is used to find elbow?

        KM_KWARGS = {'max_iter': 500}
        FACTOR_FUNC = lambda face_rectangle: np.min(face_rectangle[2:])
        DATA_MAP_FUNC = lambda face_rectangle: np.array(face_rectangle[
            0:2] + face_rectangle[2:] // 2)
コード例 #2
0
def chargement_base_image(base_images, label):
    matrice_base_images = []
    taille = len(base_images)
    for i in range(taille):
        image = uts.read_image(base_images[i])
        if label:
            image = uts.to_gray(np.asarray(image))
        matrice_base_images.append({'nom': base_images[i], 'matrice': image})

    return matrice_base_images
コード例 #3
0
def gen_sift_features(img_paths):
    img_keypoints = {}
    img_descs = []
    tot_desc = 0
    print("\\** Generation des descripteurs SIFT pour %i images **\n" %
          len(img_paths))
    for img_path in img_paths:
        img = uts.read_image(img_path)
        # conversion de l'image en niveau de gris
        gray_img = uts.to_gray(img)
        sift = cv2.xfeatures2d.SIFT_create()
        kp, desc = sift.detectAndCompute(gray_img, None)
        img_keypoints[img_path] = kp
        img_descs.append(desc)
        tot_desc += len(desc)

    print("** %i Descripteurs SIFT generes **\n" % tot_desc)

    return img_descs
コード例 #4
0
def calcul_distance_hu_base_images(base_images_gris, image_requete):
    tableau_distance_hu = []

    matrice_image_requete = uts.read_image(image_requete)
    matrice_base_images_gris = chargement_base_image(base_images_gris,
                                                     label=True)
    taille = len(matrice_base_images_gris)
    matrice_image_requete_gris = uts.to_gray(matrice_image_requete)

    for i in range(taille):
        distance = distance_moments_hu_img(
            matrice_base_images_gris[i]['matrice'], matrice_image_requete_gris)
        tableau_distance_hu.append({
            'nom': matrice_base_images_gris[i]['nom'],
            'distance': distance
        })

    tableau_distance_hu = sorted(tableau_distance_hu,
                                 key=lambda dct: dct['distance'])

    return tableau_distance_hu
コード例 #5
0
def img_to_hist(img_path, cluster_model, X_train):
    print("\n** Classification de l'image %s **" %
          (os.path.basename(img_path)))
    img = uts.read_image(img_path)
    gray = uts.to_gray(img)
    sift = cv2.xfeatures2d.SIFT_create()
    kp, desc = sift.detectAndCompute(gray, None)

    clustered_desc = cluster_model.predict(desc)

    accuracy = metrics.accuracy_score(clustered_desc, X_train)
    print("Precision de classification: %s" % "{0:.3%}".format(accuracy))

    #score_ = cluster_model.score(desc)
    #print("Precision de classification: %2f" % (score_))

    # frequence d'apparition des mots dans le dictionnaire
    img_bow_hist = np.bincount(clustered_desc,
                               minlength=cluster_model.n_clusters)

    # l'histogramme de l'image classifiee a partir du modele des mots visuels
    return img_bow_hist.reshape(1, -1)
コード例 #6
0
def encode_4_patches(image, colons,
                     p1=torch.zeros([BATCH_SIZE_DEFAULT, NUMBER_CLASSES]),
                     p2=torch.zeros([BATCH_SIZE_DEFAULT, NUMBER_CLASSES]),
                     p3=torch.zeros([BATCH_SIZE_DEFAULT, NUMBER_CLASSES]),
                     p4=torch.zeros([BATCH_SIZE_DEFAULT, NUMBER_CLASSES]),
                     p5=torch.zeros([BATCH_SIZE_DEFAULT, NUMBER_CLASSES]),
                     p6=torch.zeros([BATCH_SIZE_DEFAULT, NUMBER_CLASSES]),
                     p7=torch.zeros([BATCH_SIZE_DEFAULT, NUMBER_CLASSES]),
                     p8=torch.zeros([BATCH_SIZE_DEFAULT, NUMBER_CLASSES]),
                     p9=torch.zeros([BATCH_SIZE_DEFAULT, NUMBER_CLASSES]),
                     p0=torch.zeros([BATCH_SIZE_DEFAULT, NUMBER_CLASSES]),
                     ):

    #split_at_pixel = 19

    # image = np.reshape(image, (BATCH_SIZE_DEFAULT, 1, 28, 28))
    # image = torch.FloatTensor(image)

    # print(image.shape)
    # print(image.shape[2])
    # print(image.shape[3])
    # input()
    width = image.shape[2]
    height = image.shape[3]

    split_at_pixel = 50

    image = image[:, :, 20:70, 20:70]

    augments = {0: to_gray(image, BATCH_SIZE_DEFAULT),
                1: rotate(image, 20, BATCH_SIZE_DEFAULT),
                2: rotate(image, -20, BATCH_SIZE_DEFAULT),
                3: scale(image, 40, 5, BATCH_SIZE_DEFAULT),
                #3: scale(image, 32, 4, BATCH_SIZE_DEFAULT),
                4: vertical_flip(image, BATCH_SIZE_DEFAULT),
                5: scale(image, 30, 10, BATCH_SIZE_DEFAULT),
                #5: scale(image, 24, 8, BATCH_SIZE_DEFAULT),
                6: random_erease(image, BATCH_SIZE_DEFAULT),
                7: vertical_flip(image, BATCH_SIZE_DEFAULT)}

    ids = np.random.choice(len(augments), size=1, replace=False)

    image_2 = augments[ids[0]]

    # image = torch.transpose(image, 1, 3)
    # show_mnist(image[0], image[0].shape[1], image[0].shape[2])
    # image = torch.transpose(image, 1, 3)
    #
    # image_2 = torch.transpose(image_2, 1, 3)
    # show_mnist(image_2[0], image_2[0].shape[1], image_2[0].shape[2])
    # image_2 = torch.transpose(image_2, 1, 3)
    #
    # image_3 = torch.transpose(image_3, 1, 3)
    # show_mnist(image_3[0], image_3[0].shape[1], image_3[0].shape[2])
    # image_3 = torch.transpose(image_3, 1, 3)
    #
    # image_1 = torch.transpose(image_1, 1, 3)
    # show_mnist(image_1[0], image_1[0].shape[1], image_1[0].shape[2])
    # image_1 = torch.transpose(image_1, 1, 3)

    image = image.to('cuda')
    image_2 = image_2.to('cuda')

    p1 = p1.cuda()
    p2 = p2.cuda()
    p3 = p3.cuda()
    p4 = p4.cuda()
    p5 = p5.cuda()
    p6 = p6.cuda()
    p7 = p7.cuda()
    p8 = p8.cuda()
    p9 = p9.cuda()
    p0 = p0.cuda()

    new_preds = []
    for idx, colon in enumerate(colons):
        # colons[idx] = colons[idx].to('cuda')

        pred_1 = colons[idx](image, p1, p2, p3, p4, p5, p6, p7, p8, p9, p0)
        pred_2 = colons[idx](image_2, p1, p2, p3, p4, p5, p6, p7, p8, p9, p0)
        pred = (pred_1 + pred_2)/2

        new_preds.append(pred.to('cpu'))

    products = []
    for prod in range(10):
        product = torch.ones([BATCH_SIZE_DEFAULT, 1])

        for idx, prediction in enumerate(new_preds):
            if idx != prod:
                product *= torch.ones(prediction.shape) - prediction
            else:
                product *= prediction

        products.append(product)



    # print(len(products))
    # print(products[0])
    # print(products[0].shape)
    #
    # input()
    # image_1 = image
    # image_2 = random_erease(image, BATCH_SIZE_DEFAULT)
    # image_2 = torch.transpose(image_2, 1, 3)
    # print(image_2.shape)
    # show_mnist(image_2[0], image_2[0].shape[1], image_2[0].shape[2])
    #
    # image_3 = scale(image, BATCH_SIZE_DEFAULT)
    # show_mnist(image_3[0], image_3[0].shape[1], image_3[0].shape[2])
    #
    # image_4 = random_erease(image, BATCH_SIZE_DEFAULT)
    # show_mnist(image_4[0], image_4.shape[1], image_4.shape[2])


    return products
コード例 #7
0
def encode_4_patches(image, colons):

    #split_at_pixel = 19

    # image = np.reshape(image, (BATCH_SIZE_DEFAULT, 1, 28, 28))
    # image = torch.FloatTensor(image)

    # print(image.shape)
    # print(image.shape[2])
    # print(image.shape[3])
    # input()
    width = image.shape[2]
    height = image.shape[3]

    split_at_pixel = 50

    image = image[:, :, 20:70, 20:70]

    augments = {
        0: to_gray(image, BATCH_SIZE_DEFAULT),
        1: rotate(image, 20, BATCH_SIZE_DEFAULT),
        2: rotate(image, -20, BATCH_SIZE_DEFAULT),
        3: scale(image, 40, 5, BATCH_SIZE_DEFAULT),
        #3: scale(image, 32, 4, BATCH_SIZE_DEFAULT),
        4: vertical_flip(image, BATCH_SIZE_DEFAULT),
        5: scale(image, 30, 10, BATCH_SIZE_DEFAULT),
        #5: scale(image, 24, 8, BATCH_SIZE_DEFAULT),
        6: random_erease(image, BATCH_SIZE_DEFAULT),
        7: vertical_flip(image, BATCH_SIZE_DEFAULT)
    }

    ids = np.random.choice(len(augments), size=1, replace=False)

    image_2 = augments[ids[0]]

    # image = torch.transpose(image, 1, 3)
    # show_mnist(image[0], image[0].shape[1], image[0].shape[2])
    # image = torch.transpose(image, 1, 3)
    #
    # image_2 = torch.transpose(image_2, 1, 3)
    # show_mnist(image_2[0], image_2[0].shape[1], image_2[0].shape[2])
    # image_2 = torch.transpose(image_2, 1, 3)
    #
    # image_3 = torch.transpose(image_3, 1, 3)
    # show_mnist(image_3[0], image_3[0].shape[1], image_3[0].shape[2])
    # image_3 = torch.transpose(image_3, 1, 3)
    #
    # image_1 = torch.transpose(image_1, 1, 3)
    # show_mnist(image_1[0], image_1[0].shape[1], image_1[0].shape[2])
    # image_1 = torch.transpose(image_1, 1, 3)

    image = image.to('cuda')
    image_2 = image_2.to('cuda')

    output, c1, c2, c3, c4, c5, c6, c7, c8, c9, c0 = colons[0](image)
    output, a_c1, a_c2, a_c3, a_c4, a_c5, a_c6, a_c7, a_c8, a_c9, a_c0 = colons[
        0](image_2)

    comb1 = (c1 + a_c1) / 2
    comb2 = (c2 + a_c2) / 2
    comb3 = (c3 + a_c3) / 2
    comb4 = (c4 + a_c4) / 2
    comb5 = (c5 + a_c5) / 2
    comb6 = (c6 + a_c6) / 2
    comb7 = (c7 + a_c7) / 2
    comb8 = (c8 + a_c8) / 2
    comb9 = (c9 + a_c9) / 2
    comb0 = (c0 + a_c0) / 2

    new_preds = [
        comb1.to('cpu'),
        comb2.to('cpu'),
        comb3.to('cpu'),
        comb4.to('cpu'),
        comb5.to('cpu'),
        comb6.to('cpu'),
        comb7.to('cpu'),
        comb8.to('cpu'),
        comb9.to('cpu'),
        comb0.to('cpu')
    ]

    products = []
    for prod in range(10):
        product = torch.ones([BATCH_SIZE_DEFAULT, 1])

        for idx, prediction in enumerate(new_preds):
            if idx != prod:
                product *= torch.ones(prediction.shape) - prediction
            else:
                product *= prediction

        products.append(product)

    # print(len(products))
    # print(products[0])
    # print(products[0].shape)
    #
    # input()
    # image_1 = image
    # image_2 = random_erease(image, BATCH_SIZE_DEFAULT)
    # image_2 = torch.transpose(image_2, 1, 3)
    # print(image_2.shape)
    # show_mnist(image_2[0], image_2[0].shape[1], image_2[0].shape[2])
    #
    # image_3 = scale(image, BATCH_SIZE_DEFAULT)
    # show_mnist(image_3[0], image_3[0].shape[1], image_3[0].shape[2])
    #
    # image_4 = random_erease(image, BATCH_SIZE_DEFAULT)
    # show_mnist(image_4[0], image_4.shape[1], image_4.shape[2])

    return products, new_preds
コード例 #8
0
def preproc(patch):
    patch = resize(patch)
    patch = to_gray(patch)
    return patch
コード例 #9
0
def encode_4_patches(
        image,
        colons,
        p1=torch.zeros([BATCH_SIZE_DEFAULT, NUMBER_CLASSES]),
        p2=torch.zeros([BATCH_SIZE_DEFAULT, NUMBER_CLASSES]),
        p3=torch.zeros([BATCH_SIZE_DEFAULT, NUMBER_CLASSES]),
        p4=torch.zeros([BATCH_SIZE_DEFAULT, NUMBER_CLASSES]),
):

    #split_at_pixel = 19

    # image = np.reshape(image, (BATCH_SIZE_DEFAULT, 1, 28, 28))
    # image = torch.FloatTensor(image)

    # print(image.shape)
    # print(image.shape[2])
    # print(image.shape[3])
    # input()
    width = image.shape[2]
    height = image.shape[3]

    split_at_pixel = 50

    image = image[:, :, 5:90, 5:90]
    image_1 = image[:, :, 0:split_at_pixel, 0:split_at_pixel]
    image_2 = image[:, :, 85 - split_at_pixel:, 0:split_at_pixel]
    image_3 = image[:, :, 0:split_at_pixel, 0:split_at_pixel]
    image_4 = image[:, :, 85 - split_at_pixel:, 85 - split_at_pixel:]

    patches = {0: image_1, 1: image_2, 2: image_3, 3: image_4}

    patch_ids = np.random.choice(len(patches), size=4, replace=False)

    augments = {
        0: to_gray(patches[patch_ids[0]], BATCH_SIZE_DEFAULT),
        1: rotate(patches[patch_ids[1]], 20, BATCH_SIZE_DEFAULT),
        2: rotate(patches[patch_ids[2]], -20, BATCH_SIZE_DEFAULT),
        3: scale(patches[patch_ids[3]], 40, 5, BATCH_SIZE_DEFAULT),
        4: vertical_flip(patches[patch_ids[0]], BATCH_SIZE_DEFAULT),
        5: scale(patches[patch_ids[1]], 30, 10, BATCH_SIZE_DEFAULT),
        6: random_erease(patches[patch_ids[2]], BATCH_SIZE_DEFAULT),
        7: patches[patch_ids[3]]
    }

    ids = np.random.choice(len(augments), size=4, replace=False)

    image_1 = augments[ids[0]]
    image_2 = augments[ids[1]]
    image_3 = augments[ids[2]]
    image_4 = augments[ids[3]]

    # image_4 = torch.transpose(image_4, 1, 3)
    # show_mnist(image_4[0], image_4[0].shape[1], image_4[0].shape[2])
    # image_4 = torch.transpose(image_4, 1, 3)
    #
    # image_2 = torch.transpose(image_2, 1, 3)
    # show_mnist(image_2[0], image_2[0].shape[1], image_2[0].shape[2])
    # image_2 = torch.transpose(image_2, 1, 3)
    #
    # image_3 = torch.transpose(image_3, 1, 3)
    # show_mnist(image_3[0], image_3[0].shape[1], image_3[0].shape[2])
    # image_3 = torch.transpose(image_3, 1, 3)
    #
    # image_1 = torch.transpose(image_1, 1, 3)
    # show_mnist(image_1[0], image_1[0].shape[1], image_1[0].shape[2])
    # image_1 = torch.transpose(image_1, 1, 3)

    # image_a, image_b = torch.split(image, width//2, dim=2)
    #
    # image_1, image_2 = torch.split(image_a, height//2, dim=3)
    # image_3, image_4 = torch.split(image_b, height//2, dim=3)

    images = [image_1, image_2, image_3, image_4]

    p1 = p1.to('cuda')
    p2 = p2.to('cuda')
    p3 = p3.to('cuda')
    p4 = p4.to('cuda')

    # image_1 = image
    # image_2 = random_erease(image, BATCH_SIZE_DEFAULT)
    # image_2 = torch.transpose(image_2, 1, 3)
    # print(image_2.shape)
    # show_mnist(image_2[0], image_2[0].shape[1], image_2[0].shape[2])
    #
    # image_3 = scale(image, BATCH_SIZE_DEFAULT)
    # show_mnist(image_3[0], image_3[0].shape[1], image_3[0].shape[2])
    #
    # image_4 = random_erease(image, BATCH_SIZE_DEFAULT)
    # show_mnist(image_4[0], image_4.shape[1], image_4.shape[2])

    prod = torch.ones([BATCH_SIZE_DEFAULT, NUMBER_CLASSES])
    prev_preds = [p1, p2, p3, p4]
    preds = []

    z = images[0].to('cuda')
    pred = colons[0](z, p2, p3, p4)
    preds.append(pred)
    prod *= pred.to('cpu')

    z = images[1].to('cuda')
    pred = colons[0](z, p1, p3, p4)
    preds.append(pred)
    prod *= pred.to('cpu')

    z = images[2].to('cuda')
    pred = colons[0](z, p1, p2, p4)
    preds.append(pred)
    prod *= pred.to('cpu')

    z = images[3].to('cuda')
    pred = colons[0](z, p1, p2, p3)
    preds.append(pred)
    prod *= pred.to('cpu')

    # for idx, i in enumerate(images):
    #     z = i.to('cuda')
    #
    #     pred = colons[0](z,)
    #
    #     preds.append(pred)
    #     prod *= pred.to('cpu')

    return prod, preds