Skip to content

Commit 2ef5f4f

Browse files
committed
Update face_recognition.lua
Add support for docker
1 parent 0122b3f commit 2ef5f4f

File tree

1 file changed

+70
-28
lines changed

1 file changed

+70
-28
lines changed

contrib/face_recognition.lua

Lines changed: 70 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ local function save_preferences()
150150
dt.preferences.write(MODULE, "ignore_tags", "string", fc.ignore_tags.text)
151151
dt.preferences.write(MODULE, "category_tags", "string", fc.category_tags.text)
152152
dt.preferences.write(MODULE, "known_image_path", "directory", fc.known_image_path.value)
153+
dt.preferences.write(MODULE, "program_source", "integer", fc.program_source.selected)
153154
local val = fc.tolerance.value
154155
val = string.gsub(tostring(val), ",", ".")
155156
dt.preferences.write(MODULE, "tolerance", "float", tonumber(val))
@@ -165,6 +166,7 @@ local function reset_preferences()
165166
fc.ignore_tags.text = ""
166167
fc.category_tags.text = ""
167168
fc.known_image_path.value = dt.configuration.config_dir .. "/face_recognition"
169+
fc.program_source.selected = 1
168170
fc.tolerance.value = 0.6
169171
fc.num_cores.value = -1
170172
fc.export_format.selected = 1
@@ -200,18 +202,24 @@ local function cleanup(img_list)
200202
end
201203

202204
local function face_recognition ()
205+
local program_source = fc.program_source.value
203206

204-
local bin_path = df.check_if_bin_exists("face_recognition")
207+
if string.match(program_source, "python") then
208+
dt.print_log(string.match(program_source, "python"))
209+
local bin_path = df.check_if_bin_exists("face_recognition")
205210

206-
if not bin_path then
207-
dt.print(_("Face recognition not found"))
208-
return
211+
if not bin_path then
212+
dt.print(_("Face recognition for python not found"))
213+
return
214+
end
209215
end
210216

211217
save_preferences()
212218

213219
-- Get preferences
214-
local knownPath = dt.preferences.read(MODULE, "known_image_path", "directory")
220+
local containerKnownPath = "/known"
221+
local containerUnknownPath = "/unknown/"
222+
local hostKnownPath = dt.preferences.read(MODULE, "known_image_path", "directory")
215223
local nrCores = dt.preferences.read(MODULE, "num_cores", "integer")
216224
local ignoreTagString = dt.preferences.read(MODULE, "ignore_tags", "string")
217225
local categoryTagString = dt.preferences.read(MODULE, "category_tags", "string")
@@ -245,15 +253,24 @@ local function face_recognition ()
245253
end
246254

247255
-- Get path of exported images
248-
local path = df.get_path (img_list[1])
249-
dt.print_log ("Face recognition: Path to known faces: " .. knownPath)
250-
dt.print_log ("Face recognition: Path to unknown images: " .. path)
256+
local hostUnknownPath = df.get_path (img_list[1])
257+
dt.print_log ("Face recognition: Path to known faces: " .. hostKnownPath)
258+
dt.print_log ("Face recognition: Path to unknown images: " .. hostUnknownPath)
251259
dt.print_log ("Face recognition: Tag used for unknown faces: " .. unknownTag)
252260
dt.print_log ("Face recognition: Tag used if non person is found: " .. nonpersonsfoundTag)
253261
os.setlocale("C")
254262
local tolerance = dt.preferences.read(MODULE, "tolerance", "float")
255263

256-
local command = bin_path .. " --cpus " .. nrCores .. " --tolerance " .. tolerance .. " " .. knownPath .. " " .. path .. " > " .. OUTPUT
264+
local hostCommand = "docker run --rm -v " .. hostKnownPath .. ":" ..containerKnownPath .. " -v " .. hostUnknownPath .. ":" .. containerUnknownPath
265+
local contianerCommand = "face_recognition" .. " --cpus " .. nrCores .. " --tolerance " .. tolerance .. " " .. containerKnownPath .. " " .. containerUnknownPath .. " > " .. OUTPUT
266+
local command = ""
267+
if fc.program_source.value == "python" then
268+
command = bin_path .. " --cpus " .. nrCores .. " --tolerance " .. tolerance .. " " .. hostKnownPath .. " " .. hostUnknownPath .. " > " .. OUTPUT
269+
elseif fc.program_source.value == "docker-cpu" then
270+
command = hostCommand .. " animcogn/face_recognition:cpu " .. contianerCommand
271+
elseif fc.program_source.value == "docker-gpu" then
272+
command = hostCommand .. " animcogn/face_recognition:gpu " .. contianerCommand
273+
end
257274
os.setlocale()
258275
dt.print_log("Face recognition: Running command: " .. command)
259276
dt.print(_("Starting face recognition..."))
@@ -278,6 +295,9 @@ local function face_recognition ()
278295
for line in io.lines(OUTPUT) do
279296
if not string.match(line, "^WARNING:") and line ~= "" and line ~= nil then
280297
local file, tag = string.match (line, "(.*),(.*)$")
298+
if fc.program_source.value == "docker-cpu" or fc.program_source.value == "docker-gpu" then
299+
file = string.gsub(file, containerUnknownPath, hostUnknownPath)
300+
end
281301
tag = string.gsub (tag, "%d*$", "")
282302
dt.print_log ("File:"..file .." Tag:".. tag)
283303
tag_object = {}
@@ -430,6 +450,25 @@ fc.known_image_path = dt.new_widget("file_chooser_button"){
430450
end
431451
}
432452

453+
fc.program_source = dt.new_widget("combobox"){
454+
label = _("program_source"),
455+
tooltip = _("source of the face_recognition program"),
456+
selected = dt.preferences.read(MODULE, "program_source", "integer"),
457+
changed_callback = function(this)
458+
dt.preferences.write(MODULE, "program_source", "integer", this.selected)
459+
if this.selected == 2 or this.selected == 3 then
460+
fc.executable.visible = false
461+
else
462+
fc.executable.visible = true
463+
end
464+
end,
465+
"python", "docker-cpu", "docker-gpu",
466+
}
467+
468+
if dt.configuration.running_os == "windows" or dt.configuration.running_os == "macos" then
469+
fc.executable = df.executable_path_widget({"face_recognition"})
470+
end
471+
433472
fc.export_format = dt.new_widget("combobox"){
434473
label = _("export image format"),
435474
tooltip = _("format for exported images"),
@@ -470,27 +509,26 @@ local widgets = {
470509
fc.category_tags,
471510
dt.new_widget("label"){ label = _("face data directory")},
472511
fc.known_image_path,
512+
dt.new_widget("section_label"){ label = _("source options")},
513+
fc.program_source,
514+
fc.executable,
515+
dt.new_widget("section_label"){ label = _("processing options")},
516+
fc.tolerance,
517+
fc.num_cores,
518+
fc.export_format,
519+
dt.new_widget("box"){
520+
orientation = "horizontal",
521+
dt.new_widget("label"){ label = _("width ")},
522+
fc.width,
523+
},
524+
dt.new_widget("box"){
525+
orientation = "horizontal",
526+
dt.new_widget("label"){ label = _("height ")},
527+
fc.height,
528+
},
529+
fc.execute
473530
}
474531

475-
if dt.configuration.running_os == "windows" or dt.configuration.running_os == "macos" then
476-
table.insert(widgets, df.executable_path_widget({"face_recognition"}))
477-
end
478-
table.insert(widgets, dt.new_widget("section_label"){ label = _("processing options")})
479-
table.insert(widgets, fc.tolerance)
480-
table.insert(widgets, fc.num_cores)
481-
table.insert(widgets, fc.export_format)
482-
table.insert(widgets, dt.new_widget("box"){
483-
orientation = "horizontal",
484-
dt.new_widget("label"){ label = _("width ")},
485-
fc.width,
486-
})
487-
table.insert(widgets, dt.new_widget("box"){
488-
orientation = "horizontal",
489-
dt.new_widget("label"){ label = _("height ")},
490-
fc.height,
491-
})
492-
table.insert(widgets, fc.execute)
493-
494532
fc.widget = dt.new_widget("box"){
495533
orientation = vertical,
496534
reset_callback = function(this)
@@ -515,6 +553,10 @@ else
515553
end
516554
end
517555

556+
if fc.program_source.selected == 2 or fc.program_source.selected == 3 then
557+
fc.executable.visible = false
558+
end
559+
518560
fc.tolerance.value = dt.preferences.read(MODULE, "tolerance", "float")
519561

520562
-- preferences

0 commit comments

Comments
 (0)