Danila-Pechenev commited on
Commit
ea7c504
1 Parent(s): 78f871e

Clean code

Browse files
Files changed (2) hide show
  1. app/main.py +1 -3
  2. app/model.py +16 -15
app/main.py CHANGED
@@ -24,9 +24,7 @@ def process_image():
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)
 
24
  )
25
  if uploaded_file:
26
  placeholder: st.delta_generator.DeltaGenerator = st.empty()
27
+ placeholder.info("The image is being processed. It may take some time. Wait, please...")
 
 
28
  image: Image.Image = model.run_model(uploaded_file, st.session_state["model"])
29
  placeholder.empty()
30
  placeholder.image(image)
app/model.py CHANGED
@@ -9,23 +9,24 @@ def create_model() -> keras.Model:
9
  return from_pretrained_keras("keras-io/lowlight-enhance-mirnet")
10
 
11
 
12
- def run_model(image: io.BytesIO, model: keras.Model) -> Image.Image:
13
- image: Image.Image = Image.open(image)
14
- width: int = image.size[0]
15
- height: int = image.size[1]
 
16
  image: Image.Image = image.resize((960, 640))
17
- image: np.ndarray = keras.utils.img_to_array(image)
18
- image: np.ndarray = image.astype("float32") / 255.0
19
- image: np.ndarray = np.expand_dims(image, axis=0)
20
- output: np.ndarray = model.predict(image)
21
- output_image: np.ndarray = output[0] * 255.0
22
- output_image: np.ndarray = output_image.clip(0, 255)
23
- output_image: np.ndarray = output_image.reshape(
24
- (np.shape(output_image)[0], np.shape(output_image)[1], 3)
25
  )
26
- output_image: np.ndarray = np.uint32(output_image)
27
- output_image: np.ndarray = output_image.astype(np.uint8)
28
- output_image: Image.Image = Image.fromarray(output_image)
29
  output_image: Image.Image = output_image.resize((width, height))
30
  output_image.save("user_data/output.jpg")
31
  return output_image
 
9
  return from_pretrained_keras("keras-io/lowlight-enhance-mirnet")
10
 
11
 
12
+ def run_model(image_bytes: io.BytesIO, model: keras.Model) -> Image.Image:
13
+ image: Image.Image = Image.open(image_bytes)
14
+ width: int
15
+ height: int
16
+ width, height = image.size
17
  image: Image.Image = image.resize((960, 640))
18
+ image_array: np.ndarray = keras.utils.img_to_array(image)
19
+ image_array: np.ndarray = image_array.astype("float32") / 255.0
20
+ image_array: np.ndarray = np.expand_dims(image_array, axis=0)
21
+ output: np.ndarray = model.predict(image_array)
22
+ output_image_array: np.ndarray = output[0] * 255.0
23
+ output_image_array: np.ndarray = output_image_array.clip(0, 255)
24
+ output_image_array: np.ndarray = output_image_array.reshape(
25
+ (np.shape(output_image_array)[0], np.shape(output_image_array)[1], 3)
26
  )
27
+ output_image_array: np.ndarray = np.uint32(output_image_array)
28
+ output_image_array: np.ndarray = output_image_array.astype(np.uint8)
29
+ output_image: Image.Image = Image.fromarray(output_image_array)
30
  output_image: Image.Image = output_image.resize((width, height))
31
  output_image.save("user_data/output.jpg")
32
  return output_image