Skip to content

Commit 8e6552e

Browse files
committed
Added ignore tag and unknown tag, verbose output
1 parent 024e016 commit 8e6552e

File tree

1 file changed

+76
-24
lines changed

1 file changed

+76
-24
lines changed

contrib/face_recognition.lua

Lines changed: 76 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -54,14 +54,21 @@ local function _(msgid)
5454
return gettext.dgettext("face_recognition", msgid)
5555
end
5656

57-
-- Register preference for known faces path
57+
-- Preference: Tag for unknown_person
5858
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
6572
dt.preferences.register("FaceRecognition",
6673
"nrCores",
6774
"integer", -- type
@@ -70,41 +77,77 @@ dt.preferences.register("FaceRecognition",
7077
0, -- default
7178
0, -- min
7279
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
7387

7488
local function show_status (storage, image, format, filename, number, total, high_quality, extra_data)
7589
dt.print("Export to Face recognition "..tostring(number).."/"..tostring(total))
7690
end
7791

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+
78111
local function face_recognition (storage, image_table, extra_data) --finalize
79112
if not df.check_if_bin_exists("face_recognition") then
80113
dt.print(_("Face recognition not found"))
81114
return
82115
end
83116

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+
84135
-- list of exported images
85136
local img_list = {}
86137

87-
for _,v in pairs(image_table) do
138+
for img,v in pairs(image_table) do
88139
table.insert (img_list, v)
89140
end
90141

91142
-- Get path of exported images
92143
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)
98145

99-
if nrCores < 1 then
100-
nrCores = -1
101-
end
102-
103146
-- Output file
104-
local output = path .. "fr.txt"
147+
local output = path .. "facerecognition.txt"
105148

106149
local command = "face_recognition --cpus " .. nrCores .. " " .. knownPath .. " " .. path .. " > " .. output
107-
dt.print_error("FR: " .. command)
150+
dt.print_error("Face recognition: Running command: " .. command)
108151
dt.print(_("Starting face recognition..."))
109152

110153
dt.control.execute(command)
@@ -129,7 +172,7 @@ local function face_recognition (storage, image_table, extra_data) --finalize
129172
for line in io.lines(output) do
130173
local file, tag = string.match (line, "(.*),(.*)$")
131174
tag = string.gsub (tag, "%d*$", "")
132-
dt.print_error ("F:"..file .." T:".. tag)
175+
dt.print_error ("File:"..file .." Tag:".. tag)
133176
if result[file] ~= nil then
134177
table.insert (result[file], tag)
135178
else
@@ -143,10 +186,19 @@ local function face_recognition (storage, image_table, extra_data) --finalize
143186
for img,file2 in pairs(image_table) do
144187
if file == file2 then
145188
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
150202
end
151203
end
152204
end

0 commit comments

Comments
 (0)