예제 #1
0
def demo(args, image_path):
    # 2. load detector based on the configure file
    object_detector = ObjectDetector()

    # 3. load image
    image = cv2.imread(image_path)

    # 4. detect
    _labels, _scores, _coords = object_detector.predict(image)

    # 5. draw bounding box on the image
    for labels, scores, coords in zip(_labels, _scores, _coords):
        cv2.rectangle(image, (int(coords[0]), int(coords[1])), (int(coords[2]), int(coords[3])), COLORS[labels % 3], 2)
        cv2.putText(image, '{label}: {score:.3f}'.format(label=VOC_CLASSES[labels], score=scores), (int(coords[0]), int(coords[1])), FONT, 0.5, COLORS[labels % 3], 2)
    
    # 6. visualize result
    if args.display is True:
        cv2.imshow('result', image)
        cv2.waitKey(0)

    # 7. write result
    if args.save is True:
        os.path.makedirs(args.output_dir, exists_ok=True)
        path, _ = os.path.splitext(image_path)
        cv2.imwrite(os.path.join(args.output_dir, path + '_result.jpg'), image)
예제 #2
0
def demo_file(args, imagelist,imagefile):
    # 1. load the configure file
    cfg_from_file(args.confg_file)
    # 2. load detector based on the configure file
    object_detector = ObjectDetector()
    args = parse_args()
    for image_name in imagelist:
    # 3. load image
        image_path = os.path.join(imagefile,image_name)
        image = cv2.imread(image_path)

    # 4. detect
        _labels, _scores, _coords = object_detector.predict(image)

    # 5. draw bounding box on the image
        for labels, scores, coords in zip(_labels, _scores, _coords):
            cv2.rectangle(image, (int(coords[0]), int(coords[1])), (int(coords[2]), int(coords[3])), COLORS[labels % 3], 2)
            cv2.putText(image, '{label}: {score:.3f}'.format(label=VOC_CLASSES[labels], score=scores), (int(coords[0]), int(coords[1])), FONT, 0.5, COLORS[labels % 3], 2)
    
    # 6. visualize result
        if args.display is True:
            cv2.imshow('result', image)
            cv2.waitKey(800)
        '''
    # 7. write result
       # if args.save is True:
        path = './'
        path = os.path.join(path,image_name)
        print(path)
        #path, _ = os.path.splitext(image_path)
        #print(path)
        cv2.imwrite(path, image)
        '''
        '''
예제 #3
0
def time_benchmark(args, image_path):
    # 1. load the configure file
    cfg_from_file(args.confg_file)

    # 2. load detector based on the configure file
    object_detector = ObjectDetector()

    # 3. load imagedemo
    image = cv2.imread(image_path)

    # 4. time test
    warmup = 20
    time_iter = 100
    print('Warmup the detector...')
    _t = list()
    for i in range(warmup+time_iter):
        _, _, _, (total_time, preprocess_time, net_forward_time, detect_time, output_time) \
            = object_detector.predict(image, check_time=True)
        if i > warmup:
            _t.append([total_time, preprocess_time, net_forward_time, detect_time, output_time])
            if i % 20 == 0: 
                print('In {}\{}, total time: {} \n preprocess: {} \n net_forward: {} \n detect: {} \n output: {}'.format(
                    i-warmup, time_iter, total_time, preprocess_time, net_forward_time, detect_time, output_time
                ))
    total_time, preprocess_time, net_forward_time, detect_time, output_time = np.sum(_t, axis=0)/time_iter
    print('In average, total time: {} \n preprocess: {} \n net_forward: {} \n detect: {} \n output: {}'.format(
        total_time, preprocess_time, net_forward_time, detect_time, output_time
    ))
예제 #4
0
def demo(args, image_path):
    # 1. load the configure file
    cfg_from_file(args.confg_file)

    # 2. load detector based on the configure file
    object_detector = ObjectDetector()

    # 3. load image
    image = cv2.imread(image_path)
    image256 = image[300:556]
    # 4. detect
    _labels, _scores, _coords = object_detector.predict(image256)

    # 5. draw bounding box on the image
    for labels, scores, coords in zip(_labels, _scores, _coords):
        cv2.rectangle(image256, (int(coords[0]), int(coords[1])),
                      (int(coords[2]), int(coords[3])), COLORS[labels % 3], 2)
        cv2.putText(
            image256, '{label}: {score:.3f}'.format(label=VOC_CLASSES[labels],
                                                    score=scores),
            (int(coords[0]), int(coords[1])), FONT, 0.5, COLORS[labels % 3], 2)

    # 6. visualize result
    if args.display is True:
        cv2.imshow('result', image)
        cv2.waitKey(0)

    # 7. write result
    if args.save is True:
        # path, _ = os.path.splitext(image_path)

        img_name = image_path.split("/")[-1]
        cv2.imwrite(os.path.join(args.test_out_img_path, img_name), image)
예제 #5
0
def predict():
    args = parse_args()

    cfg_from_file(args.cfg)

    detector = ObjectDetector()

    img = cv2.imread(img_f)

    _labels, _scores, _coords = detector.predict(img)
    print('labels: {}\nscores: {}\ncoords: {}'.format(_labels, _scores, _coords))
예제 #6
0
def demo_live(args, video_path):
    # 1. load the configure file
    cfg_from_file(args.confg_file)

    # 2. load detector based on the configure file
    object_detector = ObjectDetector()

    # 3. load video
    video = cv2.VideoCapture(video_path)

    index = -1
    while (video.isOpened()):
        index = index + 1
        sys.stdout.write('Process image: {} \r'.format(index))
        sys.stdout.flush()

        # 4. read image
        flag, image = video.read()
        if flag == False:
            print("Can not read image in Frame : {}".format(index))
            break

        # 5. detect
        _labels, _scores, _coords = object_detector.predict(image,
                                                            threshold=0.6)

        # 6. draw bounding box on the image
        for labels, scores, coords in zip(_labels, _scores, _coords):
            cv2.rectangle(image, (int(coords[0]), int(coords[1])),
                          (int(coords[2]), int(coords[3])), COLORS[labels % 3],
                          2)
            cv2.putText(
                image, '{label}: {score:.3f}'.format(label=VOC_CLASSES[labels],
                                                     score=scores),
                (int(coords[0]), int(coords[1])), FONT, 0.5,
                COLORS[labels % 3], 2)

        # 7. visualize result
        if args.display is True:
            cv2.imshow('result', image)
            cv2.waitKey(33)

        # 8. write result
        if args.save is True:
            path, _ = os.path.splitext(video_path)
            path = path + '_result'
            if not os.path.exists(path):
                os.mkdir(path)
            cv2.imwrite(path + '/{}.jpg'.format(index), image)
예제 #7
0
def demo_live(args, video_path):

    # 2. load detector based on the configure file
    object_detector = ObjectDetector()

    # 3. load video
    video = cv2.VideoCapture(video_path)

    if args.save:
        path = os.path.split(os.path.splitext(video_path)[0])[1]
        output_path = os.path.join(cfg.EXP_DIR, args.output_dir, path + '_result')
        if not os.path.exists(output_path):
            os.makedirs(output_path)

    index = -1
    import time
    total_time = 0
    while(video.isOpened()):
        index = index + 1
        print('Process image: {} \r'.format(index))
        # 4. read image
        flag, image = video.read()
        if flag == False:
            print("Can not read image in Frame : {}".format(index))
            break

        if index < args.skip_count:
            continue

        # 5. detect
        _labels, _scores, _coords, times = object_detector.predict(image, check_time=True)
        total_time += times[-1]
        # 6. draw bounding box on the image
        for labels, scores, coords in zip(_labels, _scores, _coords):
            cv2.rectangle(image, (int(coords[0]), int(coords[1])), (int(coords[2]), int(coords[3])), COLORS[labels % 3], 2)
            cv2.putText(image, '{label}: {score:.3f}'.format(label=CONE_CLASSES[labels], score=scores), (int(coords[0]), int(coords[1])), FONT, 0.5, COLORS[labels % 3], 2)

        # 7. visualize result
        if args.display is True:
            cv2.imshow('result', image)
            cv2.waitKey(33)

        # 8. write result
        if args.save is True:
            cv2.imwrite(output_path + '/{}.jpg'.format(index), image)
    print("Post-process time", total_time / (index+1))
    if args.save:
        print("Images saved to", output_path)
예제 #8
0
def demo_video(args, video_path):
    # 1. load the configure file
    cfg_from_file(args.confg_file)

    # 2. load detector based on the configure file
    object_detector = ObjectDetector()

    # 3. load video
    video = cv2.VideoCapture(video_path)
    index = -1
    frametime10 = np.zeros((10,1))
    while(video.isOpened()):
        #sys.stdout.write('Process image: {} \r'.format(index))
        #sys.stdout.flush()
        # 4. read image
        flag, image = video.read()
        t0 = timeit.default_timer()
        if flag == False:
            #print("Can not read image in Frame : {}".format(index))
            break

        # 5. detect
        _labels, _scores, _coords,elapsed = object_detector.predict(image)

        # 6. draw bounding box on the image
        for labels, scores, coords in zip(_labels, _scores, _coords):
            cv2.rectangle(image, (int(coords[0]), int(coords[1])), (int(coords[2]), int(coords[3])), COLORS[labels % 3], 5)
            cv2.putText(image, '{label}: {score:.3f}'.format(label=VOC_CLASSES[labels], score=scores), (int(coords[0]), int(coords[1])), FONT, 0.5, COLORS[labels % 3], 2)
        
        #print(elapsed)
        index = index + 1
        index = index%10
        #frametime10[index] = elapsed
        #fps = 1/np.average(frametime10)
        fps = 1/elapsed
        cv2.putText(image, '{fps:%.5s}'%(fps), (int(50), int(50)), FONT, 2,COLORS[1])
        #print(fps)
        # 7. visualize result
        if args.display is True:
            image = cv2.resize(image,(540,960))
            cv2.imshow('result', image)
            key = cv2.waitKey(1) & 0xFF
            if key==ord("q"):
                break
                video.close()
            if key==ord("s"):
                key2 = cv2.waitKey(0)
예제 #9
0
def demo_live(args, video_path):
    # 1. load the configure file
    cfg_from_file(args.confg_file)

    # 2. load detector based on the configure file
    object_detector = ObjectDetector()

    # 3. load video
    cam = cv2.VideoCapture(0)
    #cam.set(cv2.CAP_PROP_FRAME_HEIGHT,1080)
    #cam.set(cv2.CAP_PROP_FRAME_WIDTH,1920)frametime10

    index = -1
    frametime20 = np.zeros((100,1))
    index = 0
    while True:
        #index = index + 1
        #sys.stdout.write('Process image: {} \r'.format(index))
        #sys.stdout.flush()

        # 4. read image
        #flag, image = video.read()
        retval,image = cam.read()
        #
        #image = cv2.resize(image,(192,108))
        #image = image[270:270+540,480:480+540,:]
        print(image.shape)
        if retval == False:
            print("Can not read image in Frame : {}".format(index))
            break
        t0 = timeit.default_timer()
        # 5. detect
        _labels, _scores, _coords = object_detector.predict(image)

        # 6. draw bounding box on the image
        for labels, scores, coords in zip(_labels, _scores, _coords):
            cv2.rectangle(image, (int(coords[0]), int(coords[1])), (int(coords[2]), int(coords[3])), COLORS[labels % 3], 2)
            cv2.putText(image, '{label}: {score:.3f}'.format(label=VOC_frametime10CLASSES[labels], score=scores), (int(coords[0]), int(coords[1])), FONT, 0.5, COLORS[labels % 3], 2)
        elapsed = timeit.default_timer() - t0
        frametime20[index] = elapsed
        index = index+1
        index = index%100
        print(elapsed)
        # 7. visualize result
        cv2.imshow('result', image)
        cv2.waitKey(1)
        '''
예제 #10
0
def demo_live(confg_file, index):
    # 1. load the configure file
    cfg_from_file(confg_file)

    # 2. load detector based on the configure file
    object_detector = ObjectDetector()

    # 3. load video
    state = "init"
    cam = cv2.VideoCapture(0)
    points = []
    record_points = []
    count_frames = 0
    all_count = 0
    success_count = 0
    circle_radius = 20
    line_thickness = 16
    close_distance = 20
    wait_frames = 10
    font_scale = 1
    font_thickness = 2
    middle_state = False
    middle_frame = 0
    bias = 50
    t = 0
    while True:
        retval, frame = cam.read()
        img_h, img_w, c = frame.shape
        s_y = int((1080 - img_h) / 2)
        s_x = int((1920 - img_w) / 2)
        bg_image = np.full((1080, 1920, 3), 127, np.uint8)
        bg_image[s_y:s_y + img_h, s_x:s_x + img_w] = frame
        frame = bg_image[:,
                         int(1920 * (1 - CROP_RATE) /
                             2):int(1920 * (1 + CROP_RATE) / 2)]
        frame = cv2.flip(frame, 1)
        show_img = np.copy(frame)
        if state == "init":
            points = random_point(
                s_x - int(1920 * (1 - CROP_RATE) / 2) + bias,
                s_x + img_w - int(1920 * (1 - CROP_RATE) / 2) - bias,
                s_y + bias, s_y + img_h - bias)
            print("points:", points)
            # input()
            state = "waiting"

        for i in range(len(points) - 1):
            cv2.line(show_img, (points[i][0], points[i][1]),
                     (points[i + 1][0], points[i + 1][1]), (255, 0, 0),
                     line_thickness)
        cv2.circle(show_img, tuple(points[0]), circle_radius, (0, 255, 0), -1)
        cv2.circle(show_img, tuple(points[-1]), circle_radius, (0, 0, 255), -1)
        for i in range(1, len(points) - 1):
            cv2.circle(show_img, tuple(points[i]), circle_radius,
                       (255, 0, 255), -1)
        _labels, _scores, _coords = object_detector.predict(frame)
        hands_points = []
        for labels, scores, coords in zip(_labels, _scores, _coords):
            tmp_x = int((coords[0] + coords[2]) / 2)
            tmp_y = int((coords[1] + coords[3]) / 2)
            cv2.circle(show_img, (tmp_x, tmp_y), circle_radius, (0, 255, 255),
                       -1)
            hands_points.append([tmp_x, tmp_y])
        if state == "waiting":
            if count_frames > wait_frames:
                state = "Recording"
                t = time.time()
                count_frames = 0
            has_found = False
            for h_point in hands_points:
                if np.sqrt((h_point[0] - points[0][0])**2 +
                           (h_point[1] - points[0][1])**2) < close_distance:
                    has_found = True
                    count_frames += 1
            if has_found == False:
                count_frames = 0
        if state == "Recording":
            record_points.append(hands_points)
            cv2.putText(show_img, "Recording", (10, 150),
                        cv2.FONT_HERSHEY_COMPLEX, font_scale, (0, 0, 255),
                        font_thickness)
            if middle_frame > wait_frames:
                middle_state = True
                record_points.append("Middle")
                middle_frame = 0
            if middle_state:
                cv2.circle(show_img, tuple(points[1]), circle_radius,
                           (0, 255, 0), -1)
            if count_frames > wait_frames:
                state = "init"
                print("----------------")
                if len(points) == 3:
                    if judge(record_points, points) and middle_state:
                        success_count += 1
                        print("yes")
                    else:
                        print("no")
                else:
                    if judge(record_points, points):
                        success_count += 1
                        print("yes")
                    else:
                        print("no")
                all_count += 1
                count_frames = 0
                middle_frame = 0
                middle_state = False
                print("{}:{}".format("time", time.time() - t))

                record_points = []
            has_found = False
            middle_found = False
            for h_point in hands_points:
                if np.sqrt((h_point[0] - points[-1][0])**2 +
                           (h_point[1] - points[-1][1])**2) < close_distance:
                    has_found = True
                    count_frames += 1
                if len(points) > 2 and middle_state is False and np.sqrt(
                    (h_point[0] - points[1][0])**2 +
                    (h_point[1] - points[1][1])**2) < close_distance:
                    middle_frame += 1
                    middle_found = True
            if not has_found:
                count_frames = 0
            if not middle_found:
                middle_frame = 0
        cv2.putText(show_img, "Result:{}/{}".format(success_count, all_count),
                    (10, 50), cv2.FONT_HERSHEY_COMPLEX, font_scale,
                    (0, 0, 255), font_thickness)
        cv2.putText(show_img, "{} {}".format(state, count_frames), (10, 100),
                    cv2.FONT_HERSHEY_COMPLEX, font_scale, (0, 0, 255),
                    font_thickness)
        cv2.imshow("Frame", show_img)
        key = cv2.waitKey(1) & 0xFF
        if key == ord("q"):
            break
예제 #11
0
def inference_img_dir(args, imgs_dir):
    if args.save:
        infer_key = imgs_dir.split("/")[-1].replace("/", "")
        model_name = args.confg_file.split("/")[-1].replace(".yml", "")
        test_img_rdir = os.path.join(args.save, infer_key)
        test_model_rdir = os.path.join(args.save, infer_key, model_name)
        test_out_img_path = os.path.join(args.save, infer_key, model_name,
                                         "det_imgs")
        pred_out_txt = os.path.join(args.save, infer_key, model_name,
                                    "pred_txt")
        model_save_path = os.path.join(args.save, infer_key, model_name,
                                       "model")
        if not os.path.exists(test_img_rdir):
            os.mkdir(test_img_rdir)
        if not os.path.exists(test_model_rdir):
            os.mkdir(test_model_rdir)
        if not os.path.exists(test_out_img_path):
            os.mkdir(test_out_img_path)
        if not os.path.exists(pred_out_txt):
            os.mkdir(pred_out_txt)
        if not os.path.exists(model_save_path):
            os.mkdir(model_save_path)

    # 1. load the configure file
    cfg_from_file(args.confg_file)
    # 2. load detector based on the configure file
    object_detector = ObjectDetector()
    for idx, img in enumerate(os.listdir(imgs_dir)):
        image_path = os.path.join(imgs_dir, img)
        # 3. load image
        image = cv2.imread(image_path)
        image256 = image[300:556]
        # 4. detect
        _labels, _scores, _coords = object_detector.predict(image256)

        # 5. draw bounding box on the image
        cxyl, stl, edl = [], [], []
        for label, score, coords in zip(_labels, _scores, _coords):
            xmin, ymin, xmax, ymax = [
                int(round(i)) for i in coords.cpu().numpy()
            ]
            cx = int(round((xmin + xmax) / 2))
            cy = int(round((ymin + ymax) / 2))
            min_d = min(xmax - xmin, ymax - ymin)
            if score >= 0.45:
                if label == 0:
                    cxyl.append([(cx, cy),
                                 [[xmin, ymin], [xmin, ymax], [xmax, ymax],
                                  [xmax, ymin]]])
                    cv2.circle(image256, (cx, cy), int(min_d / 4), (0, 0, 255),
                               2)
                    # cv2.rectangle(img,(xmin, ymin),(xmax, ymax),(0, 255, 255))
            if score >= 0.45:
                if label == 1:
                    stl.append((cx, cy))
                #     cv2.circle(image256, (cx, cy), 5, (255, 0, 0), 2)
                # elif label == 2:
                #     cv2.circle(image256, (cx, cy), 5, (0, 255, 0), 2)
                # cv2.line(img, (xmin, ymin), (xmax, ymax), (155, 155, 155), 2)
                # cv2.line(image256, (xmin + 20, ymin + 20), (xmax - 20, ymax - 20), (0, 255, 0), 2)
                # elif label == 4:
                #     # cv2.line(img, (xmin, ymin), (xmax, ymax), (0, 155, 155), 2)
                #     cv2.line(image256, (xmin + 20, ymin + 20), (xmax - 20, ymax - 20), (0, 155, 155), 2)
                # elif label == 5:
                #     cv2.circle(img256, (cx, cy), 5, (255, 255, 0))

            # cv2.rectangle(image256, (int(coords[0]), int(coords[1])), (int(coords[2]), int(coords[3])),
            #               COLORS[labels % 3], 2)
            # cv2.putText(image256, '{label}: {score:.3f}'.format(label=VOC_CLASSES[labels], score=scores),
            #             (int(coords[0]), int(coords[1])), FONT, 0.5, COLORS[labels % 3], 2)
        for idx2, cxyp in enumerate(cxyl):
            dis = []
            for idx1, stp in enumerate(stl):
                tdis = cv2.pointPolygonTest(np.array(cxyp[-1]), stp, True)
                tdis = float("inf") if tdis < 0 else tdis
                tdis2 = np.linalg.norm(np.array(stp) - np.array(cxyp[0]))
                dis.append(tdis + tdis2)
            idx = np.argmin(dis)
            # if cv2.pointPolygonTest(np.array(cxyp[-1]), stp, True):
            cv2.arrowedLine(image256, stl[idx], cxyp[0], (0, 0, 255))

        # 6. visualize result
        if args.display is True:
            cv2.imshow('result', image)
            cv2.waitKey(0)

        # 7. write result
        if args.save is not None:
            # path, _ = os.path.splitext(image_path)

            img_name = image_path.split("/")[-1]
            cv2.imwrite(os.path.join(test_out_img_path, img_name), image)
            print("save img:", img_name)