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)
def welcome(): OCRUtils.getLogger(__name__).debug('Welcome Sir!') return "Serviços de OCR de imagens."