1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
|
# standard imports
import typing
# external imports
import torch
from facenet_pytorch import MTCNN, InceptionResnetV1
# bsie imports
from bsie.matcher import nodes
from bsie.utils import bsfs, ns
# inner-module imports
from ... import base
# exports
__all__: typing.Sequence[str] = (
'FaceDetect',
)
## code ##
bsf = ns.bsn.Face()
class FaceDetect(base.Extractor):
CONTENT_READER = 'bsie.reader.face.FaceExtract'
def __init__(self):
# initialize parent with the schema
super().__init__(bsfs.schema.from_string(base.SCHEMA_PREAMBLE + f'''
prefix bsf: <https://schema.bsfs.io/ie/Node/Face#>
bsn:Face rdfs:subClassOf bsfs:Node .
<https://schema.bsfs.io/ie/Literal/Array/Feature/Face#resnet512>
rdfs:subClassOf bsa:Feature ;
bsfs:distance <https://schema.bsfs.io/core/distance#euclidean> ;
bsfs:dtype <https://schema.bsfs.io/core/dtype#f32>;
bsfs:dimension "512"^^xsd:integer .
bse:face rdfs:subClassOf bsfs:Predicate ;
rdfs:domain bsn:Entity ;
rdfs:range bsn:Face .
bsf:x rdfs:subClassOf bsfs:Predicate ;
rdfs:domain bsn:Face ;
rdfs:range xsd:float ;
bsfs:unique "true"^^xsd:boolean .
bsf:y rdfs:subClassOf bsfs:Predicate ;
rdfs:domain bsn:Face ;
rdfs:range xsd:float ;
bsfs:unique "true"^^xsd:boolean .
bsf:width rdfs:subClassOf bsfs:Predicate ;
rdfs:domain bsn:Face ;
rdfs:range xsd:float ;
bsfs:unique "true"^^xsd:boolean .
bsf:height rdfs:subClassOf bsfs:Predicate ;
rdfs:domain bsn:Face ;
rdfs:range xsd:float ;
bsfs:unique "true"^^xsd:boolean .
bsf:embedding rdfs:subClassOf bsfs:Predicate ;
rdfs:domain bsn:Face ;
rdfs:range <https://schema.bsfs.io/ie/Literal/Array/Feature/Face#resnet512> ;
bsfs:unique "true"^^xsd:boolean .
'''))
def extract(
self,
subject: nodes.Entity,
content: dict,
principals: typing.Iterable[bsfs.schema.Predicate],
) -> typing.Iterator[typing.Tuple[nodes.Node, bsfs.schema.Predicate, typing.Any]]:
# check principals
if self.schema.predicate(ns.bse.face) not in principals:
# nothing to do; abort
return
for face in content:
fnode = nodes.Face(ucid=face['ucid'])
yield subject, ns.bse.face, fnode
yield fnode, bsf.x, face['x']
yield fnode, bsf.y, face['y']
yield fnode, bsf.width, face['width']
yield fnode, bsf.height, face['height']
yield fnode, bsf.embedding, face['embedding'].detach().cpu().numpy()
## EOF ##
|