def __init__(self): if not self._created: self._created = True self._randFileNameGen = random.Random() self._randFileNameGen.seed(os.getpid()) self.OCR_UPLOAD_FOLDER = OCRUtils.getAppConf("OCR_UPLOAD_FOLDER") self.OCR_RESULTS_FOLDER = OCRUtils.getAppConf("OCR_RESULTS_FOLDER") OCRUtils.getLogger(__name__).debug( "Criado VPARManager do processo " + str(os.getpid()))
def submitImage(self, imgExt, imgData): submissionCode = str( self._randFileNameGen.randint(1, 99999999999999999999)).zfill(20) imageFileName = self.OCR_UPLOAD_FOLDER + submissionCode + "." + imgExt try: with open(imageFileName, "wb", -1) as imageFile: imageFile.write(imgData.decode('base64')) return submissionCode except IOError as err: OCRUtils.getLogger(__name__).fatal("I/O error({0}): {1}".format( errno, strerror)) raise err
def post(self): jsonContent = request.json #Verifica se o json tem o nome do arquivo e dados. if not ((jsonContent) and jsonContent.get("extArquivo") and jsonContent.get("dadosArquivo")): return Response(status=400) try: submissionCode = self.vparManager.submitImage( jsonContent.get("extArquivo"), jsonContent.get("dadosArquivo")) return jsonify(codigoLeituraOCR=submissionCode) except: OCRUtils.getLogger(__name__).debug( '*** Exceção no método POST de ABOCRServices ***') return Response(status=500)
def getOCRResult(self, submissionCode): outputFileName = self.OCR_RESULTS_FOLDER + submissionCode + ".txt" if os.path.isfile(outputFileName): ocrResultVars = {} with open(outputFileName, "r") as resultFile: for line in resultFile: name, var = line.partition(":")[::2] ocrResultVars[name.strip()] = var.strip() os.remove(outputFileName) OCRUtils.getLogger(__name__).info("Resultados do arquivo " + outputFileName + ": Placa " + ocrResultVars["placa"]) return ocrResultVars OCRUtils.getLogger(__name__).info("Arquivo de saída *" + str(outputFileName) + "* não encontrado.") return None
def get(self, codigoLeituraOCR): if len(codigoLeituraOCR) <= 15: #o tamanho do código é maior que 15. return Response(status=422) try: ocrResultVars = self.vparManager.getOCRResult(codigoLeituraOCR) if ocrResultVars == None: return Response(status=400) if ocrResultVars["placa"] == "0": return Response(status=404) return jsonify( placa=ocrResultVars["placa"], tempoProcessamento=ocrResultVars["tempoProcessamento"], confiancaGlobal=ocrResultVars["confiancaGlobal"], alturaMediaChar=ocrResultVars["alturaMediaChar"], placaRetEsq=ocrResultVars["placaRetEsq"], placaRetTopo=ocrResultVars["placaRetTopo"], placaRetDir=ocrResultVars["placaRetDir"], placaRetBase=ocrResultVars["placaRetBase"]) except: OCRUtils.getLogger(__name__).debug( '*** Exceção no método GET de ABOCRServices ***') return Response(status=500)
# -*- coding: utf-8 -*- ''' Created on 01 de dez de 2017 @author: Rodrigo Nogueira ''' from services import OCRUtils, AppConfig from services.StartOCR import main, ABOCRServicesAPP as application main() application.config.from_object(AppConfig.DevConfig) OCRUtils.setup_logging("Gunicorn")
def welcome(): OCRUtils.getLogger(__name__).debug('Welcome Sir!') return "Serviços de OCR de imagens."
@ABOCRServicesAPP.route("/") def welcome(): OCRUtils.getLogger(__name__).debug('Welcome Sir!') return "Serviços de OCR de imagens." def main(): OCRUtils.application = ABOCRServicesAPP ABOCRServicesAPP.config.from_object(DevConfig) #api resource routing _api.add_resource(ABOCRServices, '/ocr/imagens', '/ocr/leituras/<codigoLeituraOCR>') #Se este arquivo é executado, roda o servidor do Flask. Para rodar no Gunicorn, basta #iniciar o arquivo correspondente com o ABServicesAPP.run executando no contexto do Gunicorn. if __name__ == "__main__": main() OCRUtils.setup_logging("Flask") # Host e porta do servidor SERVER_HOST = '0.0.0.0' SERVER_PORT = ABOCRServicesAPP.config.get("SERVER_PORT") DEBUG = ABOCRServicesAPP.config.get("APP_LOG_LEVEL") == logging.DEBUG ABOCRServicesAPP.run(debug=DEBUG, threaded=True, host=SERVER_HOST, port=SERVER_PORT)