File size: 1,618 Bytes
8a5ef6c
 
6c7bbb9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import os
os.system("pip install openvino-dev==2023.0.1")
import gradio as gr
from openvino.inference_engine import IECore
import cv2
import numpy as np
from PIL import Image

# Load the OpenVINO model
model_xml = 'model.xml'
model_bin = 'model.bin'

ie = IECore()
net = ie.read_network(model=model_xml, weights=model_bin)
exec_net = ie.load_network(network=net, device_name="CPU")

# Define the function for image processing
def upscale_image(input_image):
    # Convert Gradio PIL Image to numpy array
    input_image = np.array(input_image)

    # Preprocess the input image
    image = cv2.cvtColor(input_image, cv2.COLOR_RGB2BGR)
    image = cv2.resize(image, (224, 224))
    image = image / 255.0
    image = np.transpose(image, (2, 0, 1))
    image = image.reshape(1, 3, 224, 224)

    # Run inference
    outputs = exec_net.infer(inputs={'input': image})

    # Post-process the output
    output_image = outputs['output'][0]
    output_image = np.transpose(output_image, (1, 2, 0))
    output_image = np.clip(output_image, 0, 1) * 255
    output_image = output_image.astype(np.uint8)
    output_image = cv2.cvtColor(output_image, cv2.COLOR_RGB2BGR)
    return output_image

# Create the Gradio interface
inputs = gr.inputs.Image(type="pil", label="Input Image")  # Use 'pil' type for uploaded images
outputs = gr.outputs.Image(type="pil", label="Upscaled Image")

title = "Image Upscaling App"
description = "Upload an image and see the upscaled result."
iface = gr.Interface(fn=upscale_image, inputs=inputs, outputs=outputs, title=title, description=description)

# Launch the Gradio interface
iface.launch()