Skip to content

Commit

Permalink
Remove Multiple from default imports
Browse files Browse the repository at this point in the history
  • Loading branch information
jrrodri committed Oct 15, 2024
1 parent 55a36b6 commit 8249b8f
Show file tree
Hide file tree
Showing 8 changed files with 22 additions and 19 deletions.
13 changes: 4 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ import numpy as np
from PIL import Image
from abraia import detect


model_uri = f"https://api.abraia.me/files/multiple/models/yolov8n.onnx"

model = detect.load_model(model_uri)
Expand All @@ -83,23 +82,19 @@ import numpy as np
from abraia.draw import load_image, save_image, render_results
from abraia.faces import Recognition


img = load_image('images/rolling-stones.jpg')
out = img.copy()

recognition = Recognition()
results = recognition.represent_faces(img)

index = []
for src in ['mick-jagger.jpg', 'keith-richards.jpg', 'ronnie-wood.jpg', 'charlie-watts.jpg']:
img = load_image(f"images/{src}")
rslt = recognition.represent_faces(img)[0]
sims = [recognition.compute_similarity(rslt['embeddings'], result['embeddings']) for result in results]
idx = np.argmax(sims)
if sims[idx] > 0.45:
results[idx]['label'] = os.path.splitext(src)[0]
results[idx]['confidence'] = sims[idx]
print(src, sims[idx], sims)
index.append({'name': os.path.splitext(src)[0], 'embeddings': rslt['embeddings']})

result = recognition.identify_faces(results, index)
render_results(out, results)
save_image('images/rolling-stones-identified.jpg', out)
```
Expand Down Expand Up @@ -255,7 +250,7 @@ The Multiple extension has being developed by [ABRAIA](https://abraia.me/about)
For instance, you can directly load and save ENVI files, and their metadata.

```python
from abraia import Multiple
from abraia.multiple import Multiple

multiple = Multiple()

Expand Down
3 changes: 1 addition & 2 deletions abraia/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,5 @@

from . import config
from .client import Abraia, APIError
from .multiple import Multiple

__all__ = ['config', 'Abraia', 'APIError', 'Multiple']
__all__ = ['config', 'Abraia', 'APIError']
14 changes: 10 additions & 4 deletions abraia/faces/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@


def euclidean_distance(feat1, feat2):
return np.sqrt(np.square(feat1 - feat2).sum())
return float(np.linalg.norm(feat1 - feat2))


def cosine_similarity(feat1, feat2):
return np.dot(feat1, feat2) / (np.linalg.norm(feat1) * np.linalg.norm(feat2))
return float(np.dot(feat1, feat2) / (np.linalg.norm(feat1) * np.linalg.norm(feat2)))


class Recognition:
Expand All @@ -31,8 +31,14 @@ def represent_faces(self, img, results=None):
result['embeddings'] = self.arcface.calculate_embeddings(face)
return results

def compute_similarity(self, feat1, feat2):
return float(cosine_similarity(feat1, feat2))
def identify_faces(self, results, index, threshold=0.45):
for result in results:
sims = [cosine_similarity(result['embeddings'], ind['embeddings']) for ind in index]
idx = np.argmax(sims)
if sims[idx] > threshold:
result['confidence'] = sims[idx]
result['label'] = index[idx]['name']
return results


__all__ = [Recognition]
1 change: 0 additions & 1 deletion abraia/multiple.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import os
import tifffile
import numpy as np
from PIL import Image
Expand Down
Binary file modified images/screenshot.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 3 additions & 1 deletion notebooks/hyperspectral-analysis.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@
"import numpy as np\n",
"import pandas as pd\n",
"import matplotlib.pyplot as plt\n",
"from abraia import Multiple, hsi\n",
"\n",
"from abraia.multiple import Multiple\n",
"from abraia import hsi\n",
"\n",
"plt.rcParams['figure.figsize'] = [12, 6]\n",
"plt.rcParams['figure.dpi'] = 100\n",
Expand Down
4 changes: 3 additions & 1 deletion notebooks/hyperspectral-classification.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@
"import numpy as np\n",
"import pandas as pd\n",
"import matplotlib.pyplot as plt\n",
"from abraia import Multiple, hsi\n",
"\n",
"from abraia.multiple import Multiple\n",
"from abraia import hsi\n",
"\n",
"plt.rcParams['figure.figsize'] = [12, 6]\n",
"plt.rcParams['figure.dpi'] = 100\n",
Expand Down
2 changes: 1 addition & 1 deletion tests/test_multiple.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import numpy as np

from abraia import Multiple
from abraia.multiple import Multiple

multiple = Multiple()

Expand Down

0 comments on commit 8249b8f

Please sign in to comment.