diff --git a/README.MD b/README.MD index 13576e3..259fd15 100644 --- a/README.MD +++ b/README.MD @@ -90,8 +90,7 @@ add dependency in your project eu.europeana.metis.image.enhancement metis-image-enhancer-client - 0.0.1-SNAPSHOT - compile + 1 ```` ### properties @@ -102,6 +101,20 @@ api.url = http://localhost:5050 connect.timeout = 300 read.timeout = 300 ``` +#### command line stand alone script +If you only want to use the image processing without the API, you can invoke this +from the command line, using the script directly. + +``` +python ./metis-image-enhancer-python-rest/src/main/mie.py -i image_input.jpg -o image_output.jpg +``` + + +## General limitations +Why this AI python model runs as a script? in this iteration the python model cannot be held +in memory otherwise you will get an Out of Memory. The model in python of image processing +doesn't free the resources every time. +Steps of usage: load, process, get results, discard, repeat again. #### See demo project run docker image previous to execute the demo app and then execute the following command in the cloned folder of diff --git a/metis-image-enhancer-client/src/main/java/eu/europeana/metis/image/enhancement/domain/worker/ImageEnhancerWorker.java b/metis-image-enhancer-client/src/main/java/eu/europeana/metis/image/enhancement/domain/worker/ImageEnhancerWorker.java index 836829f..a708036 100644 --- a/metis-image-enhancer-client/src/main/java/eu/europeana/metis/image/enhancement/domain/worker/ImageEnhancerWorker.java +++ b/metis-image-enhancer-client/src/main/java/eu/europeana/metis/image/enhancement/domain/worker/ImageEnhancerWorker.java @@ -22,6 +22,7 @@ */ public class ImageEnhancerWorker { private static final Logger LOGGER = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass()); + private static final boolean IS_FILTER_ENABLED = false; private final ImageEnhancer imageEnhancer; /** @@ -70,7 +71,9 @@ public byte[] enhance(byte[] input) { final String format = getImageFormat(input); -// ImageIO.write(sharpen(image), format, baos); + if (IS_FILTER_ENABLED) { + ImageIO.write(sharpen(image), format, baos); + } ImageIO.write(image, format, baos); } catch (IOException e) { LOGGER.error("enhancing the image", e); diff --git a/metis-image-enhancer-client/src/main/resources/application.properties b/metis-image-enhancer-client/src/main/resources/application.properties deleted file mode 100644 index ea99a42..0000000 --- a/metis-image-enhancer-client/src/main/resources/application.properties +++ /dev/null @@ -1,5 +0,0 @@ -isr.api.url = http://localhost:5050 -isr.connect.timeout = 300 -isr.read.timeout = 300 -isr.script = ../metis-image-enhancer-python-script/src/main/mie.py - diff --git a/metis-image-enhancer-demo/.gitignore b/metis-image-enhancer-demo/.gitignore index 75cba8d..29eee14 100644 --- a/metis-image-enhancer-demo/.gitignore +++ b/metis-image-enhancer-demo/.gitignore @@ -1,3 +1,4 @@ +/src/main/resources/application.properties README.MD target/ !.mvn/wrapper/maven-wrapper.jar diff --git a/metis-image-enhancer-demo/src/main/resources/application.properties b/metis-image-enhancer-demo/src/main/resources/application.properties index 306b393..ea72b57 100644 --- a/metis-image-enhancer-demo/src/main/resources/application.properties +++ b/metis-image-enhancer-demo/src/main/resources/application.properties @@ -2,5 +2,5 @@ isr.api.url = http://localhost:8080 isr.connect.timeout = 300 isr.read.timeout = 300 -isr.script = ../metis-image-enhancer-python-script/src/main/mie.py +isr.script = ../metis-image-enhancer-python-rest/src/main/mie.py worker.service.type = api diff --git a/metis-image-enhancer-demo/src/main/resources/application.properties.example b/metis-image-enhancer-demo/src/main/resources/application.properties.example index b7f6eef..0f8a84a 100644 --- a/metis-image-enhancer-demo/src/main/resources/application.properties.example +++ b/metis-image-enhancer-demo/src/main/resources/application.properties.example @@ -2,5 +2,5 @@ isr.api.url = http://localhost:8080 isr.connect.timeout = 300 isr.read.timeout = 300 -isr.script = ../metis-image-enhancer-python-script/src/main/mie.py +isr.script = ../metis-image-enhancer-python-rest/src/main/mie.py worker.service.type = script diff --git a/metis-image-enhancer-python-rest/Dockerfile b/metis-image-enhancer-python-rest/Dockerfile index 6f1391d..2b31927 100644 --- a/metis-image-enhancer-python-rest/Dockerfile +++ b/metis-image-enhancer-python-rest/Dockerfile @@ -1,21 +1,26 @@ -FROM ubuntu:22.04 +FROM ubuntu:24.10 RUN apt-get update && apt-get upgrade -y RUN apt-get install git -y -RUN apt-get install python3-pip -y +RUN apt install python3 python3-pip python3-venv python3-setuptools python3-pil python3-filetype python3-flask python3-waitress -y +RUN mkdir flask-app && cd flask-app +RUN python3 -m venv flask-venv +RUN . flask-venv/bin/activate -RUN pip3 install --upgrade pip -RUN pip3 install filetype -RUN pip3 install Flask -RUN pip3 install waitress -RUN pip3 install uuid -RUN pip3 install tensorflow==2.13.0 +RUN flask-venv/bin/pip3 install --upgrade pip +RUN flask-venv/bin/pip3 install filetype +RUN flask-venv/bin/pip3 install Flask +RUN flask-venv/bin/pip3 install waitress +RUN flask-venv/bin/pip3 install uuid +RUN flask-venv/bin/pip3 install pillow +RUN flask-venv/bin/pip3 install tensorflow==2.17.0 numpy==1.26.0 RUN git clone https://github.com/idealo/image-super-resolution.git \ && cd image-super-resolution \ && git checkout v2.2.0 \ - && sed -i 's/tensorflow==2.0.0/tensorflow==2.13.0/' setup.py \ - && sed -i 's/tensorflow-gpu==2.0.0/tensorflow-gpu==2.13.0/' setup.py \ + && sed -i 's/numpy/numpy==1.26.0/' setup.py \ + && sed -i 's/tensorflow==2.0.0/tensorflow==2.17.0/' setup.py \ + && sed -i 's/tensorflow-gpu==2.0.0/tensorflow-gpu==2.17.0/' setup.py \ && python3 setup.py install COPY src/main/api.py . diff --git a/metis-image-enhancer-python-rest/src/main/mie.py b/metis-image-enhancer-python-rest/src/main/mie.py index 8b02815..280d2f7 100644 --- a/metis-image-enhancer-python-rest/src/main/mie.py +++ b/metis-image-enhancer-python-rest/src/main/mie.py @@ -16,10 +16,10 @@ # Arguments def parameters(argv): arg_help = "{0} -i -o ".format(argv[0]) - params = dict(); + params = dict() try: - opts, args = getopt.getopt(argv[1:], "hio:v", ["help", "input=", "output="]) + opts = getopt.getopt(argv[1:], "hio:v", ["help", "input=", "output="]) except: print(arg_help) sys.exit(2) @@ -84,9 +84,9 @@ def main(params): # elapsed time print ('loading model elapsed time: ',end - start) # Declare the input and output file locations - inputFile = r'{0}'.format(params['arg_input']).strip() - outputFile = r'{0}'.format(params['arg_output']).strip() - enhance(inputFile, outputFile, model) + input_file = r'{0}'.format(params['arg_input']).strip() + output_file = r'{0}'.format(params['arg_output']).strip() + enhance(input_file, output_file, model) if __name__ == "__main__": main(parameters(sys.argv)) diff --git a/metis-image-enhancer-python-script/pom.xml b/metis-image-enhancer-python-script/pom.xml deleted file mode 100644 index fe85ad6..0000000 --- a/metis-image-enhancer-python-script/pom.xml +++ /dev/null @@ -1,19 +0,0 @@ - - - 4.0.0 - - eu.europeana.metis - metis-image-enhancer - 1-SNAPSHOT - - - eu.europeana.metis.image.enhancement - metis-image-enhancer-python-script - - - UTF-8 - - - diff --git a/metis-image-enhancer-python-script/src/main/mie.py b/metis-image-enhancer-python-script/src/main/mie.py deleted file mode 100644 index 8b02815..0000000 --- a/metis-image-enhancer-python-script/src/main/mie.py +++ /dev/null @@ -1,92 +0,0 @@ -import numpy as np -import sys -import os -import getopt -import time -import filetype - -from PIL import Image -from ISR.models import RDN - -workingdirectory = os.getcwd() - -# working directory -print('working dir:', workingdirectory) - -# Arguments -def parameters(argv): - arg_help = "{0} -i -o ".format(argv[0]) - params = dict(); - - try: - opts, args = getopt.getopt(argv[1:], "hio:v", ["help", "input=", "output="]) - except: - print(arg_help) - sys.exit(2) - - if len(opts) == 0: - print(arg_help) - else: - for opt, arg in opts: - if opt in ("-h", "--help"): - print(arg_help) # print the help message - sys.exit(2) - elif opt in ("-i", "--input"): - params['arg_input'] = arg - - elif opt in ("-o", "--output"): - params[ 'arg_output'] = arg - - print('args:') - for elem in map(str,params): - print(elem+":"+params[elem], sep="\n") - - return params -def enhance(input_file, output_file, model): - # Read the image from the file - # convert image to RGB format with white background - small_image = Image.open(input_file).convert("RGBA") - rgb_img = Image.new("RGBA", small_image.size, "WHITE") - rgb_img.paste(small_image, mask=small_image) - - # Enhance the image - enhanced_image = model.predict(np.array(rgb_img.convert("RGB"))) - # find image format - kind = filetype.guess(input_file) - description = "" - if kind is None or kind.extension not in ['jpg', 'bmp', 'gif', 'tif', 'png', 'apng', 'ico']: - if kind is None: - description = 'file type not supported!' - else: - description = 'file type '+kind.extension+' not supported!' - - if description != "": - print(description) - return 1 - - if kind.extension == 'jpg': - img_format = "jpeg" - elif kind.extension == 'tif': - img_format = "tiff" - elif kind.extension == 'apng': - img_format = "png" - else: - img_format = kind.extension - # Write the image to the output file. - Image.fromarray(enhanced_image).save(output_file, img_format) - return 0 -def main(params): - # Load the prediction model. Can be initialized once and then reused? - start = time.time() - model = RDN(weights='noise-cancel') - end = time.time() - - # elapsed time - print ('loading model elapsed time: ',end - start) - # Declare the input and output file locations - inputFile = r'{0}'.format(params['arg_input']).strip() - outputFile = r'{0}'.format(params['arg_output']).strip() - enhance(inputFile, outputFile, model) - -if __name__ == "__main__": - main(parameters(sys.argv)) diff --git a/pom.xml b/pom.xml index 627fcb3..0a9b867 100644 --- a/pom.xml +++ b/pom.xml @@ -12,10 +12,9 @@ metis-image-enhancer-demo metis-image-enhancer-client metis-image-enhancer-python-rest - metis-image-enhancer-python-script - 11 + 21 2.7.11 5.9.3 5.4.0