predict_eval.py 3.11 KB
Newer Older
LDOUBLEV's avatar
LDOUBLEV committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import utility
from ppocr.utils.utility import initial_logger
logger = initial_logger()
import cv2
import predict_system
import copy
import numpy as np
import math
import time
import json
LDOUBLEV's avatar
LDOUBLEV committed
25
26
27
28
import os
from PIL import Image, ImageDraw, ImageFont
from tools.infer.utility import draw_ocr
from ppocr.utils.utility import get_image_file_list
LDOUBLEV's avatar
LDOUBLEV committed
29
30
31
32
33

if __name__ == "__main__":
    args = utility.parse_args()
    text_sys = predict_system.TextSystem(args)

LDOUBLEV's avatar
LDOUBLEV committed
34
35
36
    if not os.path.exists(args.image_dir):
        raise Exception("{} not exists !!".format(args.image_dir))
    image_file_list = get_image_file_list(args.image_dir)
LDOUBLEV's avatar
LDOUBLEV committed
37
38
39

    total_time_all = 0
    count = 0
LDOUBLEV's avatar
LDOUBLEV committed
40
    save_path = "./inference_output/predict.txt"
LDOUBLEV's avatar
LDOUBLEV committed
41
42
    fout = open(save_path, "wb")
    for image_name in image_file_list:
LDOUBLEV's avatar
LDOUBLEV committed
43
        image_file = image_name
LDOUBLEV's avatar
LDOUBLEV committed
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
        img = cv2.imread(image_file)
        if img is None:
            logger.info("error in loading image:{}".format(image_file))
            continue
        count += 1
        total_time = 0
        starttime = time.time()
        dt_boxes, rec_res = text_sys(img)
        elapse = time.time() - starttime
        total_time_all += elapse
        print("Predict time of %s(%d): %.3fs" % (image_file, count, elapse))
        dt_num = len(dt_boxes)
        bbox_list = []
        for dno in range(dt_num):
            box = dt_boxes[dno]
            text, score = rec_res[dno]
            points = []
            for tno in range(len(box)):
                points.append([box[tno][0] * 1.0, box[tno][1] * 1.0])
            bbox_list.append({
                "transcription": text,
                "points": points,
                "scores": score * 1.0
            })
LDOUBLEV's avatar
LDOUBLEV committed
68
69
70
71
72
73
74
75
76
77
78
79
80
81
        # draw predict box and text in image
        # and save drawed image in save_path
        image = Image.open(image_file)
        boxes, txts, scores = [], [], []
        for dic in bbox_list:
            boxes.append(dic['points'])
            txts.append(dic['transcription'])
            scores.append(round(dic['scores'], 3))
        new_img = draw_ocr(image, boxes, txts, scores, draw_txt=True)
        draw_img_save = os.path.join(
            os.path.dirname(save_path), "inference_draw",
            os.path.basename(image_file))
        cv2.imwrite(draw_img_save, new_img)
        # save predicted results in txt file
LDOUBLEV's avatar
LDOUBLEV committed
82
83
84
85
86
87
        otstr = image_name + "\t" + json.dumps(bbox_list) + "\n"
        fout.write(otstr.encode('utf-8'))
    avg_time = total_time_all / count
    logger.info("avg_time: {0}".format(avg_time))
    logger.info("avg_fps: {0}".format(1.0 / avg_time))
    fout.close()