|
| 1 | +--[[ |
| 2 | + Face recognition for darktable |
| 3 | +
|
| 4 | + Copyright (c) 2017 Sebastian Witt |
| 5 | + |
| 6 | + darktable is free software: you can redistribute it and/or modify |
| 7 | + it under the terms of the GNU General Public License as published by |
| 8 | + the Free Software Foundation, either version 3 of the License, or |
| 9 | + (at your option) any later version. |
| 10 | +
|
| 11 | + darktable is distributed in the hope that it will be useful, |
| 12 | + but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | + GNU General Public License for more details. |
| 15 | +
|
| 16 | + You should have received a copy of the GNU General Public License |
| 17 | + along with darktable. If not, see <http://www.gnu.org/licenses/>. |
| 18 | +]] |
| 19 | + |
| 20 | +--[[ |
| 21 | +face_recognition |
| 22 | +Add a new storage option to send images to face_recognition. |
| 23 | +Images are exported to darktable tmp dir first. |
| 24 | +A directory with known faces must exist, the image name are the |
| 25 | +tag names which will be used. |
| 26 | +Multiple images for one face can exist, add a number to it, the |
| 27 | +number will be removed from the tag, for example: |
| 28 | +People|IknowYou1.jpg |
| 29 | +People|IknowYou2.jpg |
| 30 | +People|Another.jpg |
| 31 | +People|Youtoo.jpg |
| 32 | +
|
| 33 | +ADDITIONAL SOFTWARE NEEDED FOR THIS SCRIPT |
| 34 | +* https://github.com/ageitgey/face_recognition |
| 35 | +* https://github.com/darktable-org/lua-scripts/tree/master/lib |
| 36 | +
|
| 37 | +USAGE |
| 38 | +* require this file from your main luarc config file. |
| 39 | +
|
| 40 | +This plugin will add a new storage option and calls face_recognition after export. |
| 41 | +]] |
| 42 | + |
| 43 | +local dt = require "darktable" |
| 44 | +local df = require "lib/dtutils.file" |
| 45 | +local gettext = dt.gettext |
| 46 | + |
| 47 | +-- works with darktable API version from 2.0.0 to 5.0.0 |
| 48 | +dt.configuration.check_version(...,{2,0,0},{3,0,0},{4,0,0},{5,0,0}) |
| 49 | + |
| 50 | +-- Tell gettext where to find the .mo file translating messages for a particular domain |
| 51 | +gettext.bindtextdomain("face_recognition", dt.configuration.config_dir.."/lua/locale/") |
| 52 | + |
| 53 | +local function _(msgid) |
| 54 | + return gettext.dgettext("face_recognition", msgid) |
| 55 | +end |
| 56 | + |
| 57 | +-- Register preference for known faces path |
| 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 |
| 65 | +dt.preferences.register("FaceRecognition", |
| 66 | + "nrCores", |
| 67 | + "integer", -- type |
| 68 | + _("Face recognition: Nr of CPU cores"), -- label |
| 69 | + _("Number of CPU cores to use, 0 for all"), -- tooltip |
| 70 | + 0, -- default |
| 71 | + 0, -- min |
| 72 | + 64) -- max |
| 73 | + |
| 74 | +local function show_status (storage, image, format, filename, number, total, high_quality, extra_data) |
| 75 | + dt.print("Export to Face recognition "..tostring(number).."/"..tostring(total)) |
| 76 | +end |
| 77 | + |
| 78 | +local function face_recognition (storage, image_table, extra_data) --finalize |
| 79 | + if not df.check_if_bin_exists("face_recognition") then |
| 80 | + dt.print(_("Face recognition not found")) |
| 81 | + return |
| 82 | + end |
| 83 | + |
| 84 | + -- list of exported images |
| 85 | + local img_list = {} |
| 86 | + |
| 87 | + for _,v in pairs(image_table) do |
| 88 | + table.insert (img_list, v) |
| 89 | + end |
| 90 | + |
| 91 | + -- Get path of exported images |
| 92 | + 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") |
| 98 | + |
| 99 | + if nrCores < 1 then |
| 100 | + nrCores = -1 |
| 101 | + end |
| 102 | + |
| 103 | + -- Output file |
| 104 | + local output = path .. "fr.txt" |
| 105 | + |
| 106 | + local command = "face_recognition --cpus " .. nrCores .. " " .. knownPath .. " " .. path .. " > " .. output |
| 107 | + dt.print_error("FR: " .. command) |
| 108 | + dt.print(_("Starting face recognition...")) |
| 109 | + |
| 110 | + dt.control.execute(command) |
| 111 | + |
| 112 | + -- Remove exported images |
| 113 | + for _,v in ipairs(img_list) do |
| 114 | + os.remove (v) |
| 115 | + end |
| 116 | + |
| 117 | + -- Open output file |
| 118 | + local f = io.open(output, "rb") |
| 119 | + |
| 120 | + if not f then |
| 121 | + dt.print(_("Face recognition failed")) |
| 122 | + else |
| 123 | + dt.print(_("Face recognition finished")) |
| 124 | + f:close () |
| 125 | + end |
| 126 | + |
| 127 | + -- Read output |
| 128 | + local result = {} |
| 129 | + for line in io.lines(output) do |
| 130 | + local file, tag = string.match (line, "(.*),(.*)$") |
| 131 | + tag = string.gsub (tag, "%d*$", "") |
| 132 | + dt.print_error ("F:"..file .." T:".. tag) |
| 133 | + if result[file] ~= nil then |
| 134 | + table.insert (result[file], tag) |
| 135 | + else |
| 136 | + result[file] = {tag} |
| 137 | + end |
| 138 | + end |
| 139 | + |
| 140 | + -- Attach tags |
| 141 | + for file,tags in pairs(result) do |
| 142 | + -- Find image in table |
| 143 | + for img,file2 in pairs(image_table) do |
| 144 | + if file == file2 then |
| 145 | + 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) |
| 150 | + end |
| 151 | + end |
| 152 | + end |
| 153 | + end |
| 154 | + |
| 155 | + --os.remove (output) |
| 156 | + |
| 157 | +end |
| 158 | + |
| 159 | +-- Register |
| 160 | +dt.register_storage("module_face_recognition", _("Face recognition"), show_status, face_recognition) |
| 161 | + |
| 162 | +-- |
| 163 | +-- vim: shiftwidth=2 expandtab tabstop=2 cindent syntax=lua |
0 commit comments