Skip to content

Commit 6d68c96

Browse files
author
Roel Kluin
committed
add fromfile interrogator
1 parent 2741435 commit 6d68c96

2 files changed

Lines changed: 60 additions & 1 deletion

File tree

json_schema/interrogators_v1_schema.json

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,24 @@
3333
".*": { "$ref": "#/$defs/huggingfaceInterrogator" }
3434
}
3535
},
36+
"FromFileInterrogator": {
37+
"type": "object",
38+
"patternProperties": {
39+
".*": {
40+
"type": "object",
41+
"properties": {
42+
"path": { "type": "string" },
43+
"val": { "type": "number", "default": 1.0 }
44+
},
45+
"required": [
46+
"path"
47+
],
48+
"additionalProperties": false
49+
}
50+
}
51+
},
3652
"additionalProperties": false
37-
},
53+
},
3854
"$defs": {
3955
"huggingfaceInterrogator": {
4056
"type": "object",

tagger/interrogator.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -485,6 +485,49 @@ def load_model(self, model_path) -> None:
485485
print(f'Loaded {self.name} model from {model_path}')
486486

487487

488+
class FromFileInterrogator(Interrogator):
489+
""" Pseudo Interrogator reading preinterrogated tags files """
490+
def __init__(self, name: str, path: os.PathLike, val=1.0) -> None:
491+
super().__init__(name)
492+
self.path = path
493+
self.val = val
494+
self.tags = None
495+
496+
def load(self) -> None:
497+
print(f'Loading {self.name} from {str(self.path)}')
498+
# self.path is a directory
499+
if not os.path.isdir(self.path):
500+
raise ValueError(f'{self.path} is not a directory')
501+
else:
502+
self.tags = {}
503+
for f in os.listdir(self.path):
504+
self.tags[f] = {}
505+
self.load_file(f)
506+
507+
def load_file(self, tags_file: str) -> None:
508+
image_name = str(tags_file).split('/')[-1].split('.')[0]
509+
with open(tags_file, 'r') as f:
510+
for line in f:
511+
for x in map(str.split, line.split(',')):
512+
if x[0] == '(' and x[-1] == ')' and ':' in x:
513+
tag, val = x[1:-1].split(':')
514+
self.tags[image_name][tag] = float(val)
515+
else:
516+
self.tags[image_name][x] = self.val
517+
518+
def unload(self) -> None:
519+
self.tags = {}
520+
521+
def interrogate(
522+
self,
523+
image: Image
524+
) -> Tuple[
525+
Dict[str, float], # rating confidences
526+
Dict[str, float] # tag confidences
527+
]:
528+
return {}, self.tags[image.filename]
529+
530+
488531
class WaifuDiffusionInterrogator(HFInterrogator):
489532
""" Interrogator for Waifu Diffusion models """
490533
def __init__(

0 commit comments

Comments
 (0)