示例#1
0
    def test_image_model_save_and_extract(self):
        fname = 'test.jpg'
        img = Image(filename=fname)
        img.save()

        data = Image.query().first()

        self.assertEqual(data.id, img.id)
        self.assertEqual(data.filename, fname)
示例#2
0
def prediction():
    image_id = request.json.get('img_id')
    question = request.json.get('question')

    img = Image.get(image_id)

    if img is None:
        kwargs = {'status': 'error', 'message': 'invalid image id'}
        response = jsonify(kwargs)
        response.status_code = 400
    else:
        filepath = os.path.join(Config.UPLOAD_DIR, img.filename)
        path = os.path.join(Config.STATIC_DIR, f'{filepath}')
        pred, fig_id = asyncio.run(run_model(path, question))

        if fig_id > 0:
            #fig = WeightFigure.get(fig_id)
            #figfile = fig.filename
            #figpath = os.path.join(Config.FIG_DIR, figfile)
            pass

        kwargs = {'status': 'success'}
        data = {'prediction': pred}
        response = jsonify(data=data, **kwargs)
        response.status_code = 200

    return response
示例#3
0
def upload_image():
    img_file = request.files['file']

    if img_file:
        filename = secure_filename(img_file.filename)

        # rename if the same file name does exist
        if Image.query().filter_by(filename=filename).count() > 0:
            from datetime import datetime
            dttm = datetime.now()
            dttm = dttm.strftime('%Y%m%s%H%M%S')
            filename, ext = os.path.splitext(filename)
            # max length of filename is 64
            filename = f'{filename[:64-len(dttm)-len(ext)-1]}_{dttm}{ext}'
        elif len(filename) > 64:
            filename, ext = os.path.splitext(filename)
            # max length of filename is 64
            filename = f'{filename[:64-len(ext)]}{ext}'

        try:
            img = Image(filename=filename)
            img.save()
        except Exception:
            kwargs = {'status': 'error', 'message': 'failed to save image'}
            response = jsonify(kwargs)
            response.status_code = 400
        else:
            filepath = os.path.join(Config.UPLOAD_DIR, filename)
            img_file.save(os.path.join(Config.STATIC_DIR, filepath))

            kwargs = {'status': 'success', 'message': 'uploaded image'}
            data = {'img_id': img.id}
            response = jsonify(data=data, **kwargs)
            response.status_code = 201
    else:
        kwargs = {'status': 'error', 'message': 'failed to upload image'}
        response = jsonify(data=data, **kwargs)
        response.status_code = 400

    return response
示例#4
0
    def __call__(self):
        if not self._test_files:
            raise RuntimeError('data already created')

        img_file = self._test_files.pop()
        filename = os.path.basename(img_file)
        target_path = os.path.join(TestConfig.STATIC_DIR,
                                   TestConfig.UPLOAD_DIR, filename)

        # if not exist, copy image to actual directory
        if not os.path.isfile(target_path):
            shutil.copy(img_file, target_path)
        saved_at = fake.past_date(start_date='-30d')

        return Image(filename=filename, saved_at=saved_at)
示例#5
0
def extract_qa_by_request_log(req_id):

    log = RequestLog.get(req_id)
    kwargs = {
        'status': 'success',
        'task': 'read-only',
        'type': 'request log',
    }

    data = {
        'request_id': req_id,
        'question': Question.get(log.question_id).question,
        'prediction': log.score.prediction,
        'image': Image.get(log.image_id).filename,
        'figure': WeightFigure.get(log.fig_id).filename,
    }

    response = jsonify(data=data, **kwargs)
    response.status_code = 200
    return response
示例#6
0
    def test_update_image_state(self):
        fname = 'test.jpg'
        img = Image(filename=fname)
        img.save()

        # before update, no data is stored
        self.assertIsNotNone(img.original)

        img.update()

        target = f'{img.id:05d}.jpg'

        self.assertNotEqual(img.filename, fname)
        self.assertEqual(img.filename, target)
        # save original filename
        self.assertEqual(img.original, fname)
示例#7
0
 def test_upload_invalid_file(self):
     with self.assertRaises(ValueError):
         img = Image(filename='invalid.txt')
         img.save()
示例#8
0
async def run_prediction(reader, writer):
    global predictor
    global _processor

    data = await reader.read(1024)
    filepath, sentence = data.decode().split('\t')
    log.info(f'Run prediction: {sentence}')

    fig_id = None

    try:
        if not os.path.isfile(filepath):
            log.warning('Could not find image')
            pred = 'Could not find image.\t0'
            raise FileNotFoundError(f'Could not find image: {filepath}')
        else:
            pred, w, qtype_id = predictor.predict(sentence, filepath)
            # TODO: add attention weight for Closed-Question
            if qtype_id < 1:
                fig_id = 0
            else:
                # generate attention weighted heatmap
                sentence_ = sentence.split()
                pred_ = pred.split()
                # weights = w[0, :len(pred_)]
                # f, a = generate_heatmap(weights, sentence_, pred_)
                # fig_id = save_figure(f)
                fig_id = 0
            pred_out = pred + '\t' + str(fig_id)

    except Exception as e:
        log.error(e)
        # send error code
        pred_out = '<e>\t0'
        log.error('Could not store predict history')
        kwargs = {
            'log_text': str(e),
            'log_type': 'error'
        }
        raise
    else:
        kwargs = {
            'question_type_id': qtype_id,
            'log_text': 'success',
            'log_type': 'success'
        }

    finally:
        # save the result
        filename = os.path.basename(filepath)
        img = Image.query().filter_by(filename=filename).first()
        pred_id = None
        q = Question(question=sentence)
        q.save()

        if kwargs.get('log_type') == 'success':
            pred_log = PredictionScore(prediction=pred)
            pred_log.save()
            pred_id = pred_log.id

        log_model = RequestLog(image_id=img.id,
                               question_id=q.id,
                               fig_id=fig_id,
                               score_id=pred_id,
                               **kwargs)
        log_model.save()

        writer.write(pred_out.encode())
        await writer.drain()
        writer.close()