-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6d33ade
commit 85ea4b0
Showing
8 changed files
with
130 additions
and
104 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.