Skip to content

Commit a2d66b2

Browse files
authored
Use the already decoded query image in find (#1701)
* Quote Python 3.10 in the CI matrix * Accept typed HTTP headers in authentication validation Flask 3 supplies Werkzeug Headers to the API routes. Include that existing input alongside the dictionary used by gRPC in the validate annotation. Keep the optional header type under TYPE_CHECKING. Verified the six mypy errors on the unchanged base. With this annotation, all 144 source files pass mypy 1.14.1 under both Flask 3.1.3 and Flask 2.0.2. Pylint scores 10.00/10, the existing token-validation tests pass in both environments, and dictionary, Headers and EnvironHeaders checks cover valid, absent, invalid and disabled authentication. * Use the already decoded query image in find
1 parent 7dae5b5 commit a2d66b2

4 files changed

Lines changed: 53 additions & 6 deletions

File tree

‎.github/workflows/tests.yml‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ jobs:
2828
runs-on: ubuntu-latest
2929
strategy:
3030
matrix:
31-
python-version: [3.10]
31+
python-version: ["3.10"]
3232
steps:
3333
- uses: actions/checkout@v4
3434
- name: Set up Python ${{ matrix.python-version }}
@@ -49,7 +49,7 @@ jobs:
4949
runs-on: ubuntu-latest
5050
strategy:
5151
matrix:
52-
python-version: [3.10]
52+
python-version: ["3.10"]
5353
steps:
5454
- uses: actions/checkout@v4
5555
- name: Set up Python ${{ matrix.python-version }}
@@ -69,7 +69,7 @@ jobs:
6969
runs-on: ubuntu-latest
7070
strategy:
7171
matrix:
72-
python-version: [3.10]
72+
python-version: ["3.10"]
7373
steps:
7474
- uses: actions/checkout@v4
7575
- name: Set up Python ${{ matrix.python-version }}

‎deepface/api/src/modules/auth/service.py‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
# built-in dependencies
2-
from typing import Optional, Dict, Any
2+
from typing import Optional, Dict, Any, Union, TYPE_CHECKING
33

44
# project dependencies
55
from deepface.commons.logger import Logger
66

7+
if TYPE_CHECKING:
8+
from werkzeug.datastructures import Headers
9+
710
logger = Logger()
811

912

@@ -21,7 +24,7 @@ def extract_token(self, auth_header: Optional[str]) -> Optional[str]:
2124
return parts[1]
2225
return None
2326

24-
def validate(self, headers: Dict[str, Any]) -> bool:
27+
def validate(self, headers: Union[Dict[str, Any], "Headers"]) -> bool:
2528
if not self.is_auth_enabled:
2629
logger.debug("Authentication is disabled. Skipping token validation.")
2730
return True

‎deepface/modules/recognition.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,7 @@ def find(
324324
source_objs: List[Dict[str, Any]] = cast(
325325
List[Dict[str, Any]],
326326
detection.extract_faces(
327-
img_path=img_path,
327+
img_path=img,
328328
detector_backend=detector_backend,
329329
grayscale=False,
330330
enforce_detection=enforce_detection,
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import io
2+
from unittest import TestCase
3+
from unittest.mock import Mock, patch
4+
5+
import numpy as np
6+
7+
from deepface.modules import recognition
8+
9+
10+
class SingleReadTests(TestCase):
11+
def test_query_stream_is_consumed_once(self):
12+
class Source:
13+
def __init__(self):
14+
self.data = io.BytesIO(b"sample")
15+
self.reads = 0
16+
17+
def read(self):
18+
self.reads += 1
19+
return self.data.read()
20+
21+
source = Source()
22+
pixels = np.arange(12, dtype=np.uint8).reshape(2, 2, 3)
23+
store = Mock()
24+
store.exists.return_value = True
25+
store.list_images.return_value = []
26+
row = dict(identity="known", hash="cached", embedding=[1.0], target_x=0, target_y=0,
27+
target_w=1, target_h=1)
28+
store.read_bytes.return_value = getattr(recognition, "__serialize_npz")([row])
29+
30+
def decode(value):
31+
if isinstance(value, np.ndarray):
32+
return value, "array"
33+
self.assertEqual(value.read(), b"sample")
34+
return pixels, "io object"
35+
36+
with patch.object(recognition, "build_file_store", return_value=store), \
37+
patch.object(recognition.image_utils, "load_image", side_effect=decode), \
38+
patch.object(recognition.modeling, "build_model", return_value=Mock(output_shape=1)), \
39+
patch.object(recognition.representation, "represent", return_value=[{"embedding":[1.0]}]):
40+
result = recognition.find(source, "gallery", detector_backend="skip",
41+
distance_metric="euclidean", refresh_database=False,
42+
return_type="dict")
43+
self.assertEqual(result[0][0]["identity"], "known")
44+
self.assertEqual(source.reads, 1)

0 commit comments

Comments
 (0)