23A066X commited on
Commit
adca0b0
1 Parent(s): ed3d0c3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -27
app.py CHANGED
@@ -1,30 +1,23 @@
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
- from huggingface_hub import from_pretrained_keras
15
-
16
- model = from_pretrained_keras("23A066X/23A066X_model")
17
-
18
-
19
-
20
 
21
  PATH_TO_LABELS = 'label_map.pbtxt'
22
  category_index = label_map_util.create_category_index_from_labelmap(PATH_TO_LABELS, use_display_name=True)
23
 
24
 
25
-
26
  def pil_image_as_numpy_array(pilimg):
27
-
28
  img_array = tf.keras.utils.img_to_array(pilimg)
29
  img_array = np.expand_dims(img_array, axis=0)
30
  return img_array
@@ -38,6 +31,7 @@ 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")
42
  detection_model = tf.saved_model.load(saved_model_dir)
43
  return detection_model
@@ -49,6 +43,9 @@ def load_model2():
49
  detection_model = tf.saved_model.load(str(model_dir))
50
  return detection_model
51
 
 
 
 
52
 
53
  def predict(pilimg):
54
 
@@ -58,10 +55,13 @@ def predict(pilimg):
58
  def predict2(image_np):
59
 
60
  results = detection_model(image_np)
 
61
  # different object detection models have additional results
62
  result = {key:value.numpy() for key,value in results.items()}
 
63
  label_id_offset = 0
64
  image_np_with_detections = image_np.copy()
 
65
  viz_utils.visualize_boxes_and_labels_on_image_array(
66
  image_np_with_detections[0],
67
  result['detection_boxes'][0],
@@ -69,29 +69,32 @@ def predict2(image_np):
69
  result['detection_scores'][0],
70
  category_index,
71
  use_normalized_coordinates=True,
72
- max_boxes_to_draw=300,
73
- min_score_thresh=0.50,
74
  agnostic_mode=False,
75
  line_thickness=3)
 
76
  result_pil_img = tf.keras.utils.array_to_img(image_np_with_detections[0])
77
 
78
  return result_pil_img
79
 
80
 
81
- REPO_ID = "23A066X/23A066X_model"
82
- detection_model = from_pretrained_keras("23A066X/23A066X_model")
83
- # detection_model = load_model()
84
-
85
  # pil_image = Image.open(image_path)
86
  # image_arr = pil_image_as_numpy_array(pil_image)
87
 
88
  # predicted_img = predict(image_arr)
89
  # predicted_img.save('predicted.jpg')
90
 
91
- gr.Interface(fn=predict,
92
- inputs=gr.Image(type="pil"),
93
- outputs=gr.Image(type="pil"),
94
- title="Cauliflower and Beetroot Detection.",
95
- description="Using Model : ssd_resnet50_v1_fpn_640x640_coco17_tpu-8",
96
- ).launch(share=True)
97
 
 
 
 
 
 
 
 
 
 
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
  PATH_TO_LABELS = 'label_map.pbtxt'
16
  category_index = label_map_util.create_category_index_from_labelmap(PATH_TO_LABELS, use_display_name=True)
17
 
18
 
 
19
  def pil_image_as_numpy_array(pilimg):
20
+
21
  img_array = tf.keras.utils.img_to_array(pilimg)
22
  img_array = np.expand_dims(img_array, axis=0)
23
  return img_array
 
31
 
32
  def load_model():
33
  download_dir = snapshot_download(REPO_ID)
34
+ # download_dir = os.path.join(download_dir, "saved_model")
35
  saved_model_dir = os.path.join(download_dir, "saved_model")
36
  detection_model = tf.saved_model.load(saved_model_dir)
37
  return detection_model
 
43
  detection_model = tf.saved_model.load(str(model_dir))
44
  return detection_model
45
 
46
+ # samples_folder = 'test_samples
47
+ # image_path = 'test_samples/sample_balloon.jpeg
48
+ #
49
 
50
  def predict(pilimg):
51
 
 
55
  def predict2(image_np):
56
 
57
  results = detection_model(image_np)
58
+
59
  # different object detection models have additional results
60
  result = {key:value.numpy() for key,value in results.items()}
61
+
62
  label_id_offset = 0
63
  image_np_with_detections = image_np.copy()
64
+
65
  viz_utils.visualize_boxes_and_labels_on_image_array(
66
  image_np_with_detections[0],
67
  result['detection_boxes'][0],
 
69
  result['detection_scores'][0],
70
  category_index,
71
  use_normalized_coordinates=True,
72
+ max_boxes_to_draw=200,
73
+ min_score_thresh=0.60,
74
  agnostic_mode=False,
75
  line_thickness=3)
76
+
77
  result_pil_img = tf.keras.utils.array_to_img(image_np_with_detections[0])
78
 
79
  return result_pil_img
80
 
81
 
82
+ REPO_ID = "A23066X/A23066X_model"
83
+ detection_model = load_model()
 
 
84
  # pil_image = Image.open(image_path)
85
  # image_arr = pil_image_as_numpy_array(pil_image)
86
 
87
  # predicted_img = predict(image_arr)
88
  # predicted_img.save('predicted.jpg')
89
 
90
+ title = "Cauliflower and Beetroot Detection"
91
+ description = "Using ssd_resnet50_v1_fpn_640x640_coco17_tpu-8"
92
+
 
 
 
93
 
94
+ gr.Interface(fn=predict,
95
+ title = title,
96
+ description = description,
97
+ css=css_code,
98
+ inputs=gr.Image(type="pil", height=309),
99
+ outputs=gr.Image(type="pil", height=350)
100
+ ).launch(share=True)