Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -4,6 +4,7 @@ import PIL.Image
|
|
4 |
from PIL import Image
|
5 |
from io import BytesIO
|
6 |
import requests
|
|
|
7 |
import torch.nn as nn
|
8 |
import os
|
9 |
import tempfile
|
@@ -42,7 +43,25 @@ def add_margin(pil_img, top, right, bottom, left, color):
|
|
42 |
result.paste(pil_img, (left, top))
|
43 |
return result
|
44 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
45 |
def inference(image_path_or_url, learn):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
46 |
if image_path_or_url.startswith('http://') or image_path_or_url.startswith('https://'):
|
47 |
response = requests.get(image_path_or_url)
|
48 |
img = PIL.Image.open(BytesIO(response.content)).convert("RGB")
|
@@ -53,7 +72,7 @@ def inference(image_path_or_url, learn):
|
|
53 |
im_new.save("test.jpg", quality=95)
|
54 |
img = open_image("test.jpg")
|
55 |
p, img_hr, b = learn.predict(img)
|
56 |
-
return img_hr
|
57 |
|
58 |
# Streamlit application
|
59 |
st.title("Image Inference with Fastai")
|
|
|
4 |
from PIL import Image
|
5 |
from io import BytesIO
|
6 |
import requests
|
7 |
+
import torch
|
8 |
import torch.nn as nn
|
9 |
import os
|
10 |
import tempfile
|
|
|
43 |
result.paste(pil_img, (left, top))
|
44 |
return result
|
45 |
|
46 |
+
def tensor_to_pil(tensor):
|
47 |
+
"""
|
48 |
+
Convert a tensor to a PIL Image.
|
49 |
+
"""
|
50 |
+
tensor = tensor.cpu().clamp(0, 1)
|
51 |
+
array = tensor.numpy().transpose(1, 2, 0)
|
52 |
+
return Image.fromarray((array * 255).astype('uint8'))
|
53 |
+
|
54 |
def inference(image_path_or_url, learn):
|
55 |
+
"""
|
56 |
+
Perform inference on an image from a local path or a URL.
|
57 |
+
|
58 |
+
Parameters:
|
59 |
+
image_path_or_url (str): Path to the local image or URL of the image.
|
60 |
+
learn (Learner): The trained model.
|
61 |
+
|
62 |
+
Returns:
|
63 |
+
PIL.Image.Image: The high-resolution image generated by the model.
|
64 |
+
"""
|
65 |
if image_path_or_url.startswith('http://') or image_path_or_url.startswith('https://'):
|
66 |
response = requests.get(image_path_or_url)
|
67 |
img = PIL.Image.open(BytesIO(response.content)).convert("RGB")
|
|
|
72 |
im_new.save("test.jpg", quality=95)
|
73 |
img = open_image("test.jpg")
|
74 |
p, img_hr, b = learn.predict(img)
|
75 |
+
return tensor_to_pil(img_hr)
|
76 |
|
77 |
# Streamlit application
|
78 |
st.title("Image Inference with Fastai")
|