# TODO: supply nms threshold and confidence for YOLO as script arguments object_detector = Yolo(args.object_detector) classifier = Classifier(args.feature_detector, args.feature_descriptor, args.feature_matcher, args.outlier_rejection_method) if args.image: if not isfile(args.image): raise ValueError( "The provided path to an image does not exist! " "Please, provide a path relative to the project:) " "The directory with test images is test_images/") image = cv.imread(args.image) # Image resizing is not necessary, # OpenCV's YOLO interface takes care of that. prepared_image = prepare_image(image.copy(), resize=False) img_for_viz = image.copy() object_detector.load_data(prepared_image) layer_outputs = object_detector.detect() bboxes = object_detector.process_outputs(layer_outputs) if args.use_classification == "yes": img_for_viz = draw_bboxes_with_classes(img_for_viz, bboxes, classifier) else: img_for_viz = draw_bboxes_with_classes(img_for_viz, bboxes, None) cv.imshow("Detections", img_for_viz) cv.waitKey(0) if args.video: if args.video == "real-time":
import os import cv2 as cv from os.path import join from src.classifier import Classifier from src.inference import draw_bboxes_with_classes from src.preprocessing import prepare_image, extract_detection, IMG_WIDTH_SIZE from util.constants import TEST_IMG_PATH, ORB, YOLO_CONFIDENCE, NMS_THRESHOLD from src.object_detector import Yolo TEMPLATE_IMGS = ["1uah.jpg", "2uah.jpg", "5uah.jpg", "10uah.jpg"] if __name__ == '__main__': for test_image in os.listdir(join(TEST_IMG_PATH, "5uah_heads")): yolo = Yolo(confidence=YOLO_CONFIDENCE, nms_threshold=NMS_THRESHOLD) yolo.load_model() image = cv.imread(join(TEST_IMG_PATH, "5uah_heads", test_image)) prepared_image = prepare_image(image.copy()) img_for_viz = image.copy() # prepared_image = image yolo.load_data(prepared_image) layer_outputs = yolo.detect() bboxes = yolo.process_outputs(layer_outputs) clf = Classifier(ORB) draw_bboxes_with_classes(img_for_viz, bboxes, clf) cv.imshow("Detections", img_for_viz) cv.waitKey(0) cv.destroyAllWindows()
import numpy as np from os.path import join from time import time import cv2 as cv from cv2.xfeatures2d import matchGMS from src.preprocessing import prepare_image from util.constants import DATA_PATH, TEST_IMG_PATH from util.utils import INDEX_PARAMS, SEARCH_PARAMS if __name__ == '__main__': img1 = cv.imread(join(DATA_PATH, "1uah_heads.jpg")) img1 = prepare_image(img1, True) # img1 = cv.imread(join(DATA_PATH, "5uah_heads.jpg")) img2 = cv.imread(join(TEST_IMG_PATH, "1uah_heads", "bottom.jpeg")) img2 = prepare_image(img2, True) # img2 = cv.imread(join(TEST_IMG_PATH, "other", "test4.jpg")) detector_descriptor = cv.ORB_create(10000) keypoints1, descriptions1 = detector_descriptor.detectAndCompute( img1, None) keypoints2, descriptions2 = detector_descriptor.detectAndCompute( img2, None) bf = cv.BFMatcher_create(cv.NORM_HAMMING, True) start = time() bf_matches = bf.match(descriptions1, descriptions2) end = time()
test_images = [[cv.imread(join(TEST_IMG_PATH, cls_name, f"{test_img_name}.jpeg")) for test_img_name in TEST_IMG_NAMES] for cls_name in CLASS_NAMES] for detector_name in DETECTOR_NAMES: for descriptor_name in DESCRIPTOR_NAMES: if corner_case(detector_name, descriptor_name): continue detector_descriptor = DetectorDescriptor(detector_name, descriptor_name) # matcher = cv.BFMatcher_create() if descriptor_name in [SIFT, ROOT_SIFT] \ # else cv.BFMatcher_create(cv.NORM_HAMMING, True) detector_descriptor_score = 0 total_matching_time = [] final_matches_cnt = [] for cls_idx, class_tests in enumerate(test_images): for test_img in class_tests: prepared_test_img = prepare_image(test_img.copy()) print(f"Test image shape: {prepared_test_img.shape}") test_img_description_time_begin = time() # TODO: resolve issue with SIFT_SIFT. test_keypoints, test_descriptions =\ detector_descriptor.detect_describe(prepared_test_img) test_img_description_time_end = time() test_img_description_time = test_img_description_time_end - test_img_description_time_begin matches_counts = [] for template_img in tqdm(template_images, desc="Matching images"): print(detector_name, descriptor_name) prepared_template_img = prepare_image(template_img.copy()) print(f"Template image shape: {prepared_template_img.shape}") template_img_description_time_begin = time() template_keypoints, template_descriptions =\
"""Generate keypoints and descriptions for template images and dump them.""" import cv2 as cv import os from os.path import join from tqdm import tqdm from src.classifier import Classifier from src.preprocessing import prepare_image from util.utils import CLASS_NAMES, FEATURE_DETECTORS, FEATURE_DESCRIPTORS, corner_case from util.constants import DATA_PATH if __name__ == '__main__': template_images = [(name, prepare_image(cv.imread(join(DATA_PATH, f"{name}.jpg")))) for name in CLASS_NAMES] for detector in FEATURE_DETECTORS: for descriptor in FEATURE_DESCRIPTORS: if corner_case(detector, descriptor): continue clf = Classifier(detector, descriptor) for cls_name, image in tqdm( template_images, desc=f"Dumping features for {detector}_{descriptor}"): clf.dump_features(cls_name, image)
import cv2 as cv from os.path import join from src.preprocessing import prepare_image from util.constants import TEST_IMG_PATH if __name__ == '__main__': original_image = cv.imread(join(TEST_IMG_PATH, "other", "1_2_5_10_uah_heads.jpg")) cv.imshow("Original image", original_image) processed_image = prepare_image(original_image) cv.imshow("Processed image", processed_image) cv.waitKey(0) cv.destroyAllWindows()
import time import cv2 from cv2.xfeatures2d import matchGMS from os.path import join from src.detector_descriptor import RootSift from src.preprocessing import prepare_image from util.constants import DATA_PATH, TEST_IMG_PATH if __name__ == '__main__': img1 = cv2.imread(join(DATA_PATH, "1uah_heads.jpg")) img1 = prepare_image(img1, resize=True) # img1 = cv2.imread(join(DATA_PATH, "5uah_heads.jpg")) img2 = cv2.imread(join(TEST_IMG_PATH, "1uah_heads", "bottom.jpeg")) img2 = prepare_image(img2, resize=True) # img2 = cv2.imread(join(TEST_IMG_PATH, "other", "test4.jpg")) # detector_descriptor = cv2.ORB_create(10000, fastThreshold=0) # detector_descriptor = cv2.KAZE_create() detector_descriptor = cv2.AKAZE_create() # detector_descriptor = cv2.BRISK_create() kp1, des1 = detector_descriptor.detectAndCompute(img1, None) print(f"Template image {len(kp1)} keypoints") kp2, des2 = detector_descriptor.detectAndCompute(img2, None) print(f"Input image {len(kp2)} keypoints") matcher = cv2.BFMatcher(cv2.NORM_HAMMING, crossCheck=True) matches = matcher.match(des1, des2) print(f"Matches {len(matches)}") start = time.time()