Commit
·
0df3f4e
1
Parent(s):
8a46519
format
Browse files
app.py
CHANGED
@@ -21,6 +21,7 @@ classif_pipeline = pipeline(
|
|
21 |
"image-classification", model=classif_model, feature_extractor=feature_extractor
|
22 |
)
|
23 |
|
|
|
24 |
def load_manifest(inputs):
|
25 |
with requests.get(inputs) as r:
|
26 |
return r.json()
|
@@ -39,11 +40,10 @@ def resize_iiif_urls(image_url, size='224'):
|
|
39 |
# parts[6] = f"{size}, {size}"
|
40 |
# return "/".join(parts)
|
41 |
image_url = IIIFImageClient.init_from_url(image_url)
|
42 |
-
image_url = image_url.size(width=size,height=size)
|
43 |
return image_url.__str__()
|
44 |
|
45 |
|
46 |
-
|
47 |
async def get_image(client, url):
|
48 |
try:
|
49 |
resp = await client.get(url, timeout=30)
|
@@ -62,6 +62,11 @@ async def get_images(urls):
|
|
62 |
|
63 |
|
64 |
def predict(inputs):
|
|
|
|
|
|
|
|
|
|
|
65 |
data = load_manifest(inputs)
|
66 |
urls = get_image_urls_from_manifest(data)
|
67 |
resized_urls = [resize_iiif_urls(url) for url in urls]
|
@@ -70,7 +75,7 @@ def predict(inputs):
|
|
70 |
images = list(pluck(1, images_urls))
|
71 |
urls = list(pluck(0, images_urls))
|
72 |
predictions = classif_pipeline(images, top_k=1)
|
73 |
-
for url, pred in zip(urls,predictions):
|
74 |
top_pred = pred[0]
|
75 |
if top_pred['label'] == 'illustrated':
|
76 |
image_url = IIIFImageClient.init_from_url(url)
|
@@ -86,6 +91,7 @@ def predict(inputs):
|
|
86 |
# predicted_images.append((image, top_pred['score']))
|
87 |
# return predicted_images
|
88 |
|
|
|
89 |
gallery = gr.Gallery()
|
90 |
gallery.style(grid=3)
|
91 |
|
@@ -95,5 +101,6 @@ demo = gr.Interface(
|
|
95 |
outputs=gallery,
|
96 |
title="ImageIN",
|
97 |
description="Identify illustrations in pages of historical books!",
|
|
|
98 |
)
|
99 |
demo.launch(debug=True)
|
|
|
21 |
"image-classification", model=classif_model, feature_extractor=feature_extractor
|
22 |
)
|
23 |
|
24 |
+
|
25 |
def load_manifest(inputs):
|
26 |
with requests.get(inputs) as r:
|
27 |
return r.json()
|
|
|
40 |
# parts[6] = f"{size}, {size}"
|
41 |
# return "/".join(parts)
|
42 |
image_url = IIIFImageClient.init_from_url(image_url)
|
43 |
+
image_url = image_url.size(width=size, height=size)
|
44 |
return image_url.__str__()
|
45 |
|
46 |
|
|
|
47 |
async def get_image(client, url):
|
48 |
try:
|
49 |
resp = await client.get(url, timeout=30)
|
|
|
62 |
|
63 |
|
64 |
def predict(inputs):
|
65 |
+
return _predict(str(inputs))
|
66 |
+
|
67 |
+
|
68 |
+
@lru_cache()
|
69 |
+
def _predict(inputs):
|
70 |
data = load_manifest(inputs)
|
71 |
urls = get_image_urls_from_manifest(data)
|
72 |
resized_urls = [resize_iiif_urls(url) for url in urls]
|
|
|
75 |
images = list(pluck(1, images_urls))
|
76 |
urls = list(pluck(0, images_urls))
|
77 |
predictions = classif_pipeline(images, top_k=1)
|
78 |
+
for url, pred in zip(urls, predictions):
|
79 |
top_pred = pred[0]
|
80 |
if top_pred['label'] == 'illustrated':
|
81 |
image_url = IIIFImageClient.init_from_url(url)
|
|
|
91 |
# predicted_images.append((image, top_pred['score']))
|
92 |
# return predicted_images
|
93 |
|
94 |
+
|
95 |
gallery = gr.Gallery()
|
96 |
gallery.style(grid=3)
|
97 |
|
|
|
101 |
outputs=gallery,
|
102 |
title="ImageIN",
|
103 |
description="Identify illustrations in pages of historical books!",
|
104 |
+
examples=['https://iiif.lib.harvard.edu/manifests/drs:427603172']
|
105 |
)
|
106 |
demo.launch(debug=True)
|