def ocr(): # Read the URL try: url = request.get_json()['image_url'] except TypeError: print("TypeError trying get_json(). Trying to load from string.") try: data = json.loads(request.data.decode('utf-8'), encoding='utf-8') url = data['img_url'] except: return jsonify( {"error": "Could not get 'image_url' from the request object. Use JSON?", "data": request.data} ) except: return jsonify( {"error": "Non-TypeError. Did you send {'image_url': 'http://.....'}", "data": request.data } ) # Process the image print("URL extracted:", url) try: output = process_image(url) except OSError: return jsonify({"error": "URL not recognized as image.", "url": url}) except: return jsonify( {"error": "Unknown processing image.", "request": request.data} ) app.logger.info(output) return jsonify({"output": output})
def ocr(): # Read the URL try: url = request.get_json()['image_url'] except TypeError: print("Erro ao efetuar conversão get_json(). Tentando carregar como scring.") try: data = json.loads(request.data.decode('utf-8'), encoding='utf-8') url = data['img_url'] except: return jsonify( {"error": "Não foi possível obter a propriedade 'image_url'.", "data": request.data} ) except: return jsonify( {"error": "Tipo errado tente fazer a requisição JSON assim: {'image_url': 'http://.....'}", "data": request.data } ) # Process the image print("URL:", url) try: output = process_image(url,"por") except OSError: return jsonify({"error": "OOPPSS não encontrei uma imagem na URL.", "url": url}) except: return jsonify( {"error": "Não entendi este tipo de imagem.", "request": request.data} ) app.logger.info(output) return jsonify({"output": output})
def ocr(): try: url = request.form.keys()[0] output = process_image(url) return jsonify({"output": output}) except: return jsonify( {"error": "ocr error with image, did you send the proper url?"})
def ocr(): try: url = request.json['img'] if ('jpg' in url or 'JPG' in url or 'png' in url): output = process_image(url) return jsonify({'status': 200, 'output': output}) else: return jsonify({'status': 400, 'error': 'File should be image'}) except: return jsonify({'status': 403, 'error': 'Forbidden'})
def ocr(): try: url = request.json['image_url'] if 'jpg' in url: output = process_image(url) return jsonify({"output": output}) else: return jsonify({"error": "only .jpg files, please"}) except: return jsonify( {"error": "Did you mean to send: {'image_url': 'some_jpeg_url'}"})
def ocr(): try: url = request.json['image_url'] print "url ", url output = process_image(url) print "output ::::",output return jsonify({"output": output}) except: return jsonify( {"error": "Did you mean to send: {'image_url': 'some_jpeg_url'}"} )
def ocr(): try: url = request.json['image_url'] if 'jpg' in url: output = process_image(url) return jsonify({"output": output}) else: return jsonify({"error": "only .jpg files, please"}) except: return jsonify( {"error": "Did you mean to send: {'image_url': 'some_jpeg_url'}"} )
def ocr(): print(request.json) try: output = process_image(request.json['image_list']) print(output) if output: return jsonify({"output": output}) else: return jsonify({"error": "error processing image" + output}) except: return jsonify( {"error": "Did you mean to send: {'image_list': ['img1.jpg', 'img2.jpg']}"} )
def ocr(): file = request.files['file'] if not file: resp = jsonify({'error': 'No File Provided'}) resp.status_code = 400 return resp try: lines = process_image(file).splitlines() resp = jsonify({'text': {i: l for i, l in enumerate(lines)}}) resp.status_code = 200 return resp except: resp = jsonify({'error': 'Unprocessable Entity'}) resp.status_code = 422 return resp
def ocr(): #supported_formats = ['BMP', 'PNM', 'PNG', 'JFIF', 'JPEG', 'TIFF'] try: url = request.json['image_url'] output = process_image(url) return jsonify({"captcha": output}) # if any( _format in url for _format in supported_formats): # output = process_image(url) # return jsonify({"captcha": output}) # else: # return jsonify({"error": "only "+ ' '.join(map(str,supported_formats)) + " files, please"}) except: return jsonify( {"error": "Did you mean to send: {'image_url': 'image_url'}"})
def ocr(): try: url = request.json['image_url'] if 'jpg' in url: output = process_image(url) return jsonify({"data": output}) else: return jsonify({ "data": { "error": "Unprocessable Entity. Only .jpg files.", "code": 422, "error": True } }) except Exception as inst: # return jsonify( # {"error": "Did you mean to send: {'image_url': 'some_jpeg_url'}"} # ) return jsonify({"error": str(inst)})
def ocr(): url = request.get_json() url = url["image_url"] output = process_image(url) return jsonify({"output": output})
import sys import requests import pytesseract from PIL import Image from io import BytesIO from ocr import get_image_from_file, process_image if __name__ == "__main__": """Tool to test the raw output of pytesseract with a given input URL""" sys.stdout.write(""" ===OOOO=====CCCCC===RRRRRR=====\n ==OO==OO===CC=======RR===RR====\n ==OO==OO===CC=======RR===RR====\n ==OO==OO===CC=======RRRRRR=====\n ==OO==OO===CC=======RR==RR=====\n ==OO==OO===CC=======RR== RR====\n ===OOOO=====CCCCC===RR====RR===\n\n """) sys.stdout.write("A simple OCR utility\n") filename = input( "What is the name of the image you would like to analyze?\n") image = get_image_from_file(filename) image = process_image(image) print(image) sys.stdout.write( "The raw output from tesseract with no processing is:\n\n") sys.stdout.write("-----------------BEGIN-----------------\n") sys.stdout.write(pytesseract.image_to_string(image, lang='eng') + "\n") sys.stdout.write("------------------END------------------\n")
def process(): image_url = request.args.get('image') res = process_image(image_url) return jsonify(list(res))