def generate_output(): # Load JSON f = open('competition/test_cases.json') test_cases = json.load(f) myCashier = Cashier() userID = '5ea023be-b530-4816-8eda-5340cfabe9b0' output_paths = [] for test_db in test_cases: print(test_db['name']) dbName = test_db['name'] dbId = test_db['uuid'] receipts = myCashier.process(dbName) # Generate output file path = "outputs/output-{}.json".format(dbName) output_paths.append(path) output_json(dbId, userID, receipts, path=path) return output_paths
def evaluate_intenvory(dbs, gt_path): # Load JSON groundtruth with open(gt_path) as f: gt_data = json.load(f) gt_list = gt_data['lists'] if not DEBUG: remove_putback_products(gt_list) # with open('gt_final.json', 'w') as outfile: # json.dump(gt_data, outfile) # Metrics tp, fp, tn, fn = 0, 0, 0, 0 overall_num_preds, overall_num_gt = 0, 0 for i in range(len(dbs)): print("\n\nEvaluating database: ", dbs[i]) # Metrics per database db_tp, db_fp, db_tn, db_fn = 0, 0, 0, 0 db_pred_counts, db_gt_counts = 0, 0 ########## Generate Prediction ########## dbName = dbs[i] myCashier = Cashier() receipts = myCashier.process(dbName) ########## Evaluate Ground truth ########## gt_entry = gt_list[i] # Find groundtruth entry for this database tmp_i = 0 while (gt_entry['dataset'] != dbName): tmp_i += 1 gt_entry = gt_list[tmp_i] assert (tmp_i < len(gt_list)) assert (gt_entry['dataset'] == dbName) event_list = gt_entry['events'] for event in event_list: gt_products = event['observation']['products'] gt_customerID = event['observation']['target_id'] # Find the corresponding customer receipts if (gt_customerID in receipts): # Find the corresponding products on the receipt customer_receipt = receipts[gt_customerID] for gt_product in gt_products: gt_productID = gt_product['id'] for productID, entry in customer_receipt.purchaseList.items( ): if (gt_productID == productID): product, quantity = entry if (quantity > 0): db_tp += 1 customer_receipt.purchaseList[productID] = ( product, quantity - 1 ) # entry = (product, quantity) db_gt_counts += 1 else: # No such customer for gt_product in gt_products: db_gt_counts += 1 # Print False Items num_receipt = 0 for id, customer_receipt in receipts.items(): if VERBOSE: print("============== False Receipt {} ==============".format( num_receipt)) print("Customer ID: " + id) print("Purchase List: ") for _, entry in customer_receipt.purchaseList.items(): product, quantity = entry if quantity != 0: if VERBOSE: print( "*Name: " + product.name + ", Quantities: " + str(quantity), product.thumbnail) db_fp += quantity num_receipt += 1 db_fn = db_gt_counts - db_tp db_pred_counts = db_tp + db_fp overall_num_preds += db_pred_counts overall_num_gt += db_gt_counts tp += db_tp fp += db_fp fn += db_fn # Display DB Evaluation print( "Database: {}, Correct Items on Receipts: {}/{}, Total GT items: {}" .format(dbName, db_tp, db_pred_counts, db_gt_counts)) print("\n================== Evaluation Summary ==================") print("Databases: ", dbs) print("Ground truth version: ", gt_path) precision = 0.0 if tp + fp == 0 else tp * 100.0 / (tp + fp) print("Overall precision is: {:.1f}%".format(precision)) recall = 0.0 if tp + fn == 0 else tp * 100.0 / (tp + fn) print("Overall recall is: {:.1f}%".format(recall)) if (precision + recall == 0): f1 = 0 else: f1 = 2 * precision * recall / (precision + recall) print("Overall F1 is: {:.1f}%".format(f1))