Skip to content

Commit

Permalink
[UPDATE]add examples
Browse files Browse the repository at this point in the history
  • Loading branch information
rikenmehta03 committed Mar 19, 2020
1 parent 6d33ade commit 85ea4b0
Show file tree
Hide file tree
Showing 8 changed files with 130 additions and 104 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
78 changes: 78 additions & 0 deletions examples/index.py
Original file line number Diff line number Diff line change
@@ -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:[email protected]: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')
44 changes: 44 additions & 0 deletions examples/yolo_detector.py
Original file line number Diff line number Diff line change
@@ -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)
8 changes: 2 additions & 6 deletions imsearch/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,13 @@

default_config = {
'MONGO_URI': 'mongodb://localhost:27017/',
'REDIS_URI': 'redis://localhost:6379/0'
'REDIS_URI': 'redis://localhost:6379/0',
'DETECTOR_MODE': 'local'
}


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)
6 changes: 5 additions & 1 deletion imsearch/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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)
Expand Down
29 changes: 0 additions & 29 deletions test/test_config.py

This file was deleted.

29 changes: 0 additions & 29 deletions test/test_index.py

This file was deleted.

38 changes: 0 additions & 38 deletions test/test_yolo.py

This file was deleted.

0 comments on commit 85ea4b0

Please sign in to comment.