Example #1
0
def compute_distance(df, image, th=0.92, label="Parking Slots", plot=False):
    df.reset_index(drop=True, inplace=True)
    n = len(df)
    base_col = ['x1', 'y1', 'x2', 'y2', 'xc', 'yc', 'w', 'b']
    df.reset_index(drop=True, inplace=True)
    mat, _, _, _ = assign_next_frame(df, df, th=0.6)
    np.fill_diagonal(mat, -9)
    mat = np.tril(mat)
    count = n
    to_merge = []
    while count > 0:
        r, k = np.unravel_index(np.argmax(mat, axis=None), mat.shape)
        if mat[r, k] > th:
            to_merge.append([r, k])

        mat[r, :] = -9
        mat[:, k] = -9
        mat[k, :] = -9
        mat[:, r] = -9
        count = count - 1


#   print(to_merge)
    for i in range(len(to_merge)):
        r = to_merge[i][0]
        k = to_merge[i][1]
        df.loc[r, base_col] = (df.loc[r, base_col] * df.loc[r, "found"] +
                               df.loc[r, base_col] * df.loc[k, "found"]) / (
                                   df.loc[r, "found"] + df.loc[k, "found"])
        df.at[r, "found"] = df.at[r, "found"] + df.at[k, "found"]
        df.drop(k, axis=0, inplace=True)
    if plot:
        visualize.display_instances(image,
                                    df[["y1", "x1", "y2", "x2"]].values,
                                    None,
                                    df["class"].values,
                                    class_names,
                                    scores=None,
                                    title=label,
                                    figsize=(16, 16),
                                    ax=None,
                                    show_mask=False,
                                    show_bbox=True,
                                    colors=None,
                                    captions=df["labels"].astype(str).values)
    return df
Example #2
0
def compute_distance(df, image, th = 0.92, label = "Parking Slots", plot = False):
  df.reset_index(drop=True, inplace=True)
  n =  len(df)
  base_col = ['x1', 'y1', 'x2', 'y2',  'xc', 'yc', 'w' , 'b']
  df.reset_index(drop=True, inplace=True)
  mat, _, _, _ = assign_next_frame(df, df, th = 0.6)
  np.fill_diagonal(mat, -9)
  mat = np.tril(mat)
  count = n
  to_merge = []
  while count > 0:
    r,k = np.unravel_index(np.argmax(mat, axis=None), mat.shape)
    if mat[r,k] > th :
      to_merge.append([r,k])

    mat[r,:] = -9
    mat[:,k] = -9
    mat[k,:] = -9
    mat[:,r] = -9
    count = count -1
#   print(to_merge)
  for i in range(len(to_merge)):
    r = to_merge[i][0]
    k = to_merge[i][1]
    df.loc[r,base_col] =(df.loc[r,base_col] * df.loc[r,"found"] +  df.loc[r,base_col]* df.loc[k,"found"])/(df.loc[r,"found"]+df.loc[k,"found"])
    df.at[r,"found"] =  df.at[r,"found"]+ df.at[k,"found"]
    df.drop(k, axis=0, inplace = True)
  if plot :
    plt.figure()
    box_img = yolo.draw_rect(PILImage.fromarray(cv2.imread(image)),\
           df[["y1","x2","y2","x1"]].values, df.index.values, df["class"], df["labels"].values)
    f = plt.figure(figsize=(20,40))
    plt.imshow(box_img)
    plt.title(label)
    plt.show()
    plt.close()
  return df
Example #3
0
def look_for_slots(data,
                   img=[],
                   PRUNE_TH=3,
                   plot=True,
                   PRUNE_STEP=10,
                   MERGE_STEP=20,
                   MERGE_TH=0.65):

    n_fr = data["frame"].nunique()
    cols = [
        "labels", 'x1', 'y1', 'x2', 'y2', 'xc', 'yc', 'w', 'b', "class", 'a'
    ]
    base_col = ['x1', 'y1', 'x2', 'y2', 'xc', 'yc', 'w', 'b']
    slots = data[data["frame"] == 0][cols]
    slots["found"] = 1

    def plot_images():
        df = slots[['x1', 'y1', 'x2', 'y2', "found", "labels"]]
        df["class"] = "empty"
        msk = df["labels"].isin(id_map.values())
        df.loc[msk, "class"] = "occupy"
        nw_df = new[['x1', 'y1', 'x2', 'y2', "labels"]]
        nw_df["class"] = "new"
        nw_df["found"] = 1
        df = df.append(nw_df, sort=True).reset_index(drop=True)
        df["found"] = df["found"].astype(int)
        plot_frame(img[i], df[["y1", "x2", "y2", "x1"]].values,
                   df["class"].values, df["found"], df["labels"])


#   out_boxes,  out_classes, found, labels
# "empty":"#4a148c","occupy":"#f44336", "new":"#7cb342","del":"#80deea"

    print("LOOKING FOR PARKING SLOTS INSIDE IMAGE FRAMES")
    for i in tqdm(range(1, n_fr)):
        post = data[data["frame"] == i].reset_index(drop=True)
        _, iou, id_map, status = assign_next_frame(slots, post, th=MERGE_TH)
        #print(id_map.keys(), status.sum())

        ## found again
        mask = post["labels"].isin(id_map.keys())
        slots.loc[status, "found"] = slots.loc[status, "found"] + 1
        occupy = post[mask]
        occupy["labels"] = occupy["labels"].map(id_map)
        slots.sort_values(by=["labels"], inplace=True)
        slots.reset_index(drop=True, inplace=True)
        occupy.sort_values(by=["labels"], inplace=True)
        occupy.reset_index(drop=True, inplace=True)
        slots.loc[status, base_col] = slots.loc[status, base_col].values * (
            1 - 1 / (i + 1)) + occupy[base_col].values / (i + 1)

        # clean up
        if i % PRUNE_STEP == 0:
            slots.drop(slots[slots["found"] < PRUNE_TH + 1].index,
                       inplace=True)
            #print(slots)

        # merge
        if i % MERGE_STEP == 0:

            slots = compute_distance(slots,
                                     img[i - 1],
                                     th=MERGE_TH,
                                     label="Parking Slots " + str(i))

        # new
        idx = np.logical_not(post["labels"].isin(id_map.keys()))
        new = post[idx]
        new["labels"] = new["labels"] + slots["labels"].max() + 1
        new = new[cols]
        if len(new) > 0:
            new["found"] = 1
        slots = slots.append(new, sort=True).reset_index(drop=True)
        if plot and (i % MERGE_STEP * 5 == 0):
            plot_images()
    slots = compute_distance(slots,
                             img[0],
                             th=MERGE_TH * 0.9,
                             label="Parking Slots " + str(MERGE_STEP))
    slots.drop(slots[slots["found"] < PRUNE_TH * 3].index, inplace=True)
    print(len(slots), "SLOTS FOUND")
    plot_images()
    return slots
Example #4
0
def look_for_slots(data,
                   img=[],
                   PRUNE_TH=3,
                   ASSIGN_TH=0.6,
                   plot=True,
                   PRUNE_STEP=10,
                   MERGE_STEP=20,
                   MERGE_TH=0.75):

    n_fr = data["frame"].nunique()
    cols = [
        "labels", 'x1', 'y1', 'x2', 'y2', 'xc', 'yc', 'w', 'b', 'a', "class"
    ]
    base_col = ['x1', 'y1', 'x2', 'y2', 'xc', 'yc', 'w', 'b', 'a']
    slots = data[data["frame"] == 0][cols]
    slots["found"] = 1

    #   out_boxes,  out_classes, found, labels
    # "empty":"#4a148c","occupy":"#f44336", "new":"#7cb342","del":"#80deea"
    print("LOOKING FOR PARKING SLOTS INSIDE IMAGE FRAMES")
    for i in tqdm(range(1, n_fr)):
        post = data[data["frame"] == i].reset_index(drop=True)
        _, iou, id_map, status = assign_next_frame(slots, post, th=ASSIGN_TH)
        #print(id_map.keys(), status.sum())

        ## found again
        mask = post["labels"].isin(id_map.keys())

        slots.loc[status, "found"] = slots.loc[status, "found"] + 1
        occupy = post[mask]
        occupy["labels"] = occupy["labels"].map(id_map)

        slots.sort_values(by=["labels"], inplace=True)
        slots.reset_index(drop=True, inplace=True)
        occupy.sort_values(by=["labels"], inplace=True)
        occupy.reset_index(drop=True, inplace=True)
        slots.loc[status, base_col] = slots.loc[status, base_col].values * (
            1 - 1 / (i + 1)) + occupy[base_col].values / (i + 1)

        # clean up
        if i % PRUNE_STEP == 0:
            slots.drop(slots[slots["found"] < PRUNE_TH + 1].index,
                       inplace=True)
            #print(slots)

        # merge
        if i % MERGE_STEP == 0:

            slots = compute_distance(slots,
                                     img[i - 1],
                                     th=MERGE_TH,
                                     label="Parking Slots " + str(i))

        # new
        idx = np.logical_not(post["labels"].isin(id_map.keys()))
        new = post[idx]
        new["labels"] = new["labels"] + slots["labels"].max() + 1
        new = new[cols]
        if len(new) > 0:
            new["found"] = 1
        slots = slots.append(new, sort=True).reset_index(drop=True)
        if plot | (i % MERGE_STEP == 0):
            df = slots[['x1', 'y1', 'x2', 'y2', "found", "labels"]].copy()
            #       colors = [(0,1,0)]*len(df)
            msk = df["labels"].isin(id_map.values())

            colors = [(1, 0, 0) if ms else (0, 1, 0) for ms in msk]
            #       df.loc[msk,["R","G","B"]] =np.array([(1,0,0)]*msk.sum())
            nw_df = new[['x1', 'y1', 'x2', 'y2', "labels"]].copy()
            colors = colors + [(0, 1, 0)] * len(nw_df)
            nw_df["found"] = 1
            df = df.append(nw_df, sort=True).reset_index(drop=True)
            df["captions"] = df["labels"].astype(
                str) + " [" + df["found"].astype(str) + "]"

            #       pdb.set_trace()
            masks = np.empty((4, 4, len(df)))
            class_ids = np.array([3] * len(df))
            captions = df["captions"].tolist()
            visualize.display_instances(cv2.imread(img[i]),
                         df[["y1","x1","y2","x2"]].values,\
                        masks, class_ids, None,
                      scores=None, title=img[i],
                      figsize=(20, 20), ax=None,
                      show_mask=False, show_bbox=True,
                      colors=colors,
                            captions=captions)

    slots.drop(slots[slots["found"] < PRUNE_TH * 3].index, inplace=True)
    slots = compute_distance(slots,
                             img[0],
                             th=MERGE_TH * 0.8,
                             label="Parking Slots " + str(MERGE_STEP))
    print(len(slots), "SLOTS FOUND")
    return slots