@@ -54,14 +54,21 @@ local function _(msgid)
54
54
return gettext .dgettext (" face_recognition" , msgid )
55
55
end
56
56
57
- -- Register preference for known faces path
57
+ -- Preference: Tag for unknown_person
58
58
dt .preferences .register (" FaceRecognition" ,
59
- " knownImagePath" ,
60
- " directory" , -- type
61
- _ (" Face recognition: Known images" ), -- label
62
- _ (" Path to images with known faces, files named after tag to apply" ), -- tooltip
63
- " ~/.config/darktable/face_recognition" ) -- default
64
- -- ...and number of CPU cores to use
59
+ " unknownTag" ,
60
+ " string" , -- type
61
+ _ (" Face recognition: Unknown tag" ), -- label
62
+ _ (" Tag for faces that are not recognized" ), -- tooltip
63
+ " unknown_person" )
64
+ -- Preference: Images with this substring in tags are ignored
65
+ dt .preferences .register (" FaceRecognition" ,
66
+ " ignoreTags" ,
67
+ " string" , -- type
68
+ _ (" Face recognition: Ignore tag" ), -- label
69
+ _ (" Images with this substring in tags are ignored, separate multiple strings with ," ), -- tooltip
70
+ " " )
71
+ -- Preference: Number of CPU cores to use
65
72
dt .preferences .register (" FaceRecognition" ,
66
73
" nrCores" ,
67
74
" integer" , -- type
@@ -70,41 +77,77 @@ dt.preferences.register("FaceRecognition",
70
77
0 , -- default
71
78
0 , -- min
72
79
64 ) -- max
80
+ -- Preference: Known faces path
81
+ dt .preferences .register (" FaceRecognition" ,
82
+ " knownImagePath" ,
83
+ " directory" , -- type
84
+ _ (" Face recognition: Known images" ), -- label
85
+ _ (" Path to images with known faces, files named after tag to apply" ), -- tooltip
86
+ " ~/.config/darktable/face_recognition" ) -- default -- default
73
87
74
88
local function show_status (storage , image , format , filename , number , total , high_quality , extra_data )
75
89
dt .print (" Export to Face recognition " .. tostring (number ).. " /" .. tostring (total ))
76
90
end
77
91
92
+ -- Check if image has ignored tag attached
93
+ local function ignoreByTag (image , ignoreTags )
94
+ local tags = image :get_tags ()
95
+ local ignoreImage = false
96
+ -- For each image tag
97
+ for _ ,t in ipairs (tags ) do
98
+ -- Check if it contains a ignore tag
99
+ for _ ,it in ipairs (ignoreTags ) do
100
+ if string.find (t .name , it , 1 , true ) then
101
+ -- The image has ignored tag attached
102
+ ignoreImage = true
103
+ dt .print_error (" Face recognition: Ignored tag: " .. it .. " found in " .. image .id .. " :" .. t .name )
104
+ end
105
+ end
106
+ end
107
+
108
+ return ignoreImage
109
+ end
110
+
78
111
local function face_recognition (storage , image_table , extra_data ) -- finalize
79
112
if not df .check_if_bin_exists (" face_recognition" ) then
80
113
dt .print (_ (" Face recognition not found" ))
81
114
return
82
115
end
83
116
117
+ -- Get preferences
118
+ local knownPath = dt .preferences .read (" FaceRecognition" , " knownImagePath" , " directory" )
119
+ local nrCores = dt .preferences .read (" FaceRecognition" , " nrCores" , " integer" )
120
+ local ignoreTagString = dt .preferences .read (" FaceRecognition" , " ignoreTags" , " string" )
121
+ local unknownTag = dt .preferences .read (" FaceRecognition" , " unknownTag" , " string" )
122
+
123
+ -- face_recognition uses -1 for all cores, we use 0 in preferences
124
+ if nrCores < 1 then
125
+ nrCores = - 1
126
+ end
127
+
128
+ -- Split ignore tags (if any)
129
+ ignoreTags = {}
130
+ for tag in string.gmatch (ignoreTagString , ' ([^,]+)' ) do
131
+ table.insert (ignoreTags , tag )
132
+ dt .print_error (" Face recognition: Ignore tag: " .. tag )
133
+ end
134
+
84
135
-- list of exported images
85
136
local img_list = {}
86
137
87
- for _ ,v in pairs (image_table ) do
138
+ for img ,v in pairs (image_table ) do
88
139
table.insert (img_list , v )
89
140
end
90
141
91
142
-- Get path of exported images
92
143
local path = df .get_path (img_list [1 ])
93
- dt .print_error (" FR: Unknown path: " .. path )
94
-
95
- -- Path to known images and number of CPU cores to use
96
- local knownPath = dt .preferences .read (" FaceRecognition" , " knownImagePath" , " directory" )
97
- local nrCores = dt .preferences .read (" FaceRecognition" , " nrCores" , " integer" )
144
+ dt .print_error (" Face recognition: Path to unknown images: " .. path )
98
145
99
- if nrCores < 1 then
100
- nrCores = - 1
101
- end
102
-
103
146
-- Output file
104
- local output = path .. " fr .txt"
147
+ local output = path .. " facerecognition .txt"
105
148
106
149
local command = " face_recognition --cpus " .. nrCores .. " " .. knownPath .. " " .. path .. " > " .. output
107
- dt .print_error (" FR : " .. command )
150
+ dt .print_error (" Face recognition: Running command : " .. command )
108
151
dt .print (_ (" Starting face recognition..." ))
109
152
110
153
dt .control .execute (command )
@@ -129,7 +172,7 @@ local function face_recognition (storage, image_table, extra_data) --finalize
129
172
for line in io.lines (output ) do
130
173
local file , tag = string.match (line , " (.*),(.*)$" )
131
174
tag = string.gsub (tag , " %d*$" , " " )
132
- dt .print_error (" F :" .. file .. " T :" .. tag )
175
+ dt .print_error (" File :" .. file .. " Tag :" .. tag )
133
176
if result [file ] ~= nil then
134
177
table.insert (result [file ], tag )
135
178
else
@@ -143,10 +186,19 @@ local function face_recognition (storage, image_table, extra_data) --finalize
143
186
for img ,file2 in pairs (image_table ) do
144
187
if file == file2 then
145
188
for _ ,t in ipairs (tags ) do
146
- dt .print_error (" I:" .. img .id .. " T: " .. t )
147
- -- Create tag if it does not exists
148
- local tag = dt .tags .create (t )
149
- img :attach_tag (tag )
189
+ -- Check if image is ignored
190
+ if ignoreByTag (img , ignoreTags ) then
191
+ dt .print_error (" Face recognition: Ignoring image with ID " .. img .id )
192
+ else
193
+ -- Check of unrecognized unknown_person
194
+ if t == " unknown_person" then
195
+ t = unknownTag
196
+ end
197
+ dt .print_error (" ImgId:" .. img .id .. " Tag:" .. t )
198
+ -- Create tag if it does not exists
199
+ local tag = dt .tags .create (t )
200
+ img :attach_tag (tag )
201
+ end
150
202
end
151
203
end
152
204
end
0 commit comments