def textPredict(input_path):

    #CPU mode setting
    if len(sys.argv)>1 and sys.argv[1]=="--no-gpu":
        caffe.set_mode_cpu()
    else:
        caffe.set_mode_gpu()
        caffe.set_device(cfg.TEST_GPU_ID)

    model_path = "../models/"

    # initialize the detectors
    NET_DEF_FILE = model_path + "deploy.prototxt"
    MODEL_FILE = model_path + "ctpn_trained_model.caffemodel"

    text_proposals_detector=TextProposalDetector(CaffeModel(NET_DEF_FILE, MODEL_FILE))
    text_detector=TextDetector(text_proposals_detector)

    im=cv2.imread(input_path)
    #h = im.shape[0]
    #w = im.shape[1]
    im, f=resize_im(im, cfg.SCALE, cfg.MAX_SCALE)
    text_lines=text_detector.detect(im)

    return text_lines,f
示例#2
0
def detect_text_boxes(video_pk, cpu_mode=False):
    """
    Detect Text Boxes in frames for a video using CTPN, must be run in dva_ctpn container
    :param detector_pk
    :param video_pk
    :return:
    """
    setup_django()
    from dvaapp.models import Region, Frame
    from django.conf import settings
    from PIL import Image
    import sys
    video_pk = int(video_pk)
    sys.path.append('/opt/ctpn/CTPN/tools/')
    sys.path.append('/opt/ctpn/CTPN/src/')
    from cfg import Config as cfg
    from other import resize_im, CaffeModel
    import cv2, caffe
    from detectors import TextProposalDetector, TextDetector
    NET_DEF_FILE = "/opt/ctpn/CTPN/models/deploy.prototxt"
    MODEL_FILE = "/opt/ctpn/CTPN/models/ctpn_trained_model.caffemodel"
    if cpu_mode:  # Set this to true for CPU only mode
        caffe.set_mode_cpu()
    else:
        caffe.set_mode_gpu()
        caffe.set_device(cfg.TEST_GPU_ID)
    text_proposals_detector = TextProposalDetector(
        CaffeModel(NET_DEF_FILE, MODEL_FILE))
    text_detector = TextDetector(text_proposals_detector)
    for f in Frame.objects.all().filter(video_id=video_pk):
        path = "{}/{}/frames/{}.jpg".format(settings.MEDIA_ROOT, video_pk,
                                            f.frame_index)
        im = cv2.imread(path)
        old_h, old_w, channels = im.shape
        im, _ = resize_im(im, cfg.SCALE, cfg.MAX_SCALE)
        new_h, new_w, channels = im.shape
        mul_h = float(old_h) / float(new_h)
        mul_w = float(old_w) / float(new_w)
        text_lines = text_detector.detect(im)
        for k in text_lines:
            left, top, right, bottom, score = k
            left, top, right, bottom = int(left * mul_w), int(
                top * mul_h), int(right * mul_w), int(bottom * mul_h)
            r = Region()
            r.region_type = r.DETECTION
            r.confidence = int(100.0 * score)
            r.object_name = "CTPN_TEXTBOX"
            r.y = top
            r.x = left
            r.w = right - left
            r.h = bottom - top
            r.frame_id = f.pk
            r.video_id = video_pk
            r.save()
            right = r.w + r.x
            bottom = r.h + r.y
            img = Image.open(path)
            img2 = img.crop((left, top, right, bottom))
            img2.save("{}/{}/detections/{}.jpg".format(settings.MEDIA_ROOT,
                                                       video_pk, r.pk))
示例#3
0
    def __init__(self, mode):
        if mode == "GPU":
            caffe.set_mode_gpu()
            caffe.set_device(cfg.TEST_GPU_ID)
        else:
            caffe.set_mode_cpu()

        netfile = cfg.NET_FILE
        modelfile = cfg.MODEL_FILE

        # initialize the detectors
        self.text_proposals_detector = TextProposalDetector(
            CaffeModel(netfile, modelfile))
        self.text_detector = TextDetector(self.text_proposals_detector)
        self.timer = Timer()

        self.char_classifier = caffe.Classifier(
            cfg.FONT_PROTO,
            cfg.FONT_MODEL,
            mean=np.load(cfg.FONT_MEANFILE).mean(1).mean(1),
            channel_swap=(2, 1, 0),
            raw_scale=255,
            image_dims=(cfg.FONT_DIMS, cfg.FONT_DIMS))
        with open(cfg.FONT_LBLFILE, 'r') as f:
            self.fontLabels = [x.strip() for x in f]
示例#4
0
def init(model_file, trained_file):
    # initialize the detectors
    global text_proposals_detector
    global text_detector
    caffe_model = CaffeModel(model_file, trained_file)
    text_proposals_detector = TextProposalDetector(caffe_model)
    text_detector = TextDetector(text_proposals_detector)
示例#5
0
 def get_text_lines(self, im, NET_DEF_FILE, MODEL_FILE):
     # initialize the detectors
     text_proposals_detector = TextProposalDetector(
         CaffeModel(NET_DEF_FILE, MODEL_FILE))
     text_detector = TextDetector(text_proposals_detector)
     im, f = resize_im(im, cfg.SCALE, cfg.MAX_SCALE)
     text_lines = text_detector.detect(im)
     return text_lines / f
示例#6
0
 def load(self):
     logging.info('Creating networks and loading parameters')
     NET_DEF_FILE = "/opt/ctpn/CTPN/models/deploy.prototxt"
     MODEL_FILE = "/opt/ctpn/CTPN/models/ctpn_trained_model.caffemodel"
     caffe.set_mode_gpu()
     caffe.set_device(cfg.TEST_GPU_ID)
     text_proposals_detector = TextProposalDetector(
         CaffeModel(NET_DEF_FILE, MODEL_FILE))
     self.session = TextDetector(text_proposals_detector)
示例#7
0
def ctpnSource():
    DEMO_IMAGE_DIR = "img/"
    NET_DEF_FILE = "CTPN/models/deploy.prototxt"
    MODEL_FILE = "CTPN/models/ctpn_trained_model.caffemodel"
    caffe.set_mode_gpu()
    caffe.set_device(cfg.TEST_GPU_ID)
    # initialize the detectors
    text_proposals_detector = TextProposalDetector(CaffeModel(NET_DEF_FILE, MODEL_FILE))
    text_detector = TextDetector(text_proposals_detector)
    return text_detector
示例#8
0
 def __init__(self):
     '''
     @Construction for text detector. 
     This class initiates the constructor for 
     '''
     self.NET_DEF_FILE = "models/deploy.prototxt"
     self.MODEL_FILE = "models/ctpn_trained_model.caffemodel"
     caffe.set_mode_gpu()
     caffe.set_device(cfg.TEST_GPU_ID)
     self.text_proposals_detector = TextProposalDetector(CaffeModel(self.NET_DEF_FILE, self.MODEL_FILE))
     self.text_detector = TextDetector(self.text_proposals_detector)
示例#9
0
def init_models(args):
    if cfg.PLATFORM == "GPU":
        caffe.set_mode_gpu()
        caffe.set_device(cfg.TEST_GPU_ID)
    else:
        caffe.set_mode_cpu()

    # initialize the detectors
    text_proposals_detector=TextProposalDetector(CaffeModel(args.DET_NET_DEF_FILE, args.DET_MODEL_FILE))
    text_detector=TextDetector(text_proposals_detector)

    return text_detector
示例#10
0
 def load(self):
     logging.info('Creating networks and loading parameters')
     if os.environ.get('GPU_AVAILABLE', False):
         caffe.set_mode_gpu()
         caffe.set_device(cfg.TEST_GPU_ID)
         logging.info("GPU mode")
     else:
         caffe.set_mode_cpu()
         logging.info("CPU mode")
     text_proposals_detector = TextProposalDetector(
         CaffeModel(self.network_def, self.model_path))
     self.session = TextDetector(text_proposals_detector)
     logging.info('model loaded!')
示例#11
0
    def __init__(self, dir_model, gpu_id):
        NET_DEF_FILE = dir_model + "/bib_number/CTPN/deploy.prototxt"
        MODEL_FILE = dir_model + "/bib_number/CTPN/ctpn_trained_model.caffemodel"
        if False:  # Set this to true for CPU only mode
            caffe.set_mode_cpu()
        else:
            caffe.set_mode_gpu()
            caffe.set_device(int(gpu_id))  # (cfg.TEST_GPU_ID)

        text_proposals_detector = TextProposalDetector(
            CaffeModel(NET_DEF_FILE, MODEL_FILE))
        self.text_detector = TextDetector(text_proposals_detector)

        length_regexp = 'Duration: (\d{2}):(\d{2}):(\d{2})\.\d+,'
        self.re_length = re.compile(length_regexp)
def ctpnSource():
    DEMO_IMAGE_DIR = "img/"
    # net类
    NET_DEF_FILE = "CTPN/models/deploy.prototxt"
    # 训练好的模型文件
    MODEL_FILE = "CTPN/models/ctpn_trained_model.caffemodel"
    caffe.set_mode_gpu()
    caffe.set_device(cfg.TEST_GPU_ID)
    # initialize the detectors
    # 初步的proposal检测器类
    text_proposals_detector = TextProposalDetector(
        CaffeModel(NET_DEF_FILE, MODEL_FILE))
    # 进一步的(含文本线构造器)检测器类
    text_detector = TextDetector(text_proposals_detector)
    return text_detector
示例#13
0
def text_detec(img_url):
    caffe.set_mode_gpu()
    caffe.set_device(cfg.TEST_GPU_ID)

    # initialize the detectors
    text_proposals_detector = TextProposalDetector(
        CaffeModel(NET_DEF_FILE, MODEL_FILE))
    text_detector = TextDetector(text_proposals_detector)
    im = cv2.imread(img_url)
    timer.tic()
    im, f = resize_im(im, cfg.SCALE, cfg.MAX_SCALE)
    text_lines = text_detector.detect(im)
    obj_num = len(text_lines)
    print "Number of the detected text lines: %s" % len(text_lines)
    print "Time: %f" % timer.toc()

    boxstr = u''

    count = 0
    #http://192.168.7.37:8393/static/jz66f1d49d97d048fe9e4a62004199d0b2_1_for_trail.jpg
    print text_lines
    for bbox in text_lines:
        print bbox
        count += 1
        boxstr += "text[%d]:[%f,%f,%f,%f]<br/>" % (count, bbox[0], bbox[1],
                                                   bbox[2], bbox[3])
    im_name = img_url.split('/')[-1]
    im_name.replace("?", '_')
    im_name.replace("%", '_')
    im_name.replace("&", '_')
    im_name.replace("=", '_')
    local_url = img_url
    write_path = "/data1/mingmingzhao/data_sets/test/text_detect/text_detect_%s" % (
        local_url.split('/')[-1])
    print "write_path:" + write_path
    im_with_text_lines = draw_boxes_zmm(im,
                                        text_lines,
                                        caption=write_path,
                                        wait=False)
    server_url = "http://192.168.7.37:8393/static/text_detect/%s" % (
        write_path.split('/')[-1])
    print "server_url:" + server_url
    return boxstr, server_url, count
示例#14
0
    def __init__(self, NET_DEF_FILE, MODEL_FILE, caffe_path):
        sys.path.insert(0, "%s/python" % caffe_path)
        import caffe
        from other import draw_boxes, resize_im, CaffeModel
        from detectors import TextProposalDetector, TextDetector
        sys.path.remove("%s/python" % caffe_path)
        #def ctpnSource(NET_DEF_FILE, MODEL_FILE, use_gpu):
        #NET_DEF_FILE = "CTPN/models/deploy.prototxt"
        #MODEL_FILE = "CTPN/models/ctpn_trained_model.caffemodel"
        self.caffe = caffe
        #if use_gpu:
        #    caffe.set_mode_gpu()
        #    caffe.set_device(cfg.TEST_GPU_ID)
        #else:
        #    caffe.set_mode_cpu()

        # initialize the detectors
        text_proposals_detector = TextProposalDetector(
            CaffeModel(NET_DEF_FILE, MODEL_FILE))
        self.text_detector = TextDetector(text_proposals_detector)
        self.resize_im = resize_im
        self.draw_boxes = draw_boxes
示例#15
0
文件: demo.py 项目: vicident/CTPN
from detectors import TextProposalDetector, TextDetector
import os.path as osp
from ctpn_utils.timer import Timer

DEMO_IMAGE_DIR = "demo_images/"
NET_DEF_FILE = "models/deploy.prototxt"
MODEL_FILE = "models/ctpn_trained_model.caffemodel"

if len(sys.argv) > 1 and sys.argv[1] == "--no-gpu":
    caffe.set_mode_cpu()
else:
    caffe.set_mode_gpu()
    caffe.set_device(cfg.TEST_GPU_ID)

# initialize the detectors
text_proposals_detector = TextProposalDetector(
    CaffeModel(NET_DEF_FILE, MODEL_FILE))
text_detector = TextDetector(text_proposals_detector)

demo_imnames = os.listdir(DEMO_IMAGE_DIR)
timer = Timer()

for im_name in demo_imnames:
    print "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
    print "Image: %s" % im_name

    im_file = osp.join(DEMO_IMAGE_DIR, im_name)
    im = cv2.imread(im_file)

    timer.tic()

    im, f = resize_im(im, cfg.SCALE, cfg.MAX_SCALE)
示例#16
0
 def load_model(self):
     caffe.set_mode_cpu()
     text_proposals_detector = TextProposalDetector(
         CaffeModel(NET_DEF_FILE, MODEL_FILE))
     self.text_detector = TextDetector(text_proposals_detector)