1st try for pipeline file
Browse files
app.py
CHANGED
@@ -8,171 +8,190 @@ from tensorflow.keras.models import load_model
|
|
8 |
# most of this code has been obtained from Datature's prediction script
|
9 |
# https://github.com/datature/resources/blob/main/scripts/bounding_box/prediction.py
|
10 |
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
def
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
34 |
|
35 |
-
def predict_class(image, model):
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
def plot_boxes_on_img(color_map, classes, bboxes, image_origi, origi_shape):
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
# Webpage code starts here
|
84 |
-
|
85 |
-
#TODO change this
|
86 |
-
st.title('Distribution Grid - Belgium - Equipment detection')
|
87 |
-
st.text('made by LabelFlow')
|
88 |
-
st.markdown('## Description about your project')
|
89 |
-
|
90 |
-
with st.spinner('Model is being loaded...'):
|
91 |
-
|
92 |
-
|
93 |
-
# ask user to upload an image
|
94 |
-
file = st.file_uploader("Upload image", type=["jpg", "png"])
|
95 |
-
|
96 |
-
if file is None:
|
97 |
-
|
98 |
-
else:
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
|
157 |
-
|
158 |
-
|
159 |
-
|
160 |
-
|
161 |
-
|
162 |
-
|
163 |
-
|
164 |
-
|
165 |
-
|
166 |
-
|
167 |
-
|
168 |
-
|
169 |
-
|
170 |
-
|
171 |
-
|
172 |
-
|
173 |
-
|
174 |
-
|
175 |
-
|
176 |
-
|
177 |
-
|
178 |
-
|
|
|
8 |
# most of this code has been obtained from Datature's prediction script
|
9 |
# https://github.com/datature/resources/blob/main/scripts/bounding_box/prediction.py
|
10 |
|
11 |
+
|
12 |
+
|
13 |
+
class PreTrainedPipeline():
|
14 |
+
def __init__(self, path: str):
|
15 |
+
# load the model
|
16 |
+
self.model = tf.saved_model.load('./saved_model')
|
17 |
+
|
18 |
+
def __call__(self, inputs: "Image.Image")-> List[Dict[str, Any]]:
|
19 |
+
|
20 |
+
image = np.array(inputs)
|
21 |
+
image = tf.cast(image, tf.float32)
|
22 |
+
image = tf.image.resize(image, [150, 150])
|
23 |
+
image = np.expand_dims(image, axis = 0)
|
24 |
+
predictions = self.model.predict(image)
|
25 |
+
|
26 |
+
labels = []
|
27 |
+
labels = [{"score":0.9509243965148926,"label":"car","box":{"xmin":142,"ymin":106,"xmax":376,"ymax":229}},{"score":0.9981777667999268,"label":"car","box":{"xmin":405,"ymin":146,"xmax":640,"ymax":297}},{"score":0.9963648915290833,"label":"car","box":{"xmin":0,"ymin":115,"xmax":61,"ymax":167}},{"score":0.974663257598877,"label":"car","box":{"xmin":155,"ymin":104,"xmax":290,"ymax":141}},{"score":0.9986898303031921,"label":"car","box":{"xmin":39,"ymin":117,"xmax":169,"ymax":188}},{"score":0.9998276233673096,"label":"person","box":{"xmin":172,"ymin":60,"xmax":482,"ymax":396}},{"score":0.9996274709701538,"label":"skateboard","box":{"xmin":265,"ymin":348,"xmax":440,"ymax":413}}]
|
28 |
+
|
29 |
+
return labels
|
30 |
+
|
31 |
+
|
32 |
+
# # -----------------
|
33 |
+
# def load_model():
|
34 |
+
# return tf.saved_model.load('./saved_model')
|
35 |
+
|
36 |
+
# def load_label_map(label_map_path):
|
37 |
+
# """
|
38 |
+
# Reads label map in the format of .pbtxt and parse into dictionary
|
39 |
+
# Args:
|
40 |
+
# label_map_path: the file path to the label_map
|
41 |
+
# Returns:
|
42 |
+
# dictionary with the format of {label_index: {'id': label_index, 'name': label_name}}
|
43 |
+
# """
|
44 |
+
# label_map = {}
|
45 |
+
|
46 |
+
# with open(label_map_path, "r") as label_file:
|
47 |
+
# for line in label_file:
|
48 |
+
# if "id" in line:
|
49 |
+
# label_index = int(line.split(":")[-1])
|
50 |
+
# label_name = next(label_file).split(":")[-1].strip().strip('"')
|
51 |
+
# label_map[label_index] = {"id": label_index, "name": label_name}
|
52 |
+
# return label_map
|
53 |
|
54 |
+
# def predict_class(image, model):
|
55 |
+
# image = tf.cast(image, tf.float32)
|
56 |
+
# image = tf.image.resize(image, [150, 150])
|
57 |
+
# image = np.expand_dims(image, axis = 0)
|
58 |
+
# return model.predict(image)
|
59 |
+
|
60 |
+
# def plot_boxes_on_img(color_map, classes, bboxes, image_origi, origi_shape):
|
61 |
+
# for idx, each_bbox in enumerate(bboxes):
|
62 |
+
# color = color_map[classes[idx]]
|
63 |
+
|
64 |
+
# ## Draw bounding box
|
65 |
+
# cv2.rectangle(
|
66 |
+
# image_origi,
|
67 |
+
# (int(each_bbox[1] * origi_shape[1]),
|
68 |
+
# int(each_bbox[0] * origi_shape[0]),),
|
69 |
+
# (int(each_bbox[3] * origi_shape[1]),
|
70 |
+
# int(each_bbox[2] * origi_shape[0]),),
|
71 |
+
# color,
|
72 |
+
# 2,
|
73 |
+
# )
|
74 |
+
# ## Draw label background
|
75 |
+
# cv2.rectangle(
|
76 |
+
# image_origi,
|
77 |
+
# (int(each_bbox[1] * origi_shape[1]),
|
78 |
+
# int(each_bbox[2] * origi_shape[0]),),
|
79 |
+
# (int(each_bbox[3] * origi_shape[1]),
|
80 |
+
# int(each_bbox[2] * origi_shape[0] + 15),),
|
81 |
+
# color,
|
82 |
+
# -1,
|
83 |
+
# )
|
84 |
+
# ## Insert label class & score
|
85 |
+
# cv2.putText(
|
86 |
+
# image_origi,
|
87 |
+
# "Class: {}, Score: {}".format(
|
88 |
+
# str(category_index[classes[idx]]["name"]),
|
89 |
+
# str(round(scores[idx], 2)),
|
90 |
+
# ),
|
91 |
+
# (int(each_bbox[1] * origi_shape[1]),
|
92 |
+
# int(each_bbox[2] * origi_shape[0] + 10),),
|
93 |
+
# cv2.FONT_HERSHEY_SIMPLEX,
|
94 |
+
# 0.3,
|
95 |
+
# (0, 0, 0),
|
96 |
+
# 1,
|
97 |
+
# cv2.LINE_AA,
|
98 |
+
# )
|
99 |
+
# return image_origi
|
100 |
+
|
101 |
+
|
102 |
+
# # Webpage code starts here
|
103 |
+
|
104 |
+
# #TODO change this
|
105 |
+
# st.title('Distribution Grid - Belgium - Equipment detection')
|
106 |
+
# st.text('made by LabelFlow')
|
107 |
+
# st.markdown('## Description about your project')
|
108 |
+
|
109 |
+
# with st.spinner('Model is being loaded...'):
|
110 |
+
# model = load_model()
|
111 |
+
|
112 |
+
# # ask user to upload an image
|
113 |
+
# file = st.file_uploader("Upload image", type=["jpg", "png"])
|
114 |
+
|
115 |
+
# if file is None:
|
116 |
+
# st.text('Waiting for upload...')
|
117 |
+
# else:
|
118 |
+
# st.text('Running inference...')
|
119 |
+
# # open image
|
120 |
+
# test_image = Image.open(file).convert("RGB")
|
121 |
+
# origi_shape = np.asarray(test_image).shape
|
122 |
+
# # resize image to default shape
|
123 |
+
# default_shape = 320
|
124 |
+
# image_resized = np.array(test_image.resize((default_shape, default_shape)))
|
125 |
+
|
126 |
+
# ## Load color map
|
127 |
+
# category_index = load_label_map("./label_map.pbtxt")
|
128 |
+
|
129 |
+
# # TODO Add more colors if there are more classes
|
130 |
+
# # color of each label. check label_map.pbtxt to check the index for each class
|
131 |
+
# color_map = {
|
132 |
+
# 1: [69, 109, 42],
|
133 |
+
# 2: [107, 46, 186],
|
134 |
+
# 3: [9, 35, 183],
|
135 |
+
# 4: [27, 1, 30],
|
136 |
+
# 5: [0, 0, 0],
|
137 |
+
# 6: [5, 6, 7],
|
138 |
+
# 7: [11, 5, 12],
|
139 |
+
# 8: [209, 205, 211],
|
140 |
+
# 9: [17, 17, 17],
|
141 |
+
# 10: [101, 242, 50],
|
142 |
+
# 11: [51, 204, 170],
|
143 |
+
# 12: [106, 0, 132],
|
144 |
+
# 13: [7, 111, 153],
|
145 |
+
# 14: [8, 10, 9],
|
146 |
+
# 15: [234, 250, 252],
|
147 |
+
# 16: [58, 68, 30],
|
148 |
+
# 17: [24, 178, 117],
|
149 |
+
# 18: [21, 22, 21],
|
150 |
+
# 19: [53, 104, 83],
|
151 |
+
# 20: [12, 5, 10],
|
152 |
+
# 21: [223, 192, 249],
|
153 |
+
# 22: [234, 234, 234],
|
154 |
+
# 23: [119, 68, 221],
|
155 |
+
# 24: [224, 174, 94],
|
156 |
+
# 25: [140, 74, 116],
|
157 |
+
# 26: [90, 102, 1],
|
158 |
+
# 27: [216, 143, 208]
|
159 |
+
# }
|
160 |
+
|
161 |
+
# ## The model input needs to be a tensor
|
162 |
+
# input_tensor = tf.convert_to_tensor(image_resized)
|
163 |
+
# ## The model expects a batch of images, so add an axis with `tf.newaxis`.
|
164 |
+
# input_tensor = input_tensor[tf.newaxis, ...]
|
165 |
+
|
166 |
+
# ## Feed image into model and obtain output
|
167 |
+
# detections_output = model(input_tensor)
|
168 |
+
# num_detections = int(detections_output.pop("num_detections"))
|
169 |
+
# detections = {key: value[0, :num_detections].numpy() for key, value in detections_output.items()}
|
170 |
+
# detections["num_detections"] = num_detections
|
171 |
+
|
172 |
+
# ## Filter out predictions below threshold
|
173 |
+
# # if threshold is higher, there will be fewer predictions
|
174 |
+
# # TODO change this number to see how the predictions change
|
175 |
+
# confidence_threshold = 0.6
|
176 |
+
# indexes = np.where(detections["detection_scores"] > confidence_threshold)
|
177 |
+
|
178 |
+
# ## Extract predicted bounding boxes
|
179 |
+
# bboxes = detections["detection_boxes"][indexes]
|
180 |
+
# # there are no predicted boxes
|
181 |
+
# if len(bboxes) == 0:
|
182 |
+
# st.error('No boxes predicted')
|
183 |
+
# # there are predicted boxes
|
184 |
+
# else:
|
185 |
+
# st.success('Boxes predicted')
|
186 |
+
# classes = detections["detection_classes"][indexes].astype(np.int64)
|
187 |
+
# scores = detections["detection_scores"][indexes]
|
188 |
+
|
189 |
+
# # plot boxes and labels on image
|
190 |
+
# image_origi = np.array(Image.fromarray(image_resized).resize((origi_shape[1], origi_shape[0])))
|
191 |
+
# image_origi = plot_boxes_on_img(color_map, classes, bboxes, image_origi, origi_shape)
|
192 |
+
|
193 |
+
# # show image in web page
|
194 |
+
# st.image(Image.fromarray(image_origi), caption="Image with predictions", width=400)
|
195 |
+
# st.markdown("### Predicted boxes")
|
196 |
+
# for idx in range(len((bboxes))):
|
197 |
+
# st.markdown(f"* Class: {str(category_index[classes[idx]]['name'])}, confidence score: {str(round(scores[idx], 2))}")
|