def main(photo_file): """Run a text detection request on a single image""" access_token = os.environ.get('VISION_API') service = Service('vision', 'v1', access_token=access_token) with open(photo_file, 'rb') as image: base64_image = encode_image(image) body = { 'requests': [{ 'image': { 'content': base64_image, }, 'features': [{ 'type': 'TEXT_DETECTION', 'maxResults': 1, }] }] } response = service.execute(body=body) text = response['responses'][0]['textAnnotations'][0]['description'] #print('Found text: {}'.format(text)) print('Found text:') a = text.split("\n") b = re.sub(' +|\-|\.', '', a[len(a) - 2]) #c = re.sub('-', '', b) print b
def vision_from_file(image_name, photo_file): print("Sending to Google Vision from file...") access_token = keyring.get_password("system", "VISION_API_KEY") service = Service('vision', 'v1', access_token=access_token) with open(photo_file, 'rb') as image: base64_image = encode_image(image) body = { 'requests': [{ 'image': { 'content': base64_image, }, 'features': [ { 'type': 'LABEL_DETECTION', 'maxResults': 1000, } ] }] } response = service.execute(body=body) labels = response['responses'][0]['labelAnnotations'] for label in labels: print(label['description'] + ": " + str(label['score'])) # print(response) return create_json(labels, image_name)
def main(photo_file): """Run a text detection request on a single image""" print "Into Main Function" access_token = os.environ.get('VISION_API') print access_token service = Service('vision', 'v1', access_token=access_token) with open(photo_file, 'rb') as image: print "into with section" base64_image = encode_image(image) body = { 'requests': [{ 'image': { 'content': base64_image, }, 'features': [{ 'type': 'TEXT_DETECTION', 'maxResults': 1, }] }] } print "out" response = service.execute(body=body) print "After response" text = response['responses'][0]['textAnnotations'][0]['description'] print "after text" print('Found text: {}'.format(text))
def main(photo_file): """Run a text detection request on a single image""" access_token = os.environ.get('VISION_API') print(access_token) if access_token == 'None': print("Import VISION API KEY") service = Service('vision', 'v1', access_token=access_token) with open(photo_file, 'rb') as image: base64_image = encode_image(image) body = { 'requests': [{ 'image': { 'content': base64_image, }, 'features': [{ 'type': 'TEXT_DETECTION', 'maxResults': 1, }] }] } response = service.execute(body=body) print(response) if response['responses'][0]: text = response['responses'][0]['textAnnotations'][0]['description'] print('Found text: {}'.format(text)) else: text = " " file1=open("./text/pdf_to_text.txt","a") #file1.write(text,'\n') file1.write("{}\n".format(text)) file1.close() print("Text File modified")
def main(text_file): access_token = os.environ.get('LANGUAGE_API') service = Service('language', 'v1beta1', 'analyzeSentiment', access_token=access_token) with open(text_file, 'rb') as f: text = f.read().decode('utf-8') body = {"document": {"type": "PLAIN_TEXT", "content": text}} response = service.execute(body=body) print(response)
def vision_from_data(self, image_content): access_token = keyring.get_password("system", "VISION_API_KEY") service = Service('vision', 'v1', access_token=access_token) base64_image = base64.b64encode(image_content).decode() body = { 'requests': [{ 'image': { 'content': base64_image, }, 'features': [{ 'type': 'LABEL_DETECTION', 'maxResults': 1000, }] }] } response = service.execute(body=body) labels = response['responses'][0]['labelAnnotations'] for label in labels: print(label['description'] + ": " + str(label['score']))
def vision_from_data(image_name, image_content, request_type): print("Sending to Google Vision from Box...") access_token = keyring.get_password("system", "VISION_API_KEY") service = Service('vision', 'v1', access_token=access_token) base64_image = base64.b64encode(image_content).decode() body = { 'requests': [{ 'image': { 'content': base64_image, }, 'features': [ { 'type': request_type } ] }] } return service.execute(body=body)
def main(photo_file): """Run a face detection request on a single image""" access_token = os.environ.get('VISION_API') service = Service('vision', 'v1', access_token=access_token) with open(photo_file, 'rb') as image: base64_image = encode_image(image) body = { 'requests': [{ 'image': { 'content': base64_image, }, 'features': [{ 'type': 'FACE_DETECTION', 'maxResults': 1, }] }] } response = service.execute(body=body) faces = response['responses'][0]['faceAnnotations'] print('Found faces: {}'.format(faces))
def main(photo_file): """Run a label request on a single image""" access_token = os.environ.get('VISION_API') service = Service('vision', 'v1', access_token=access_token) with open(photo_file, 'rb') as image: base64_image = encode_image(image) body = { 'requests': [{ 'image': { 'content': base64_image, }, 'features': [{ 'type': 'LABEL_DETECTION', 'maxResults': 1, }] }] } response = service.execute(body=body) label = response['responses'][0]['labelAnnotations'][0]['description'] print('Found label: {}'.format(label))
def main(): if request.method == 'POST': image_path = request.form['image_path'] base64_image=base64.b64encode(rp.get(image_path).content) access_token = 'AIzaSyDMTvUK6Mlr_BWwwjJ3eFVxhGDKixlFgjQ' service = Service('vision', 'v1', access_token=access_token) body = { 'requests': [{ 'image': { 'content': base64_image, }, 'features': [{ 'type': 'TEXT_DETECTION', 'maxResults': 1, }] }] } response = service.execute(body=body) res = response['responses'][0]['textAnnotations'] json_data = json.dumps({'res':res[0]}) return json_data return None
def main(photo_file): """Run a text detection request on a single image""" access_token = 'AIzaSyAX6XocQFCDJK3EJcp4ZdfAx06hrYzhU-I' service = Service('vision', 'v1', access_token=access_token) with open(photo_file, 'rb') as image: base64_image = encode_image(image) body = { 'requests': [{ 'image': { 'content': base64_image, }, 'features': [{ 'type': 'TEXT_DETECTION', 'maxResults': 1, }] }] } response = service.execute(body=body) print response text = response['responses'][0]['textAnnotations'][0]['description'] print('Found text: {}'.format(text))
def get_text(image_file): """Run a text detection request on a single image""" access_token = google_vision_api_key if access_token == "None": print("set VISION API KEY in config.ini") sys.exit() service = Service("vision", "v1", access_token=access_token) with open(image_file, "rb") as image: base64_image = encode_image(image) body = { "requests": [{ "image": { "content": base64_image }, "features": [{ "type": "TEXT_DETECTION", "maxResults": 1 }], }] } response = service.execute(body=body) #print(response) if "error" in response: print(response["error"]["message"]) sys.exit() if response["responses"][0]: text = response["responses"][0]["textAnnotations"][0][ "description"] # print('Found text: {}'.format(text)) else: text = " " return text
async def test_service_class(): """Проверяем работоспособность класса Service.""" service = Service('docker') name = service.service_name assert name == 'docker' is_active = await service.is_active assert is_active in (True, False) await service.stop() stop_is_active = await service.is_active assert stop_is_active is False await service.start() start_is_active = await service.is_active assert start_is_active is True await service.restart() restart_is_active = await service.is_active assert restart_is_active is True
class ServiseView(web.View): """Оснавная view для работы с сервисом.""" service = Service('docker') @aiohttp_jinja2.template('index.html') async def get(self): flagged_status = { 'yes': True, 'no': False, } connect = await aioredis.create_redis_pool(('localhost', 6379)) is_flagged = flagged_status.get( await connect.get('is_flagged', encoding='utf-8'), False ) connect.close() await connect.wait_closed() context = { 'is_active': await self.service.is_active, 'service_name': self.service.service_name, 'is_flagged': is_flagged } return context async def post(self): functions = { 'start': self.service.start, 'stop': self.service.stop, 'restart': self.service.restart } try: data = await self.request.json() except JSONDecodeError: response = {'status': 'bad request'} status = 400 else: button = data.get('button') try: function = functions[button] except KeyError: response = {'status': 'bad request'} status = 400 else: await function() response = {'status': 'ok'} status = 200 return web.json_response(response, status=status)
from handlers import MainHandler from workers import GpsModuleWorker PORT = 12345 # replace this with random port when register will be working def make_app(coords): return Application([ (r'/', MainHandler, { "coords": coords }), (r'/healthcheck', HealthCheckHandler), ]) if __name__ == '__main__': os.environ['GPS_ROOT'] = os.getcwd() logging_config() logging.info("starting gps app") coords = Coords() #gps_worker = GpsModuleWorker(coords) methods = [ method for method in dir(coords) if callable(getattr(coords, method)) and method[0] != '_' ] service = Service('192.168.1.22', PORT, 'GPS', methods) service.register_interface() app = make_app(coords) app.listen(PORT) IOLoop.current().start()
def main1(): directory = askopenfile() filename = directory.name W = 500 H = 600 tex.delete(1.0, END) tex.insert(tk.END, filename) if (".png" in filename or ".jpg" in filename or ".pdf" in filename): if (".pdf" in filename): images = convert_from_path(filename) im1 = images[0] im1.save('jpg1.jpg') img = cv2.imread('jpg1.jpg') filename = 'jpg1.jpg' else: img = cv2.imread(filename) im1 = cv2.resize(img, (W, H)) im1 = cv2.cvtColor(im1, cv2.COLOR_BGR2RGB) im1 = Image.fromarray(im1) im1 = ImageTk.PhotoImage(im1) panelA = Label(image=im1) panelA.image = im1 panelA.place(x=10, y=50) canvas = tk.Canvas(window, width=W, height=H) canvas.create_image(0, 0, image=im1, anchor=NW) canvas.place(x=10, y=50) """Run a text detection request on a single image""" # os.environ['VISION_API'] = 'F:/Mycompleted task/OCR/vendornam/key.json' access_token = 'AIzaSyDMTvUK6Mlr_BWwwjJ3eFVxhGDKixlFgjQ' service = Service('vision', 'v1', access_token=access_token) with open(filename, 'rb') as image: base64_image = encode_image(image) body = { 'requests': [{ 'image': { 'content': base64_image, }, 'features': [{ 'type': 'TEXT_DETECTION', 'maxResults': 1, }] }] } response = service.execute(body=body) res = response['responses'][0]['textAnnotations'] invoice_num = find_invoice_number(res) invoice_dat = invoice_date(res) invoice_total = Totla_amount(res) invoice_name, index = vendorname(res) invoice_address = vendor_address(res, invoice_name, index) items = item_find(res) outtext.delete(1.0, END) result = "Vendor name: " + invoice_name + '\n' result += "Vendor address: " + invoice_address + '\n' result += "Invoice number: " + invoice_num + '\n' result += "Invoice Date: " + invoice_dat + '\n' if (len(items) != 0): for t in range(len(items)): result += "item" + str(t + 1) + ": " + items[t] + '\n' result += "Total Amount: " + invoice_total + '\n' outtext.insert(tk.END, result) print("invoice_num: " + invoice_num) print("invoice_date: " + invoice_dat) print("invoice_total: " + invoice_total) print("invoice_name: " + invoice_name) print("invoice_address: " + invoice_address) if os.path.exists('jpg1.jpg'): os.remove("jpg1.jpg")