-
Notifications
You must be signed in to change notification settings - Fork 0
/
labelizer.py
621 lines (501 loc) · 16.3 KB
/
labelizer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
from datetime import datetime
import functools
import json
from random import shuffle
import signal
import sqlite3
import traceback
from typing import List
from PIL import Image
import requests
import sys
import os
import torch
import re
import glob
import logging
import coloredlogs
import argparse
from pprint import pformat, pprint
from urllib import parse as urlparse
from transformers import (
BlipProcessor,
BlipForConditionalGeneration,
Blip2Processor,
Blip2ForConditionalGeneration,
BlipForQuestionAnswering,
)
logging.basicConfig()
coloredlogs.install()
log = logging.getLogger()
@functools.cache
def use_captions():
return os.getenv("ENABLE_CAPTION", "false") == "true"
@functools.cache
def use_vqa():
return os.getenv("ENABLE_VQA", "false") == "true"
@functools.cache
def get_vqa_prompts():
with open(os.getenv("VQA_PROMPTS_FILE")) as f:
return json.load(f)
# todo: mps?
__model_instances = {}
__processor_instances = {}
supported_image_extensions = ["jpg", "jpeg", "png", "bmp", "gif"]
device = "cuda" if torch.cuda.is_available() else "cpu"
stop_tokens = [
"it",
"is",
"they",
"yes",
"no",
"none",
"don't know",
"unknown",
"i'm not sure",
"i don't know",
"no animals",
"not food",
"not a selfie",
"no words",
"joke",
"0",
"1",
"2",
"3",
"4",
"5",
"6",
"7",
"8",
"9",
"not dog",
"not animals",
"no one",
"in ",
]
# sqlite wrapper to retrieve a value by the provided key
def get_last_update(photo_id):
db_path = os.getenv("STATE_DB_PATH", None)
if not db_path:
return None
try:
with sqlite3.connect(db_path) as db:
cursor = db.cursor()
cursor.execute(
"SELECT last_update FROM last_update WHERE photo_id=?", (photo_id,)
)
ret = cursor.fetchone()
cursor.close()
return ret
except Exception:
return None
def store_last_update(photo_id, last_update, photo_data=""):
db_path = os.getenv("STATE_DB_PATH", None)
if not db_path:
return None
try:
with sqlite3.connect(db_path) as db:
cursor = db.cursor()
cursor.execute(
"INSERT INTO last_update VALUES (?, ?, ?)",
(photo_id, last_update, photo_data),
)
db.commit()
except Exception:
log.warn(traceback.format_exc())
def create_state_db():
db_path = os.getenv("STATE_DB_PATH", None)
if not db_path:
return None
try:
with sqlite3.connect(db_path) as db:
cursor = db.cursor()
cursor.execute(
"CREATE TABLE IF NOT EXISTS last_update (photo_id text, last_update text, full_photo_data text)"
)
return True
except Exception:
log.warn(traceback.format_exc())
return False
def get_model(model_name):
global __model_instances
if model_name in __model_instances:
return __model_instances[model_name]
use_blip2 = is_blip2(model_name)
vqa = use_vqa() and model_name == os.getenv("MODEL_VQA_HFID")
log.info(f"Loading model {model_name}, blip2 = {use_blip2}, vqa = {vqa}...")
if use_blip2:
cls = Blip2ForConditionalGeneration
else:
cls = BlipForQuestionAnswering if vqa else BlipForConditionalGeneration
__model_instances[model_name] = cls.from_pretrained(model_name).to(device)
return __model_instances[model_name]
@functools.cache
def is_blip2(model_name):
return "blip2" in model_name or os.getenv("FORCE_BLIP2", False)
@functools.cache
def get_processor(model_name):
global __processor_instances
if model_name in __processor_instances:
return __processor_instances[model_name]
processor_ = Blip2Processor if is_blip2(model_name) else BlipProcessor
processor = processor_.from_pretrained(model_name)
__processor_instances[model_name] = processor
return processor
## ---------------------- ##
def is_iterable(o):
if isinstance(o, str):
return False
try:
iter(o)
return True
except Exception:
return False
def cleanup_list(l):
ret = []
if not is_iterable(l):
if " and " in l:
return cleanup_list(l.split(" and "))
for i, el in enumerate(l):
if "," in el:
el = el.split(",")
if is_iterable(el):
ret += cleanup_list(el)
else:
el = el.lower().strip()
if el in stop_tokens:
continue
ret.append(el)
return list(set(filter(lambda el: el, ret)))
def generate_caption_multi(
model, processor, image, prompt: List[str] | str | List[List[str]]
):
new_prompt = []
relabel = []
if not is_iterable(prompt):
new_prompt.append(prompt)
else:
for x in prompt:
if is_iterable(x):
new_prompt.append(x[0])
relabel.append(x[1])
else:
new_prompt.append(x)
relabel.append(None)
if not new_prompt:
return ""
ret = []
for i, p in enumerate(new_prompt):
caption = generate_caption(model, processor, image, p)
if caption[0] == "yes" and relabel[i]:
ret.append(relabel[i])
else:
ret.append(caption[0])
return ret
def generate_caption(
model, processor, image, prompt: List[str] | str | List[List[str]]
):
if is_iterable(prompt):
generated_text = generate_caption_multi(model, processor, image, prompt)
else:
inputs = processor(image, prompt, return_tensors="pt").to(device)
generated_ids = model.generate(**inputs, max_new_tokens=64)
generated_text = processor.batch_decode(generated_ids, skip_special_tokens=True)
log.info(f"Model prompt: {prompt} -> {generated_text}")
return (
generated_text
if is_iterable(generated_text)
else [
generated_text,
]
)
def generate_caption_plain(image):
if not use_captions():
return ""
model = get_model(os.getenv("MODEL_BLIP_HFID"))
processor = get_processor(os.getenv("MODEL_BLIP_HFID"))
ret = generate_caption(model, processor, image, "a photograph of")
log.info(f"VC description: {ret}")
return ret[0]
def generate_caption_vqa(image, prompts):
if use_vqa():
model = get_model(os.getenv("MODEL_VQA_HFID"))
processor = get_processor(os.getenv("MODEL_VQA_HFID"))
else:
model = get_model(os.getenv("MODEL_BLIP_HFID"))
processor = get_processor(os.getenv("MODEL_BLIP_HFID"))
# todo: batch prompts
p = prompts or get_vqa_prompts()
ret = []
ret = cleanup_list(generate_caption(model, processor, image, p))
log.info(f"VQA keywords: {ret}")
return ",".join(ret)
def process_image(file_path: str | bytes, prompts=None):
if is_url_of_image_file(file_path) and not os.path.exists(file_path):
image = Image.open(requests.get(file_path, stream=True).raw).convert("RGB")
else:
image = Image.open(file_path).convert("RGB")
keywords = generate_caption_vqa(image, prompts) if use_vqa() else ""
caption = generate_caption_plain(image) if use_captions() else ""
return keywords, caption
def is_image_filename(filename):
pat = r".*\.(" + "|".join(supported_image_extensions) + ")"
return re.match(pat, filename, re.I)
def process_folder(folder_path):
return filter(
lambda file_path: os.path.isfile(file_path)
and is_image_filename(os.path.basename(file_path)),
glob.iglob(folder_path + "**/*", recursive=True),
)
def cleanup_string(s):
# just use a csv writer
single_quote = "'"
double_quote = '"'
s = re.sub("\\b(" + "|".join(stop_tokens) + ")\\b", "", s)
s = re.sub(r",{2+}", ",", s)
s = re.sub(r"^,|,$", "", s)
s = re.sub(r"\s+", " ", s)
return s.replace(double_quote, single_quote).strip()
def is_url_of_image_file(url: str):
# make it's a URI
if not url or not isinstance(url, str) or not url.strip().startswith("http"):
return False
parsed_url = urlparse.urlparse(url)
return is_image_filename(parsed_url.path)
def get_file_paths(root_):
# resolve any symbolic links
root = os.path.realpath(root_) if not is_iterable(root_) else root_
# determine if root argument is a file, folder or url
if is_iterable(root):
for r in root:
yield from get_file_paths(r)
elif os.path.exists(root):
if os.path.isdir(root):
yield from process_folder(root)
elif os.path.isfile(root) or is_url_of_image_file(root):
yield root
else:
raise Exception(f"Invalid argument: {root}")
def get_args():
parser = argparse.ArgumentParser(
description="Caption images in a folder or photoprism url"
)
parser.add_argument(
"mode",
metavar="mode",
type=str,
help="Operating mode - photoprism or url/folder/path; make sure to set proper environment variables for PhotoPrism",
choices=["photoprism", "local"],
default="local",
nargs="?",
)
parser.add_argument(
"input_path",
metavar="input_path",
type=str,
nargs="*",
help="image url or folder path/file",
)
parser.add_argument(
"-o",
"--output",
metavar="output",
type=str,
help="output log file path (stdout)",
)
parser.add_argument(
"-r",
"--readonly",
action="store_true",
help="readonly mode for photoprism - no field updates will be made",
)
parser.add_argument(
"-q",
"--query",
metavar="query",
default="original:*",
type=str,
help="photoprism query (default: original:*)",
)
parser.add_argument(
"-m",
"--max_items",
metavar="max_items",
default=0,
type=int,
help="max items to process",
)
parser.add_argument(
"-ppof",
"--offset",
metavar="offset",
default=0,
type=int,
help="starting offset for photoprism",
)
parser.add_argument(
"-ob",
"--order_by",
metavar="order_by",
default="newest",
type=str,
help="photoprism ordering mode: oldest, newest, etc",
)
# todo:
# photoprism filtering opts
return parser.parse_args()
def handle_local(args, path_=None):
outlog = open(args.output, "w+") if args.output else sys.stdout
p = path_ or args.input_path
for path in get_file_paths(p):
p = os.path.abspath(path)
try:
ret = cleanup_string(",".join(process_image(p)))
# todo: cleanup
print(f'"{cleanup_string(p)}","{ret}"', file=outlog)
except Exception as e:
print(traceback.format_exc())
log.error(f"{p},{e}")
def get_file_extension(filename):
return filename.split(".")[-1].lower()
def delete_local(path):
if "true" != os.getenv("PHOTOPRISM_DOWNLOAD_NO_DELETE", "false").lower():
log.info(f"Removing downloaded file {path}")
os.unlink(path)
def handle_photoprism_photo(photo, photo_instance, readonly=True):
if not photo or not photo.get("Hash") or not photo.get("Type") == "image":
# todo: proper logging
log.debug(f"Skipping photo - {pformat(photo)}")
return None
hash = photo["Hash"]
file_extension = get_file_extension(photo["FileName"])
if file_extension not in supported_image_extensions:
log.debug(f"Skipping photo - {pformat(photo)}")
return None
p = os.path.abspath(
os.path.join(os.getenv("PHOTOPRISM_DOWNLOAD_PATH"), f"{hash}.{file_extension}")
)
log.info(f"Fetching {hash}/{photo['UID']} -> {p}")
if photo_instance.download_file(
hash=hash, path=os.getenv("PHOTOPRISM_DOWNLOAD_PATH"), filename=hash
) and os.path.exists(p):
(keywords, caption) = process_image(p)
if not readonly:
photo_instance.update_photo_description_and_keywords(
photo,
caption + f" ({keywords})" if keywords else "",
keywords,
)
delete_local(p)
else:
log.error(f"Failed to download {pformat(photo)}")
return True
def handle_photoprism(args):
# imports here to relieve need for requirements.txt / local mode etc
from photoprism.Session import Session
from photoprism.Photo import Photo
pp_session = Session(
os.getenv("PHOTOPRISM_USERNAME"),
os.getenv("PHOTOPRISM_PASSWORD"),
os.getenv("PHOTOPRISM_BASE_DOMAIN"),
use_https=os.getenv("PHOTOPRISM_USE_HTTPS", True),
verify_cert=False if os.getenv("PHOTOPRISM_ALLOW_SELF_SIGNED", False) else True,
)
pp_session.create()
log.info(f"Connected to PhotoPrism: {pp_session.session_id}")
# todo: pagination
photo_instance = Photo(pp_session)
num_photos = int(os.getenv("PHOTOPRISM_BATCH_SIZE", 10))
offset = args.offset or 0
order = args.order_by
def do_search():
return photo_instance.search(
query=args.query, count=num_photos, offset=offset, order=order
)
data = do_search()
have_db = not args.readonly and create_state_db()
while data:
log.info(
f"Fetched {len(data)} photos from PhotoPrism (offset={offset}, pagesize={num_photos})..."
)
if os.getenv("PHOTOPRISM_SHUFFLE", False):
shuffle(data)
for photo in data:
offset += 1
if have_db:
last_update = get_last_update(photo["UID"])
if last_update:
log.info(
f"Skipping {photo['UID']}, already processed on {last_update}"
)
continue
if args.max_items and offset >= args.max_items:
log.info(f"Done - processed {offset} items")
return
try:
handle_photoprism_photo(photo, photo_instance, readonly=args.readonly)
except Exception:
log.error(photo)
log.error(traceback.format_exc())
continue
# store current time as last update
if have_db:
store_last_update(
photo["UID"], datetime.now().isoformat(), json.dumps(photo)
)
data = do_search()
# sigint trap
def maybe_trap_sigint():
def trap_sigint(signum, frame):
log.info("Exiting...")
sys.exit(0)
try:
signal.signal(signal.SIGINT, trap_sigint)
signal.signal(signal.SIGTERM, trap_sigint)
except:
pass
# preloads models and does a basic sanity check
def validate_env(args=None):
if not (os.getenv("MODEL_VQA_HFID", False) and os.getenv("MODEL_BLIP_HFID", False)):
raise Exception(
"Please set one or both of MODEL_VQA_HFID and MODEL_BLIP_HFID environment variables"
)
if not (os.getenv("ENABLE_VQA", False) and os.getenv("ENABLE_CAPTION", False)):
raise Exception(
"Please set one or both of ENABLE_VQA and ENABLE_CAPTION environment variables"
)
if use_vqa():
if not (
os.getenv("MODEL_VQA_HFID", None)
and os.getenv("VQA_PROMPTS_FILE", None)
and os.path.exists(os.getenv("VQA_PROMPTS_FILE"))
):
raise Exception(
"Please set MODEL_VQA_HFID environment variable when ENABLE_VQA is set"
)
get_model(os.getenv("MODEL_VQA_HFID"))
if use_captions():
if not os.getenv("MODEL_BLIP_HFID"):
raise Exception(
"Please set MODEL_BLIP_HFID environment variable when ENABLE_CAPTION is set"
)
get_model(os.getenv("MODEL_BLIP_HFID"))
if args and args.mode == "photoprism":
for field in ["PASSWORD", "USERNAME", "BASE_DOMAIN"]:
if not os.getenv(f"PHOTOPRISM_{field}"):
raise Exception(
f"Please set PHOTOPRISM_{field} environment variable when operating in photoprism mode"
)
def main():
maybe_trap_sigint()
args = get_args()
validate_env(args)
if args.mode == "local":
handle_local(args)
elif args.mode == "photoprism":
handle_photoprism(args)
else:
args.print_help()
if __name__ == "__main__":
main()