コード例 #1
0
def main(weights_file='./weights/yolov3.weights',
         class_names_file='./data/labels/coco.names'):
    class_names = load_class_names(class_names_file)
    n_classes = len(class_names)

    model = Yolo_v3(n_classes=n_classes,
                    model_size=(416, 416),
                    max_output_size=5,
                    iou_threshold=0.5,
                    confidence_threshold=0.5)

    inputs = tf.compat.v1.placeholder(tf.float32, [1, 416, 416, 3])

    model(inputs, training=False)

    model_vars = tf.compat.v1.global_variables(scope='yolo_v3_model')
    assign_ops = load_weights(model_vars, weights_file)

    saver = tf.compat.v1.train.Saver(
        tf.compat.v1.global_variables(scope='yolo_v3_model'))

    with tf.compat.v1.Session() as sess:
        sess.run(assign_ops)
        saver.save(sess, './weights/model.ckpt')
        print('Model has been saved successfully.')
コード例 #2
0
ファイル: image.py プロジェクト: peternabil/yolov3-tf-api
def main(img,model):
    # model = YOLOv3Net(cfgfile,model_size,num_classes)
    # model.load_weights(weightfile)
    #
    class_names = load_class_names(class_name)

    # image = cv2.imread(img_path)
    image = img
    image = np.array(image)
    image = tf.expand_dims(image, 0)

    resized_frame = resize_image(image, (model_size[0],model_size[1]))
    pred = model.predict(resized_frame)

    boxes, scores, classes, nums = output_boxes( \
        pred, model_size,
        max_output_size=max_output_size,
        max_output_size_per_class=max_output_size_per_class,
        iou_threshold=iou_threshold,
        confidence_threshold=confidence_threshold)

    image = np.squeeze(image)
    img,person_num = draw_outputs(image, boxes, scores, classes, nums, class_names)
    # cv2.putText(img, str(person_num)+" Persons", (10,200), cv2.FONT_HERSHEY_SIMPLEX, 3, (0, 255, 0), 2, cv2.LINE_AA)
    win_name = 'Image detection'
    return img,person_num,boxes,scores, classes, nums,class_names
    cv2.imshow(win_name, img)
    cv2.waitKey(0)
    cv2.destroyAllWindows()
コード例 #3
0
ファイル: caffe_yolo_detect.py プロジェクト: dyz-zju/MVision
def detect(cfgfile, weightfile, imgfile):
    if cfgfile.find('.prototxt') >= 0:
        from caffenet import CaffeNet
        m = CaffeNet(cfgfile)
    else:
        m = Darknet(cfgfile)

    m.print_network()
    m.load_weights(weightfile)
    print('Loading weights from %s... Done!' % (weightfile))

    if m.num_classes == 20:
        namesfile = 'data/voc.names'
    elif m.num_classes == 80:
        namesfile = 'data/coco.names'
    else:
        namesfile = 'data/names'
    
    use_cuda = 1
    if use_cuda:
        m.cuda()

    img = Image.open(imgfile).convert('RGB')
    sized = img.resize((m.width, m.height))
    
    for i in range(2):
        start = time.time()
        boxes = do_detect(m, sized, 0.5, 0.4, use_cuda)
        finish = time.time()
        if i == 1:
            print('%s: Predicted in %f seconds.' % (imgfile, (finish-start)))

    class_names = load_class_names(namesfile)
    plot_boxes(img, boxes, 'predictions.jpg', class_names)
コード例 #4
0
ファイル: detect.py プロジェクト: zhangxgu/pytorch-yolo2
def detect(cfgfile, weightfile, imgfile):
    m = Darknet(cfgfile)

    m.print_network()
    m.load_weights(weightfile)
    print('Loading weights from %s... Done!' % (weightfile))

    if m.num_classes == 20:
        namesfile = 'data/voc.names'
    elif m.num_classes == 80:
        namesfile = 'data/coco.names'
    else:
        namesfile = 'data/names'
    
    use_cuda = 1
    if use_cuda:
        m.cuda()

    img = Image.open(imgfile).convert('RGB')
    sized = img.resize((m.width, m.height))
    
    for i in range(2):
        start = time.time()
        boxes = do_detect(m, sized, 0.5, 0.4, use_cuda)
        finish = time.time()
        if i == 1:
            print('%s: Predicted in %f seconds.' % (imgfile, (finish-start)))

    class_names = load_class_names(namesfile)
    plot_boxes(img, boxes, 'predictions.jpg', class_names)
コード例 #5
0
def yolo_detect(imgfile, cfgfile="cfg/yolo.cfg", weightfile="yolo.weights"):
    m = Darknet(cfgfile)
    m.load_weights(weightfile)
    #print('Loading weights from %s... Done!' % (weightfile))

    if m.num_classes == 20:
        namesfile = 'data/voc.names'
    elif m.num_classes == 80:
        namesfile = 'data/coco.names'
    else:
        namesfile = 'data/names'

    use_cuda = 1
    if use_cuda:
        m.cuda()

    img = Image.open(imgfile).convert('RGB')
    sized = img.resize((m.width, m.height))

    for i in range(2):
        start = time.time()
        boxes = do_detect(m, sized, 0.5, 0.4, use_cuda)
        finish = time.time()
    class_names = load_class_names(namesfile)
    width = img.width
    height = img.height
    draw = ImageDraw.Draw(img)
    for i in range(len(boxes)):
        box = boxes[i]
        if len(box) >= 7 and class_names:
            cls_conf = box[5]
            cls_id = box[6]
            if class_names[cls_id] == "person" and cls_conf >= 0.3:
                return True
    return False
コード例 #6
0
    def _init_model(self):
        """ Initialize yolo model.
        Returns:
            m - model used for detection.
            class_names - name of classes.
        """
        m = Darknet(self._configs_path)
        m.print_network()
        m.load_weights(self._weights_path)
        rospy.loginfo('Loading weights... Done!')

        if m.num_classes == 20:
            file_name = self._voc_names
        elif m.num_classes == 80:
            file_name = self._coco_names
        else:
            file_name = self._voc_names
        rospy.loginfo('Number of classes: {}'.format(m.num_classes))

        class_names = load_class_names(file_name)

        if self._use_cuda:
            m.cuda()

        return m, class_names
コード例 #7
0
    def detect_image(self, image_file, output_file):
        print("Detecting objects loading....")

        img_path = os.path.abspath("yolov3_tf2\\" + image_file)
        class_names = load_class_names(self.class_names_file)

        img_raw = tf.image.decode_image(
            open(img_path, 'rb').read(), channels=3)

        # create one more dimension for batch (1, 416, 416, 3)
        img = tf.expand_dims(img_raw, 0)
        img = transform_images(img, self.model_size[0])  # 416 size
        # boxes, classes, scores, nums = model.predict(resized_frame)
        boxes, classes, scores, nums = self.model(img)
        for i in range(nums[0]):
            print('\t{}, {}, {}'.format(class_names[int(classes[0][i])],
                                        np.array(scores[0][i]),
                                        np.array(boxes[0][i])))

        img = cv2.cvtColor(img_raw.numpy(), cv2.COLOR_RGB2BGR)
        img = cv2.resize(img, dsize=(
            self.model_size[0], self.model_size[0]))  # 416 size
        img = draw_output(img, boxes, scores,
                          classes, nums, class_names)
        win_name = 'Detected objects'
        cv2.imshow(win_name, img)
        cv2.waitKey(0)
        cv2.destroyAllWindows()

        # For saving the result
        cv2.imwrite(os.path.abspath(output_file), img)
        print("Image saved....")
コード例 #8
0
def main(img_path, image_name):
    model = YOLOv3Net(cfgfile,model_size,num_classes)
    model.load_weights(weightfile)
    class_names = load_class_names(class_name)
    image = cv2.imread(os.path.join(img_path, "{}.jpg".format(image_name)))
    image = np.array(image)
    image = tf.expand_dims(image, 0)
    resized_frame = resize_image(image, (model_size[0],model_size[1]))
    pred = model.predict(resized_frame)
    boxes, scores, classes, nums = output_boxes( \
        pred, model_size,
        max_output_size=max_output_size,
        max_output_size_per_class=max_output_size_per_class,
        iou_threshold=iou_threshold,
        confidence_threshold=confidence_threshold)
    image = np.squeeze(image)
    img = draw_outputs(image, boxes, scores, classes, nums, class_names)
    # win_name = 'Image detection'
    # cv2.imshow(win_name, img)
    # time.sleep(20)
    # cv2.destroyAllWindows()

    #If you want to save the result, uncommnent the line below:
    os.path.join(img_path, 'image_yolo.jpg')
    cv2.imwrite(os.path.join(img_path, "{}_yolo.jpg".format(image_name)), img)
コード例 #9
0
def main(args):
    model = Darknet(args.config)

    model.print_network()
    model.load_weights(args.weights)
    print(f'Loading weights from {args.weights}... Done!')

    use_cuda = torch.cuda.is_available()
    if use_cuda:
        model.cuda()

    size = (model.width, model.height)

    inputs = []
    if args.zip_imgs:
        inputs = _traverse_zip(args.zip_imgs)
    else:
        inputs = _traverse_image_list(args.imgfiles)

    model.eval()
    start = time.time()
    for input_ in inputs:
        img = input_['image'].convert('RGB').resize(size)
        imgfile = input_['filename']
        # used to be higher confidence threshold and nms threshold
        # boxes = detect(model, img, 0.5, 0.4, use_cuda)
        boxes = detect(model=model, img=img, conf_thresh=args.conf_thresh, nms_thresh=args.nms_thresh, use_cuda=use_cuda)
        class_names = load_class_names(args.names)
        savename = f'predicted_{os.path.basename(imgfile)}'
        plot_boxes(img, boxes, savename, class_names)

    finish = time.time()
    print(f'{args.imgfiles}: Predicted in {finish - start} seconds.')
コード例 #10
0
def get_prediction(inputimage):
    model = YOLOv3Net(cfgfile, model_size, num_classes)
    model.load_weights(weightfile)
    class_names = load_class_names(class_name)
    win_name = 'Yolov3 detection'
    cv2.namedWindow(win_name)
    #specify the vidoe input.
    # 0 means input from cam 0.
    # For vidio, just change the 0 to video path
    frame = cv2.imread(inputimage, 1)
    frame_size = frame.shape

    try:
        # Read frame
        resized_frame = tf.expand_dims(frame, 0)
        resized_frame = resize_image(resized_frame,
                                     (model_size[0], model_size[1]))
        pred = model.predict(resized_frame)
        boxes, scores, classes, nums = output_boxes( \
            pred, model_size,
            max_output_size=max_output_size,
            max_output_size_per_class=max_output_size_per_class,
            iou_threshold=iou_threshold,
            confidence_threshold=confidence_threshold)
        img = draw_outputs(frame, boxes, scores, classes, nums, class_names)
        cv2.imshow(win_name, img)
        cv2.imwrite('outputimgage.jpg', img)
        # print("Time taken : {0} seconds".format(seconds))
        # Calculate frames per second

    finally:
        cv2.waitKey()
        cv2.destroyAllWindows()
        print('Detections have been performed successfully.')
        return img
コード例 #11
0
def _do_python_eval(testlist, namelist, output_dir = 'output'):

    cachedir = os.path.join(output_dir, 'annotations_cache')
    aps = []
    if not os.path.isdir(output_dir):
        os.mkdir(output_dir)

    global classes
    classes = load_class_names(namelist)

    for i, cls in enumerate(classes):
        rec, prec, ap = eval(testlist, cls, cachedir, ovthresh=0.5)
        aps += [ap]
        print('AP for {} = {:.4f}'.format(cls, ap))
        with open(os.path.join(output_dir, cls + '_pr.pkl'), 'wb') as f:
            cPickle.dump({'rec': rec, 'prec': prec, 'ap': ap}, f)

    print('Mean AP = {:.4f}'.format(np.mean(aps)))
    print('~~~~~~~~~~~~~')
    print('   Results:')
    print('-------------')
    for i, ap in enumerate(aps):
        print('{:<10s}\t{:.3f}'.format(classes[i], ap))
    print('=============')
    print('{:^10s}\t{:.3f}'.format('Average', np.mean(aps)))
    print('~~~~~~~~~~~~~')
    print('')
    print('--------------------------------------------------------------')
    print('Results computed with the **unofficial** Python eval code.')
    print('Results should be very close to the official MATLAB eval code.')
    print('Recompute with `./tools/reval.py --matlab ...` for your paper.')
    print('-- Thanks, The Management')
    print('--------------------------------------------------------------')
コード例 #12
0
def main():

    model = YOLOv3Net(cfgfile, model_size, num_classes)

    model.load_weights(weightfile)

    class_names = load_class_names(class_name)

    win_name = 'Yolov3 detection'
    cv2.namedWindow(win_name)

    #specify the vidoe input.
    # 0 means input from cam 0.
    # For vidio, just change the 0 to video path
    cap = cv2.VideoCapture(0)
    frame_size = (cap.get(cv2.CAP_PROP_FRAME_WIDTH),
                  cap.get(cv2.CAP_PROP_FRAME_HEIGHT))

    try:
        while True:
            start = time.time()
            ret, frame = cap.read()
            if not ret:
                break

            resized_frame = tf.expand_dims(frame, 0)
            resized_frame = resize_image(resized_frame,
                                         (model_size[0], model_size[1]))

            pred = model.predict(resized_frame)

            boxes, scores, classes, nums = output_boxes( \
                pred, model_size,
                max_output_size=max_output_size,
                max_output_size_per_class=max_output_size_per_class,
                iou_threshold=iou_threshold,
                confidence_threshold=confidence_threshold)

            img = draw_outputs(frame, boxes, scores, classes, nums,
                               class_names)
            cv2.imshow(win_name, img)

            stop = time.time()

            seconds = stop - start
            # print("Time taken : {0} seconds".format(seconds))

            # Calculate frames per second
            fps = 1 / seconds
            print("Estimated frames per second : {0}".format(fps))

            key = cv2.waitKey(1) & 0xFF

            if key == ord('q'):
                break

    finally:
        cv2.destroyAllWindows()
        cap.release()
        print('Detections have been performed successfully.')
コード例 #13
0
def _do_python_eval(testlist, namelist, output_dir='output'):

    cachedir = os.path.join(output_dir, 'annotations_cache')
    aps = []
    if not os.path.isdir(output_dir):
        os.mkdir(output_dir)

    global classes
    classes = load_class_names(namelist)

    for i, cls in enumerate(classes):
        rec, prec, ap = eval(testlist, cls, cachedir, classes, ovthresh=0.5)
        aps += [ap]
        print('AP for {} = {:.4f}'.format(cls, ap))
        with open(os.path.join(output_dir, cls + '_pr.pkl'), 'wb') as f:
            cPickle.dump({'rec': rec, 'prec': prec, 'ap': ap}, f)

    print('Mean AP = {:.4f}'.format(np.mean(aps)))
    print('~~~~~~~~~~~~~')
    print('   Results:')
    print('-------------')
    for i, ap in enumerate(aps):
        print('{:<10s}\t{:.3f}'.format(classes[i], ap))
    print('=============')
    print('{:^10s}\t{:.3f}'.format('Average', np.mean(aps)))
    print('~~~~~~~~~~~~~')
    print('')
コード例 #14
0
ファイル: image.py プロジェクト: Bakso14/YOLOv3_TF2
def main():

    model = YOLOv3Net(cfgfile, model_size, num_classes)
    model.load_weights(weightfile)

    class_names = load_class_names(class_name)

    image = cv2.imread(img_path)
    image = np.array(image)
    image = tf.expand_dims(image, 0)

    resized_frame = resize_image(image, (model_size[0], model_size[1]))
    pred = model.predict(resized_frame)

    boxes, scores, classes, nums = output_boxes( \
        pred, model_size,
        max_output_size=max_output_size,
        max_output_size_per_class=max_output_size_per_class,
        iou_threshold=iou_threshold,
        confidence_threshold=confidence_threshold)

    image = np.squeeze(image)
    img = draw_outputs(image, boxes, scores, classes, nums, class_names)

    win_name = 'Image detection'
    cv2.imshow(win_name, img)
    cv2.waitKey(0)
    cv2.destroyAllWindows()
コード例 #15
0
ファイル: detect.py プロジェクト: Vikas013/YoloV3
def main(iou_threshold, confidence_threshold, img_names):
    batch_size = len(img_names)
    batch = load_images(img_names, model_size=_MODEL_SIZE)
    class_names = load_class_names(_CLASS_NAMES_FILE)
    n_classes = len(class_names)

    model = Yolo_v3(n_classes=n_classes,
                    model_size=_MODEL_SIZE,
                    max_output_size=_MAX_OUTPUT_SIZE,
                    iou_threshold=iou_threshold,
                    confidence_threshold=confidence_threshold)

    inputs = tf.placeholder(tf.float32, [batch_size, *_MODEL_SIZE, 3])

    detections = model(inputs, training=False)

    saver = tf.train.Saver(tf.global_variables(scope='yolo_v3_model'))

    with tf.Session() as sess:
        saver.restore(sess, r'/weights/model.ckpt')
        detection_result = sess.run(detections, feed_dict={inputs: batch})

    draw_boxes(img_names, detection_result, class_names, _MODEL_SIZE)

    print('Detections have been saved successfully.')
コード例 #16
0
    def main():
        # Cargando el archivo de etiquetas
        class_names = load_class_names(_CLASS_NAMES_FILE)
        n_classes = len(class_names)

        #Llamada al modelo Yolo-v3
        model = Yolo_v3(n_classes=n_classes,
                        model_size=_MODEL_SIZE,
                        max_output_size=_MAX_OUTPUT_SIZE,
                        iou_threshold=iou_threshold,
                        confidence_threshold=confidence_threshold)

        inputs = tf.compat.v1.placeholder(tf.float32, [1, *_MODEL_SIZE, 3])
        detections = model(inputs, training=False)
        saver = tf.compat.v1.train.Saver(
            tf.compat.v1.global_variables(scope='yolo_v3_model'))

        #iniciando secion si cuenta con tensorflow 1
        with tf.compat.v1.Session() as sess:

            saver.restore(sess, './weights/model.ckpt')  # Pesos del modelo
            url = 'http://192.168.1.111:8080/shot.jpg'  # Direccion de streaming
            # Convercion de las imagenes de streaming
            image = urllib.request.urlopen(url)
            img = np.array(bytearray(image.read()), dtype=np.uint8)
            frame = cv2.imdecode(img, -1)
            frame_size = (850, 500)

            try:
                while True:

                    # Obtencion de las imagenes
                    image = urllib.request.urlopen(url)
                    img = np.array(bytearray(image.read()), dtype=np.uint8)
                    frame = cv2.imdecode(img, -1)
                    frame = cv2.resize(frame,
                                       dsize=(850, 500),
                                       interpolation=cv2.INTER_NEAREST)

                    resized_frame = cv2.resize(frame,
                                               dsize=_MODEL_SIZE[::-1],
                                               interpolation=cv2.INTER_NEAREST)

                    # Activamos yolo
                    detection_result = sess.run(
                        detections, feed_dict={inputs: [resized_frame]})
                    # Clasificacion del imagenes
                    clasification = clasifier(detection_result, class_names)

                    # Obtencion de los datos Diccionario
                    print(json.dumps(clasification))
                    time.sleep(0.05)  # Time Slepeer
                    yield (json.dumps(clasification) + '\n')

            finally:
                print('FaceID end')
コード例 #17
0
def main(iou_threshold, confidence_threshold, input_names):
    global detection_result
    class_names = load_class_names(_CLASS_NAMES_FILE)
    n_classes = len(class_names)

    model = Yolo_v3(n_classes=n_classes,
                    model_size=_MODEL_SIZE,
                    max_output_size=_MAX_OUTPUT_SIZE,
                    iou_threshold=iou_threshold,
                    confidence_threshold=confidence_threshold)

    inputs = tf.placeholder(tf.float32, [1, *_MODEL_SIZE, 3])
    detections = model(inputs, training=False)
    saver = tf.train.Saver(tf.global_variables(scope='yolo_v3_model'))

    with tf.Session() as sess:
        saver.restore(sess, './weights/model.ckpt')

        win_name = 'Video detection'
        cv2.namedWindow(win_name)
        cap = cv2.VideoCapture(input_names)
        frame_size = (cap.get(cv2.CAP_PROP_FRAME_WIDTH),
                      cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
        fourcc = int(cap.get(cv2.CAP_PROP_FOURCC))
        fps = cap.get(cv2.CAP_PROP_FPS)
        if not os.path.exists('detections'):
            os.mkdir('detections')
        head, tail = os.path.split(input_names)
        name = './detections/' + tail[:-4] + '_yolo.mp4'
        out = cv2.VideoWriter(name, fourcc, fps,
                              (int(frame_size[0]), int(frame_size[1])))

        try:
            print("Show video")
            while (cap.isOpened()):
                ret, frame = cap.read()
                if not ret:
                    break
                resized_frame = cv2.resize(frame,
                                           dsize=_MODEL_SIZE[::-1],
                                           interpolation=cv2.INTER_NEAREST)
                detection_result = sess.run(
                    detections, feed_dict={inputs: [resized_frame]})
                draw_frame(frame, frame_size, detection_result, class_names,
                           _MODEL_SIZE)
                if ret == True:
                    cv2.imshow(win_name, frame)
                    out.write(frame)

                if cv2.waitKey(1) & 0xFF == ord('q'):
                    break

        finally:
            cv2.destroyAllWindows()
            cap.release()
            print('Detections have been saved successfully.')
コード例 #18
0
    def main():
        class_names = load_class_names(_CLASS_NAMES_FILE)
        n_classes = len(class_names)

        model = Yolo_v3(n_classes=n_classes,
                        model_size=_MODEL_SIZE,
                        max_output_size=_MAX_OUTPUT_SIZE,
                        iou_threshold=iou_threshold,
                        confidence_threshold=confidence_threshold)

        inputs = tf.compat.v1.placeholder(tf.float32, [1, *_MODEL_SIZE, 3])
        detections = model(inputs, training=False)
        saver = tf.compat.v1.train.Saver(
            tf.compat.v1.global_variables(scope='yolo_v3_model'))

        with tf.compat.v1.Session() as sess:
            saver.restore(sess, './weights/model.ckpt')
            url = 'http://192.168.1.111:8080/shot.jpg'
            image = urllib.request.urlopen(url)
            img = np.array(bytearray(image.read()), dtype=np.uint8)
            frame = cv2.imdecode(img, -1)
            frame_size = (850, 500)

            try:
                while True:

                    image = urllib.request.urlopen(url)
                    img = np.array(bytearray(image.read()), dtype=np.uint8)
                    frame = cv2.imdecode(img, -1)
                    frame = cv2.resize(frame,
                                       dsize=(850, 500),
                                       interpolation=cv2.INTER_NEAREST)

                    resized_frame = cv2.resize(frame,
                                               dsize=_MODEL_SIZE[::-1],
                                               interpolation=cv2.INTER_NEAREST)

                    detection_result = sess.run(
                        detections, feed_dict={inputs: [resized_frame]})

                    clasification = clasifier(frame, frame_size,
                                              detection_result, class_names,
                                              _MODEL_SIZE)

                    print(json.dumps(clasification))

                    yield (json.dumps(clasification) + '\n')

                    key = cv2.waitKey(10) & 0xFF

                    if key == ord('q'):
                        break

            finally:
                print('FaceID end')
コード例 #19
0
def _do_python_eval(res_prefix,
                    imagesetfile,
                    classesfile,
                    output_dir='output'):

    filename = res_prefix + '{:s}.txt'

    cachedir = os.path.join(output_dir, 'annotations_cache')
    aps = []
    # The PASCAL VOC metric changed in 2010
    use_07_metric = False
    #print ('VOC07 metric? ' + ('Yes' if use_07_metric else 'No'))
    if not os.path.isdir(output_dir):
        os.mkdir(output_dir)

    global _classes
    _classes = load_class_names(classesfile)

    total = 0
    for i, cls in enumerate(_classes):
        if cls == '__background__':
            continue

        rec, prec, ap, noccur = coco_eval(filename,
                                          imagesetfile,
                                          cls,
                                          cachedir,
                                          ovthresh=0.5,
                                          use_07_metric=use_07_metric)
        aps += [ap]
        total += noccur
        print('AP for {:<10s} = {:.4f} with {:4d} views'.format(
            cls, ap, noccur))
        with open(os.path.join(output_dir, cls + '_pr.pkl'), 'wb') as f:
            cPickle.dump({'rec': rec, 'prec': prec, 'ap': ap}, f)

    print('Mean AP = {:.4f} with total {:4d} views'.format(
        np.mean(aps), total))

    print('~' * 30)
    print(' ' * 10, 'Results:')
    print('-' * 30)
    for i, ap in enumerate(aps):
        print('{:<10s}\t{:.3f}'.format(_classes[i], ap))
    print('=' * 30)
    print('{:^10s}\t{:.3f}'.format('Average', np.mean(aps)))
    print('~' * 30)
    print('')
    print('--------------------------------------------------------------')
    print('Results computed with the **unofficial** Python eval code.')
    print('Results should be very close to the official MATLAB eval code.')
    print('Recompute with `./tools/reval.py --matlab ...` for your paper.')
    print('-- Thanks, The Management')
    print('--------------------------------------------------------------')
コード例 #20
0
def Evaluation_from_Valid(res_prefix,
                          imagesetfile,
                          classesfile,
                          output_dir='output'):
    filename = res_prefix + '{:s}.txt'

    if os.path.exists(output_dir):
        shutil.rmtree(output_dir)
    os.mkdir(output_dir)

    cachedir = os.path.join(output_dir, 'annotations_cache')
    aps = []
    # The PASCAL VOC metric changed in 2010
    use_07_metric = False
    # print ('VOC07 metric? ' + ('Yes' if use_07_metric else 'No'))

    _classes = load_class_names(classesfile)
    total = 0
    for i, cls in enumerate(_classes):
        if cls == '__background__':
            continue

        rec, prec, ap, noccur = my_eval(filename,
                                        imagesetfile,
                                        cls,
                                        cachedir,
                                        _classes,
                                        ovthresh=0.5,
                                        use_07_metric=use_07_metric)
        aps += [ap]
        total += noccur
        print('AP for {:<10s} = {:.4f} with {:4d} views'.format(
            cls, ap, noccur))
        with open(os.path.join(output_dir, cls + '_pr.pkl'), 'wb') as f:
            cPickle.dump({'rec': rec, 'prec': prec, 'ap': ap}, f)

    print('Mean AP = {:.4f} with total {:4d} views'.format(
        np.mean(aps), total))

    print('~' * 30)
    print(' ' * 10, 'Results:')
    print('-' * 30)
    for i, ap in enumerate(aps):
        print('{:<10s}\t{:.3f}'.format(_classes[i], ap))
    print('=' * 30)
    mAP = np.mean(aps)
    print('{:^10s}\t{:.3f}'.format('Average', mAP))
    print('~' * 30)
    print('done')
    print('precision: ' % (prec))
    print('recall: ' % (rec))
    return mAP
コード例 #21
0
 def class_names(self):
     """
     Write class names into csv file
     """
     class_names = utils.load_class_names(self.classesFileEntry.get())
     class_names.insert(0, "frame")
     print(class_names)
     filename = "test_file.csv"
     with open(filename, 'w') as csvfile:
         csvwriter = csv.writer(csvfile)
         csvfile.write("sep=,")
         csvfile.write('\n')
         csvwriter.writerow(class_names)
コード例 #22
0
    def load_model(self):
        self.shared_variables.class_names = load_class_names(_CLASS_NAMES_FILE)
        self.shared_variables.n_classes = len(
            self.shared_variables.class_names)
        self.shared_variables.model_size = _MODEL_SIZE

        model = Yolo_v3(n_classes=self.shared_variables.n_classes,
                        model_size=_MODEL_SIZE,
                        max_output_size=_MAX_OUTPUT_SIZE,
                        iou_threshold=iou_threshold,
                        confidence_threshold=confidence_threshold)

        self.inputs = tf.placeholder(tf.float32, [1, *_MODEL_SIZE, 3])
        self.detections = model(self.inputs, training=False)
        return tf.train.Saver(tf.global_variables(scope='yolo_v3_model'))
コード例 #23
0
ファイル: detect.py プロジェクト: yoxf/pytorch_code
def detect(cfgfile, weightfile, imgfile):
    m = Darknet(cfgfile)
    m.load_weights(weightfile)
    print('Loading weights from %s... Done!' % (weightfile))

    img = Image.open(imgfile).convert('RGB')
    sized = img.resize((m.width, m.height))
    
    start = time.time()
    boxes = utils.do_detect(m, sized, 0.5, 0.4)
    finish = time.time()
    print('%s: Predicted in %f seconds.' % (imgfile, (finish-start)))

    class_names = utils.load_class_names(namesfile)
    utils.plot_boxes(img, boxes, 'predictions.jpg', class_names)
コード例 #24
0
def eval_list(cfgfile, namefile, weightfile, testfile):
    m = Darknet(cfgfile)
    m.load_weights(weightfile)
    use_cuda = 1
    if use_cuda:
        m.cuda()

    class_names = load_class_names(namefile)

    file_list = []
    with open(testfile, "r") as fin:
        for f in fin:
            file_list.append(f.strip())

    for imgfile in file_list:
        img = Image.open(imgfile).convert('RGB')
        sized = img.resize((m.width, m.height))
        filename = os.path.basename(imgfile)
        filename = os.path.splitext(filename)[0]
        #print(filename, img.width, img.height, sized_width, sized_height)

        if m.width * m.height > 1024 * 2560:
            print('omit %s' % filename)
            continue

        if False:
            boxes = do_detect(m, sized, conf_thresh, nms_thresh, use_cuda)
        else:
            m.eval()
            sized = image2torch(sized).cuda();
            #output = m(Variable(sized, volatile=True)).data
            output = m(sized)
            #boxes = get_region_boxes(output, conf_thresh, m.num_classes, m.anchors, m.num_anchors, 0, 1)[0]
            boxes = get_all_boxes(output, conf_thresh, m.num_classes)[0]
            boxes = np.array(nms(boxes, nms_thresh))

        if False:
            savename = get_det_image_name(imgfile)
            print('img: save to %s' % savename)
            plot_boxes(img, boxes, savename, class_names)

        if False:
            savename = get_det_result_name(imgfile)
            print('det: save to %s' % savename)
            save_boxes(imgfile, img, boxes, savename)
コード例 #25
0
ファイル: image.py プロジェクト: Shrimadh/YOLOv3-Tensorflow
def main():
    model = YOLOv3Net(cfgfile, model_size, num_classes)
    model.load_weights(weightfile)
    class_names = load_class_names(class_name)
    print("class_names", class_names)
    image = cv2.imread(img_path)
    image = np.array(image)
    image = tf.expand_dims(image, 0)
    resized_frame = resize_image(image, (model_size[0], model_size[1]))
    pred = model.predict(resized_frame)
    boxes, scores, classes, nums = output_boxes( \
        pred, model_size,
        max_output_size=max_output_size,
        max_output_size_per_class=max_output_size_per_class,
        iou_threshold=iou_threshold,
        confidence_threshold=confidence_threshold)
    image = np.squeeze(image)
    img = draw_outputs(image, boxes, scores, classes, nums, class_names)
    cv2.imwrite('result1.jpg', img)
コード例 #26
0
def init_model(cfgfile, weightfile, voc_names, coco_names):
    global use_cuda
    m = Darknet(cfgfile)
    m.print_network()
    m.load_weights(weightfile)
    print('Loading weights... Done!')

    if m.num_classes == 20:
        namesfile = voc_names
    elif m.num_classes == 80:
        namesfile = coco_names
    else:
        namesfile = voc_names
    class_names = load_class_names(namesfile)

    use_cuda = 1
    if use_cuda:
        m.cuda()

    return m, class_names
コード例 #27
0
def predict(image_path, model_path, top_k, class_names_path):
    image = utils.prepare_image(image_path)
    model = utils.load_model(model_path)

    # Predict
    predictions = model.predict(image)[0]

    # Get indexes of top k predictions
    # Sort indexes, in reverse (descending) order
    sorted_indexes = np.argsort(predictions)[::-1]
    top_k_indexes = sorted_indexes[0:top_k]

    # Extract probabilites and class names based on indexes
    probabilities = [predictions[i] for i in top_k_indexes]
    if class_names_path != None:
        # +1 because class_names key range is from 1 to 102
        class_names = utils.load_class_names(class_names_path, top_k_indexes)
    else:
        # Put indexes for class names
        class_names = [i for i in range(1, 103)]
    return class_names, probabilities
コード例 #28
0
def main(Args):
    model_size = (Args.model_size, Args.model_size, 3)
    model = YOLOv3Net(Args.cfgfile, Args.num_classes, model_size, Args.max_total_size,\
                      Args.max_output_size_per_class, Args.iou_threshold, Args.score_threshold)
    model.load_weights(Args.weightfile)
    # tf.keras.utils.plot_model(model, to_file='Model.png', show_shapes=True)
    class_names = load_class_names(Args.class_name)
    image = cv2.imread(Args.img_path)
    original_image_size = image.shape
    org_img = image
    image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
    image = tf.expand_dims(image, 0)
    resized_frame = resize_image(image, (model_size[0], model_size[1]))
    boxes, scores, classes, num_detections = model.predict(resized_frame)
    print('Number of detections = ', num_detections)
    img_final = draw_outputs(org_img, boxes, scores, classes, num_detections,
                             class_names)
    cv2.imwrite('./Result/' + Args.img_path.split('/')[-1], img_final)
    cv2.imshow('Detections', img_final)
    print('Press ESC on image display wondow to exit')
    cv2.waitKey(0)
コード例 #29
0
def detect_image(img_path):
    model = YOLOv3Net(cfg.CFGFILE,cfg.MODEL_SIZE,cfg.NUM_CLASSES)
    model.load_weights(cfg.WEIGHTFILE)
    class_names = load_class_names(cfg.CLASS_NAME)
    image = cv2.imread(img_path)
    image = np.array(image)
    image = tf.expand_dims(image, 0)
    resized_frame = resize_image(image, (cfg.MODEL_SIZE[0],cfg.MODEL_SIZE[1]))
    pred = model.predict(resized_frame)
    boxes, scores, classes, nums = output_boxes( \
        pred, cfg.MODEL_SIZE,
        max_output_size=max_output_size,
        max_output_size_per_class=max_output_size_per_class,
        iou_threshold=cfg.IOU_THRESHOLD,
        confidence_threshold=cfg.CONFIDENCE_THRESHOLD)
    image = np.squeeze(image)
    img = draw_outputs(image, boxes, scores, classes, nums, class_names)
    win_name = 'Detection'
    cv2.imshow(win_name, img)
    cv2.waitKey(0)
    cv2.destroyAllWindows()
コード例 #30
0
def detect_video(video_path):
    model = YOLOv3Net(cfg.CFGFILE, cfg.MODEL_SIZE, cfg.NUM_CLASSES)
    model.load_weights(cfg.WEIGHTFILE)
    class_names = load_class_names(cfg.CLASS_NAME)
    win_name = 'Detection'
    cv2.namedWindow(win_name)
    cap = cv2.VideoCapture(returnCameraOrFile(video_path))
    frame_size = (cap.get(cv2.CAP_PROP_FRAME_WIDTH),
                  cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
    try:
        while True:
            start = time.time()
            ret, frame = cap.read()
            if not ret:
                break
            resized_frame = tf.expand_dims(frame, 0)
            resized_frame = resize_image(
                resized_frame, (cfg.MODEL_SIZE[0], cfg.MODEL_SIZE[1]))
            pred = model.predict(resized_frame)
            boxes, scores, classes, nums = output_boxes( \
                pred, cfg.MODEL_SIZE,
                max_output_size=max_output_size,
                max_output_size_per_class=max_output_size_per_class,
                iou_threshold=cfg.IOU_THRESHOLD,
                confidence_threshold=cfg.CONFIDENCE_THRESHOLD)
            img = draw_outputs(frame, boxes, scores, classes, nums,
                               class_names)
            cv2.imshow(win_name, img)
            stop = time.time()
            seconds = stop - start
            # Calculate frames per second
            fps = 1 / seconds
            print("Frames per second : {0}".format(fps))
            key = cv2.waitKey(1) & 0xFF
            if key == ord('q'):
                break
    finally:
        cv2.destroyAllWindows()
        cap.release()
        print('Detections performed successfully.')
コード例 #31
0
def main():
    model = yolov3_net(cfg_file, num_classes)
    model.load_weights(weights_file)

    class_names = load_class_names(class_names_file)

    image = cv2.imread(img_path)
    image = tf.expand_dims(image, 0)
    resized_frame = resize_image(image, (model_size[0], model_size[1]))
    start_time = time.time()
    pred = model.predict(resized_frame, steps=1)
    print("Time inference: ", time.time() - start_time)
    boxes, scores, classes, nums = output_boxes(pred, model_size, max_output_size, max_output_size_per_class,
                                                iou_threshold, confidence_threshold)

    image = np.squeeze(image)
    img = draw_output(image, boxes, scores, classes, nums, class_names)

    img = cv2.resize(img, (0, 0), fx=0.5, fy=0.5)
    win_name = "Image detection"
    cv2.imshow(win_name, img)
    cv2.waitKey(0)
    cv2.destroyAllWindows()