rbarman commited on
Commit
6bad718
·
1 Parent(s): 47bec72

type hints + simplify code if input is numpy array

Browse files
Files changed (1) hide show
  1. app.py +5 -11
app.py CHANGED
@@ -16,22 +16,14 @@ output_layer = compiled_model.output(0)
16
  #####
17
  #Inference
18
  #####
19
- def predict(img):
20
-
21
- print(f'input type: {type(img)}')
22
-
23
- # https://stackoverflow.com/a/32264327
24
- image = cv2.cvtColor(np.array(img), cv2.COLOR_BGR2RGB)
25
 
26
  # The MobileNet model expects images in RGB format.
27
- #image = cv2.cvtColor(cv2.imread(img), code=cv2.COLOR_BGR2RGB)
28
  # Resize to MobileNet image shape.
29
  input_image = cv2.resize(src=image, dsize=(224, 224))
30
  # Reshape to model input shape.
31
  input_image = np.expand_dims(input_image, 0)
32
-
33
-
34
- # TODO: get n best results with corresponding probabilities?
35
 
36
  # Get inference result
37
  result_infer = compiled_model([input_image])[output_layer]
@@ -43,7 +35,9 @@ def predict(img):
43
  # The model description states that for this model, class 0 is a background.
44
  # Therefore, a background must be added at the beginning of imagenet_classes.
45
  imagenet_classes = ['background'] + imagenet_classes
46
- return imagenet_classes[result_index]
 
 
47
 
48
  #####
49
  #Gradio Setup
 
16
  #####
17
  #Inference
18
  #####
19
+ def predict(img: np.ndarray) -> str:
 
 
 
 
 
20
 
21
  # The MobileNet model expects images in RGB format.
22
+ image = cv2.cvtColor(img, code=cv2.COLOR_BGR2RGB)
23
  # Resize to MobileNet image shape.
24
  input_image = cv2.resize(src=image, dsize=(224, 224))
25
  # Reshape to model input shape.
26
  input_image = np.expand_dims(input_image, 0)
 
 
 
27
 
28
  # Get inference result
29
  result_infer = compiled_model([input_image])[output_layer]
 
35
  # The model description states that for this model, class 0 is a background.
36
  # Therefore, a background must be added at the beginning of imagenet_classes.
37
  imagenet_classes = ['background'] + imagenet_classes
38
+ best_class = imagenet_classes[result_index]
39
+ # TODO: get n best results with corresponding probabilities?
40
+ return best_class
41
 
42
  #####
43
  #Gradio Setup