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 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 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""" 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
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")