diff --git a/README.md b/README.md index 29f68e9..ed4780f 100644 --- a/README.md +++ b/README.md @@ -42,7 +42,7 @@ index.createIndex() # global: Overall similarity using single feature space on the whole image. similar = index.knnQuery('path/to/query/image', k=10, policy='object') ``` - +For detailed usage see [`examples/index.py`](examples/index.py) ## Credit ### YOLOv3: An Incremental Improvement diff --git a/examples/index.py b/examples/index.py new file mode 100644 index 0000000..d035d8f --- /dev/null +++ b/examples/index.py @@ -0,0 +1,78 @@ +import cv2 +import glob +import os +import sys + +import imsearch + + +def create_index(name, images): + # Initialize the index + index = imsearch.init(name) + + # Clear the index and data if any exists + index.cleanIndex() + + # Add single image to index (image path locally stored) + index.addImage(images[0]) + + # Add image using URL with same interface + index.addImage( + "https://www.wallpaperup.com/uploads/wallpapers/2014/04/14/332423/d5c09641cb3af3a18087937d55125ae3-700.jpg") + + # Add images in batch (List of image paths locally stored) + index.addImageBatch(images[1:]) + + return index + + +def create_index_with_config(name): + ''' + parameters: + name: name of the index (unique identifier name) + MONGO_URI + REDIS_URI + DETECTOR_MODE: select 'local' or 'remote'. + local: detector backend should be running on the same machine + remote: Process should not start detector backend + pass any configuration you want to expose as environment variable. + ''' + index = imsearch.init(name=name, + MONGO_URI='mongodb://localhost:27017/', + REDIS_URI='redis://dummy:Welcome00@123.456.78.111:6379/0', + DETECTOR_MODE='local') + + + +def show_results(similar, qImage): + qImage = imsearch.utils.load_image(qImage) + cv2.imshow('qImage', qImage) + for _i, _s in similar: + rImage = cv2.imread(_i['image']) + print([x['name'] for x in _i['primary']]) + print(_s) + cv2.imshow('rImage', rImage) + cv2.waitKey(0) + + +if __name__ == "__main__": + all_images = glob.glob(os.path.join( + os.path.dirname(__file__), '..', 'images/*.jpg')) + index = create_index('test', all_images[:25]) + + # query index with image path + ''' + image_path: path to image or URL + k: Number of results + policy: choose policy from 'object' or 'global'. Search results will change accordingly. + ''' + similar = index.knnQuery(image_path=all_images[25], k=10, policy='object') + show_results(similar, all_images[25]) + + # query with image URL + img_url = 'https://www.wallpaperup.com/uploads/wallpapers/2014/04/14/332423/d5c09641cb3af3a18087937d55125ae3-700.jpg' + similar = index.knnQuery(image_path=img_url, k=10, policy='global') + show_results(similar, img_url) + + # Create index with configuration + index = create_index_with_config('test') diff --git a/examples/yolo_detector.py b/examples/yolo_detector.py new file mode 100644 index 0000000..4c30c6c --- /dev/null +++ b/examples/yolo_detector.py @@ -0,0 +1,44 @@ +from imsearch.backend.object_detector.yolo import Detector +from imsearch import utils +from PIL import Image +import numpy as np + +import matplotlib.pyplot as plt +import matplotlib.patches as patches +from matplotlib.ticker import NullLocator + + +def show_output(output, img): + fig, ax = plt.subplots(1) + ax.imshow(img) + + for obj in output: + box = obj['box'] + x1, y1, x2, y2 = box[0], box[1], box[2], box[3] + box_w = x2 - x1 + box_h = y2 - y1 + bbox = patches.Rectangle((x1, y1), box_w, box_h, + linewidth=2, edgecolor='red', facecolor="none") + ax.add_patch(bbox) + plt.text( + x1, + y1, + s=obj['name'], + color="white", + verticalalignment="top", + bbox={"color": 'red', "pad": 0}, + ) + + plt.axis("off") + plt.gca().xaxis.set_major_locator(NullLocator()) + plt.gca().yaxis.set_major_locator(NullLocator()) + plt.show() + + +if __name__ == "__main__": + PATH = '../images/000000000139.jpg' + detector = Detector() + + img = utils.load_image(PATH) + output = detector.predict(img) + show_output(output, img) diff --git a/imsearch/config.py b/imsearch/config.py index bd2e595..621d296 100644 --- a/imsearch/config.py +++ b/imsearch/config.py @@ -3,7 +3,8 @@ default_config = { 'MONGO_URI': 'mongodb://localhost:27017/', - 'REDIS_URI': 'redis://localhost:6379/0' + 'REDIS_URI': 'redis://localhost:6379/0', + 'DETECTOR_MODE': 'local' } @@ -11,9 +12,4 @@ def config_init(config): final_config = copy.deepcopy(default_config) for k, v in config.items(): final_config[k] = v - if final_config['REDIS_URI'] == default_config['REDIS_URI']: - final_config['DETECTOR_MODE'] = 'local' - else: - final_config['DETECTOR_MODE'] = 'remote' - os.environ.update(final_config) diff --git a/imsearch/index.py b/imsearch/index.py index e6275c1..0aa38dc 100644 --- a/imsearch/index.py +++ b/imsearch/index.py @@ -130,7 +130,7 @@ def addImageBatch(self, image_list): for image_path in image_list: response.append(self.addImage(image_path)) return response - + def createIndex(self): """ Creates the index. Set create time paramenters and query-time parameters for nmslib index. @@ -146,6 +146,10 @@ def knnQuery(self, image_path, k=10, policy='global'): The path to the query image. k=10 Number of results. + policy='global' + choose policy from 'object' or 'global'. Search results will change accordingly. + object: Object level matching. The engine will look for similarity at object level for every object detected in the image. + global: Overall similarity using single feature space on the whole image. """ features = self._feature_extractor.extract(image_path, save=False) diff --git a/test/test_config.py b/test/test_config.py deleted file mode 100644 index ed1b0d6..0000000 --- a/test/test_config.py +++ /dev/null @@ -1,29 +0,0 @@ -import cv2 -import glob -import os, sys - -sys.path.append(os.path.join(os.path.dirname(__file__), '..')) - -import imsearch - -all_images = glob.glob(os.path.join(os.path.dirname(__file__), '..', 'images/*.jpg')) - -index = imsearch.init('test', MONGO_URI='mongodb://localhost:27017/', REDIS_URI='redis://denis:Welcome00@216.165.71.213:6379/0') -# index.cleanIndex() -# index.addImageBatch(all_images[0:25]) -# index.addImage(all_images[25]) -index.createIndex() - -# similar = index.knnQuery(all_images[25], policy='object') -# qImage = imsearch.utils.load_image(all_images[25]) - -similar = index.knnQuery('https://www.wallpaperup.com/uploads/wallpapers/2014/04/14/332423/d5c09641cb3af3a18087937d55125ae3-700.jpg', policy='object') -qImage = imsearch.utils.load_image('https://www.wallpaperup.com/uploads/wallpapers/2014/04/14/332423/d5c09641cb3af3a18087937d55125ae3-700.jpg') - -cv2.imshow('qImage', qImage) -for _i, _s in similar: - rImage = cv2.imread(_i['image']) - print([x['name'] for x in _i['primary']]) - print(_s) - cv2.imshow('rImage', rImage) - cv2.waitKey(0) \ No newline at end of file diff --git a/test/test_index.py b/test/test_index.py deleted file mode 100644 index 7cf5359..0000000 --- a/test/test_index.py +++ /dev/null @@ -1,29 +0,0 @@ -import cv2 -import glob -import os, sys - -sys.path.append(os.path.join(os.path.dirname(__file__), '..')) - -import imsearch - -all_images = glob.glob(os.path.join(os.path.dirname(__file__), '..', 'images/*.jpg')) - -index = imsearch.init('test') -# index.cleanIndex() -# index.addImageBatch(all_images[0:25]) -# index.addImage(all_images[25]) -index.createIndex() - -similar = index.knnQuery(all_images[25], policy='object') -qImage = imsearch.utils.load_image(all_images[25]) - -# similar = index.knnQuery('https://www.wallpaperup.com/uploads/wallpapers/2014/04/14/332423/d5c09641cb3af3a18087937d55125ae3-700.jpg', policy='object') -# qImage = imsearch.utils.load_image('https://www.wallpaperup.com/uploads/wallpapers/2014/04/14/332423/d5c09641cb3af3a18087937d55125ae3-700.jpg') - -cv2.imshow('qImage', qImage) -for _i, _s in similar: - rImage = cv2.imread(_i['image']) - print([x['name'] for x in _i['primary']]) - print(_s) - cv2.imshow('rImage', rImage) - cv2.waitKey(0) \ No newline at end of file diff --git a/test/test_yolo.py b/test/test_yolo.py deleted file mode 100644 index b82af01..0000000 --- a/test/test_yolo.py +++ /dev/null @@ -1,38 +0,0 @@ -from imsearch.backend.object_detector.yolo import Detector -from imsearch import utils -from PIL import Image -import numpy as np - -import matplotlib.pyplot as plt -import matplotlib.patches as patches -from matplotlib.ticker import NullLocator - -path = '../images/000000000139.jpg' -detector = Detector() -img = utils.load_image(path) -output = detector.predict(img) - -fig, ax = plt.subplots(1) -ax.imshow(img) - -for obj in output: - box = obj['box'] - x1, y1, x2, y2 = box[0], box[1], box[2], box[3] - box_w = x2 - x1 - box_h = y2 - y1 - bbox = patches.Rectangle((x1, y1), box_w, box_h, - linewidth=2, edgecolor='red', facecolor="none") - ax.add_patch(bbox) - plt.text( - x1, - y1, - s=obj['name'], - color="white", - verticalalignment="top", - bbox={"color": 'red', "pad": 0}, - ) - -plt.axis("off") -plt.gca().xaxis.set_major_locator(NullLocator()) -plt.gca().yaxis.set_major_locator(NullLocator()) -plt.show()