forked from simonw/blip-caption
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathblip_caption.py
More file actions
67 lines (62 loc) · 1.93 KB
/
Copy pathblip_caption.py
File metadata and controls
67 lines (62 loc) · 1.93 KB
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
import click
import json
import PIL
import torch
from transformers import pipeline
@click.command()
@click.argument(
"paths",
type=click.Path(exists=True, dir_okay=False, allow_dash=True),
nargs=-1,
required=True,
)
@click.option("gpu","--gpu", is_flag=True, default=False, help="Run the model on a GPU")
@click.option("--large", is_flag=True, help="Use the large model")
@click.option("json_", "--json", is_flag=True, help="Output as JSON")
def cli(paths, large, gpu, json_):
device = -1
if gpu:
if torch.cuda.is_available():
device = 0
else:
click.echo("No GPU available despite specifying --gpu. Defaulting to CPU")
captioner = pipeline(
"image-to-text",
device=device,
model="Salesforce/blip-image-captioning-base"
if not large
else "Salesforce/blip-image-captioning-large",
)
multi = len(paths) > 1
is_first = True
for path, is_last in zip(paths, [False] * (len(paths) - 1) + [True]):
if multi and not json_:
click.echo(path)
prefix = ""
if json_ and is_first:
prefix = "["
else:
prefix = " "
is_first = False
try:
caption = captioner(str(path), max_new_tokens=100)
except PIL.UnidentifiedImageError as ex:
if not json_:
click.echo(f"Error: {ex}")
else:
click.echo(
prefix
+ json.dumps({"path": path, "error": str(ex)})
+ ("," if not is_last else "]")
)
continue
if json_:
click.echo(
prefix
+ json.dumps({"path": path, "caption": caption[0]["generated_text"]})
+ ("," if not is_last else "]")
)
else:
click.echo(caption[0]["generated_text"])
if __name__ == "__main__":
cli()