Skip to content
Snippets Groups Projects
Commit e2317bf0 authored by onb1259's avatar onb1259
Browse files

fix not existing directories

parent 7e86a177
No related branches found
No related tags found
No related merge requests found
%% Cell type:markdown id:82ab6471 tags:
# Extract figures
This notebook uses a YOLOv8 model trained on five annotated ABO books (1700 pages) to extract figures from them.
After giving the iiif manifest url of the book from which you want to extract images, the book pages get dwonloaded, the figure recognition model gets applied and the resulting bounding boxes get extracted and can be donwloaded as a zip file.
%% Cell type:code id:e1bcdac6 tags:
``` python
!pip install pillow
!pip install zipp
!pip install ultralytics
import os, shutil
from zipapp import zipfile
from PIL import Image
from ultralytics import YOLO
from IPython.display import display, FileLink, HTML
from iiif_utils import get_imgurls_from_manifesturl, create_paths_from_iiifurls, download_images_multithreded
# Url of iiif manifest
input_manifest = 'https://iiif.onb.ac.at/presentation/ABO/+Z105572305/manifest/'
```
%% Cell type:code id:65afd0ba tags:
``` python
# Donwload the book pages
IMAGES_DIR = 'images'
imgurls = get_imgurls_from_manifesturl(input_manifest)
imgpaths = create_paths_from_iiifurls(imgurls)
shutil.rmtree(IMAGES_DIR)
if os.path.isdir(IMAGES_DIR):
shutil.rmtree(IMAGES_DIR)
os.mkdir(IMAGES_DIR)
imgpaths = [os.path.join(IMAGES_DIR, e) for e in imgpaths]
download_images_multithreded(imgurls, imgpaths, 10)
```
%% Cell type:code id:c6b37e20 tags:
``` python
# Apply figure extraction model
RESULTS_DIR = 'extracted_figures'
shutil.rmtree(RESULTS_DIR)
if os.path.isdir(RESULTS_DIR):
shutil.rmtree(RESULTS_DIR)
os.mkdir(RESULTS_DIR)
model = YOLO('model_extract_figures.pt')
counter = 0
for path in imgpaths:
result = model.predict(path, verbose=False)
img = Image.open(path)
for i, bb in enumerate(result[0].boxes.xyxy.tolist()):
#print(f'Figure detected in: {path}')
counter += 1
bb = [int(e) for e in bb]
img_cropped = img.crop(bb)
path_cropped = os.path.join(RESULTS_DIR, os.path.basename(path.rsplit('.', 1)[0] + '_crop' + str(i) + '.' + path.rsplit('.', 1)[1]))
img_cropped.save(path_cropped)
print(f'Total number of extracted figures: {counter}')
```
%% Cell type:code id:a997833e tags:
``` python
# Create a zip file with extracted figures and show them
RESULT_ZIP = 'exatrcted_figures.zip'
with zipfile.ZipFile(RESULT_ZIP, 'w') as myzip:
for f in os.listdir(RESULTS_DIR):
myzip.write(os.path.join(RESULTS_DIR, f))
zipped_extracted_figures = FileLink(RESULT_ZIP, result_html_prefix="Click here to download zipped extracted figures: ")
display(zipped_extracted_figures)
def show_images(filenames, width, margin):
lis = [f'<img src="{name}" style="display:inline;margin:{margin}px" width="{width}"/>' for name in filenames]
html = ''.join(lis)
display(HTML(html))
show_images([os.path.join(RESULTS_DIR, e) for e in sorted(os.listdir(RESULTS_DIR))], 150, 1)
```
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment