Danila-Pechenev commited on
Commit
ffe582f
·
1 Parent(s): cf23f59

Implement application

Browse files
Files changed (2) hide show
  1. app/main.py +42 -0
  2. app/model.py +30 -0
app/main.py ADDED
@@ -0,0 +1,42 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from tensorflow import keras
3
+ from PIL import Image
4
+ import io
5
+
6
+ import model
7
+
8
+
9
+ def configure():
10
+ st.set_page_config(page_title="Low-light image enhancement")
11
+ if "model" not in st.session_state:
12
+ st.session_state["model"]: keras.Model = model.create_model()
13
+
14
+
15
+ def describe_service():
16
+ st.title("Low-light image enhancement")
17
+ st.subheader("Just upload your low-light image and get the processed one!")
18
+
19
+
20
+ def process_image():
21
+ uploaded_file: io.BytesIO = st.file_uploader(
22
+ label="Choose a file (you can upload new files without refreshing the page)",
23
+ type=["png", "jpg", "jpeg"],
24
+ )
25
+ if uploaded_file:
26
+ placeholder: st.delta_generator.DeltaGenerator = st.empty()
27
+ placeholder.info(
28
+ "The image is being processed. It may take some time. Wait, please..."
29
+ )
30
+ image: Image.Image = model.run_model(uploaded_file, st.session_state["model"])
31
+ placeholder.empty()
32
+ placeholder.image(image)
33
+
34
+
35
+ def main():
36
+ describe_service()
37
+ process_image()
38
+
39
+
40
+ if __name__ == "__main__":
41
+ configure()
42
+ main()
app/model.py ADDED
@@ -0,0 +1,30 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from tensorflow import keras
2
+ from PIL import Image
3
+ import numpy as np
4
+ import io
5
+
6
+
7
+ def create_model() -> keras.Model:
8
+ return keras.models.load_model("model")
9
+
10
+
11
+ def run_model(image: io.BytesIO, model: keras.Model) -> Image.Image:
12
+ image: Image.Image = Image.open(image)
13
+ width: int = image.size[0]
14
+ height: int = image.size[1]
15
+ image: Image.Image = image.resize((960, 640))
16
+ image: np.ndarray = keras.utils.img_to_array(image)
17
+ image: np.ndarray = image.astype("float32") / 255.0
18
+ image: np.ndarray = np.expand_dims(image, axis=0)
19
+ output: np.ndarray = model.predict(image)
20
+ output_image: np.ndarray = output[0] * 255.0
21
+ output_image: np.ndarray = output_image.clip(0, 255)
22
+ output_image: np.ndarray = output_image.reshape(
23
+ (np.shape(output_image)[0], np.shape(output_image)[1], 3)
24
+ )
25
+ output_image: np.ndarray = np.uint32(output_image)
26
+ output_image: np.ndarray = output_image.astype(np.uint8)
27
+ output_image: Image.Image = Image.fromarray(output_image)
28
+ output_image: Image.Image = output_image.resize((width, height))
29
+ output_image.save("user_data/output.jpg")
30
+ return output_image