Upload app.py
Browse files
app.py
ADDED
@@ -0,0 +1,69 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import numpy as np
|
3 |
+
from PIL import Image
|
4 |
+
import os
|
5 |
+
import cv2
|
6 |
+
import random
|
7 |
+
import numpy as np
|
8 |
+
from glob import glob
|
9 |
+
from PIL import Image, ImageOps
|
10 |
+
import matplotlib.pyplot as plt
|
11 |
+
|
12 |
+
import tensorflow as tf
|
13 |
+
from tensorflow import keras
|
14 |
+
from tensorflow.keras import layers
|
15 |
+
|
16 |
+
|
17 |
+
def plot_results(images, titles, figure_size=(12, 12)):
|
18 |
+
fig = plt.figure(figsize=figure_size)
|
19 |
+
for i in range(len(images)):
|
20 |
+
fig.add_subplot(1, len(images), i + 1).set_title(titles[i])
|
21 |
+
_ = plt.imshow(images[i])
|
22 |
+
plt.axis("off")
|
23 |
+
plt.show()
|
24 |
+
|
25 |
+
|
26 |
+
def infer(original_image):
|
27 |
+
image = keras.utils.img_to_array(original_image)
|
28 |
+
image = image.astype("float32") / 255.0
|
29 |
+
image = np.expand_dims(image, axis=0)
|
30 |
+
output = model.predict(image)
|
31 |
+
output_image = output[0] * 255.0
|
32 |
+
output_image = output_image.clip(0, 255)
|
33 |
+
output_image = output_image.reshape(
|
34 |
+
(np.shape(output_image)[0], np.shape(output_image)[1], 3)
|
35 |
+
)
|
36 |
+
output_image = Image.fromarray(np.uint8(output_image))
|
37 |
+
original_image = Image.fromarray(np.uint8(original_image))
|
38 |
+
return output_image
|
39 |
+
|
40 |
+
# Mock model for image prediction (replace this with your actual model)
|
41 |
+
def predict_image(img):
|
42 |
+
|
43 |
+
original_image = Image.open(uploaded_image)
|
44 |
+
enhanced_image = infer(original_image)
|
45 |
+
plot_results(
|
46 |
+
[original_image, enhanced_image],
|
47 |
+
["Original", "MIRNet Enhanced"],
|
48 |
+
(20, 12),
|
49 |
+
)
|
50 |
+
|
51 |
+
# Streamlit UI
|
52 |
+
st.title("Image Prediction App")
|
53 |
+
|
54 |
+
# Upload image window
|
55 |
+
uploaded_image = st.file_uploader("Choose an image...", type="jpg")
|
56 |
+
|
57 |
+
if uploaded_image is not None:
|
58 |
+
# Display uploaded image
|
59 |
+
image = Image.open(uploaded_image)
|
60 |
+
st.image(image, caption="Uploaded Image.", use_column_width=True)
|
61 |
+
|
62 |
+
# Button to run prediction
|
63 |
+
if st.button("Predict"):
|
64 |
+
# Get the prediction
|
65 |
+
prediction = predict_image(image)
|
66 |
+
|
67 |
+
# Display the prediction
|
68 |
+
st.write(f"Prediction: {prediction}")
|
69 |
+
st.image(image, caption=f"Predicted as {prediction}.", use_column_width=True)
|