Skip to content

Commit e556c5a

Browse files
author
Artsiom Khadzkou
committed
Added options in Global Compreface
1 parent 36f708a commit e556c5a

24 files changed

+118
-120
lines changed

.gitignore

+5-1
Original file line numberDiff line numberDiff line change
@@ -92,5 +92,9 @@ Thumbs.db
9292
.history
9393
.ionide
9494

95-
95+
# Vscode
9696
.vscode
97+
98+
#Idea
99+
.idea
100+
.idea/*

.idea/.gitignore

-8
This file was deleted.

.idea/compreface-python-sdk.iml

-8
This file was deleted.

.idea/inspectionProfiles/Project_Default.xml

-6
This file was deleted.

.idea/inspectionProfiles/profiles_settings.xml

-6
This file was deleted.

.idea/misc.xml

-4
This file was deleted.

.idea/modules.xml

-8
This file was deleted.

.idea/vcs.xml

-6
This file was deleted.

compreface/client/verify_face_from_image.py

+2-5
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,6 @@
1717
from compreface.common.multipart_constructor import multipart_constructor_with_two_images
1818
import requests
1919
from compreface.config.api_list import VERIFICATION_API
20-
import os
21-
22-
from requests_toolbelt.multipart.encoder import MultipartEncoder
2320
from compreface.common.typed_dict import ExpandedOptionsDict, check_fields_by_name
2421
from compreface.common.client import ClientRequest
2522

@@ -42,8 +39,8 @@ def get(self):
4239
"""
4340
POST request for verify face in image using source and target images.
4441
45-
:param source_image_path: Path to source image in file system.
46-
:param target_image_path: Path to target image in file system.
42+
:param source_image: Path to source image in file system.
43+
:param target_image: Path to target image in file system.
4744
:param image_id: subject id from previously added image.
4845
:param options: dictionary with options for server.
4946

compreface/collections/face_collections.py

+5-4
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
permissions and limitations under the License.
1515
"""
1616

17-
from compreface.common.typed_dict import ExpandedOptionsDict, DetProbOptionsDict
17+
from compreface.common.typed_dict import AllOptionsDict, ExpandedOptionsDict, DetProbOptionsDict, pass_dict
1818
from ..use_cases import (
1919
AddExampleOfSubject,
2020
ListOfAllSavedSubjects,
@@ -25,10 +25,11 @@
2525

2626

2727
class FaceCollection:
28-
def __init__(self, api_key: str, domain: str, port: str):
28+
def __init__(self, api_key: str, domain: str, port: str, options: AllOptionsDict = {}):
2929
"""Init service with define API Key"""
3030
self.available_services = []
3131
self.api_key = api_key
32+
self.options = options
3233
self.add_example: AddExampleOfSubject = AddExampleOfSubject(
3334
domain=domain,
3435
port=port,
@@ -67,7 +68,7 @@ def add(self, image_path: str, subject: str, options: DetProbOptionsDict = {}) -
6768
image_path=image_path,
6869
subject=subject
6970
)
70-
return self.add_example.execute(request, options)
71+
return self.add_example.execute(request, pass_dict(options, DetProbOptionsDict) if options == {} else options)
7172

7273
def list(self) -> dict:
7374
"""
@@ -112,4 +113,4 @@ def verify(self, image_path: str, image_id: str, options: ExpandedOptionsDict =
112113
image_path=image_path,
113114
image_id=image_id
114115
)
115-
return self.verify_face_from_image.execute(request, options)
116+
return self.verify_face_from_image.execute(request, pass_dict(options, ExpandedOptionsDict) if options == {} else options)

compreface/common/service.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,25 @@
1515
"""
1616

1717
from abc import ABC, abstractmethod
18+
from compreface.common.typed_dict import AllOptionsDict
1819

1920

2021
class Service(ABC):
2122
"""The best class of all services"""
2223

2324
@abstractmethod
24-
def __init__(self, api_key: str):
25+
def __init__(self, api_key: str, options: AllOptionsDict):
2526
self._api_key = api_key
27+
self._options = options
2628

2729
@property
2830
def api_key(self):
2931
return self._api_key
3032

33+
@property
34+
def options(self):
35+
return self._options
36+
3137
@abstractmethod
3238
def get_available_functions(self):
3339
pass

compreface/common/typed_dict.py

+9
Original file line numberDiff line numberDiff line change
@@ -58,3 +58,12 @@ def check_fields_by_name(name: str, value: Any):
5858
if row.find('age') == -1 and row.find('calculator') == -1 and row.find('gender') == -1 and row.find('landmarks') == -1:
5959
raise IncorrectFieldException(
6060
"face_plugins must be only contains calculator,age,gender,landmarks. Incorrect value {}".format(row))
61+
62+
63+
def pass_dict(options: AllOptionsDict, type: DetProbOptionsDict or ExpandedOptionsDict):
64+
converted_options: ExpandedOptionsDict or DetProbOptionsDict = {}
65+
for key in type.__annotations__.keys():
66+
value = options.get(key)
67+
if value != None:
68+
converted_options[key] = value
69+
return converted_options

compreface/core/model.py

+19-5
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
permissions and limitations under the License.
1515
"""
1616

17+
from compreface.common.typed_dict import AllOptionsDict
1718
from typing import Optional
1819
from ..service import (
1920
RecognitionService,
@@ -27,9 +28,10 @@ class CompreFace(object):
2728
Main class
2829
"""
2930

30-
def __init__(self, domain: str, port: str):
31+
def __init__(self, domain: str, port: str, options: AllOptionsDict = {}):
3132
self._domain: str = domain
3233
self._port: str = port
34+
self._options: AllOptionsDict = options
3335
self.recognition: Optional[RecognitionService] = None
3436
self.verification: Optional[VerificationService] = None
3537
self.detection: Optional[DetectionService] = None
@@ -50,6 +52,14 @@ def port(self):
5052
def port(self, port: str):
5153
self._port = port
5254

55+
@property
56+
def options(self):
57+
return self._options
58+
59+
@options.setter
60+
def options(self, options: AllOptionsDict):
61+
self._options = options
62+
5363
def init_face_recognition(self, api_key: str) -> RecognitionService:
5464
"""
5565
Init Face Recognition Service
@@ -58,7 +68,8 @@ def init_face_recognition(self, api_key: str) -> RecognitionService:
5868
"""
5969
self.recognition = RecognitionService(api_key=api_key,
6070
domain=self.domain,
61-
port=self.port)
71+
port=self.port,
72+
options=self.options)
6273
return self.recognition
6374

6475
def init_face_verification(self, api_key: str) -> VerificationService:
@@ -69,7 +80,8 @@ def init_face_verification(self, api_key: str) -> VerificationService:
6980
"""
7081
self.verification = VerificationService(api_key=api_key,
7182
domain=self.domain,
72-
port=self.port)
83+
port=self.port,
84+
options=self.options)
7385
return self.verification
7486

7587
def init_face_detection(self, api_key: str) -> DetectionService:
@@ -78,6 +90,8 @@ def init_face_detection(self, api_key: str) -> DetectionService:
7890
:param api_key:
7991
:return:
8092
"""
81-
self.detection = DetectionService(api_key=api_key, domain=self.domain,
82-
port=self.port)
93+
self.detection = DetectionService(api_key=api_key,
94+
domain=self.domain,
95+
port=self.port,
96+
options=self.options)
8397
return self.detection

compreface/service/detection_service.py

+5-4
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
permissions and limitations under the License.
1515
"""
1616

17-
from compreface.common.typed_dict import ExpandedOptionsDict
17+
from compreface.common.typed_dict import AllOptionsDict, ExpandedOptionsDict, pass_dict
1818
from compreface.use_cases.detect_face_from_image import DetectFaceFromImage
1919
from typing import List
2020

@@ -24,9 +24,9 @@
2424
class DetectionService(Service):
2525
"""Detection service"""
2626

27-
def __init__(self, api_key: str, domain: str, port: str):
27+
def __init__(self, api_key: str, domain: str, port: str, options: AllOptionsDict = {}):
2828
"""Init service with define API Key"""
29-
super().__init__(api_key)
29+
super().__init__(api_key, options)
3030
self.available_services = []
3131
self.detect_face_from_image: DetectFaceFromImage = DetectFaceFromImage(
3232
domain=domain,
@@ -52,4 +52,5 @@ def detect(self, image_path: str, options: ExpandedOptionsDict = {}) -> dict:
5252
api_key=self.api_key,
5353
image_path=image_path
5454
)
55-
return self.detect_face_from_image.execute(request, options)
55+
return self.detect_face_from_image.execute(request, pass_dict(
56+
self.options, ExpandedOptionsDict) if options == {} else options)

compreface/service/recognition_service.py

+5-4
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@
2727
class RecognitionService(Service):
2828
"""Recognition service"""
2929

30-
def __init__(self, api_key: str, domain: str, port: str):
30+
def __init__(self, api_key: str, domain: str, port: str, options: AllOptionsDict = {}):
3131
"""Init service with define API Key"""
32-
super().__init__(api_key)
32+
super().__init__(api_key, options)
3333
self.available_services = []
3434
self.recognize_face_from_images: RecognizeFaceFromImage = RecognizeFaceFromImage(
3535
domain=domain,
@@ -39,7 +39,8 @@ def __init__(self, api_key: str, domain: str, port: str):
3939
self.face_collection: FaceCollection = FaceCollection(
4040
domain=domain,
4141
port=port,
42-
api_key=api_key
42+
api_key=api_key,
43+
options=options
4344
)
4445

4546
def get_available_functions(self) -> List[str]:
@@ -60,7 +61,7 @@ def recognize(self, image_path: str, options: AllOptionsDict = {}) -> dict:
6061
api_key=self.api_key,
6162
image_path=image_path
6263
)
63-
return self.recognize_face_from_images.execute(request, options)
64+
return self.recognize_face_from_images.execute(request, self.options if options == {} else options)
6465

6566
def get_face_collection(self) -> FaceCollection:
6667
"""

compreface/service/verification_service.py

+8-7
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
"""
1616

1717
from compreface.use_cases.verifiy_face_from_images import VerifyFaceFromImage
18-
from compreface.common.typed_dict import ExpandedOptionsDict
18+
from compreface.common.typed_dict import AllOptionsDict, ExpandedOptionsDict, pass_dict
1919
from compreface.client.verify_face_from_image import VerifyFaceFromImageClient
2020
from typing import List
2121

@@ -25,9 +25,9 @@
2525
class VerificationService(Service):
2626
"""Verification service"""
2727

28-
def __init__(self, api_key: str, domain: str, port: str):
28+
def __init__(self, api_key: str, domain: str, port: str, options: AllOptionsDict = {}):
2929
"""Init service with define API Key"""
30-
super().__init__(api_key)
30+
super().__init__(api_key, options)
3131
self.available_services = []
3232
self.verify_face_from_image: VerifyFaceFromImageClient = VerifyFaceFromImageClient(
3333
domain=domain,
@@ -42,7 +42,7 @@ def get_available_functions(self) -> List[str]:
4242
"""
4343
return self.available_services
4444

45-
def verify(self, source_image_path: str, target_image_path: str, options: ExpandedOptionsDict) -> dict:
45+
def verify(self, source_image_path: str, target_image_path: str, options: ExpandedOptionsDict = {}) -> dict:
4646
"""
4747
Verify face in images
4848
:param source_image_path:
@@ -54,7 +54,8 @@ def verify(self, source_image_path: str, target_image_path: str, options: Expand
5454
source_image_path=source_image_path,
5555
target_image_path=target_image_path)
5656
return self.verify_face_from_image.post(
57-
source_image_path=request.source_image_path,
58-
target_image_path=request.target_image_path,
59-
options=options
57+
source_image=request.source_image_path,
58+
target_image=request.target_image_path,
59+
options=pass_dict(
60+
self.options, ExpandedOptionsDict) if options == {} else options
6061
)

examples/add_example_of_a_subject.py

+5-4
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
permissions and limitations under the License.
1515
"""
1616

17+
from compreface.common.typed_dict import ExpandedOptionsDict
1718
from compreface import CompreFace
1819
from compreface.service import RecognitionService
1920
from compreface.collections import FaceCollection
@@ -22,7 +23,9 @@
2223
PORT: str = '8000'
2324
RECOGNITION_API_KEY: str = 'b97fbc0a-518a-4b1d-a93a-581b1d3814cc'
2425

25-
compre_face: CompreFace = CompreFace(DOMAIN, PORT)
26+
compre_face: CompreFace = CompreFace(DOMAIN, PORT, {
27+
"det_prob_threshold": 0.8
28+
})
2629

2730
recognition: RecognitionService = compre_face.init_face_recognition(
2831
RECOGNITION_API_KEY)
@@ -33,6 +36,4 @@
3336
image: str = 'examples/common/jonathan-petit-unsplash.jpg'
3437
subject: str = 'Jonathan Petit'
3538

36-
print(face_collection.add(image, subject, {
37-
"det_prob_threshold": 0.8
38-
}))
39+
print(face_collection.add(image, subject))

examples/delete_all_examples_of_subject.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
DOMAIN: str = 'http://localhost'
2222
PORT: str = '8000'
23-
RECOGNITION_API_KEY: str = '9916f5d1-216f-4049-9e06-51c140bfa898'
23+
RECOGNITION_API_KEY: str = 'b97fbc0a-518a-4b1d-a93a-581b1d3814cc'
2424

2525

2626
compre_face: CompreFace = CompreFace(DOMAIN, PORT)

examples/delete_example_by_id.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
DOMAIN: str = 'http://localhost'
2222
PORT: str = '8000'
23-
RECOGNITION_API_KEY: str = '9916f5d1-216f-4049-9e06-51c140bfa898'
23+
RECOGNITION_API_KEY: str = 'b97fbc0a-518a-4b1d-a93a-581b1d3814cc'
2424

2525

2626
compre_face: CompreFace = CompreFace(DOMAIN, PORT)

0 commit comments

Comments
 (0)