text: #
  # The codes are used for implementing CTPN for scene text detection, described in: 
  #
  # Z. Tian, W. Huang, T. He, P. He and Y. Qiao: Detecting Text in Natural Image with
  # Connectionist Text Proposal Network, ECCV, 2016.
  #
  # Online demo is available at: textdet.com
  # 
  # These demo codes (with our trained model) are for text-line detection (without 
  # side-refiement part).  
  #
  #
  # ====== Copyright by Zhi Tian, Weilin Huang, Tong He, Pan He and Yu Qiao==========
  
  #            Email: zhi.tian@siat.ac.cn; wl.huang@siat.ac.cn
  # 
  #   Shenzhen Institutes of Advanced Technology, Chinese Academy of Sciences
  #
  #
  
  from cfg import Config as cfg
  from other import draw_boxes, resize_im, CaffeModel
  import cv2, os, caffe, sys
  from detectors import TextProposalDetector, TextDetector
  import os.path as osp
  from utils.timer import Timer
  from PIL import Image
  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)
      print "re_im"
      text_lines=text_detector.detect(im)
  
      print "Number of the detected text lines: %s"%len(text_lines)
      print "Time: %f"%timer.toc()
  
      _,im_with_text_lines=draw_boxes(im, text_lines, caption=im_name, wait=False)
      im_with_text_lines = cv2.cvtColor(im_with_text_lines,cv2.COLOR_BGR2RGB)
      Image.fromarray(im_with_text_lines).save('./res/'+im_name)
  
  
  print "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
  print "Thank you for trying our demo. Press any key to exit..."
  cv2.waitKey(0)
  
  