Exemplo n.º 1
0
                label_scale_up = Label(
                    tl=label_sub.tl() * WH + label_FR.tl() * WH_sub,
                    br=label_sub.tl() * WH + label_FR.br() * WH_sub)
                tl = label_scale_up.tl().astype(int)
                br = label_scale_up.br().astype(int)
                txt_file.write(frd[0][0] + ' ' + str('%.2f' % frd[0][1]) +
                               ' ' + str(tl[0]) + ' ' + str(tl[1]) + ' ' +
                               str(br[0]) + ' ' + str(br[1]) + '\n')
                print '\twrote result to', join(
                    output_dir,
                    basename(splitext(img_path)[0]) + '.txt')
        txt_file.close()


# manual arguments, it will automatically generate .txt file with the format for calculating mAP in this project:
# https://github.com/Cartucho/mAP
# the default dir path for txt files -> output_txt/
FR_weights = '/home/shaoheng/Documents/darknet-Alex/backup/FRNet_YOLOv3/FRNet_YOLOv3_50000.weights'
FR_netcfg = 'data/FRD/FRNet_YOLOv3.cfg'
FR_data = 'data/FRD/FRNet_YOLOv3.data'
valid_dataset = '500'
ROOT = '/home/shaoheng/Documents/mAP/input/images-optional'  # root for validation images

print 'FRD Net pre-loading...'
FR_net = dn.load_net(FR_netcfg, FR_weights, 0)
FR_meta = dn.load_meta(FR_data)
threshold = 0.5

imgs_paths = image_files_from_folder(ROOT)
do_fr(imgs_paths)
# do_fr_after_YOLO(imgs_paths)
                (WH, WH))).tolist()
            tl = np.array([cx - w / 2., cy - h / 2.])
            br = np.array([cx + w / 2., cy + h / 2.])
            print '\t\t\tFR number', i, 'position:', tl, br, 'prob:', result[1]
            FRs.append(Label(tl=tl, br=br))
            category.append(result[0])
        return np.array(FRs), category

    else:
        print '\t\t\tFR detection failed'


if __name__ == '__main__':
    input_dir = 'samples/kr'
    output_dir = 'output'
    imgs_paths = image_files_from_folder(input_dir)
    imgs_paths.sort()

    for img_path in imgs_paths:
        img = cv2.imread(img_path)
        bname = basename(splitext(img_path)[0])

        try:
            FRs, cate = fr_detect(img)

            for i in range(len(FRs)):
                draw_bb(img, FRs[i], cate=cate[i])
        except:
            traceback.print_exc()

        cv2.imwrite(join(output_dir, bname + '_out.jpg'), img)
    outdir = args.output_dir

    iterations = args.iterations
    batch_size = args.batch_size
    dim = 208

    if not isdir(outdir):
        makedirs(outdir)

    model, model_stride, xshape, yshape = load_network(args.model, dim)

    opt = getattr(keras.optimizers, args.optimizer)(lr=args.learning_rate)
    model.compile(loss=loss, optimizer=opt)

    print 'Checking input directory...'
    Files = image_files_from_folder(train_dir)

    Data = []
    for file in Files:
        labfile = splitext(file)[0] + '.txt'
        if isfile(labfile):
            L = readShapes(labfile)
            I = cv2.imread(file)
            Data.append([I, L[0]])

    print '%d images with labels found' % len(Data)

    dg = DataGenerator( data=Data, \
         process_data_item_func=lambda x: process_data_item(x,dim,model_stride),\
         xshape=xshape, \
         yshape=(yshape[0],yshape[1],yshape[2]+1), \
from glob import glob
from os.path import splitext, basename, isfile
from WPOD_src.utils import crop_region, image_files_from_folder
from WPOD_src.drawing_utils import draw_label, draw_losangle, write2img
from WPOD_src.label import lread, Label, readShapes

from pdb import set_trace as pause

YELLOW = (0, 255, 255)
RED = (0, 0, 255)

input_dir = sys.argv[1]
output_dir = sys.argv[2]

img_files = image_files_from_folder(input_dir)

for img_file in img_files:

    bname = splitext(basename(img_file))[0]

    I = cv2.imread(img_file)

    detected_cars_labels = '%s/%s_cars.txt' % (output_dir, bname)

    Lcar = lread(detected_cars_labels)

    sys.stdout.write('%s' % bname)

    if Lcar: