프로젝트

일반

사용자정보

통계
| 개정판:

hytos / DTI_PID / WebServer / app / recognition / index.py @ 31cd836b

이력 | 보기 | 이력해설 | 다운로드 (4.42 KB)

1 78a9d434 humkyung
from flask import Flask, Blueprint, jsonify, request, render_template
2
import cv2
3
import numpy as np
4
import sys, os
5
import json, base64
6
from PIL import Image
7
8
# craft
9 2e744c88 esham21
sys.path.insert(0, os.path.dirname(os.path.realpath(__file__)) + '\\..\\..\\CRAFT_pytorch_master')
10 78a9d434 humkyung
# service streamer
11 2e744c88 esham21
sys.path.insert(0, os.path.dirname(os.path.realpath(__file__)) + '\\..\\..\\service_streamer_master')
12 78a9d434 humkyung
# deep ocr
13
# sys.path.insert(0, os.path.dirname(os.path.realpath(__file__)) + '\\deep_text_recognition_benchmark_master')
14
# symbol
15 2e744c88 esham21
sys.path.insert(0, os.path.dirname(os.path.realpath(__file__)) + '\\..\\..\\symbol_recognition')
16 78a9d434 humkyung
17
recognition_service = Blueprint('recognition', __name__, url_prefix='/recognition')
18
19
20
@recognition_service.route('/', methods=['GET'])
21
def index():
22 5f6cb499 humkyung
    return render_template("/recognition/index.html")
23
24 78a9d434 humkyung
25
def recognition(params=None):
26 fa83a646 esham21
    if params[0][0] == 'Text Area':
27
        res = text_craft.get_text_box_batch(params[0][1])
28
        return [res]
29 78a9d434 humkyung
    elif params[0] == 'Symbol':
30
        pass
31
32
33
try:
34
    # from model import get_prediction, batch_prediction
35
    import text_craft
36
    import test_doftech_all_images
37
    from service_streamer import ThreadedStreamer
38
39
    # for error at 3.8
40
    # import ctypes
41
    # ctypes.cdll.LoadLibrary('caffe2_nvrtc.dll')
42
43
    streamer = ThreadedStreamer(recognition, batch_size=64)
44
except ImportError as ex:
45
    ex
46
    pass
47
48
49
@recognition_service.route('/symbol_box', methods=['POST'])
50
def symbol_box():
51
    if request.method == 'POST':
52
        r = request
53 edcd2d38 esham21
        data = json.loads(r.data)
54 78a9d434 humkyung
55 edcd2d38 esham21
        str_img = base64.b64decode(data['img'])
56
        nparr = np.fromstring(str_img, np.uint8)
57 78a9d434 humkyung
        imgs = []
58
        imgs.append(Image.fromarray(cv2.imdecode(nparr, cv2.IMREAD_COLOR)))
59
60 edcd2d38 esham21
        data_path = os.path.join(os.path.dirname(os.path.realpath(__file__)) + '\\..\\..\\symbol_training\\Data\\', data['name'])
61 6374c2c6 esham21
62 db5c22f1 esham21
        boxes = test_doftech_all_images.get_symbol(imgs, data['name'], root_path=data_path, trained_model1=os.path.join(data_path, 'checkpoint', data['name'] + "_only_params.pth"), \
63 6c9a3ed5 esham21
                                                   trained_model2=None, save=False)
64 78a9d434 humkyung
65
        return jsonify({'symbol_box': boxes[0]})
66
67
68
@recognition_service.route('/text_box', methods=['POST'])
69
def text_box():
70
    if request.method == 'POST':
71
        r = request
72
        nparr = np.fromstring(r.data, np.uint8)
73
74
        img = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
75
        # img = img.reshape(1, -1)
76
77
        boxes = text_craft.get_text_box(img, img_path=None, score_path=None, trained_model=os.path.dirname(
78
            os.path.realpath(__file__)) + '\\CRAFT_pytorch_master\\weights\\craft_mlt_25k.pth')
79
80
        return jsonify({'text_box': boxes})
81 a02da871 esham21
    
82
@recognition_service.route('/ocr', methods=['POST'])
83
def gcloud_ocr():
84
    if request.method == 'POST':
85
        r = request
86
        nparr = np.fromstring(r.data, np.uint8)
87
88
        img = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
89
        # img = img.reshape(1, -1)
90
91
        boxes = text_craft.get_gcloud_ocr(img)
92
93
        return jsonify({'text_box': boxes})
94 78a9d434 humkyung
95
96
@recognition_service.route('/stream_text_box', methods=['POST'])
97
def stream_text_box():
98
    if request.method == 'POST':
99
        r = request
100
        str_imgs = json.loads(r.data)
101
        imgs = []
102
        for str_img in str_imgs:
103
            str_img = base64.b64decode(str_img)
104
            nparr = np.fromstring(str_img, np.uint8)
105
            img = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
106
            imgs.append(img)
107
108
        boxes_list = []
109
        '''
110
        for img in imgs:
111
            # faster
112
            #boxes = streamer.predict([[img, None, None, os.path.dirname(os.path.realpath(__file__)) + '\\CRAFT_pytorch_master\\weights\\craft_ic15_20k.pth']])
113

114
            # More accurate
115
            boxes = streamer.predict([[img, None, None, os.path.dirname(os.path.realpath(__file__)) + '\\CRAFT_pytorch_master\\weights\\craft_mlt_25k.pth']])
116
            boxes_list.append(boxes[0])
117
        '''
118
119
        '''
120
        infos = []
121
        for img in imgs:
122
            infos.append([img, None, None, os.path.dirname(os.path.realpath(__file__)) + '\\CRAFT_pytorch_master\\weights\\craft_mlt_25k.pth'])
123
        boxes = streamer.predict(infos)
124
        boxes_list = boxes
125
        '''
126
127 02bb6172 esham21
        infos = [['Text Area', [False, os.path.dirname(os.path.realpath(__file__)) + '\\..\\..\\CRAFT_pytorch_master\\weights\\craft_mlt_25k.pth', imgs]]]
128 78a9d434 humkyung
        boxes = streamer.predict(infos)
129 fa83a646 esham21
        boxes_list = boxes[0][0]
130 78a9d434 humkyung
131 2e744c88 esham21
        return jsonify({'text_box_list': boxes_list})
클립보드 이미지 추가 (최대 크기: 500 MB)