David commited on
Commit
a06267d
1 Parent(s): 1e22412

Add application files

Browse files
app.py ADDED
@@ -0,0 +1,98 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # ! pip install gradio
2
+
3
+ import gradio as gr
4
+
5
+ import tensorflow as tf
6
+ from tensorflow import keras
7
+ from tensorflow.keras.models import Model, load_model
8
+
9
+ import numpy as np
10
+
11
+ import cv2
12
+
13
+ from PIL import Image
14
+
15
+ import matplotlib.pyplot as plt
16
+ import matplotlib.patches as mpatches
17
+
18
+ from pathlib import Path
19
+
20
+ current_directory_path = Path(__file__).parent.resolve()
21
+ object_detection_model_path = current_directory_path + "/" + "carla-image-segmentation-model.h5"
22
+ lane_detection_model_path = current_directory_path + "/" + "lane-detection-for-carla-model.h5"
23
+
24
+ label_map_object = {0: 'Unlabeled', 1: 'Building', 2: 'Fence', 3: 'Other',
25
+ 4: 'Pedestrian', 5: 'Pole', 6: 'RoadLine', 7: 'Road', 8: 'SideWalk',
26
+ 9: 'Vegetation', 10: 'Vehicles', 11: 'Wall', 12: 'TrafficSign'}
27
+
28
+ lane_label_map = {0: 'Unlabeled', 1: 'Left Lane', 2: 'Right Lane'}
29
+
30
+ # Load the object detection model
31
+ object_detection_model = load_model(object_detection_model_path)
32
+
33
+ # Load the lane detection model
34
+ lane_detection_model = load_model(lane_detection_model_path)
35
+
36
+
37
+ def create_mask(object_detection_model, lane_detection_model, image):
38
+ # tensor = tf.convert_to_tensor(image, dtype=tf.float32)
39
+
40
+ image = tf.io.read_file(image.name)
41
+ image = tf.image.decode_png(image, channels=3)
42
+ image = tf.image.convert_image_dtype(image, tf.float32)
43
+ tensor = tf.image.resize(image, (256, 256), method='nearest')
44
+
45
+ # convert to tensor (specify 3 channels explicitly since png files contains additional alpha channel)
46
+ # set the dtypes to align with pytorch for comparison since it will use uint8 by default
47
+ # tensor = tf.io.decode_image(image_tensor, channels=3, dtype=tf.float32)
48
+
49
+ # resize tensor to 224 x 224
50
+ # tensor = tf.image.resize(tensor, [256, 256])
51
+
52
+ # add another dimension at the front to get NHWC shape
53
+ input_tensor = tf.expand_dims(tensor, axis=0)
54
+
55
+ # with mp_selfie.SelfieSegmentation(model_selection=0) as model:
56
+ # Create Masks for with Object Detection Model
57
+ pred_masks_object_detect = object_detection_model.predict(input_tensor)
58
+ pred_masks_object_detect = tf.expand_dims(tf.argmax(pred_masks_object_detect, axis=-1), axis=-1)
59
+ pred_masks_object_detect = np.array(pred_masks_object_detect)
60
+
61
+ # Create Masks for with Lane Detection Model
62
+ pred_masks_lane_detect = lane_detection_model.predict(input_tensor)
63
+ pred_masks_lane_detect = tf.expand_dims(tf.argmax(pred_masks_lane_detect, axis=-1), axis=-1)
64
+ pred_masks_lane_detect = np.array(pred_masks_lane_detect)
65
+
66
+ return pred_masks_object_detect, pred_masks_lane_detect
67
+
68
+
69
+ def segment_object(image):
70
+ pred_masks_object_detect, pred_masks_lane_detect = create_mask(object_detection_model, lane_detection_model, image)
71
+
72
+ # image = cv2.resize(image, dsize=(256, 256), interpolation=cv2.INTER_CUBIC)
73
+
74
+ used_classes_object = np.unique(pred_masks_object_detect[0])
75
+ used_classes_lane = np.unique(pred_masks_lane_detect[0])
76
+
77
+ fig_object = plt.figure()
78
+ im = plt.imshow(tf.keras.preprocessing.image.array_to_img(pred_masks_object_detect[0]))
79
+ patches_1 = [mpatches.Patch(color=im.cmap(im.norm(int(cls))), label="{}".format(label_map_object[int(cls)])) for cls in used_classes_object]
80
+ # put those patched as legend-handles into the legend
81
+ plt.legend(handles=patches_1, bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0.)
82
+ plt.axis("off")
83
+
84
+ fig_lane = plt.figure()
85
+ im = plt.imshow(tf.keras.preprocessing.image.array_to_img(pred_masks_lane_detect[0]))
86
+ patches_1 = [mpatches.Patch(color=im.cmap(im.norm(int(cls))), label="{}".format(lane_label_map[int(cls)])) for cls in used_classes_lane]
87
+ # put those patched as legend-handles into the legend
88
+ plt.legend(handles=patches_1, bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0.)
89
+ plt.axis("off")
90
+
91
+ return fig_object
92
+
93
+
94
+ webcam = gr.inputs.Image(shape=(800, 600), source="upload", type='file') #upload
95
+
96
+ webapp = gr.interface.Interface(fn=segment_object, inputs=webcam, outputs="plot") #, live=False
97
+
98
+ webapp.launch(debug=True)
carla-image-segmentation-model.h5 ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:b7f72d74610f5f8d39be28970b66a9aee8ccb8b50a768c57fa06803a62c4fbcc
3
+ size 104177428
lane-detection-for-carla-model.h5 ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:9716b12103b745114ec39a54479eb2c38697925dfef06e88557354a08aa67bf1
3
+ size 104173056