run in google colab
friend, could you provide it as an ipynb file to run in colab, or how could I run it in colab, please
Hi @GabrielBR , here's a Colab notebook showing how to run our models:
https://colab.research.google.com/drive/1Cg05e_Nxl1vhV6JZQjkD8vSQnQ8-6eX0?usp=sharing
Cheers
friend, this error is occurring, is there a video teaching how to execute it?
FileNotFoundError                         Traceback (most recent call last)
/tmp/ipykernel_36/158458528.py in <cell line: 0>()
      1 from PIL import Image
      2
----> 3 image = Image.open("example_1.png").convert("RGB")
      4 upscaled_image = upscale(image, "modelx4.ort")
/usr/local/lib/python3.11/dist-packages/PIL/Image.py in open(fp, mode, formats)
   3503
   3504     if filename:
-> 3505         fp = builtins.open(filename, "rb")
   3506         exclusive_fp = True
   3507     else:
FileNotFoundError: [Errno 2] No such file or directory: 'example_1.png'
@GabrielBR , we don't have a video. The error pertains to a wrong image path. You have to upload the image you want to upscale to Google Colab, and then change the path to that image.
image = Image.open("YOUR_IMAGE").convert("RGB")
friend, what am I doing wrong? I couldn't use it: modelx2.ort
!pip install -q onnxruntime
import numpy as np
import cv2
import onnxruntime
def pre_process(img: np.array) -> np.array:
    # H, W, C -> C, H, W
    img = np.transpose(img[:, :, 0:3], (2, 0, 1))
    # C, H, W -> 1, C, H, W
    img = np.expand_dims(img, axis=0).astype(np.float32)
    return img
def post_process(img: np.array) -> np.array:
    # 1, C, H, W -> C, H, W
    img = np.squeeze(img)
    # C, H, W -> H, W, C
    img = np.transpose(img, (1, 2, 0))[:, :, ::-1].astype(np.uint8)
    return img
def inference(model_path: str, img_array: np.array) -> np.array:
    options = onnxruntime.SessionOptions()
    options.intra_op_num_threads = 1
    options.inter_op_num_threads = 1
    ort_session = onnxruntime.InferenceSession(model_path, options)
    ort_inputs = {ort_session.get_inputs()[0].name: img_array}
    ort_outs = ort_session.run(None, ort_inputs)
return ort_outs[0]
def convert_pil_to_cv2(image):
    # pil_image = image.convert("RGB")
    open_cv_image = np.array(image)
    # RGB to BGR
    open_cv_image = open_cv_image[:, :, ::-1].copy()
    return open_cv_image
def upscale(image, model_path):
    img = convert_pil_to_cv2(image)
    if img.ndim == 2:
        img = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)
if img.shape[2] == 4:
    alpha = img[:, :, 3]  # GRAY
    alpha = cv2.cvtColor(alpha, cv2.COLOR_GRAY2BGR)  # BGR
    alpha_output = post_process(inference(model_path, pre_process(alpha)))  # BGR
    alpha_output = cv2.cvtColor(alpha_output, cv2.COLOR_BGR2GRAY)  # GRAY
    img = img[:, :, 0:3]  # BGR
    image_output = post_process(inference(model_path, pre_process(img)))  # BGR
    image_output = cv2.cvtColor(image_output, cv2.COLOR_BGR2BGRA)  # BGRA
    image_output[:, :, 3] = alpha_output
elif img.shape[2] == 3:
    image_output = post_process(inference(model_path, pre_process(img)))  # BGR
return image_output
download sample image
!wget https://huggingface.co/spaces/bookbot/Image-Upscaling-Playground/resolve/main/examples/example_1.png
NOTE: change to download whichever model you want!
!wget https://huggingface.co/spaces/bookbot/Image-Upscaling-Playground/resolve/main/models/modelx2.ort
from PIL import Image
image = Image.open("1 clarity-upscaler.png").convert("RGB")
upscaled_image = upscale(image, "modelx2.ort")
upscaled_image
array([[[244, 166,  36],
        [250, 169,  16],
        [255, 162,   4],
        ...,
        [247, 157,  10],
        [243, 151,  16],
        [223, 161,  42]],
   [[247, 168,  22],
    [255, 160,  12],
    [254, 164,   2],
    ...,
    [252, 156,   4],
    [247, 164,   6],
    [241, 158,  13]],
   [[249, 159,   3],
    [249, 153,   0],
    [254, 160,   0],
    ...,
    [247, 158,   1],
    [249, 163,   3],
    [250, 161,   1]],
   ...,
   [[ 17,  25,  46],
    [ 18,  25,  46],
    [ 18,  26,  47],
    ...,
    [ 28,  26,  42],
    [ 27,  25,  41],
    [ 27,  24,  38]],
   [[ 20,  26,  46],
    [ 19,  26,  47],
    [ 21,  27,  48],
    ...,
    [ 29,  25,  39],
    [ 27,  24,  37],
    [ 24,  22,  35]],
   [[ 21,  26,  43],
    [ 22,  26,  46],
    [ 22,  27,  48],
    ...,
    [ 29,  24,  38],
    [ 26,  22,  35],
    [ 24,  22,  33]]], dtype=uint8)
@GabrielBR , there is nothing wrong with your code. The final result is just the upscaled image in raw array format. You just need to export that to a saveable image:
Image.fromarray(upscaled_image).save("output.png")
It works now, I never messed with codes, thanks for your attention friend, I'm from Brazil

