def imagesAnnotate(file): url = "https://vision.googleapis.com/v1/images:annotate?key=%s" % GOOGLE_API_KEY payload = json.dumps({ "requests": [{ "image": { "content": (base64.b64encode(file.read())).decode('UTF-8') }, "features": [{ "type": "DOCUMENT_TEXT_DETECTION" }], "imageContext": { "languageHints": ["en-t-i0-handwrit"] } }] }) (status, response) = post(url, payload, verbose=False) if status == requests.codes.ok: if response["responses"][0] == {}: return {"Text": "Unable to retrieve any information from the file"} #return response["responses"][0]["textAnnotations"][0]["description"] text = lsgapp.mergeNearByWords(response["responses"][0]) rcpt = receipt.Receipt(parser_config, text) #print(rcpt.date) return { "Text": billText(rcpt), "Company": rcpt.company, "InvoiceDate": rcpt.date, "InvoiceNo": rcpt.billno, "InvoiceAmount": rcpt.sum } else: return {"Text": "imageAnnotate error %s: %s" % (status, response)}
def read_in_receipt(file_name): f = open(file_name, 'r') reader = csv.reader(f) groc_list = {} for row in reader: new_groc = groc.Groc(filter_name(row[0]), filter_mass(row[1]), (row[2])) groc_list[new_groc.get_name()] = (new_groc.get_mass(), new_groc.get_price()) f.close() new_receipt = receipt.Receipt(groc_list) return new_receipt
def begin_session(self, user): """METHOD: The lifeblood of the ATM's functions. >> begin_session(self, user) PARAMETERS @user: The card to begin the session. RETURN None. """ assert isinstance(user, card.Card) try: match = self.insert_card(user) if not self.match_pin(match): raise SystemExit( "ERROR: Contact the administrator for assistance.\n") except SystemExit: raise except: match = None if match is not None: while True: try: choice = self.action_prompt() print() if choice == 1: output = self.withdraw(match) self._accounts.writeChanges("accounts.txt") elif choice == 2: output = self.deposit(match) self._accounts.writeChanges("accounts.txt") elif choice == 3: output = receipt.Receipt(match, globals.ACTIONS[2], None) elif choice == 4: pass # the user choose to quit break except ValueError as e: if str(e) == "ERROR: Insufficient funds.\n": print(str(e)) else: raise if choice != 4: print(output) else: sys.stderr.write("ERROR: Account not found.\n") print("Thank you for choosing this ATM!")
def deposit(self, acct): """METHOD: The deposit function. >> deposit(self, acct) PARAMETERS @acct: The account that has been matched. RETURN A Receipt object containing the ATM response. """ money = self.get_money(globals.ACTIONS[1]) acct.deposit(money) return receipt.Receipt(acct, globals.ACTIONS[1], money)
def withdraw(self, acct): """METHOD: The withdrawal function. >> withdraw(self, acct) PARAMETERS @acct: The account that has been matched. RETURN A Receipt object containing the ATM response. """ money = self.get_money(globals.ACTIONS[0]) acct.withdraw(money) return receipt.Receipt(acct, globals.ACTIONS[0], money)
def get_salary_employee(): emp_id = input('Enter the employee ID: ') first = input('Enter first name of the employee: ') last = input('Enter last name of the employee: ') salary = float(input('Enter the employees salary: ')) commission = float(input('Enter the employees commission rate(%): ')) dues = float(input('Enter the employees weekly dues: ')) employee_data = salariedemployee.SalariedEmployee(emp_id, first, last, salary, commission, dues) print('Enter the employees address.') street = input('Street Address: ') city = input('City: ') state = input('State: ') zipcode = input('Zip: ') address_data = address.Address(street, city, state, zipcode) print() print('Enter the employee\'s sales data.') date = input('Enter today\'s date (MM/DD/YY): ') sales = float(input('Please enter the employee\'s sales: ')) receipt_data = receipt.Receipt( date, sales) # setup for loop to calc sales receipts over month print() salary_pay = ( salary / YEAR_SALARY_HOURS ) * SALARY_HOURS # needs to be updated to account for 5 week months commission_pay = receipt_data.get_sales() * commission / COMMISSION_CONVERT deposit = salary_pay + commission_pay - employee_data.get_weekly_dues() print('Enter your bank account information.') bank = input('Enter your bank name: ') routing = input('Enter the routing number: ') account = input('Enter the account number: ') bank_data = bankaccount.BankAccount(bank, routing, account) print() print(str(employee_data.get_full_name())) print(str(address_data.get_address())) print('Depositing your monthly paycheck...') print('Depositing $', format(deposit, ',.2f'), 'in', str(bank_data.get_bank_name()), 'Account Number:', \ str(bank_data.get_account_id()), 'using Routing Number:', \ str(bank_data.get_routing_number()), sep=' ') print()
def filesAnnotate(file): url = "https://vision.googleapis.com/v1/files:annotate?key=%s" % GOOGLE_API_KEY payload = json.dumps({ "requests": [{ "inputConfig": { "content": (base64.b64encode(file.read())).decode('UTF-8'), "mimeType": "application/pdf" }, "features": [{ "type": "DOCUMENT_TEXT_DETECTION" }], "imageContext": { "languageHints": ["en-t-i0-handwrit"] } }] }) (status, response) = post(url, payload, verbose=False) if status == requests.codes.ok: if response["responses"][0] == {}: return {"Text": "Unable to retrieve any information from the file"} text = '' for page in response["responses"][0]["responses"]: text += page["fullTextAnnotation"]["text"] + "\n" #return text rcpt = receipt.Receipt(parser_config, text) #print(rcpt.date) return { "Text": text, "Company": rcpt.company, "InvoiceDate": rcpt.date, "InvoiceNo": rcpt.billno, "InvoiceAmount": rcpt.sum } else: return {"Text": "filesAnnotate error %s: %s" % (status, response)}
def save(self): for i in range(0, self.personnum): if not self.personname[0].get(): tkinter.messagebox.showinfo("이름 입력", "모든 사람의 이름을 입력해주세요.") break if not self.personmenu[i].get(): tkinter.messagebox.showinfo( "개인메뉴 가격 입력", "모든 개인메뉴 가격을 입력해주세요." "\n개임메뉴가격이 없는 경우 0을 입력해주세요") break if not self.groupmenu.get(): tkinter.messagebox.showinfo( "그룹메뉴 가격 입력", "그룹메뉴 가격을 입력해주세요." "\n그룹메뉴가격이 없는 경우 0을 입력해주세요") break else: self.ID = random.randint(1, 1000) self.now = datetime.datetime.utcnow() sqlCon = pymysql.connect(host="127.0.0.1", user="******", password="******", database="dutchdb", charset="utf8") cur = sqlCon.cursor(pymysql.cursors.DictCursor) cur.execute( "INSERT INTO dutchdb VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)", ( self.ID, self.now.strftime('%Y-%m-%d %H:%M:%S'), self.personnum, # 명수 self.personname[1 - 1].get(), # 이름1 self.personname[2 - 1].get(), # 이름2 self.personname[3 - 1].get(), # 이름3 self.personname[4 - 1].get(), # 이름4 self.personname[5 - 1].get(), # 이름5 self.personname[6 - 1].get(), # 이름6 self.personmenu[1 - 1].get(), # 메뉴1 self.personmenu[2 - 1].get(), # 메뉴2 self.personmenu[3 - 1].get(), # 메뉴3 self.personmenu[4 - 1].get(), # 메뉴4 self.personmenu[5 - 1].get(), # 메뉴5 self.personmenu[6 - 1].get(), # 메뉴6 self.groupmenu.get(), self.inputLocation.get(), # 장소 self.inputMenu.get(), # 그룹메뉴가격 self.RadioVariety_1.get())) sqlCon.commit() sqlCon.close() inputm = [ self.inputLocation.get(), self.inputMenu.get(), [ self.personname[1 - 1].get(), self.personmenu[1 - 1].get() ], [ self.personname[2 - 1].get(), self.personmenu[2 - 1].get() ], [ self.personname[3 - 1].get(), self.personmenu[3 - 1].get() ], [ self.personname[4 - 1].get(), self.personmenu[4 - 1].get() ], [ self.personname[5 - 1].get(), self.personmenu[5 - 1].get() ], [ self.personname[6 - 1].get(), self.personmenu[6 - 1].get() ], self.groupmenu.get(), self.RadioVariety_1.get() ] Move = receipt.Receipt(self.menu, inputm, self.personnum, self.ID) break
def receipt(self, prefix, receiptId, dateTime, sumA, sumB, sumC, sumD, sumE, sigSystem, dummy=False, reversal=False, override=dict()): algorithm = algorithms.ALGORITHMS[prefix] dateTimeStr = dateTime.strftime("%Y-%m-%dT%H:%M:%S") # replacing '.' with ',' because reference does it too, still weird sumAStr = ("%.2f" % sumA).replace('.', ',') sumBStr = ("%.2f" % sumB).replace('.', ',') sumCStr = ("%.2f" % sumC).replace('.', ',') sumDStr = ("%.2f" % sumD).replace('.', ',') sumEStr = ("%.2f" % sumE).replace('.', ',') regularRec = receipt.Receipt(sigSystem.zda, self.registerId, receiptId, dateTimeStr, sumAStr, sumBStr, sumCStr, sumDStr, sumEStr, '', sigSystem.serial, '') rec = MangledReceipt(regularRec, override) if 'turnoverCounterSize' in override: self.turnoverCounterSize = override['turnoverCounterSize'] encTurnoverCounter = None if dummy: encTurnoverCounter = b'TRA' if 'turnoverCounter' in override: self.turnoverCounter = int(override['turnoverCounter']) else: # TODO: check if counter can still be represented with # given size self.turnoverCounter += int( round((sumA + sumB + sumC + sumD + sumE) * 100)) if 'turnoverCounter' in override: self.turnoverCounter = int(override['turnoverCounter']) if reversal: encTurnoverCounter = b'STO' else: if not algorithm.verifyKey(self.key): raise Exception(_("Invalid key.")) encTurnoverCounter = algorithm.encryptTurnoverCounter( rec, self.turnoverCounter, self.key, self.turnoverCounterSize) encTurnoverCounter = base64.b64encode(encTurnoverCounter) encTurnoverCounter = encTurnoverCounter.decode("utf-8") if 'encTurnoverCounter' not in override: rec.encTurnoverCounter = encTurnoverCounter previousChain = algorithm.chain(rec, self.lastReceiptSig) if 'previousChain' not in override: rec.previousChain = base64.b64encode(previousChain).decode("utf-8") prefix = override.get('algorithmPrefix', prefix) jwsString = sigSystem.sign(rec.toPayloadString(prefix), algorithm) self.lastReceiptSig = jwsString header, payload, signature = jwsString.split('.') header = base64.urlsafe_b64decode( utils.restoreb64padding(header).encode('utf-8')).decode('utf-8') rec.sign(header, signature) if 'header' in override: rec.header = override['header'] if 'signature' in override: rec.signature = override['signature'] return rec
def main(args): fileNames = os.listdir(trainTextDir) fileNames = [i for i in fileNames if (i.endswith('.json'))] for fileName in fileNames: with open(os.path.join(trainTextDir, fileName)) as text_json: text_data = json.load(text_json) text_data = tx.filterGarbage(text_data) tx.calculateAngles(text_data) tx.calculateCenterPoints(text_data) text_lines = tx.createLines(text_data) with open( os.path.join(trainLabelsDir, fileName.split('_')[0] + '_labels.json')) as ground_truth_json: truth = json.load(ground_truth_json) truth = tx.removeSwedishLetters(truth) receipt = rc.Receipt(fileName, text_lines, truth) receipts.append(receipt) f = open('./data/test/test.txt', "r") for line in f: testFilePaths.append(line[:-1]) test_reciepts = [] for receipt in receipts: if receipt.path in testFilePaths: test_reciepts.append(receipt) if args[1] == 'plot_bert': d1 = pd.DataFrame( { 'train synthetic 10000': plot.train_10000_v2, 'validation synthetic 10000': plot.val_10000_v2 }, index=range(1, 31)) d2 = pd.DataFrame( { 'train synthetic 1000': plot.train_1000, 'validation synthetic 1000': plot.val_1000 }, index=range(1, 31)) d3 = pd.DataFrame( { 'train real data': plot.train, 'validation real data': plot.val }, index=range(1, 31)) data = pd.concat([d1, d2, d3], axis=1) sns.set_style("darkgrid") ax = sns.lineplot(data=data) ax.set(xlabel='epoch', ylabel='loss') plt.show() if args[1] == 'plot_lstm': f1 = open('/Users/markolazic/Desktop/sroie-task3/data/trainLoss.txt', 'r') f2 = open('/Users/markolazic/Desktop/sroie-task3/data/valLoss.txt', 'r') f3 = open( '/Users/markolazic/Desktop/sroie-task3/data/trainLoss1000.txt', 'r') f4 = open('/Users/markolazic/Desktop/sroie-task3/data/valLoss1000.txt', 'r') f5 = open( '/Users/markolazic/Desktop/sroie-task3/data/trainLoss10000.txt', 'r') f6 = open( '/Users/markolazic/Desktop/sroie-task3/data/valLoss10000.txt', 'r') f1Lines = f1.readlines() f2Lines = f2.readlines() f3Lines = f3.readlines() f4Lines = f4.readlines() f5Lines = f5.readlines() f6Lines = f6.readlines() train_loss = [] for line in f1Lines: train_loss.append(float(line[:-1])) val_loss = [] for line in f2Lines: val_loss.append(float(line[:-1])) train_loss1000 = [] for line in f3Lines: train_loss1000.append(float(line[:-1])) val_loss1000 = [] for line in f4Lines: val_loss1000.append(float(line[:-1])) train_loss10000 = [] for line in f5Lines: train_loss10000.append(float(line[:-1])) val_loss10000 = [] for line in f6Lines: val_loss10000.append(float(line[:-1])) d1 = pd.DataFrame( { 'train synthetic 10000': train_loss10000, 'validation synthetic 10000': val_loss10000 }, index=range(1, 2001)) d2 = pd.DataFrame( { 'train synthetic 1000': train_loss1000, 'validation synthetic 1000': val_loss1000 }, index=range(1, 2001)) d3 = pd.DataFrame( { 'train real data': train_loss, 'validation real data': val_loss }, index=range(1, 2001)) data = pd.concat([d1, d2, d3], axis=1) data = data.rolling(100).mean() sns.set_style("darkgrid") ax = sns.lineplot(data=data) ax.set(xlabel='epoch', ylabel='loss') plt.show() if args[1] == 'create_data_statistics': stats = util.create_data_statistics(receipts, 'vendor') for k, v in sorted(stats.items(), reverse=True, key=lambda item: item[1]): print(k, '---', v) if args[1] == 'generate_gcn_data': test_data_dict = {} train_data_dict = {} for i, receipt in enumerate(receipts): if receipt.path in testFilePaths: test_data_dict[i] = data_gen.generateWordClasses(receipt) else: train_data_dict[i] = data_gen.generateWordClasses( receipt, correcting=False) gcn.create(receipts, testFilePaths) if args[1] == 'create_result': path = './data/results/10000_synt' test_dict_path = os.path.join(path, 'res_dict.pth') res_dict = torch.load(test_dict_path) result = list(res_dict.items()) res_list = [] for i, (_, (labels, words)) in enumerate(result): res = extract(labels, words) res_list.append(res) calculateMetrics(test_reciepts, res_list, writeToFile=True, path=path) if args[1] == 'generate_word_data': generateSynthetic = False if args[2] and util.isInt(args[2]): generateSynthetic = True number = int(args[2]) train_data_dict = {} test_data_dict = {} for i, receipt in enumerate(receipts): if receipt.path in testFilePaths: test_data_dict[i] = data_gen.generateWordClasses(receipt) else: train_data_dict[i] = data_gen.generateWordClasses( receipt, correcting=False) train_receipts = [] for r in receipts: if r.path not in testFilePaths: train_receipts.append(r) if generateSynthetic: synthetic = generateSintheticData(train_receipts, number) for i, (words, labels) in enumerate(synthetic): train_data_dict[i + len(receipts)] = (words, labels) ''' vocab = data_gen.createVocabulary(receipts + synthetic) f=open('./data/synt_vocab.txt',"w+") for w in vocab: f.write(w + '\n') f.write('[UNK]' + '\n') f.write('[CLS]' + '\n') f.write('[SEP]' + '\n') f.write('[MASK]' + '\n') f.close() ''' torch.save(train_data_dict, "./data/synt_10000_train_data_dict.pth") torch.save(test_data_dict, "./data/synt_test_data_dict.pth") if args[1] == 'oracle': for i, receipt in enumerate(test_reciepts): _ = data_gen.generateWordClasses(receipt) oracle(test_reciepts) if args[1] == 'rule_based': for receipt in test_reciepts: predict(receipt) calculateRuleBasedAccuracy(test_reciepts) if args[1] == 'create_lstm_result': result_jsons = os.listdir(lstmResultDir) result_jsons = [i for i in result_jsons if (i.endswith('.json'))] result_jsons.sort(key=lambda r: int(r.split('.')[0])) results_dicts = [] for fileName in result_jsons: with open(os.path.join(lstmResultDir, fileName)) as text_json: text_data = json.load(text_json) results_dicts += [text_data] calculateLSTMaccuracy(test_reciepts, results_dicts) elif args[1] == 'create_char_data': generateSynthetic = True number = 10000 train_data_dict = {} test_data_dict = {} for i, receipt in enumerate(receipts): if receipt.path in testFilePaths: test_data_dict[i] = data_gen.generateCharClasses( receipt, includeProducts=True) else: train_data_dict[i] = data_gen.generateCharClasses( receipt, includeProducts=True) if generateSynthetic: VOCAB = ascii_uppercase + digits + punctuation + " \t\n" for r in receipts: data_gen.generateWordClasses(r) synthetic = generateSintheticData(receipts, number) for i, (words, labels) in enumerate(synthetic): t_new_words = '' t_new_labels = [] for w, l in zip(words, labels): t_new_words += w.upper() + ' ' t_new_labels += [ util.getClassInt(l) for i in range(len(w)) ] + [0] new_words = '' new_labels = [] for index in range(len(t_new_words)): if t_new_words[index] in VOCAB: new_words += t_new_words[index] new_labels.append(t_new_labels[index]) new_words = new_words[0:-1] new_labels = new_labels[0:-1] for i in range(1, len(new_words) - 1): if new_labels[i] == 0 and new_labels[i - 1] == new_labels[i + 1]: new_labels[i] = new_labels[i - 1] train_data_dict[len(receipts) + i] = (new_words, new_labels) print(train_data_dict) torch.save( train_data_dict, "/Users/markolazic/Desktop/sroie-task3/data/train_char_data_prod_synt10000.pth" ) torch.save( test_data_dict, "/Users/markolazic/Desktop/sroie-task3/data/test_char_data_prod_synt10000.pth" )
def to_receipt(self): Move = receipt.Receipt(self.menu)
#coding=utf-8 import receipt if __name__ == '__main__': DATA = { "merchid": 1484901323, "devid": "123", "amount": 1, "authcode": "286579303676489102", "orderinfo": "111" } APPID = "12345" APPKEY = '12345' GATEWAYURL = "http://api.test.shuwang.info/receipt/rest" receiptpay = receipt.Receipt(GATEWAYURL, APPID, APPKEY) response = receiptpay.scan_authcode(DATA)