CoolFace
Apppublic

davidmd/lane_detection_UNet_Model

sourceHugging Faceotherupdated 5y agoView on Hugging Face
1likes
app.py99 linesDownload Raw Back to root
1# ! pip install gradio2 3import gradio as gr4 5import tensorflow as tf 6from tensorflow import keras7from tensorflow.keras.models import Model, load_model8 9import numpy as np10 11# import cv212 13from PIL import Image14 15import matplotlib.pyplot as plt16import matplotlib.patches as mpatches17 18from pathlib import Path19 20current_directory_path = Path(__file__).parent.resolve()21object_detection_model_path = current_directory_path / "carla-image-segmentation-model.h5"22lane_detection_model_path = current_directory_path / "lane-detection-for-carla-model.h5"23 24label_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 28lane_label_map = {0: 'Unlabeled', 1: 'Left Lane', 2: 'Right Lane'}29 30# Load the object detection model31object_detection_model = load_model(object_detection_model_path)32 33# Load the lane detection model34lane_detection_model = load_model(lane_detection_model_path)35 36 37def 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 default47    # tensor = tf.io.decode_image(image_tensor, channels=3, dtype=tf.float32)48 49    # resize tensor to 224 x 22450    # tensor = tf.image.resize(tensor, [256, 256])51 52    # add another dimension at the front to get NHWC shape53    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_detect67 68 69def 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 legend81    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 legend88    plt.legend(handles=patches_1, bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0.)89    plt.axis("off")   90 91    return fig_lane92 93 94webcam = gr.inputs.Image(shape=(800, 600), source="upload", type='file') #upload95 96webapp = gr.interface.Interface(fn=segment_object, inputs=webcam, outputs="plot") #, live=False97 98webapp.launch(debug=True)99