Skip to content

Commit 200bf91

Browse files
author
ManuelFay
committed
improve usage of api
1 parent 794f42b commit 200bf91

File tree

4 files changed

+39
-14
lines changed

4 files changed

+39
-14
lines changed

README.md

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ group pictures, screenshots, etc...
1313

1414
In a Python 3.8+ virtual environment, install either from PIP or from source:
1515

16-
####Installation from the PIP package:
16+
#### Installation from the PIP package:
1717
```bash
1818
pip install image-searcher
1919

@@ -41,22 +41,29 @@ the picture, enable the `include_faces` flag (note that it makes the indexing pr
4141
```python
4242
from image_searcher import Search
4343

44-
searcher = Search(image_dir_path="/home/manu/perso/ImageSearcher/data/", traverse=True, include_faces=False)
44+
searcher = Search(image_dir_path="/home/manu/perso/ImageSearcher/data/",
45+
traverse=True,
46+
include_faces=False)
4547
```
4648

4749
Once this process has been done once, through Python, the library is used as such:
4850
```python
4951
from image_searcher import Search
5052

51-
searcher = Search(image_dir_path="/home/manu/perso/ImageSearcher/data/", traverse=True, include_faces=False)
53+
searcher = Search(image_dir_path="/home/manu/perso/ImageSearcher/data/",
54+
traverse=True,
55+
include_faces=False)
5256

53-
ranked_images = searcher.rank_images("A photo of a bird.", n=5)
54-
55-
# Display best images
57+
# Option 1: Pythonic API
5658
from PIL import Image
5759

60+
ranked_images = searcher.rank_images("A photo of a bird.", n=5)
5861
for image in ranked_images:
5962
Image.open(image.image_path).convert('RGB').show()
63+
64+
# Option 2: Launch Flask api from code
65+
from image_searcher.api import run
66+
run(searcher=searcher)
6067
```
6168

6269
### Using tags in the query
@@ -93,7 +100,15 @@ threaded:
93100
```python
94101
from image_searcher.api import run
95102

103+
# Option 1: Through a config file
96104
run(config_path="path_to_config_file.yml")
105+
106+
# Option 2: Through an instanciated Search object
107+
from image_searcher import Search
108+
109+
run(searcher=Search(image_dir_path="/home/manu/perso/ImageSearcher/data/",
110+
traverse=True,
111+
include_faces=False))
97112
```
98113

99114
A gunicorn process can also be launched locally with:

api/run_flask_gunicorn.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@ def create_app(config_path: str) -> Flask:
77
Entry point for gunicorn
88
"""
99

10-
command = RunFlaskCommand(config_path)
10+
command = RunFlaskCommand(config_path=config_path)
1111
return command.run(start=False)

image_searcher/api/run_flask_command.py

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import logging
2+
from typing import Optional
23
import yaml
34

45
from flask import Flask
@@ -12,9 +13,19 @@
1213
class RunFlaskCommand:
1314
logging.basicConfig(level=logging.INFO)
1415

15-
def __init__(self, config_path: str):
16-
self.searcher = None
17-
self.config = FlaskConfig(**yaml.load(open(config_path, 'r'), Loader=yaml.FullLoader))
16+
def __init__(self, config_path: Optional[str] = None, searcher: Optional[Search] = None):
17+
if config_path is None and searcher is None:
18+
raise AttributeError("Either the config path or the searcher need to be defined")
19+
20+
self.searcher = searcher
21+
if config_path:
22+
self.config = FlaskConfig(**yaml.load(open(config_path, 'r'), Loader=yaml.FullLoader))
23+
else:
24+
self.config = FlaskConfig(image_dir_path=self.searcher.loader.image_dir_path,
25+
traverse=self.searcher.loader.traverse,
26+
save_path=self.searcher.stored_embeddings.save_path,
27+
reindex=False,
28+
include_faces=len(self.searcher.stored_embeddings.face_embedding_paths) > 0)
1829

1930
def get_best_images(self):
2031
"""Routine that runs the QA inference pipeline.
@@ -55,6 +66,6 @@ def run(self, start=True):
5566
return app
5667

5768

58-
def run(config_path: str):
59-
command = RunFlaskCommand(config_path=config_path)
69+
def run(**kwargs):
70+
command = RunFlaskCommand(**kwargs)
6071
command.run()

image_searcher/interfaces/stored_embeddings.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,7 @@ def set_all_face_embeddings(self):
5454
embedding_path = []
5555

5656
for key, value in self.embeddings.items():
57-
# TODO: This line should not be needed
58-
if "face_embeddings" in value.keys():
57+
if "face_embeddings" in value.keys(): # Normally, this line should not be needed
5958
for x in value["face_embeddings"]:
6059
face_embeddings.append(x)
6160
embedding_path.append(key)

0 commit comments

Comments
 (0)