def add_2d_detection(img, dets, expected_cls, show_txt=True, center_thresh=0.5): right_label = 0 wrong_labels = 0 for cat in dets: for i in range(len(dets[cat])): if dets[cat][i, -1] > center_thresh: if cat - 1 == expected_cls: right_label += 1 else: wrong_labels += 1 bbox = dets[cat][i, :4] add_bbox( img, [int(bbox[0]), int(bbox[1]), int(bbox[2]), int(bbox[3])], cat - 1, ['ov', 'mif'], dets[cat][i, -1], show_txt=show_txt) if right_label > 0 and wrong_labels == 0: score = 1 elif right_label == 0 and wrong_labels > 0: score = 2 else: score = 0 if right_label + wrong_labels > 0: score = 3 return img, score
for box, score, label in zip(boxes[0], scores[0], labels[0]): # scores are sorted so we can break if score < 0.5: break # color = label_color(label) if (label == 0 and 'mif' in pth.lower()) or (label == 1 and 'ov' in pth.lower()): right_label.append(label) else: wrong_labels.append(label) b = box.astype(int) # draw_box(draw, b, color=[(255, 0, 0), (0, 255, 0)][label]) draw = add_bbox(draw, b, label, labels_to_names, score) caption = "{} {:.3f}".format(labels_to_names[label], score) # draw_caption(draw, b, caption) if len(wrong_labels) == 0 and len(right_label) > 0: flag = 'correct' elif len(wrong_labels) > 0 and len(right_label) > 0: flag = 'mixed' elif len(wrong_labels) == 0 and len(right_label) == 0: flag = 'not_found' else: flag = 'wrong' img = Image.fromarray(draw) img.save( os.path.join(
# scores are sorted so we can break if score < 0.5: break b = box.astype(int) minimum = (float('inf'), 0) with torch.no_grad(): target_image_ori = pad(draw[b[1]:b[3], b[0]:b[2]], b) target_image_ori = Image.fromarray(target_image_ori[..., ::-1]) target_image = transform(target_image_ori) x = torch.zeros((1, 3, 224, 224)) x[0] = target_image target_features = model.model._forward_impl(x.cuda()) for query_folder in os.listdir(query_path): for query_image_path in os.listdir(os.path.join(query_path, query_folder)): query = os.path.join(query_path, query_folder, query_image_path) cache_dict, query_features = memory_cache(cache_dict, model.model, query, os.path.join(cache_path, query_folder, query_image_path + '.pth'), transform) y = LSHash.euclidean_dist(target_features.cpu().numpy()[0], query_features.cpu().numpy()[0]) if y < minimum[0]: minimum = (y, query_folder) if minimum[0] > 1: minimum = (minimum[0], 'obj') else: if minimum[1] in names_to_labels: label = names_to_labels[minimum[1]] draw = add_bbox(draw, b, label, labels_to_names, score) else: draw = add_bbox(draw, b, 0, [minimum[1]], score) os.makedirs(dst, exist_ok=True) cv2.imwrite(os.path.join(dst, i), draw)
color = [(0, 0, 255), (255, 0, 0) ][classes[i] == int('mif' in d["filename"].lower())] if 'mif' in d["filename"].lower(): if classes[i] == 1: color = (255, 0, 0) else: color = (0, 0, 255) else: if classes[i] == 0: color = (255, 0, 0) else: color = (0, 0, 255) im = add_bbox(im, bboxes[i].astype('uint16'), classes[i], ['ov', 'mif'], scores[i], show_txt=True, color=color) for i in range(len(d['object'])): box = [ d['object'][i]['xmin'], d['object'][i]['ymin'], d['object'][i]['xmax'], d['object'][i]['ymax'] ] im = add_bbox(im, box, 2, ['ov', 'mif', 'obj'], show_txt=False, color=(0, 200, 0)) cv2.imwrite( os.path.join(
import cv2 import csv from retinanet.preprocessing.csv_generator import _read_annotations, _open_for_csv, _read_classes from boxutils import add_bbox if __name__ == '__main__': with _open_for_csv( '/home/palm/PycharmProjects/algea/dataset/classes') as file: classes = _read_classes(csv.reader(file, delimiter=',')) with _open_for_csv( '/home/palm/PycharmProjects/algea/dataset/train_annotations' ) as file: train_image_data = _read_annotations(csv.reader(file, delimiter=','), classes) with _open_for_csv( '/home/palm/PycharmProjects/algea/dataset/test_annotations' ) as file: test_image_data = _read_annotations(csv.reader(file, delimiter=','), classes) for k in train_image_data: anns = train_image_data[k] image = cv2.imread(k) for ann in anns: bbox = (ann['x1'], ann['y1'], ann['x2'], ann['y2']) image = add_bbox(image, bbox, classes[ann['class']], ['mif', 'ov']) cv2.imwrite( f'/home/palm/PycharmProjects/algea/dataset/plotted/{k.split("/")[-1]}', image)