コード例 #1
0
def main(mode, model, size, engine, batch_size):
    size = int(size)
    batch_size = int(batch_size)

    net = load_model(model, size, engine)

    if mode == 'test':
        img = cv2.imread('samples/sample.jpg')
        res = net.predict([img])
        draw_all(res, model)

        cv2.imshow('a', img)
        cv2.waitKey(0)
        cv2.destroyAllWindows()

    else:
        from glob import glob
        from tqdm import tqdm
        imgs = glob('data/VOCdevkit/VOC2007/JPEGImages/*.jpg')

        for i in tqdm(range(0, len(imgs), batch_size)):
            ims = [cv2.imread(im) for im in imgs[i:i + batch_size]]
            res = net.predict(ims)

    print(f'Total imgs: {net.count:.2f}')
    print(f'Total FPS: {net.get_total_FPS():.2f}')
    print(f'Inference FPS: {net.get_inference_FPS():.2f}')
コード例 #2
0
ファイル: test_lottery.py プロジェクト: Sakuten/backend
def test_draw_all(client):
    """attempt to draw all lotteries
        1. make some applications to some lotteries
        2. draws all the lotteries in one time index
        3. test: status code
        4. test: DB is properly changed
        target_url: /draw_all [POST]
    """
    time_index = 1

    with client.application.app_context():
        target_lotteries = Lottery.query.filter_by(index=time_index)
        non_target_lotteries = Lottery.query.filter_by(index=time_index+1)
        users = (user for user in User.query.all()
                 if user.authority != "admin")
        for i, user in enumerate(users):
            target_lottery = target_lotteries[i % len(list(target_lotteries))]
            non_target_lottery = non_target_lotteries[i % len(
                list(non_target_lotteries))]
            application1 = Application(lottery=target_lottery, user_id=user.id)
            application2 = Application(
                lottery=non_target_lottery, user_id=user.id)
            db.session.add(application1)
            db.session.add(application2)
        db.session.commit()

    token = login(client,
                  admin['secret_id'],
                  admin['g-recaptcha-response'])['token']
    _, en = client.application.config['TIMEPOINTS'][time_index]
    en_margin = client.application.config['TIMEPOINT_END_MARGIN']
    draw_time = mod_time(en, en_margin)

    resp = draw_all(client, token, time=draw_time)

    assert resp.status_code == 200

    winners_id = [winner['id'] for winner in resp.get_json()]

    with client.application.app_context():
        users = User.query.all()
        for user in users:
            for lottery in target_lotteries:
                application = Application.query.filter_by(
                    lottery=lottery, user_id=user.id).first()
                if application:
                    if user.id in winners_id:
                        assert application.status == "won"
                    else:
                        assert application.status in {"lose", "waiting"}

            for lottery in non_target_lotteries:
                application = Application.query.filter_by(
                    lottery=lottery, user_id=user.id).first()
                if application:
                    assert application.status == "pending"
コード例 #3
0
ファイル: test_lottery.py プロジェクト: Sakuten/backend
def test_apply_member_consecutive(client):
    """attempt to apply after a member's previous won / lose / waiting
        test: win -> refused (because he/she should be watching a show)
        test: lose / waiting -> accepted
        target_url: /lotteries/<id> [POST]
    """
    idx1 = 1
    token_admin = get_token(client, admin)

    with client.application.app_context():
        lottery1 = Lottery.query.get(idx1)
        index1 = lottery1.index
        lottery2 = Lottery.query.filter_by(
            classroom_id=lottery1.classroom_id,
            index=lottery1.index + 1).one()
        idx2 = lottery2.id
        index2 = lottery2.index

        # 5 won, 2 lose, 3 waiting
        users = User.query.filter_by(authority='normal').limit(10).all()
        apps = users2application(users, lottery1)
        add_db(apps)

        draw_all(client, token_admin, index=index1)

        people = [Application.query.filter_by(status=s).first().user
                  for s in ('won', 'lose', 'waiting')]
        sids = [person.secret_id for person in people]
        tokens = [login(client, person.secret_id, '')['token']
                  for person in people]

        for i, (token_i, member_i) in enumerate(((0, 1), (2, 0), (1, 2))):
            with mock.patch('api.routes.api.get_time_index',
                            return_value=index2):
                resp = post(client, f'/lotteries/{idx2}',
                            token=tokens[token_i],
                            group_members=[sids[member_i]])
                if i == 2:
                    assert resp.status_code == 200
                else:
                    assert resp.status_code == 403
                    assert 'while watching' in resp.get_json()['message']
コード例 #4
0
ファイル: test_lottery.py プロジェクト: Sakuten/backend
def test_draw_all_multiple(client):
    """hit /draw_all twice in one time index
        test: the result does not change
        target_url: /draw_all [POST]
    """
    idx = 1

    with client.application.app_context():
        target_lottery = Lottery.query.get(idx)
        index = target_lottery.index
        users = (user for user in User.query.all()
                 if user.authority == 'normal')
        for user in users:
            app = Application(lottery=target_lottery, user_id=user.id)
            db.session.add(app)
        db.session.commit()

        token = login(client,
                      admin['secret_id'],
                      admin['g-recaptcha-response'])['token']

        resp1 = draw_all(client, token, index=index)
        assert resp1.status_code == 200
        win1 = {
            app.id for app in Application.query.filter_by(status='won')}
        lose1 = {
            app.id for app in Application.query.filter_by(status='lose')}
        waiting1 = {
            app.id for app in Application.query.filter_by(status='waiting')}

        resp2 = draw_all(client, token, index=index)
        assert resp2.status_code == 200
        win2 = {
            app.id for app in Application.query.filter_by(status='won')}
        lose2 = {
            app.id for app in Application.query.filter_by(status='lose')}
        waiting2 = {
            app.id for app in Application.query.filter_by(status='waiting')}

        assert win1 == win2
        assert lose1 == lose2
        assert waiting1 == waiting2
コード例 #5
0
ファイル: test_lottery.py プロジェクト: Sakuten/backend
def test_draw_all_noperm(client):
    """attempt to draw without proper permission.
        target_url: /draw_all [POST]
    """
    token = login(client, test_user['secret_id'],
                  test_user['g-recaptcha-response'])['token']
    resp = draw_all(client, token)

    assert resp.status_code == 403
    assert 'You have no permission to perform the action' in \
        resp.get_json()['message']
コード例 #6
0
ファイル: test_lottery.py プロジェクト: Sakuten/backend
def test_apply_consecutive(client):
    """attempt to apply after previous won / lose / waiting
        test: win -> refused (because he/she should be watching a show)
        test: lose / waiting -> accepted
        target_url: /lotteries/<id> [POST]
    """
    idx1 = 1
    token_admin = get_token(client, admin)

    with client.application.app_context():
        lottery1 = Lottery.query.get(idx1)
        index1 = lottery1.index
        lottery2 = Lottery.query.filter_by(
            classroom_id=lottery1.classroom_id,
            index=lottery1.index + 1).one()
        idx2 = lottery2.id
        index2 = lottery2.index

        # 5 won, 2 lose, 3 waiting
        users = User.query.filter_by(authority='normal').limit(10).all()
        apps = users2application(users, lottery1)
        add_db(apps)
        user_sids = [user.secret_id for user in users]
        app_ids = [app.id for app in apps]

        draw_all(client, token_admin, index=index1)

        for user_sid, app_id in zip(user_sids, app_ids):
            app_after = Application.query.get(app_id)

            token = login(client, user_sid, '')['token']
            with mock.patch('api.routes.api.get_time_index',
                            return_value=index2):
                resp = post(client, f'/lotteries/{idx2}', token=token,
                            group_members=[])

            if app_after.status == 'won':
                assert resp.status_code == 403
                assert 'while watching a show' in resp.get_json()['message']
            else:
                assert resp.status_code == 200
コード例 #7
0
ファイル: test_lottery.py プロジェクト: Sakuten/backend
    def try_with_datetime(t):
        resp = draw_all(client, token, time=t)

        assert resp.status_code == 400
        assert 'Not acceptable' in resp.get_json()['message']