-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
executable file
·140 lines (116 loc) · 5.09 KB
/
app.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
import typer
from typing import Optional
import os
import sys
import json
from alternat.generation import Generator
from alternat.generation.exceptions import InvalidConfigFile, InvalidGeneratorDriver
from alternat.collection import Collector
import subprocess
import shutil
app = typer.Typer()
# This key determines the driver (if present in the json file)
_DRIVER_KEY = "DRIVER"
# This key determines the generator level config (if present in the json file)
_GENERATOR_KEY = "GENERATOR"
@app.command("collect")
def collect(
url: str = None,
output_dir_path: str = None,
download_recursive: bool = False,
collect_using_apify: bool = True,
):
"""Collects image from URL specified.
:param url: The URL from where the image needs to be downloaded.
:type url: str
:param output_dir_path: Path to directory where the images downloaded should be dumped
:type output_dir_path: str
:param download_recursive: Whether to recursively crawl a site, defaults to False
:type download_recursive: bool, optional
:param collect_using_apify: Whether to crawl using appify crawler, defaults to True
:type collect_using_apify: bool, optional
"""
collector = Collector()
if url is None:
typer.echo("ERROR : Specify url from which images needs to be collected using --url argument")
return None
if output_dir_path is None:
typer.echo("ERROR : Specify output directory path where the images needs to be collected using "
"--output-dir-path argument")
return None
collector.process(url, output_dir_path, download_recursive, collect_using_apify)
@app.command("generate")
def generate(
output_dir_path: str = typer.Option(...),
input_dir_path: str = None,
input_image_file_path: str = None,
base64_image: str = None,
driver_config_file_path: str = None
):
"""Analyze the image to generate alt-text
:param output_dir_path: Output dir path to store the results, defaults to typer.Option(...)
:type output_dir_path: str, optional
:param input_dir_path: Directory path to the folder containing images, defaults to None
:type input_dir_path: str, optional
:param input_image_file_path: Path to image file to be processed (used only if single image needs to be processed), defaults to None
:type input_image_file_path: str, optional
:param base64_image: Base64 image to be processed (used only if single image needs to be processed), defaults to None
:type base64_image: str, optional
:param driver_config_file_path: Path to the generator JSON config file (defaults will be used if not provided)
:return:, defaults to None
:type driver_config_file_path: str, optional
:raises InvalidGeneratorDriver: Driver invalid error.
:raises InvalidConfigFile: Configuration file is invalid.
:return: collection of JSON representing alt-text data for images
:rtype: [type]
"""
generator = Generator()
if driver_config_file_path is not None:
file_extension = driver_config_file_path.split(".")[-1]
if file_extension.lower() == "json":
#read the json file
with open(driver_config_file_path) as f:
data = json.load(f)
if _DRIVER_KEY in data.keys():
generator = Generator(data[_DRIVER_KEY])
generator.set_driver_config(data)
else:
raise InvalidGeneratorDriver(Generator.ALLOWED_DRIVERS)
# check if generator conf is present
if _GENERATOR_KEY in data.keys():
generator_conf = data[_GENERATOR_KEY]
generator.set_config(generator_conf)
else:
raise InvalidConfigFile()
results = []
if input_dir_path is None:
if input_image_file_path is None:
if base64_image is not None:
result_json = generator.generate_alt_text_from_base64(base64_image)
print(result_json)
return result_json
else:
typer.echo("One of --base64_image or --input-image-file-path is missing")
return
else:
typer.echo("Processing image : %s" % input_image_file_path)
result_json = generator.generate_alt_text_from_file(input_image_file_path, output_dir_path)
print(result_json)
return result_json
else:
for path, subdirs, files in os.walk(input_dir_path):
for filename in files:
if (
filename.endswith(".jpg")
or filename.endswith(".png")
or filename.endswith(".jpeg")
):
image_path = os.path.join(path, filename)
typer.echo("Processing image : %s" % image_path)
result_json = generator.generate_alt_text_from_file(image_path, output_dir_path)
print(result_json)
results.append(result_json)
typer.echo("Result saved at : %s" % output_dir_path)
return results
if __name__ == "__main__":
app()