ckyrkou commited on
Commit
f1a9b07
·
verified ·
1 Parent(s): 243c318

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.py +37 -21
  2. requirements.txt.bak +1 -0
app.py CHANGED
@@ -1,30 +1,46 @@
1
  import gradio as gr
2
  import onnxruntime as ort
 
 
3
 
4
  # Load the ONNX model
5
- ort_session = ort.InferenceSession("superresolution.onnx")
6
-
7
- # Define the prediction function
8
- def predict(image):
9
- # Preprocess image (resize, normalization)
10
- # ... (Implement your pre-processing logic here)
11
-
12
- # Convert image to input format for ONNX model
13
- # ... (Convert image to tensor or format expected by the model)
14
-
 
15
  # Run inference
16
- outputs = ort_session.run(None, {"input": input_data})
17
-
18
- # Postprocess output (denormalization, resizing)
19
- # ... (Implement your post-processing logic here)
20
-
21
- # Return the enhanced image
22
- return enhanced_image
 
 
 
 
23
 
24
  # Define the Gradio interface
25
- inputs = gr.Image(label="Input Image")
26
- outputs = gr.Image(label="Enhanced Image")
27
- interface = gr.Interface(fn=predict, inputs=inputs, outputs=outputs)
 
 
 
 
 
 
 
28
 
29
  # Launch the Gradio app
30
- launch(interface, debug=True)
 
 
 
1
  import gradio as gr
2
  import onnxruntime as ort
3
+ import numpy as np
4
+ from PIL import Image
5
 
6
  # Load the ONNX model
7
+ model_path = "modelSR.onnx" # Replace with your model path
8
+ ort_session = ort.InferenceSession(model_path)
9
+
10
+ # Define the superresolution function
11
+ def superresolve(image):
12
+ # Preprocess the image
13
+ image = np.array(image.resize((256, 256))) # Resize to expected input size
14
+ image = image[:, :, ::-1] # Convert to BGR (some models expect this)
15
+ image = np.transpose(image, (2, 0, 1)) # Transpose to NHWC format
16
+ image = image[np.newaxis, ...] # Add batch dimension
17
+
18
  # Run inference
19
+ ort_inputs = {ort_session.get_inputs()[0].name: image.astype(np.float32)}
20
+ ort_outs = ort_session.run(None, ort_inputs)
21
+ output = ort_outs[0][0]
22
+
23
+ # Postprocess the output
24
+ output = output.transpose(1, 2, 0) # Transpose back to HWC
25
+ output = np.clip(output, 0, 1) # Clip values to [0, 1]
26
+ output = (output * 255).astype(np.uint8) # Convert to uint8 for PIL
27
+ output = Image.fromarray(output[:, :, ::-1]) # Convert back to RGB
28
+
29
+ return output
30
 
31
  # Define the Gradio interface
32
+ interface = gr.Interface(
33
+ fn=superresolve,
34
+ inputs="image",
35
+ outputs="image",
36
+ title="Super Resolution",
37
+ description="Upload an image to upscale its resolution.",
38
+ allow_flagging=False,
39
+ thumbnail="https://i.imgur.com/nXhVq3y.png", # Optional thumbnail image
40
+ theme="huggingface", # Choose a theme (optional)
41
+ )
42
 
43
  # Launch the Gradio app
44
+ interface.launch(server_port=12345) # Change port if needed
45
+
46
+ print("App launched! Visit http://localhost:12345 to use it.")
requirements.txt.bak ADDED
@@ -0,0 +1 @@
 
 
1
+ onnxruntime