def generateGridLabel(x_path, y, img_size):
    # scale_file = open(scale_path, 'rb')
    # label_min = pickle.load(scale_file)
    # label_max = pickle.load(scale_file)
    # scale_file.close()

    if len(img_size) != len(y):
        print('The length of size_info and y is not same.')
        os._exit(0)

    if Check_Resize2:
        for i in range(len(y)):
            x_path[i] = '.' + x_path[i]
        x, y = sr.sampleResize(x_path=x_path,
                               y=y,
                               im_size=img_size,
                               short_side=ShortSide,
                               long_side=LongSide,
                               resizeh=ResizeH,
                               resizew=ResizeW,
                               resizeX=True,
                               resizeY=True)
        sp.show_bbox_in_one_image(x, y)
        input('Stop')

    y = sr.sampleResize(y=y,
                        im_size=img_size,
                        short_side=ShortSide,
                        long_side=LongSide,
                        resizeh=ResizeH,
                        resizew=ResizeW,
                        resizeY=True)

    label_c, label_location = hs.relabel_2D_location(y, ResizeH, ResizeW,
                                                     DSR_x, DSR_y, CLASS,
                                                     CrossRatio)
    # label_c = np_utils.to_categorical(label_c, num_classes=CLASS + 1)
    # label_bbox = mms.vtMinMaxScaler(label_bbox, label_min, label_max)
    return label_c, label_location
def checkResize(x_path, y, im_size):
    for i in range(len(y)):
        x_path[i] = '.' + x_path[i]
        sx, sy = sr.sampleResize(x_path=x_path[i:i + 1],
                                 y=y[i:i + 1],
                                 im_size=im_size[i:i + 1],
                                 short_side=ShortSide,
                                 long_side=LongSide,
                                 resizeh=ResizeH,
                                 resizew=ResizeW,
                                 resizeX=True,
                                 resizeY=True)
        # print(len(sx[0]),len(sy[0]))
        print(x_path[i])
        sp.show_bbox_in_one_image(sx, sy)
Exemple #3
0
def getBatch(X_path, batch_size, label_path, val=False):
    FMAP_w = ResizeW // DSR_x
    FMAP_h = ResizeH // DSR_y
    X_path = np.array(X_path)
    while 1:
        # 打乱数据非常重要
        permutation = np.random.permutation(X_path.shape[0])
        X_path = X_path[permutation]
        # label_c = label_c[permutation, :, :, :]
        # label_bbox = label_bbox[permutation, :, :, :]
        # X, label_c, label_bbox = shuffleTogether(X_path, label_c, label_bbox)
        print(X_path[0])  # 用于确认是否打乱
        if val:
            Epoch_len = X_path.shape[0]
        else:
            Epoch_len = X_path.shape[0]  # Todo:如果要每次用部分训练样本,设置这里
        for i in range(0, Epoch_len, batch_size):
            x = sr.sampleResize(x_path=X_path[i:i + batch_size],
                                short_side=ShortSide,
                                long_side=LongSide,
                                resizeh=ResizeH,
                                resizew=ResizeW,
                                resizeX=True)
            Y_number = len(X_path[i:i + batch_size])
            cls = np.zeros((Y_number, FMAP_h, FMAP_w, 1))
            loc = np.zeros((Y_number, FMAP_h, FMAP_w, 1))
            for j in range(Y_number):
                row = 0
                Y_path = label_path + str(
                    os.path.basename(X_path[i + j]).split('.')[0]) + '.txt'
                with open(Y_path, 'r') as f:
                    for line in f:
                        line_split = line.strip().split(' ')
                        cls[j][row // FMAP_w][row % FMAP_w][0] = int(
                            float(line_split[0]))
                        loc[j][row // FMAP_w][row % FMAP_w][0] = int(
                            float(line_split[1]))
                        row += 1
            # cls = label_c[i:i + batch_size, :, :, :]
            # bbox = label_bbox[i:i + batch_size, :, :, :]
            # 最重要的就是这个yield,它代表返回,返回以后循环还是会继续,然后再返回。
            # 就比如有一个机器一直在作累加运算,但是会把每次累加中间结果告诉你一样,直到把所有数加完
            yield ({
                'input': x
            }, {
                'cls_out': cls,
                'loc_out': np_utils.to_categorical(loc, num_classes=5)
            })
def addTest_ResizeInfo(test_img):
    # start_time = time.time()
    X_path, Y, size = getInformation(test_img)
    # print(len(Y))
    # x = sr.sampleResize(x_path=X_path, short_side=ShortSide, long_side=LongSide,
    #                     resizeh=ResizeH, resizew=ResizeW, resizeX=True)
    size_info, pad_info = sr.sampleResize(y=Y,
                                          im_size=size,
                                          short_side=ShortSide,
                                          long_side=LongSide,
                                          resizeh=ResizeH,
                                          resizew=ResizeW,
                                          resizeY=True,
                                          sizeinfo=True)
    for i in range(len(Y)):
        # test_img[i]['pixel'] = x[i]
        test_img[i]['resizeinfo'] = size_info[i]
        test_img[i]['padinfo'] = pad_info[i]
        # print(test_img[i]['filepath'])
        # print(test_img[i]['resizeinfo'])
        # print(test_img[i]['padinfo'])
        # input('stop')
    # print('The average time of reading an image is {} seconds.'.format((time.time() - start_time) / len(test_img)))
    return test_img
Exemple #5
0
def testFast(test_img, md, name_model):
    # (height, width, channel) = test_img[0]['pixel'].shape
    # test_x = np.zeros((len(test_img), height, widt h, channel))
    predict_cls = np.zeros((len(test_img), ResizeH // DSR_y, ResizeW // DSR_x, 1))
    predict_loc = np.zeros((len(test_img), ResizeH // DSR_y, ResizeW // DSR_x, 5))

    # scale_file = open(scale_path, 'rb')
    # label_min = pickle.load(scale_file)
    # label_max = pickle.load(scale_file)
    # scale_file.close()

    X_path, Y, size = getInformation(test_img)
    x = sr.sampleResize(x_path=X_path, short_side=ShortSide, long_side=LongSide,
                        resizeh=ResizeH, resizew=ResizeW, resizeX=True)

    bbox = []
    start = time.time()
    for i in range(len(test_img)):
        # predicting...
        img = x[i]
        test_x = img.reshape((1, img.shape[0], img.shape[1], img.shape[2]))
        predict_cls[i], predict_loc[i] = md.predict(test_x)
        box = pp.processLocation(img, copy.deepcopy(predict_cls[i]), copy.deepcopy(predict_loc[i]),
                                 DSR_x, DSR_y, filterBox=FilterBBox)
        bbox.append(box)

    # inverse data standardizing
    # predict_bbox = mms.vtMinMaxScaler_inverse(predict_bbox, label_min, label_max)  # 有点疑问,是否要reshape
    # handle bounding box
    # 明天从这里写下去
    # bbox = handleBBOX(predict_loc, predict_cls)
    # current_model = ModelPath + name_model + '.hdf5'
    # NMS
    # for i in range(len(bbox)):
    #     bbox[i] = NMS.non_max_suppression_fast(bbox[i], overlap_thresh=0.5,
    #                                            max_boxes=20, current_model=name_model)
    cost_time = time.time() - start
    print('The cost time for an image is', str(cost_time / len(test_img)) + '.')

    # if len(bbox) != len(test_img):
    #     print('The lengths of bboxes and images are inconsistent.')
    #     os._exit(0)

    # 画小切块的类别
    if GridImage:
        ResultImage2_md = ResultImage + name_model + '_grid/'
        if not os.path.exists(ResultImage2_md):
            os.makedirs(ResultImage2_md)
        for i in tqdm(range(x.shape[0])):
            img_path = test_img[i]['filepath']
            base_name = str(os.path.basename(img_path).split('.')[0])
            img2 = x[i] * 255
            img2 = sp.draw_grid_location(img2, predict_cls[i], predict_loc[i], CLASS)
            result_image2_path = ResultImage2_md + base_name + '.png'
            cv2.imwrite(result_image2_path, img2)

    if LocationImage:
        ResultImage3_md = ResultImage + name_model + '_location/'
        if not os.path.exists(ResultImage3_md):
            os.makedirs(ResultImage3_md)
        for i in tqdm(range(x.shape[0])):
            img_path = test_img[i]['filepath']
            base_name = str(os.path.basename(img_path).split('.')[0])
            img3 = x[i] * 255
            img3 = pp.processLocation(img3, predict_cls[i], predict_loc[i], DSR_x, DSR_y, drawBBox=True,
                                      filterBox=FilterBBox)
            result_image3_path = ResultImage3_md + base_name + '.png'
            cv2.imwrite(result_image3_path, img3)

    # if ResizedImage_bbox:
    #     ResultImage3_md = ResultImage + name_model + '_bbox/'
    #     if not os.path.exists(ResultImage3_md):
    #         os.makedirs(ResultImage3_md)
    #     for i in tqdm(range(x.shape[0])):
    #         img_path = test_img[i]['filepath']
    #         base_name = str(os.path.basename(img_path).split('.')[0])
    #         img3 = x[i] * 255
    #         bboxes = {}
    #         for b in range(len(bbox[i])):
    #             class_name = int(bbox[i][b][5])
    #             class_prob = round(bbox[i][b][4], 2)
    #             x1 = round(bbox[i][b][0], 2)
    #             y1 = round(bbox[i][b][1], 2)
    #             x2 = round(bbox[i][b][2], 2)
    #             y2 = round(bbox[i][b][3], 2)
    #             if class_name not in bboxes:
    #                 bboxes[class_name] = []
    #             bboxes[class_name].append([x1, y1, x2, y2, class_prob, class_name])
    #
    #         img3 = sp.draw_bbox_in_one_image(img3, bboxes)
    #         # result_image_path = ResultImage_md + mark + base_name + '.png'
    #         result_image3_path = ResultImage3_md + base_name + '.png'
    #         cv2.imwrite(result_image3_path, img3)
    #
    for i in range(len(bbox)):
        bbox[i] = sr.bboxesTransform_inverse(bbox[i], test_img[i]['resizeinfo'], test_img[i]['padinfo'], isWH=True)
    return bbox