File size: 1,401 Bytes
7094aa3
ffe582f
 
 
 
 
 
 
7094aa3
ffe582f
 
ea7c504
03def4f
ea7c504
 
03def4f
 
 
2bbe009
 
 
 
 
 
 
ffe582f
2bbe009
 
 
 
947bd52
 
2bbe009
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
from huggingface_hub import from_pretrained_keras
from tensorflow import keras
from PIL import Image
import numpy as np
import io


def create_model() -> keras.Model:
    return from_pretrained_keras("keras-io/lowlight-enhance-mirnet")


def run_model(image_bytes: io.BytesIO, model: keras.Model) -> Image.Image:
    image1: Image.Image = Image.open(image_bytes)
    width: int
    height: int
    width, height = image1.size
    image2: Image.Image = image1.resize((960, 640))
    image_array1: np.ndarray = keras.utils.img_to_array(image2)
    image_array2: np.ndarray = image_array1.astype("float32") / 255.0
    image_array3: np.ndarray = np.expand_dims(image_array2, axis=0)
    output: np.ndarray = model.predict(image_array3)
    output_image_array1: np.ndarray = output[0] * 255.0
    output_image_array2: np.ndarray = output_image_array1.clip(0, 255)
    output_image_array3: np.ndarray = output_image_array2.reshape(
        (np.shape(output_image_array2)[0], np.shape(output_image_array2)[1], 3)
    )
    output_image_array4: np.ndarray = np.uint32(output_image_array3)
    output_image_array5: np.ndarray = output_image_array4.astype(np.uint8)
    output_image1: Image.Image = Image.fromarray(output_image_array5)
    output_image2: Image.Image = output_image1.resize((width, height))
    # Uncomment if necessary:
    # output_image.save("user_data/output.jpg")
    return output_image2