rto_git commited on
Commit
177c1b5
1 Parent(s): 05d8b02

first commit

Browse files
README.md CHANGED
@@ -1,10 +1,11 @@
1
  ---
2
  title: 23A054Q
3
- emoji:
4
- colorFrom: pink
5
- colorTo: indigo
 
6
  sdk: gradio
7
- sdk_version: 4.8.0
8
  app_file: app.py
9
  pinned: false
10
  license: apache-2.0
 
1
  ---
2
  title: 23A054Q
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
app.py ADDED
@@ -0,0 +1,82 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+
56
+ viz_utils.visualize_boxes_and_labels_on_image_array(
57
+ image_np_with_detections[0],
58
+ result['detection_boxes'][0],
59
+ (result['detection_classes'][0] + label_id_offset).astype(int),
60
+ result['detection_scores'][0],
61
+ category_index,
62
+ use_normalized_coordinates=True,
63
+ max_boxes_to_draw=200,
64
+ min_score_thresh=.60,
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")
82
+ ).launch(share=True)
data/label_map.pbtxt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ item {
2
+ id: 1
3
+ name: 'balloon'
4
+ }
requirements.txt ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ #tf2-tensorflow-object-detection-api
2
+ tf-models-research-object-detection
3
+ matplotlib
4
+ wget
5
+ Pillow==9.5
test_samples/sample_balloon.jpeg ADDED