コード例 #1
0
def predict_images():
    config = Config(FLAGS.model_dir)
    detector = Detector(FLAGS.model_dir,
                        config,
                        use_gpu=FLAGS.use_gpu,
                        run_mode=config.mode)
    if FLAGS.run_benchmark:
        detector.predict(FLAGS.image_file,
                         detector.config.draw_threshold,
                         warmup=10,
                         repeats=10)
    else:
        # if os.path.exists(FLAGS.output_dir): shutil.rmtree(FLAGS.output_dir)
        # os.makedirs(FLAGS.output_dir)
        if not os.path.exists(FLAGS.output_dir): os.makedirs(FLAGS.output_dir)

        postprocess = config.postprocess
        if postprocess == 'numpy_nms':
            pcfg = PostprocessNumpyNMSConfig()

        # 获取颜色
        num_classes = len(detector.config.labels)
        colors = get_colors(num_classes)

        path_dir = os.listdir(FLAGS.image_dir)
        # warm up
        if FLAGS.use_gpu:
            for k, filename in enumerate(path_dir):
                img_path = FLAGS.image_dir + filename
                if postprocess == 'fastnms':
                    results = detector.predict(img_path,
                                               detector.config.draw_threshold)
                elif postprocess == 'numpy_nms':
                    results = detector.predict_with_postprocess(
                        img_path, detector.config.draw_threshold, pcfg)
                if k == 10:
                    break

        num_imgs = len(path_dir)
        start = time.time()

        for k, filename in enumerate(path_dir):
            img_path = FLAGS.image_dir + filename
            if postprocess == 'fastnms':
                results = detector.predict(img_path,
                                           detector.config.draw_threshold)
            elif postprocess == 'numpy_nms':
                results = detector.predict_with_postprocess(
                    img_path, detector.config.draw_threshold, pcfg)
            image = cv2.imread(img_path)
            if len(results['boxes']) > 0:
                draw(image, results['boxes'], results['scores'],
                     results['classes'], detector.config.labels, colors)
            out_path = os.path.join(FLAGS.output_dir, filename)
            cv2.imwrite(out_path, image)
            print("Detection bbox results save in {}".format(out_path))
        cost = time.time() - start
        print('total time: {0:.6f}s'.format(cost))
        print('Speed: %.6fs per image,  %.1f FPS.' % ((cost / num_imgs),
                                                      (num_imgs / cost)))
コード例 #2
0
def play_video():
    config = Config(FLAGS.model_dir)
    detector = Detector(FLAGS.model_dir,
                        config,
                        use_gpu=FLAGS.use_gpu,
                        run_mode=config.mode)
    # if os.path.exists(FLAGS.output_dir): shutil.rmtree(FLAGS.output_dir)
    # os.makedirs(FLAGS.output_dir)
    if not os.path.exists(FLAGS.output_dir): os.makedirs(FLAGS.output_dir)

    # 获取颜色
    num_classes = len(detector.config.labels)
    colors = get_colors(num_classes)

    # warm up
    image_dir = 'images/test/'
    path_dir = os.listdir(image_dir)
    if FLAGS.use_gpu:
        for k, filename in enumerate(path_dir):
            img_path = image_dir + filename
            results = detector.predict(img_path,
                                       detector.config.draw_threshold)
            if k == 10:
                break

    capture = cv2.VideoCapture(FLAGS.play_video)
    fps = 30
    width = int(capture.get(cv2.CAP_PROP_FRAME_WIDTH))
    height = int(capture.get(cv2.CAP_PROP_FRAME_HEIGHT))
    fourcc = cv2.VideoWriter_fourcc(*'mp4v')
    video_name = os.path.split(FLAGS.play_video)[-1]
    if not os.path.exists(FLAGS.output_dir):
        os.makedirs(FLAGS.output_dir)
    out_path = os.path.join(FLAGS.output_dir, video_name)
    writer = cv2.VideoWriter(out_path, fourcc, fps, (width, height))
    index = 1
    start = time.time()
    while (1):
        ret, frame = capture.read()
        if not ret:
            break
        print('detect frame:%d' % (index))
        index += 1
        results = detector.predict(frame, detector.config.draw_threshold)
        im = visualize_box_mask(frame, results, detector.config.labels)
        cv2.imshow("detection", frame)
        writer.write(im)
        if cv2.waitKey(110) & 0xff == 27:
            break
    writer.release()
    num_imgs = 100
    cost = time.time() - start
    print('total time: {0:.6f}s'.format(cost))
    print('Speed: %.6fs per image,  %.1f FPS.' % ((cost / num_imgs),
                                                  (num_imgs / cost)))
コード例 #3
0
ファイル: test_client.py プロジェクト: yawudede/Paddle-YOLOv4
from tools.cocotools import get_classes
from tools.visualize import get_colors, draw
from paddle_serving_client import Client
import cv2
import sys
import numpy as np




classes_path = 'data/coco_classes.txt'
all_classes = get_classes(classes_path)
num_classes = len(all_classes)
colors = get_colors(num_classes)




prototxt_path = sys.argv[1]
client = Client()
client.load_client_config(prototxt_path)
client.connect(['127.0.0.1:9494'])


img_path = sys.argv[3]
print(img_path)   # 这是图片的路径
input_shape = (608, 608)
image = cv2.imread(img_path)
h, w = image.shape[:2]
img = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)