wongshennan commited on
Commit
393ef5f
β€’
1 Parent(s): 047e6d0
Files changed (4) hide show
  1. README.md +2 -2
  2. app.py +32 -42
  3. data/label_map.pbtxt +6 -2
  4. requirements.txt +2 -0
README.md CHANGED
@@ -3,9 +3,9 @@ title: 23A070B
3
  emoji: 🐒
4
  colorFrom: indigo
5
  colorTo: red
6
- python_version: 3.8
7
  sdk: gradio
8
- sdk_version: 4.0.2
9
  app_file: app.py
10
  pinned: false
11
  license: apache-2.0
 
3
  emoji: 🐒
4
  colorFrom: indigo
5
  colorTo: red
6
+ # python_version: 3.8
7
  sdk: gradio
8
+ sdk_version: 4.12.2
9
  app_file: app.py
10
  pinned: false
11
  license: apache-2.0
app.py CHANGED
@@ -1,55 +1,50 @@
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
 
 
 
 
13
 
14
- PATH_TO_LABELS = 'data/label_map.pbtxt'
15
  category_index = label_map_util.create_category_index_from_labelmap(PATH_TO_LABELS, use_display_name=True)
16
 
17
- def pil_image_as_numpy_array(pilimg):
18
-
19
- img_array = tf.keras.utils.img_to_array(pilimg)
20
  img_array = np.expand_dims(img_array, axis=0)
21
  return img_array
22
-
23
- def load_image_into_numpy_array(path):
24
-
25
- image = None
26
- image_data = tf.io.gfile.GFile(path, 'rb').read()
27
- image = Image.open(BytesIO(image_data))
28
- return pil_image_as_numpy_array(image)
29
 
30
  def load_model():
 
 
 
 
 
 
 
31
  wget.download("https://nyp-aicourse.s3-ap-southeast-1.amazonaws.com/pretrained-models/balloon_model.tar.gz")
32
  tarfile.open("balloon_model.tar.gz").extractall()
33
- model_dir = 'saved_model'
34
- detection_model = tf.saved_model.load(str(model_dir))
35
- return detection_model
36
-
37
- # samples_folder = 'test_samples
38
- # image_path = 'test_samples/sample_balloon.jpeg
39
- #
40
 
41
- def predict(pilimg):
42
-
43
- image_np = pil_image_as_numpy_array(pilimg)
44
- return predict2(image_np)
45
-
46
- def predict2(image_np):
47
 
 
 
48
  results = detection_model(image_np)
 
49
 
50
- # different object detection models have additional results
51
- result = {key:value.numpy() for key,value in results.items()}
52
-
53
  label_id_offset = 0
54
  image_np_with_detections = image_np.copy()
55
 
@@ -65,17 +60,12 @@ def predict2(image_np):
65
  agnostic_mode=False,
66
  line_thickness=2)
67
 
68
- result_pil_img = tf.keras.utils.array_to_img(image_np_with_detections[0])
69
-
70
- return result_pil_img
71
 
 
72
  detection_model = load_model()
73
- # pil_image = Image.open(image_path)
74
- # image_arr = pil_image_as_numpy_array(pil_image)
75
-
76
- # predicted_img = predict(image_arr)
77
- # predicted_img.save('predicted.jpg')
78
 
 
79
  gr.Interface(fn=predict,
80
  inputs=gr.Image(type="pil"),
81
  outputs=gr.Image(type="pil")
 
1
+ import os
2
+ import tarfile
3
+ import wget
4
  import numpy as np
 
 
5
  import tensorflow as tf
6
+ from huggingface_hub import snapshot_download
7
+ import gradio as gr
8
+
9
  from object_detection.utils import label_map_util
10
  from object_detection.utils import visualization_utils as viz_utils
 
 
 
 
11
 
12
+ # Constants
13
+ REPO_ID = "wongshennan/iti107_model"
14
+ PATH_TO_LABELS = 'data/label_map.pbtxt'
15
 
16
+ # Load category index
17
  category_index = label_map_util.create_category_index_from_labelmap(PATH_TO_LABELS, use_display_name=True)
18
 
19
+ def pil_image_as_numpy_array(pil_img):
20
+ """Convert PIL image to numpy array."""
21
+ img_array = tf.keras.utils.img_to_array(pil_img)
22
  img_array = np.expand_dims(img_array, axis=0)
23
  return img_array
 
 
 
 
 
 
 
24
 
25
  def load_model():
26
+ """Load model from Hugging Face Hub."""
27
+ download_dir = snapshot_download(REPO_ID)
28
+ saved_model_dir = os.path.join(download_dir, "saved_model")
29
+ return tf.saved_model.load(saved_model_dir)
30
+
31
+ def load_model2():
32
+ """Load model from a tar.gz file."""
33
  wget.download("https://nyp-aicourse.s3-ap-southeast-1.amazonaws.com/pretrained-models/balloon_model.tar.gz")
34
  tarfile.open("balloon_model.tar.gz").extractall()
35
+ model_dir = 'saved_model'
36
+ return tf.saved_model.load(str(model_dir))
 
 
 
 
 
37
 
38
+ def predict(pil_img):
39
+ """Predict method for Gradio interface."""
40
+ image_np = pil_image_as_numpy_array(pil_img)
41
+ return detect_and_visualize(image_np)
 
 
42
 
43
+ def detect_and_visualize(image_np):
44
+ """Helper function to run object detection and visualize results."""
45
  results = detection_model(image_np)
46
+ result = {key: value.numpy() for key, value in results.items()}
47
 
 
 
 
48
  label_id_offset = 0
49
  image_np_with_detections = image_np.copy()
50
 
 
60
  agnostic_mode=False,
61
  line_thickness=2)
62
 
63
+ return tf.keras.utils.array_to_img(image_np_with_detections[0])
 
 
64
 
65
+ # Load the model
66
  detection_model = load_model()
 
 
 
 
 
67
 
68
+ # Launch Gradio Interface
69
  gr.Interface(fn=predict,
70
  inputs=gr.Image(type="pil"),
71
  outputs=gr.Image(type="pil")
data/label_map.pbtxt CHANGED
@@ -1,4 +1,8 @@
1
  item {
2
  id: 1
3
- name: 'balloon'
4
- }
 
 
 
 
 
1
  item {
2
  id: 1
3
+ name: 'tshirt'
4
+ }
5
+ item {
6
+ id: 2
7
+ name: 'shoes'
8
+ }
requirements.txt CHANGED
@@ -3,3 +3,5 @@ tf-models-research-object-detection
3
  matplotlib
4
  wget
5
  Pillow==9.5
 
 
 
3
  matplotlib
4
  wget
5
  Pillow==9.5
6
+ huggingface_hub
7
+ tensorflow