magcheong commited on
Commit
5407d7c
1 Parent(s): e2cc267

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.py +92 -0
  2. requirements.txt +6 -0
app.py ADDED
@@ -0,0 +1,92 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 = 'data/label_map.pbtxt'
16
+ category_index = label_map_util.create_category_index_from_labelmap(PATH_TO_LABELS, use_display_name=True)
17
+
18
+ def pil_image_as_numpy_array(pilimg):
19
+
20
+ img_array = tf.keras.utils.img_to_array(pilimg)
21
+ img_array = np.expand_dims(img_array, axis=0)
22
+ return img_array
23
+
24
+ def load_image_into_numpy_array(path):
25
+
26
+ image = None
27
+ image_data = tf.io.gfile.GFile(path, 'rb').read()
28
+ image = Image.open(BytesIO(image_data))
29
+ return pil_image_as_numpy_array(image)
30
+
31
+ def load_model():
32
+ # download_dir = snapshot_download(REPO_ID)
33
+ download_dir = os.path.join(download_dir, "saved_model")
34
+ saved_model_dir = os.path.join(download_dir, "saved_model")
35
+ detection_model = tf.saved_model.load(saved_model_dir)
36
+ return detection_model
37
+
38
+ def load_model2():
39
+ wget.download("https://nyp-aicourse.s3-ap-southeast-1.amazonaws.com/pretrained-models/balloon_model.tar.gz")
40
+ tarfile.open("balloon_model.tar.gz").extractall()
41
+ model_dir = 'saved_model'
42
+ detection_model = tf.saved_model.load(str(model_dir))
43
+ return detection_model
44
+
45
+ # samples_folder = 'test_samples
46
+ # image_path = 'test_samples/sample_balloon.jpeg
47
+ #
48
+
49
+ def predict(pilimg):
50
+
51
+ image_np = pil_image_as_numpy_array(pilimg)
52
+ return predict2(image_np)
53
+
54
+ def predict2(image_np):
55
+
56
+ results = detection_model(image_np)
57
+
58
+ # different object detection models have additional results
59
+ result = {key:value.numpy() for key,value in results.items()}
60
+
61
+ label_id_offset = 0
62
+ image_np_with_detections = image_np.copy()
63
+
64
+ viz_utils.visualize_boxes_and_labels_on_image_array(
65
+ image_np_with_detections[0],
66
+ result['detection_boxes'][0],
67
+ (result['detection_classes'][0] + label_id_offset).astype(int),
68
+ result['detection_scores'][0],
69
+ category_index,
70
+ use_normalized_coordinates=True,
71
+ max_boxes_to_draw=200,
72
+ min_score_thresh=.60,
73
+ agnostic_mode=False,
74
+ line_thickness=2)
75
+
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 = "magcheong/ITI107_assignment2"
82
+ detection_model = load_model()
83
+ # pil_image = Image.open(image_path)
84
+ # image_arr = pil_image_as_numpy_array(pil_image)
85
+
86
+ # predicted_img = predict(image_arr)
87
+ # predicted_img.save('predicted.jpg')
88
+
89
+ gr.Interface(fn=predict,
90
+ inputs=gr.Image(type="pil"),
91
+ outputs=gr.Image(type="pil")
92
+ ).launch(share=True)
requirements.txt ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ #tf2-tensorflow-object-detection-api
2
+ tf-models-research-object-detection
3
+ matplotlib
4
+ wget
5
+ Pillow==9.5
6
+ huggingface_hub