Example #1
0
def task1(img_path):
    print("calculating features for {}".format(img_path))
    features = extract_img_features(img_path)
    if features:
        color_moments = features[2]
        mean = ""
        std = ""
        skew = ""
        i = 1
        for color_moment in color_moments:
            char = "  "
            if i % 12 == 0:
                char = "\n"
            mean += "["
            mean += ",".join(["{:5.1f}".format(vector)
                              for vector in color_moment.get("mean")])
            mean += "]" + char

            std += "["
            std += ",".join(["{:5.1f}".format(vector)
                              for vector in color_moment.get("std")])
            std += "]" + char

            skew += "["
            skew += ",".join(["{:5.1f}".format(vector)
                              for vector in color_moment.get("skew")])
            skew += "]" + char
            i += 1

        keypoints = ""
        for kp in features[0]:
            keypoints += "["
            keypoints += ",".join(["{:5.1f}".format(coordinate)
                                   for coordinate in kp.pt])
            keypoints += "{:4.2f}, {:4.2f}]  ".format(kp.size, kp.angle)

        print("\n--------------------First Color Moment--------------------")
        print(mean)
        print("\n--------------------Second Color Moment--------------------")
        print(std)
        print("\n--------------------Third Color Moment--------------------")
        print(skew)
        print("\n--------------------SIFT Features--------------------")
        print(keypoints)
    else:
        print("something went wrong")
Example #2
0
def get_caption(encoder, caption_model, image_fullpath, model_info, device):
    """
    Function to take the user uploaded image and run the model on it

    Parameters:
    _______________
    encoder: CNNModel from Model
        the encoder
    caption_model: torch model
        the captioning model
    image_fullpath:
        path of the image from view.py -- sys.argv[2] "second variable passed in"
    model_info:
        from "{DATA_PATH}/results/model_info.json"
    device:
        torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
    """
    img_feature = extract_img_features([image_fullpath], encoder, device)
    results = {}
    results[image_name] = generate_caption(caption_model, img_feature,
                                           model_info['max_length'],
                                           model_info['vocab_size'],
                                           model_info['wordtoidx'],
                                           model_info['idxtoword'], device)

    try:
        with open(f"{DATA_PATH}/raw/upload_model_caption.json", 'r') as fp:
            single_captions = json.load(fp)
        single_captions.update(results)
    except:
        single_captions = results

    with open(f"{DATA_PATH}/raw/upload_model_caption.json", 'w') as fp:
        json.dump(single_captions, fp)

    with open(f"{BASE_DIR}/mda_mds/image_name.json", 'w') as fp:
        json.dump({'image_name': image_name}, fp)

    return ['NA', results[image_name]]
def find_best_sift_image(base_img, images, k):
    """
    Given a base image, find the k most similar images based on the color
    moment specified
    :param base_img: base image we want to compare to
    :param images: our list of images we are comparing against
    :param k: k most similar images
    :return:
    """
    try:
        base_image_features = extract_img_features(base_img)
    except Exception:
        raise Exception("Could not find image. "
                        "Please make sure the image path is correct")
    base_image_obj = cv2.imread(base_img)
    sift_avgs = []
    for image in images:
        if image[3] == base_img:
            continue
        avg_dist, matches = compare_sifts(base_image_features[1], image[1],
                                          base_image_features[0], image[0])
        sift_avgs.append((avg_dist, matches, image))
    sift_avgs.sort(key=operator.itemgetter(0), reverse=True)

    rank = 1
    for sift_avg in sift_avgs[:k]:
        image_to_compare = cv2.imread(sift_avg[2][3])
        result = cv2.drawMatches(base_image_obj, base_image_features[0],
                                 image_to_compare, sift_avg[2][0], sift_avg[1],
                                 None)
        result = cv2.resize(result, (800, 300))
        cv2.imshow("RANK {}: {}".format(rank, sift_avg[2][3]), result)
        print("RANK {}: {} with a matching score of {:4.2f}%".format(
            rank, sift_avg[2][3], sift_avg[0]))
        rank += 1

    print("Press 0 on a window to close all images")
    cv2.waitKey(0)
    cv2.destroyAllWindows()
def find_best_color_moment_image(base_img, images, k, feature):
    """
    Given a base image, find the k most similar images based on the color
    moment specified
    :param base_img: base image we want to compare to
    :param images: our list of images we are comparing against
    :param k: k most similar images
    :param feature: color moment feature: mean, std, or skew
    :return:
    """
    try:
        base_image_features = extract_img_features(base_img)
    except Exception:
        raise Exception(
            "Could not find image. "
            "Please make sure the image id is correct ie. Hand_0000002")
    color_moment_avgs = []
    for image in images:
        if image[3] == base_img:
            continue
        color_moment_avgs.append(
            (compare_color_moments(base_image_features[2], image[2],
                                   feature), image[3]))
    color_moment_avgs.sort(key=operator.itemgetter(0))

    rank = 1
    for color_moment in color_moment_avgs[:k]:
        result = cv2.imread(color_moment[1])
        result = cv2.resize(result, (400, 300))
        cv2.imshow("RANK {}: {}".format(rank, color_moment[1]), result)
        print("RANK {}: {} with an distance of {:4.2f}".format(
            rank, color_moment[1], color_moment[0]))
        rank += 1
    print("Press 0 on a window to close all images")
    cv2.waitKey(0)
    cv2.destroyAllWindows()
Example #5
0
    def detect_cells(self):

        #Время начала выполнения функции
        t1 = time.time()

        #Загрузка картинки
        loaded_img = io.imread(self.image_fname, as_grey=False)

        #Очистить изображение интерфейса для создания нового
        self.plot.clf()

        #Добавить plot
        sub_plot = self.plot.add_subplot(111)

        pos = 0

        #Выставить 0 в очередь состояния прогресса
        self.queue_progress.put(0)

        #Кол-во "скользящих окон" в изображении
        total_wds = 759

        # If the width or height of the scaled image is less than
        # the width or height of the window, then end the iterations.
        for (x, y, im_window) in self.sliding_window(loaded_img):
            if im_window.shape[0] != self.min_wdw_sz[1] or im_window.shape[
                    1] != self.min_wdw_sz[0]:
                continue

            #Получить основные признаки изображение
            fd = extract_img_features(im_window)

            #Необходимо для классификатора
            fd = [fd]

            #Спрогнозировать объект находящийся в текущем скользящем окне
            pred = self.clf.predict(fd)
            #Вероятность прогнозирования
            predict_probability = np.amax(self.clf.predict_proba(fd))

            #Если объект не "не клетка", то окно обнаружило клетку
            if pred[0] != 'Non_cell':
                #print('DETECTED '+pred[0])
                self.detections.append(
                    (x, y, np.amax(predict_probability), self.min_wdw_sz[0],
                     self.min_wdw_sz[1], pred[0]))

            #Для прогресса загрузки
            pos = pos + 1

            #Подсчет прогресса
            progress_old = self.progress
            self.progress = round((pos * 100) / total_wds)

            #Выставить новое значение прогресса
            if progress_old != self.progress:
                self.queue_progress.put(self.progress)

        self.queue_progress.put(100)

        #Время завершения скрипта
        t2 = time.time()

        print('Exec duration: ', (t2 - t1))

        # Perform Non Maxima Suppression
        self.detections = nms(self.detections, 0.2)

        cell_color = 'red'

        for (x_tl, y_tl, _, w, h, cell_label) in self.detections:

            # Draw the detections
            if cell_label == "RBC":
                cell_color = 'red'
            elif cell_label == "WBC":
                cell_color = 'blue'
            elif cell_label == "Platelet":
                cell_color = 'green'

            #Нарисовать прямоугольник
            rect = mpatches.Rectangle((x_tl, y_tl),
                                      w,
                                      h,
                                      fill=False,
                                      edgecolor=cell_color,
                                      linewidth=1.2)
            sub_plot.add_patch(rect)
            #Добавить имя клетки
            sub_plot.text(x_tl, y_tl - 5, cell_label)

        #Показать обработанное изображение
        sub_plot.imshow(loaded_img)

        #Обновить канвас
        self.canvas.draw()
Example #6
0
    def detect_cells(self):

        t1 = time.time()

        self.plot.clf()
        sub_plot = self.plot.add_subplot(111)

        pos = 0

        self.queue_progress.put(0)

        #Общее кол-во файлов в папке
        path, dirs, files = next(os.walk(self.folder_path))
        file_count = len(files)

        #Пробежать по всем файлам заданной папки
        for file in os.listdir(self.folder_path):

            #Если файл не .jpg или .jpeg, то перейти к следуюшему
            if not (file.endswith('.jpg') or file.endswith('.jpeg')):
                continue

            #Загр файл
            loaded_img = io.imread(self.folder_path + '/' + file,
                                   as_grey=False)

            for (x, y, im_window) in self.sliding_window(loaded_img):
                if im_window.shape[0] != self.min_wdw_sz[1] or im_window.shape[
                        1] != self.min_wdw_sz[0]:
                    continue

                fd = extract_img_features(im_window)

                fd = [fd]

                pred = self.clf.predict(fd)
                predict_probability = np.amax(self.clf.predict_proba(fd))

                if pred[0] != 'Non_cell':
                    #print('DETECTED '+pred[0])
                    self.detections.append(
                        (x, y, np.amax(predict_probability),
                         self.min_wdw_sz[0], self.min_wdw_sz[1], pred[0]))

            # Perform Non Maxima Suppression
            self.detections = nms(self.detections, 0.2)

            for det in self.detections:
                if det[5] == 'Platelet':
                    self.platelet_counter = self.platelet_counter + 1
                elif det[5] == 'WBC':
                    self.wbc_counter = self.wbc_counter + 1
                else:
                    self.rbc_counter = self.rbc_counter + 1

            pos = pos + 1

            progress_old = self.progress
            self.progress = round((pos * 100) / file_count)

            if progress_old != self.progress:
                self.queue_progress.put(self.progress)

        self.queue_progress.put(100)

        t2 = time.time()

        print('Exec duration: ', (t2 - t1))

        #Рисование диаграммы кол-ва найденных клеток
        cell_types = ('RBC', 'WBC', 'Platelet')
        y_pos = np.arange(len(cell_types))
        performance = [
            self.rbc_counter, self.wbc_counter, self.platelet_counter
        ]

        sub_plot.bar(y_pos,
                     performance,
                     color=("red", "blue", "green"),
                     alpha=0.5,
                     width=0.7)
        sub_plot.set_xticks(y_pos)
        sub_plot.set_xticklabels(cell_types)

        sub_plot.set_ylabel('Счетчик')
        sub_plot.set_title('Количество найденных клеток')

        self.canvas.draw()
Xlist = []
Ylist = []

#Инит. счетчиков
wbc_counter = 0
rbc_counter = 0
platelet_counter = 0

#Извлекаем все признаки картинок RBC
for file in os.listdir(rbc_path):
    print(rbc_path + file)

    if file.endswith('.jpg'):
        cur_img = io.imread(rbc_path + file)

        fd = extract_img_features(cur_img)

        Xlist.append(fd)
        Ylist.append('RBC')

#Извлекаем все признаки картинок WBC
for file in os.listdir(wbc_path):
    print(wbc_path + file)

    if file.endswith('.jpg'):
        cur_img = io.imread(wbc_path + file)

        fd = extract_img_features(cur_img)

        Xlist.append(fd)
        Ylist.append('WBC')
t1 = time.time()

# Load the classifier
clf = joblib.load('training/bccd_model')

# List to store the detections
detections = []
img_heatmap = []

# If the width or height of the scaled image is less than
# the width or height of the window, then end the iterations.
for (x, y, im_window) in sliding_window(image, min_wdw_sz, step_size):
    if im_window.shape[0] != min_wdw_sz[1] or im_window.shape[1] != min_wdw_sz[0]:
        continue

    fd = extract_img_features(im_window)

    fd = [fd]

    pred = clf.predict(fd)
    predict_probability = np.amax(clf.predict_proba(fd))

    if pred[0] != 'Non_cell':
        print('DETECTION!', pred[0])

        if pred[0] == 'WBC':
            cell_color = 'blue'
        elif pred[0] == 'RBC':
            cell_color = 'red'
        else:
            cell_color = 'green'