23A066X commited on
Commit
adbc16f
1 Parent(s): 138d82c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +50 -27
app.py CHANGED
@@ -1,28 +1,23 @@
 
1
  import matplotlib.pyplot as plt
2
  import numpy as np
3
- from six import BytesIO
4
- from PIL import Image
5
- import tensorflow as tf
6
- from object_detection.utils import label_map_util
7
- from object_detection.utils import visualization_utils as viz_utils
8
- from object_detection.utils import ops as utils_op
9
  import tarfile
10
  import wget
11
- import gradio as gr
12
  from huggingface_hub import snapshot_download
13
  import os
14
-
15
- def greet(name):
16
- return "Hello " + name + "!!"
17
-
18
- #iface = gr.Interface(fn=greet, inputs="text", outputs="text")
19
- #iface.launch()
20
-
21
 
22
 
23
  PATH_TO_LABELS = 'label_map.pbtxt'
24
  category_index = label_map_util.create_category_index_from_labelmap(PATH_TO_LABELS, use_display_name=True)
25
 
 
 
26
  def pil_image_as_numpy_array(pilimg):
27
 
28
  img_array = tf.keras.utils.img_to_array(pilimg)
@@ -38,34 +33,62 @@ def load_image_into_numpy_array(path):
38
 
39
  def load_model():
40
  download_dir = snapshot_download(REPO_ID)
41
- saved_model_dir = os.path.join(download_dir, "saved_model.pb")
42
  detection_model = tf.saved_model.load(saved_model_dir)
43
  return detection_model
44
 
 
 
 
 
 
 
 
45
 
46
  def predict(pilimg):
47
 
48
  image_np = pil_image_as_numpy_array(pilimg)
49
  return predict2(image_np)
50
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
51
  detection_model = load_model()
52
  # pil_image = Image.open(image_path)
53
  # image_arr = pil_image_as_numpy_array(pil_image)
 
54
  # predicted_img = predict(image_arr)
55
  # predicted_img.save('predicted.jpg')
56
 
57
- REPO_ID = "23A066X/23A066X_model"
58
  gr.Interface(fn=predict,
59
- inputs=[gr.Image(type="pil",label="Input Image")],
60
- outputs=gr.Image(type="pil",label="Output Image"),
61
- title="Cauliflower and Beetroot Detection",
62
- description="Model: ssd_resnet50_v1_fpn_640x640_coco17_tpu-8",
63
- theme=gr.themes.Soft(primary_hue="blue", secondary_hue="sky")
64
  ).launch(share=True)
65
 
66
-
67
-
68
-
69
-
70
-
71
-
 
1
+ import gradio as gr
2
  import matplotlib.pyplot as plt
3
  import numpy as np
 
 
 
 
 
 
4
  import tarfile
5
  import wget
 
6
  from huggingface_hub import snapshot_download
7
  import os
8
+ from object_detection.utils import label_map_util
9
+ from object_detection.utils import visualization_utils as viz_utils
10
+ from object_detection.utils import ops as utils_op
11
+ from six import BytesIO
12
+ from PIL import Image
13
+ import tensorflow as tf
 
14
 
15
 
16
  PATH_TO_LABELS = 'label_map.pbtxt'
17
  category_index = label_map_util.create_category_index_from_labelmap(PATH_TO_LABELS, use_display_name=True)
18
 
19
+
20
+
21
  def pil_image_as_numpy_array(pilimg):
22
 
23
  img_array = tf.keras.utils.img_to_array(pilimg)
 
33
 
34
  def load_model():
35
  download_dir = snapshot_download(REPO_ID)
36
+ saved_model_dir = os.path.join(download_dir, "saved_model")
37
  detection_model = tf.saved_model.load(saved_model_dir)
38
  return detection_model
39
 
40
+ def load_model2():
41
+ wget.download("https://nyp-aicourse.s3-ap-southeast-1.amazonaws.com/pretrained-models/balloon_model.tar.gz")
42
+ tarfile.open("balloon_model.tar.gz").extractall()
43
+ model_dir = 'saved_model'
44
+ detection_model = tf.saved_model.load(str(model_dir))
45
+ return detection_model
46
+
47
 
48
  def predict(pilimg):
49
 
50
  image_np = pil_image_as_numpy_array(pilimg)
51
  return predict2(image_np)
52
 
53
+ def predict2(image_np):
54
+
55
+ results = detection_model(image_np)
56
+
57
+ # different object detection models have additional results
58
+ result = {key:value.numpy() for key,value in results.items()}
59
+
60
+ label_id_offset = 0
61
+ image_np_with_detections = image_np.copy()
62
+
63
+ viz_utils.visualize_boxes_and_labels_on_image_array(
64
+ image_np_with_detections[0],
65
+ result['detection_boxes'][0],
66
+ (result['detection_classes'][0] + label_id_offset).astype(int),
67
+ result['detection_scores'][0],
68
+ category_index,
69
+ use_normalized_coordinates=True,
70
+ max_boxes_to_draw=300,
71
+ min_score_thresh=0.50,
72
+ agnostic_mode=False,
73
+ line_thickness=3)
74
+
75
+ result_pil_img = tf.keras.utils.array_to_img(image_np_with_detections[0])
76
+
77
+ return result_pil_img
78
+
79
+
80
+ REPO_ID = "23A066X/23A066X_model"
81
  detection_model = load_model()
82
  # pil_image = Image.open(image_path)
83
  # image_arr = pil_image_as_numpy_array(pil_image)
84
+
85
  # predicted_img = predict(image_arr)
86
  # predicted_img.save('predicted.jpg')
87
 
 
88
  gr.Interface(fn=predict,
89
+ inputs=gr.Image(type="pil"),
90
+ outputs=gr.Image(type="pil"),
91
+ title="Cauliflower and Beetroot Detection.",
92
+ description="Using Model : ssd_resnet50_v1_fpn_640x640_coco17_tpu-8",
 
93
  ).launch(share=True)
94