def main(argv):
    pycaffe_dir = os.path.dirname(__file__)

    mean, channel_swap = None, None
    mean = np.load('./gnetmodels/ilsvrc_2012_mean.npy').mean(1).mean(1)

    crop_mode = "selective_search"
    choices = CROP_MODES
    pretrained_model = "./gnetmodels/rcnn/bvlc_reference_rcnn_ilsvrc13.caffemodel"
    model_def = "./gnetmodels/rcnn/deploy.prototxt"

    raw_scale = 255.0

    channel_swap = [2, 1, 0]  #'2,1,0'
    context_pad = 16

    output_file = "someoutput.h5"

    detector = caffe.Detector(model_def,
                              pretrained_model,
                              mean=mean,
                              input_scale=None,
                              raw_scale=raw_scale,
                              channel_swap=channel_swap,
                              context_pad=context_pad)

    inputs = ['./gnetmodels/one_car.jpg']
    #inputs = pd.read_csv("./test.csv", sep=',', dtype={'filename':str})
    #inputs.set_index('filename', inplace=True)

    img = cv2.imread('./gnetmodels/one_car.jpg')
    width, height, channels = img.shape
    #data = [{'filename':'/home/oscar/Dokument/Development/video_code/coddetection/gnetmodels/one_car.jpg',
    #        'xmin':0,'ymin':0,'xmax':width,'ymax':height}]

    images_windows = [(
        '/home/oscar/Dokument/Development/video_code/coddetection/gnetmodels/one_car.jpg',
        np.array([[0, 0, width, height]]))]
    #inputs = pd.DataFrame(data)
    #inputs.set_index('filename', inplace=True)

    #images_windows = [(ix, inputs.iloc[np.where(inputs.index == ix)][COORD_COLS].values)
    #                    for ix in inputs.index.unique()]

    print(type(images_windows))
    print(images_windows)

    detections = detector.detect_windows(images_windows)
    #images_windows = [(ix, inputs[np.where(inputs.index == ix)][COORD_COLS].values())
    #                    for (ix,val) in enumerate(inputs)]
    #images_windows = [(ix, inputs.iloc[np.where(inputs.index == ix)][COORD_COLS].values())
    #                    for ix in inputs.index.unique()]
    #detections = detector.detect_windows()
    df = pd.DataFrame(detections)
    df.set_index('filename', inplace=True)
    df[COORD_COLS] = pd.DataFrame(data=np.vstack(df['window']),
                                  index=df.index,
                                  columns=COORD_COLS)
    del (df['window'])
예제 #2
0
def detection_example():
    if 'posix' == os.name:
        caffe_home_dir_path = '/home/sangwook/lib_repo/cpp/caffe_github'
    else:
        caffe_home_dir_path = 'D:/lib_repo/cpp/rnd/caffe_github'
        #caffe_home_dir_path = 'D:/lib_repo/cpp/caffe_github'

    model_definition_filepath = caffe_home_dir_path + '/models/bvlc_reference_caffenet/deploy.prototxt'
    trained_model_weights_filepath = caffe_home_dir_path + '/models/bvlc_reference_caffenet/bvlc_reference_caffenet.caffemodel'
    mean_filepath = caffe_home_dir_path + '/python/caffe/imagenet/ilsvrc_2012_mean.npy'

    input_image_filepath = caffe_home_dir_path + '/examples/images/cat.jpg'
    output_image_filepath = caffe_home_dir_path + '/examples/images/cat_detection.csv'

    #inputs_for_detect_windows = [(input_image_filepath, np.array([[0, 0, 360, 480]]))]  # If crop_mode = 'list'.
    inputs_for_detect_windows = [(input_image_filepath,
                                  np.array([[32, 32, 296,
                                             416]]))]  # If crop_mode = 'list'.
    inputs_for_detect_selective_search = [
        input_image_filepath
    ]  # If crop_mode = 'selective_search'.

    inputs = [caffe.io.load_image(input_image_filepath)]
    mean = np.load(mean_filepath) if mean_filepath else None

    crop_mode = 'list'  #'selective_search'
    input_scale = 1.0
    raw_scale = 255.0
    channel_swap = [
        2, 1, 0
    ]  #None  # RGB -> BGR since BGR is the Caffe default by way of OpenCV.
    context_pad = 16

    # Make a detector.
    detector = caffe.Detector(model_definition_filepath,
                              trained_model_weights_filepath,
                              mean=mean,
                              input_scale=input_scale,
                              raw_scale=raw_scale,
                              channel_swap=channel_swap,
                              context_pad=context_pad)

    # Detect.
    if 'list' == crop_mode:
        detections = detector.detect_windows(inputs_for_detect_windows)
    else:
        detections = detector.detect_selective_search(
            inputs_for_detect_selective_search)

    print('**************', type(detections))

    # Save results.
    """
예제 #3
0
def classify_boxes(image_filename, box_list, model_cfg=None, verbose=False):
    import os
    os.environ['GLOG_minloglevel'] = '2'  # reduce caffe output
    import caffe
    import time

    model_path, model_def, model_weights = ''
    if model_cfg is None:
        model_path = '/home/econser/School/Thesis/external/caffe/models/'
        model_def = model_path + 'bvlc_reference_rcnn_ilsvrc13/deploy.prototxt'
        model_weights = model_path + 'bvlc_reference_rcnn_ilsvrc13/bvlc_reference_rcnn_ilsvrc13.caffemodel'
    else:
        model_path = model_cfg.base_path
        model_def = model_cfg.definitions
        model_weights = model_cfg.weights

    #model_weights = model_path + '/rcnn/caffe_nets/ilsvrc_2012_train_iter_310k'
    detector = caffe.Detector(model_def, model_weights, raw_scale=255)

    caffe_boxes = box_list.copy()
    caffe_boxes[:, 0], caffe_boxes[:,
                                   1] = caffe_boxes[:,
                                                    1], caffe_boxes[:,
                                                                    0].copy()
    caffe_boxes[:, 2], caffe_boxes[:,
                                   3] = caffe_boxes[:,
                                                    3], caffe_boxes[:,
                                                                    2].copy()
    boxes = [caffe_boxes]
    images = [image_filename]
    images_and_windows = zip(images, boxes)

    t_start = time.time()
    detections = detector.detect_windows(images_and_windows)
    if verbose:
        print 'RCNN: processed {} boxes in {:.1f} sec'.format(
            len(box_list),
            time.time() - t_start)

    return detections
def load_detectors():
    mean = np.load('./gnetmodels/ilsvrc_2012_mean.npy').mean(1).mean(1)

    #pretrained_model = "./gnetmodels/rcnn/bvlc_reference_rcnn_ilsvrc13.caffemodel"
    pretrained_model = "./gnetmodels/referencecaffenet/bvlc_reference_caffenet.caffemodel"
    #model_def = "./gnetmodels/rcnn/deploy.prototxt"
    model_def = "./gnetmodels/referencecaffenet/deploy.prototxt"

    raw_scale = 255.0

    channel_swap = [2, 1, 0]  #'2,1,0'
    context_pad = 16

    output_file = "someoutput.h5"

    global detector
    detector = caffe.Detector(model_def,
                              pretrained_model,
                              mean=mean,
                              input_scale=None,
                              raw_scale=raw_scale,
                              channel_swap=channel_swap,
                              context_pad=context_pad)
    return detector
예제 #5
0
def main(argv):
    pycaffe_dir = os.path.dirname(__file__)

    parser = argparse.ArgumentParser()
    # Required arguments: input and output.
    parser.add_argument(
        "input_file",
        help="Input txt/csv filename. If .txt, must be list of filenames.\
        If .csv, must be comma-separated file with header\
        'filename, xmin, ymin, xmax, ymax'")
    parser.add_argument(
        "output_file",
        help="Output h5/csv filename. Format depends on extension.")
    # Optional arguments.
    parser.add_argument(
        "--model_def",
        default=os.path.join(
            pycaffe_dir,
            "../models/bvlc_reference_caffenet/deploy.prototxt.prototxt"),
        help="Model definition file.")
    parser.add_argument(
        "--pretrained_model",
        default=os.path.join(
            pycaffe_dir,
            "../models/bvlc_reference_caffenet/bvlc_reference_caffenet.caffemodel"
        ),
        help="Trained model weights file.")
    parser.add_argument("--crop_mode",
                        default="selective_search",
                        choices=CROP_MODES,
                        help="How to generate windows for detection.")
    parser.add_argument("--gpu",
                        action='store_true',
                        help="Switch for gpu computation.")
    parser.add_argument(
        "--mean_file",
        default=os.path.join(pycaffe_dir,
                             'caffe/imagenet/ilsvrc_2012_mean.npy'),
        help="Data set image mean of H x W x K dimensions (numpy array). " +
        "Set to '' for no mean subtraction.")
    parser.add_argument(
        "--input_scale",
        type=float,
        help="Multiply input features by this scale to finish preprocessing.")
    parser.add_argument(
        "--raw_scale",
        type=float,
        default=255.0,
        help="Multiply raw input by this scale before preprocessing.")
    parser.add_argument(
        "--channel_swap",
        default='2,1,0',
        help="Order to permute input channels. The default converts " +
        "RGB -> BGR since BGR is the Caffe default by way of OpenCV.")
    parser.add_argument(
        "--context_pad",
        type=int,
        default='16',
        help="Amount of surrounding context to collect in input window.")
    args = parser.parse_args()

    mean, channel_swap = None, None
    if args.mean_file:
        mean = np.load(args.mean_file)
    if args.channel_swap:
        channel_swap = [int(s) for s in args.channel_swap.split(',')]

    # Make detector.
    detector = caffe.Detector(args.model_def,
                              args.pretrained_model,
                              gpu=args.gpu,
                              mean=mean,
                              input_scale=args.input_scale,
                              raw_scale=args.raw_scale,
                              channel_swap=channel_swap,
                              context_pad=args.context_pad)

    if args.gpu:
        print 'GPU mode'

    # Load input.
    t = time.time()
    print('Loading input...')
    if args.input_file.lower().endswith('txt'):
        with open(args.input_file) as f:
            inputs = [_.strip() for _ in f.readlines()]
    elif args.input_file.lower().endswith('csv'):
        inputs = pd.read_csv(args.input_file, sep=',', dtype={'filename': str})
        inputs.set_index('filename', inplace=True)
    else:
        raise Exception("Unknown input file type: not in txt or csv.")

    # Detect.
    if args.crop_mode == 'list':
        # Unpack sequence of (image filename, windows).
        images_windows = [
            (ix, inputs.iloc[np.where(inputs.index == ix)][COORD_COLS].values)
            for ix in inputs.index.unique()
        ]
        detections = detector.detect_windows(images_windows)
        print images_windows
    else:
        detections = detector.detect_selective_search(inputs)
    print("Processed {} windows in {:.3f} s.".format(len(detections),
                                                     time.time() - t))

    # Collect into dataframe with labeled fields.
    df = pd.DataFrame(detections)
    df.set_index('filename', inplace=True)
    df[COORD_COLS] = pd.DataFrame(data=np.vstack(df['window']),
                                  index=df.index,
                                  columns=COORD_COLS)
    del (df['window'])

    # Save results.
    t = time.time()
    if args.output_file.lower().endswith('csv'):
        # csv
        # Enumerate the class probabilities.
        class_cols = ['class{}'.format(x) for x in range(NUM_OUTPUT)]
        df[class_cols] = pd.DataFrame(data=np.vstack(df['feat']),
                                      index=df.index,
                                      columns=class_cols)
        df.to_csv(args.output_file, cols=COORD_COLS + class_cols)
    else:
        # h5
        df.to_hdf(args.output_file, 'df', mode='w')
    print("Saved to {} in {:.3f} s.".format(args.output_file, time.time() - t))
예제 #6
0
    def __init__(self,
                 camera_id,
                 mean,
                 pretrained_model,
                 model_def,
                 raw_scale,
                 channel_swap,
                 context_pad,
                 synset_file,
                 related_word_detector,
                 information_base,
                 process_images=False,
                 mode_gpu=False):
        threading.Thread.__init__(self)

        self.stop_var = False

        self.camera_id = camera_id
        self.process_images = process_images

        self.mean = np.load(mean).mean(1).mean(1)

        # Enable GPU computations with the mode_gpu=True. This is only possible if caffe are
        # compiled with CUDA. By default it is set to use the CPU.
        if (mode_gpu):
            caffe.set_mode_gpu()
        else:
            caffe.set_mode_cpu()

        # Setup the detector
        self.detector = caffe.Detector(model_def,
                                       pretrained_model,
                                       mean=self.mean,
                                       input_scale=None,
                                       raw_scale=raw_scale,
                                       channel_swap=channel_swap,
                                       context_pad=context_pad)

        self.classifier = caffe.Classifier(model_def,
                                           pretrained_model,
                                           image_dims=None,
                                           mean=self.mean,
                                           input_scale=None,
                                           raw_scale=raw_scale,
                                           channel_swap=channel_swap)

        self.net = caffe.Net(model_def, pretrained_model, caffe.TEST)
        self.transformer = caffe.io.Transformer(
            {'data': self.net.blobs['data'].data.shape})
        self.transformer.set_transpose('data', (2, 0, 1))
        self.transformer.set_mean('data', self.mean)
        self.transformer.set_raw_scale('data', raw_scale)
        self.transformer.set_channel_swap('data', channel_swap)

        # Parse the labels for the classifications
        f = open(synset_file)
        self.labels_df = pd.DataFrame([{
            'synset_id':
            l.strip().split(' ')[0],
            'name':
            ' '.join(l.strip().split(' ')[1:]).split(',')[0]
        } for l in f.readlines()])
        f.close()
        self.labels_df.sort_values(by='synset_id')

        self.labels = np.loadtxt(synset_file, str, delimiter='\t')
        self.related_word_detector = related_word_detector
        self.informationbase = information_base
예제 #7
0
def main(argv):
    # Change default locations to match personal system setup.
    parser = argparse.ArgumentParser()
    # Required arguments: input and output.
    parser.add_argument(
        "input_file",
        help="Input txt/csv/h5/image filename. If .txt, must be list of \
        filenames. If .csv, must be comma-separated file with header \
        'filename, xmin, ymin, xmax, ymax'. If image file, must have extension \
        JPEG/jpg/png. If h5 file, must be the result of RCNN."
    )
    parser.add_argument(
        "output_file",
        help="Output h5/csv filename. Format depends on extension."
    )
    # Optional Arguments
    parser.add_argument(
        "--model",
        default="/home/jonathanzhang/workspace/models/fpga_vgg16/"
                "VGG_ILSVRC_16_layers_deploy.prototxt",
        help="Model definition file."
    )
    parser.add_argument(
        "--weights",
        default="/home/jonathanzhang/workspace/models/fpga_vgg16/finetune_fpga_2/"
        "ilsvrc13_det_finetune_fpga_2_iter_60000.caffemodel",
        help="Trained model weights file."
    )

    parser.add_argument(
        "--crop_mode",
        default="pycrop",
        choices=CROP_MODES,
        help="How to generate windows for detection."
    )
    parser.add_argument(
        "--gpu",
        action='store_true',
        help="Switch for gpu computation."
    )
    parser.add_argument(
        "--mean_file",
        default=None,
        help="Data set image mean of H x W x K dimensions (numpy array). " +
             "Set to '' for no mean subtraction."
    )
    parser.add_argument(
        "--input_scale",
        type=float,
        help="Multiply input features by this scale to finish preprocessing."
    )
    parser.add_argument(
        "--raw_scale",
        type=float,
        default=None,
        help="Multiply raw input by this scale before preprocessing."
    )
    parser.add_argument(
        "--channel_swap",
        default='2,1,0',
        help="Order to permute input channels. The default converts " +
             "RGB -> BGR since BGR is the Caffe default by way of OpenCV."

    )
    parser.add_argument(
        "--context_pad",
        type=int,
        default='16',
        help="Amount of surrounding context to collect in input window."
    )
    parser.add_argument(
        "--image_test",
        default=True,
        action='store_true',
        help="Show bounding box plots of input images. Default is True")
    parser.add_argument(
        "--caffe_root",
        default="/opt/caffe/",
        help="Default location of caffe files")
    parser.add_argument(
        "--workspace_root",
        default="/home/jonathanzhang/workspace/",
        help="Default location to search for images and synset_list")

    args = parser.parse_args()

    if args.input_file.strip().endswith('h5'):
        print "Loading hdf5 file..."
        process_results(pd.read_hdf(args.input_file.strip()),
                        args.workspace_root,
                        args.caffe_root)
        return

    mean, channel_swap = None, None
    if args.mean_file:
        mean = np.load(args.mean_file)
        if mean.shape[1:] != (1, 1):
            mean = mean.mean(1).mean(1)
    if args.channel_swap:
        channel_swap = [int(s) for s in args.channel_swap.split(',')]

    if args.gpu:
        caffe.set_mode_gpu()
        print("GPU mode")
    else:
        caffe.set_mode_cpu()
        print("CPU mode")

    # Make detector.
    detector = caffe.Detector(args.model, args.weights,
                              mean=mean,
                              input_scale=args.input_scale,
                              raw_scale=args.raw_scale,
                              channel_swap=channel_swap,
                              context_pad=args.context_pad)

    # Load input.
    print("Loading input...")
    if args.input_file.lower().endswith('txt'):
        with open(args.input_file) as f:
            inputs = [_.strip() for _ in f.readlines()]
    elif args.input_file.lower().endswith('csv'):
        inputs = pd.read_csv(args.input_file, sep=',', dtype={'filename': str})
        inputs.set_index('filename', inplace=True)
    elif args.input_file.lower().endswith(IMG_TYPES):
        inputs = [args.input_file]
    else:
        raise Exception("Unknown input file type: not in txt, csv, jpg, jpeg,"
                        "or png.")

    # Detect.

    if args.crop_mode == 'list':
        # Unpack sequence of (image filename, windows).
        images_windows = [
            (ix, inputs.iloc[np.where(inputs.index == ix)][COORD_COLS].values)
            for ix in inputs.index.unique()
        ]

    elif args.crop_mode == 'pycrop':
        # Thresholds for Felzenszwalb and Huttenlocher segmentation algorithm
        # By default, we set minSize = k, and sigma = 0.8
        # k = scale
        t1 = time.time()
        print "Calculating region propsals..."

        ks = [20, 50, 100, 150, 200, 250, 300, 350, 500, 1000]
        sigma = 0.8

        ssearch_regions = [[] for i in range(len(inputs))]
        region_candidates = []
        # Process all images
        for imname in inputs:
            img = caffe.io.load_image(imname).astype(np.float32)

            # k is the Segmentation Threshold
            for k in ks:
                _, regions = selectivesearch.selective_search(
                    img, scale=k, sigma=sigma, min_size=k)

                ssearch_regions[i] += regions

            for image_regions in ssearch_regions:

                # Extract all unique boxes
                candidates = set()

                for region in image_regions:

                    # caffe.Detector expects coordinates to be in
                    # (ymin, xmin, ymax, xmax) format
                    x, y, w, h = region['rect']
                    region['rect'] = (y, x, y + h, x + w)

                    if region['rect'] in candidates or h < 20 or w < 20:
                        continue

                    candidates.add(region['rect'])

                region_candidates.append(np.array(list(candidates)))

        #  print(zip(inputs, region_candidates))
        images_windows = zip(inputs, region_candidates)

        print "Found {} region propsals in {:.3f} seconds.".format(
            np.size(region_candidates[0], 0), time.time() - t1)

    t = time.time()
    detections = detector.detect_windows(images_windows)
    print("Processed {} windows in {:.3f} s.".format(len(detections),
                                                     time.time() - t))

    # Collect into dataframe with labeled fields.
    df = pd.DataFrame(detections)
    df.set_index('filename', inplace=True)
    df[COORD_COLS] = pd.DataFrame(
        data=np.vstack(df['window']), index=df.index, columns=COORD_COLS)
    del(df['window'])

    # Save results.
    t = time.time()
    if args.output_file.lower().endswith('h5'):
        df.to_hdf(args.output_file, 'df', mode='w')
        print("Saved to {} in {:.3f} s.".format(args.output_file,
                                                time.time() - t))
    else:
        print "Invalid file type"

    if args.image_test:
        process_results(df, args.workspace_root, args.caffe_root)
예제 #8
0
파일: detect.py 프로젝트: trendiguru/core
def main(argv):
    pycaffe_dir = os.path.dirname(__file__)

    parser = argparse.ArgumentParser()
    # Required arguments: input and output.
    parser.add_argument(
        "input_file",
        help="Input txt/csv filename. If .txt, must be list of filenames.\
        If .csv, must be comma-separated file with header\
        'filename, xmin, ymin, xmax, ymax'")
    parser.add_argument(
        "output_file",
        help="Output h5/csv filename. Format depends on extension.")
    # Optional arguments.
    parser.add_argument(
        "--model_def",
        default=os.path.join(
            pycaffe_dir,
            #                "../models/bvlc_reference_caffenet/deploy.prototxt"),
            # see https://github.com/BVLC/caffe/issues/2268
            "../models/bvlc_reference_rcnn_ilsvrc13/deploy.prototxt"),
        help="Model definition file.")
    parser.add_argument(
        "--pretrained_model",
        default=os.path.join(
            pycaffe_dir,
            #                "../models/bvlc_reference_caffenet/bvlc_reference_caffenet.caffemodel"),
            "../models/bvlc_reference_rcnn_ilsvrc13/bvlc_reference_rcnn_ilsvrc13.caffemodel"
        ),
        help="Trained model weights file.")
    parser.add_argument("--crop_mode",
                        default="selective_search",
                        choices=CROP_MODES,
                        help="How to generate windows for detection.")
    parser.add_argument("--gpu",
                        action='store_true',
                        help="Switch for gpu computation.")
    parser.add_argument(
        "--mean_file",
        default=os.path.join(pycaffe_dir,
                             'caffe/imagenet/ilsvrc_2012_mean.npy'),
        #                             'caffe/imagenet/ilsvrc_2012_mean.npy')
        help="Data set image mean of H x W x K dimensions (numpy array). " +
        "Set to '' for no mean subtraction.")
    parser.add_argument(
        "--input_scale",
        type=float,
        help="Multiply input features by this scale to finish preprocessing.")
    parser.add_argument(
        "--raw_scale",
        type=float,
        default=255.0,
        help="Multiply raw input by this scale before preprocessing.")
    parser.add_argument(
        "--channel_swap",
        default='2,1,0',
        help="Order to permute input channels. The default converts " +
        "RGB -> BGR since BGR is the Caffe default by way of OpenCV.")
    parser.add_argument(
        "--context_pad",
        type=int,
        default='16',
        help="Amount of surrounding context to collect in input window.")
    args = parser.parse_args()

    mean, channel_swap = None, None
    if args.mean_file:
        mean = np.load(args.mean_file)
        if mean.shape[1:] != (1, 1):
            mean = mean.mean(1).mean(1)
    if args.channel_swap:
        channel_swap = [int(s) for s in args.channel_swap.split(',')]

    if args.gpu:
        caffe.set_mode_gpu()
        print("GPU mode")
    else:
        caffe.set_mode_cpu()
        print("CPU mode")

    # Make detector.
    detector = caffe.Detector(args.model_def,
                              args.pretrained_model,
                              mean=mean,
                              input_scale=args.input_scale,
                              raw_scale=args.raw_scale,
                              channel_swap=channel_swap,
                              context_pad=args.context_pad)

    # Load input.
    t = time.time()
    print("Loading input...")
    if args.input_file.lower().endswith('txt'):
        with open(args.input_file) as f:
            inputs = [_.strip() for _ in f.readlines()]
    elif args.input_file.lower().endswith('csv'):
        inputs = pd.read_csv(args.input_file, sep=',', dtype={'filename': str})
        inputs.set_index('filename', inplace=True)
    else:
        raise Exception("Unknown input file type: not in txt or csv.")

    # Detect.
    if args.crop_mode == 'list':
        # Unpack sequence of (image filename, windows).
        images_windows = [
            (ix, inputs.iloc[np.where(inputs.index == ix)][COORD_COLS].values)
            for ix in inputs.index.unique()
        ]
        detections = detector.detect_windows(images_windows)
    else:
        detections = detector.detect_selective_search(inputs)
    print("Processed {} windows in {:.3f} s.".format(len(detections),
                                                     time.time() - t))

    # Collect into dataframe with labeled fields.
    df = pd.DataFrame(detections)
    df.set_index('filename', inplace=True)
    df[COORD_COLS] = pd.DataFrame(data=np.vstack(df['window']),
                                  index=df.index,
                                  columns=COORD_COLS)
    del (df['window'])

    print df
    #    print('output:'+str(df))
    topwindows = []
    topindices = []
    threshold = 0.95

    caffe_root = '/opt/caffe/'
    labels_file = caffe_root + 'data/ilsvrc12/synset_words.txt'
    #    if not os.path.exists(labels_file):
    #    !../data/ilsvrc12/get_ilsvrc_aux.sh

    labels = np.loadtxt(labels_file, str, delimiter='\t')

    for d in detections:

        #	print d
        predictions = d['prediction']
        filename = d['filename']
        img_arr = cv2.imread(filename)
        window = d['window']
        class1 = np.argmax(predictions)
        sorted = predictions.argsort()[-1:-5:-1]
        vals = []
        for s in sorted:
            vals.append(predictions[s])
        if vals[0] > threshold:
            window = [int(x) for x in window]
            topwindows.append(window)
            topindices.append(vals[0])
            label = labels[class1]
            if vals[1] > threshold:
                print('2nd value {} also > threshold {}'.format(
                    vals, threshold))
            print('window {} topindices {} vals {} label {}'.format(
                window, sorted, vals, label))
            cv2.rectangle(img_arr, (window[1], window[0]),
                          (window[3], window[2]), [255, 255, 100], 2)
            cv2.putText(img_arr, label, (window[1], window[0]),
                        cv2.FONT_HERSHEY_SIMPLEX, 0.5, [100, 255, 0], 1)

            cv2.imshow('test', img_arr)
            cv2.waitKey(0)
    cv2.waitKey(0)
    # Save results.
    t = time.time()
    if args.output_file.lower().endswith('csv'):
        # csv
        # Enumerate the class probabilities.
        NUM_OUTPUT = 10
        class_cols = ['class{}'.format(x) for x in range(NUM_OUTPUT)]
        df[class_cols] = pd.DataFrame(data=np.vstack(df['feat']),
                                      index=df.index,
                                      columns=class_cols)
        df.to_csv(args.output_file, cols=COORD_COLS + class_cols)
    else:
        # h5
        df.to_hdf(args.output_file, 'df', mode='w')
    print("Saved to {} in {:.3f} s.".format(args.output_file, time.time() - t))
예제 #9
0
    if args.mean_file:
        mean = np.load(args.mean_file)
        if mean.shape[1:] != (1, 1):
            mean = mean.mean(1).mean(1)
    if args.channel_swap:
        channel_swap = [int(s) for s in args.channel_swap.split(',')]

    if args.gpu:
        caffe.set_mode_gpu()
        print("GPU mode")
    else:
        caffe.set_mode_cpu()
        print("CPU mode")

    # Make detector.
    detector = caffe.Detector(args.model_def, args.pretrained_model, mean=mean,
            input_scale=args.input_scale, raw_scale=args.raw_scale,
            channel_swap=channel_swap,
            context_pad=args.context_pad)

    # Load input.
    t = time.time()
    print("Loading input...")
    if args.input_file.lower().endswith('txt'):
        with open(args.input_file) as f:
            inputs = [_.strip() for _ in f.readlines()]
    elif args.input_file.lower().endswith('csv'):
        inputs = pd.read_csv(args.input_file, sep=',', dtype={'filename': str})
        inputs.set_index('filename', inplace=True)
    else:
        raise Exception("Unknown input file type: not in txt or csv.")
예제 #10
0
파일: detector.py 프로젝트: yisampi/SeqFace
    def __init__(self,
                 deploy_prototxt,
                 model_file,
                 mean_file=None,
                 mean_value=None,
                 ratio_file=None,
                 label_file=None,
                 device_id=-1,
                 score_thred=0.0,
                 top_k=-1,
                 ratio=-1.0,
                 image_size=256,
                 raw_scale=255.0,
                 gray=False):
        """
        初始化
        """
        Tester.__init__(self)

        self.score_thred = score_thred
        self.top_k = top_k
        self.ratio = ratio
        self.gray = gray

        self.ratios = None
        if ratio_file is not None and os.path.exists(ratio_file):
            self.ratios = dict()
            for i, line in enumerate(open(ratio_file)):
                items = line.strip(" \0\t\r\n").split(";")
                self.ratios[i] = float(items[1])

        self.labels = None
        if label_file is not None:
            self.labels = open(label_file).readlines()
            self.labels = filter(lambda x: len(x.split(";")) == 2, self.labels)
            self.labels = map(lambda x: x.strip().decode("utf-8"), self.labels)

        if device_id < 0:
            caffe.set_mode_cpu()
        else:
            caffe.set_mode_gpu()
            caffe.set_device(device_id)

        mean = None
        # mean: num, channels, height, width
        if mean_file and os.path.exists(mean_file):
            mean_suffix = os.path.splitext(mean_file)[1]
            if mean_suffix == ".binaryproto":
                binary_data = open(mean_file, "rb").read()
                proto_data = caffe.io.caffe_pb2.BlobProto.FromString(
                    binary_data)
                mean = caffe.io.blobproto_to_array(proto_data)
            elif mean_suffix == ".npy":
                mean = np.load(mean_file)
        elif mean_value is not None:
            mean = np.tile(np.array(mean_value),
                           (1, image_size, image_size, 1))
            mean = mean.transpose((0, 3, 1, 2))

        if mean is not None and mean.ndim == 4:
            mean = mean[0]
        assert mean is None or mean.ndim == 3

        self.detector = caffe.Detector(deploy_prototxt, model_file, \
                                       image_dims=(image_size, image_size), mean=mean, \
                                       raw_scale=raw_scale, channel_swap=(2, 1, 0) if not self.gray else None)

        # thread lock
        self.mutex = threading.Lock()

        # init over
        self.is_init = True
예제 #11
0
 def __init__(self, caffemodel, prototxt, classes):
     self.caffemodel = caffemodel
     self.prototxt = prototxt
     self.mean = np.array([104, 117, 123], np.uint8)
     self.net = caffe.Detector(prototxt, caffemodel,mean=self.mean)
예제 #12
0
 def __init__(self, model_def, model_weights, class_labels, image_mean=None, gpu_mode=True):
     super(RCNN, self).__init__(model_def, model_weights, class_labels, image_mean=image_mean, gpu_mode=gpu_mode)
     # TODO: send in optional parameters to caffe Detector
     context_pad = 5
     self.detector = caffe.Detector(model_def, model_weights, mean=image_mean, input_scale=self.input_scale, raw_scale=self.raw_scale, channel_swap=self.channel_swap, context_pad=context_pad)