def __getitem__(self, idx): #pdb.set_trace() n_his = self.args.n_his frame_offset = self.args.frame_offset idx_video, idx_frame = self.valid_idx[idx][0], self.valid_idx[idx][1] objs = [] attrs = [] hws = [] for i in range(idx_frame - n_his * frame_offset, idx_frame + frame_offset + 1, frame_offset): frame = self.metadata[idx_video]['frames'][i] #frame_filename = frame['frame_filename'] frame_filename = os.path.join( 'video_' + str(idx_video).zfill(5), str(frame['frame_index'] + 1) + '.png') #pdb.set_trace() objects = frame['objects'] n_objects = len(objects) sub_id = idx_video // 1000 full_img_dir = os.path.join( self.data_dir, 'image_' + str(sub_id * 1000).zfill(5) + '-' + str( (sub_id + 1) * 1000).zfill(5)) img = self.loader(os.path.join(full_img_dir, frame_filename)) img = np.array(img)[:, :, ::-1].copy() img = cv2.resize(img, (self.W, self.H), interpolation=cv2.INTER_AREA).astype( np.float) / 255. ### prepare object inputs object_inputs = [] for j in range(n_objects): material = objects[j]['material'] shape = objects[j]['shape'] if i == idx_frame - n_his * frame_offset: attrs.append( encode_attr(material, shape, self.bbox_size, self.args.attr_dim)) mask_raw = decode(objects[j]['mask']) mask = cv2.resize(mask_raw, (self.W, self.H), interpolation=cv2.INTER_NEAREST) # cv2.imshow("mask", mask * 255) # cv2.waitKey(0) #bbox, pos, box_hw = convert_mask_to_bbox_hw(mask_raw, self.H, self.W, self.bbox_size, objects[j]['mask']) bbox, pos = convert_mask_to_bbox(mask_raw, self.H, self.W, self.bbox_size) pos_mean = torch.FloatTensor( np.array([self.H / 2., self.W / 2.])) pos_mean = pos_mean.unsqueeze(1).unsqueeze(1) pos_std = pos_mean pos = normalize(pos, pos_mean, pos_std) mask_crop = normalize(crop(mask, bbox, self.H, self.W), 0.5, 1).unsqueeze(0) img_crop = normalize(crop(img, bbox, self.H, self.W), 0.5, 0.5).permute(2, 0, 1) if self.args.box_only_flag: bbx_xyxy, ret, crop_box, crop_box_v2 = decode_mask_to_box( objects[j]['mask'], [self.bbox_size, self.bbox_size], self.H, self.W) ret_mean = torch.FloatTensor( np.array([1 / 2., 1 / 2., 1 / 2., 1 / 2.])) ret_mean = ret_mean.unsqueeze(1).unsqueeze(1) ret_std = ret_mean ret = normalize(ret, ret_mean, ret_std) pos = ret[:2] hw = ret[2:] elif self.args.add_hw_state_flag: bbx_xyxy, ret, crop_box, crop_box_v2 = decode_mask_to_box( objects[j]['mask'], [self.bbox_size, self.bbox_size], self.H, self.W) ret_mean = torch.FloatTensor( np.array([1 / 2., 1 / 2., 1 / 2., 1 / 2.])) ret_mean = ret_mean.unsqueeze(1).unsqueeze(1) ret_std = ret_mean ret = normalize(ret, ret_mean, ret_std) hw = ret[2:] elif self.args.add_xyhw_state_flag: bbx_xyxy, ret, crop_box, crop_box_v2 = decode_mask_to_box( objects[j]['mask'], [self.bbox_size, self.bbox_size], self.H, self.W) ret_mean = torch.FloatTensor( np.array([1 / 2., 1 / 2., 1 / 2., 1 / 2.])) ret_mean = ret_mean.unsqueeze(1).unsqueeze(1) ret_std = ret_mean ret = normalize(ret, ret_mean, ret_std) pos = ret[:2] hw = ret[2:] identifier = get_identifier(objects[j]) if self.args.box_only_flag: s = torch.cat([pos, hw], 0).unsqueeze(0), identifier elif self.args.add_hw_state_flag or self.args.add_xyhw_state_flag: s = torch.cat([mask_crop, pos, img_crop, hw], 0).unsqueeze(0), identifier elif self.args.rm_mask_state_flag: s = torch.cat([mask_crop * 0, pos, img_crop], 0).unsqueeze(0), identifier else: s = torch.cat([mask_crop, pos, img_crop], 0).unsqueeze(0), identifier object_inputs.append(s) objs.append(object_inputs) attr = torch.cat(attrs, 0).view(n_objects, self.args.attr_dim, self.bbox_size, self.bbox_size) feats = [] for x in range(n_objects): feats.append(objs[0][x][0]) for i in range(1, len(objs)): for x in range(n_objects): for y in range(n_objects): id_x = objs[0][x][1] id_y = objs[i][y][1] if check_same_identifier(id_x, id_y): feats[x] = torch.cat([feats[x], objs[i][y][0]], 1) try: feats = torch.cat(feats, 0) except: print(idx_video, idx_frame) # print("feats shape", feats.size()) ### prepare relation attributes n_relations = n_objects * n_objects Ra = torch.FloatTensor( np.ones((n_relations, self.args.relation_dim * (self.args.n_his + 2), self.bbox_size, self.bbox_size)) * -0.5) # change to relative position relation_dim = self.args.relation_dim state_dim = self.args.state_dim if self.args.box_only_flag: for i in range(n_objects): for j in range(n_objects): idx = i * n_objects + j Ra[idx, 1::relation_dim] = feats[i, 0::state_dim] - feats[ j, 0::state_dim] # x Ra[idx, 2::relation_dim] = feats[i, 1::state_dim] - feats[ j, 1::state_dim] # y else: for i in range(n_objects): for j in range(n_objects): idx = i * n_objects + j Ra[idx, 1::relation_dim] = feats[i, 1::state_dim] - feats[ j, 1::state_dim] # x Ra[idx, 2::relation_dim] = feats[i, 2::state_dim] - feats[ j, 2::state_dim] # y # add collision attr gt = self.metadata[idx_video]['ground_truth'] gt_ids = gt['objects'] gt_collisions = gt['collisions'] label_rel = torch.FloatTensor( np.ones((n_objects * n_objects, 1)) * -0.5) if self.args.edge_superv: for i in range(idx_frame - n_his * frame_offset, idx_frame + frame_offset + 1, frame_offset): for j in range(len(gt_collisions)): frame_id = gt_collisions[j]['frame'] if 0 <= frame_id - i < self.args.frame_offset: id_0 = gt_collisions[j]['object'][0] id_1 = gt_collisions[j]['object'][1] for k in range(len(gt_ids)): if id_0 == gt_ids[k]['id']: id_x = get_identifier(gt_ids[k]) if id_1 == gt_ids[k]['id']: id_y = get_identifier(gt_ids[k]) # id_0 = get_identifier(gt_ids[gt_collisions[j]['object'][0]]) # id_1 = get_identifier(gt_ids[gt_collisions[j]['object'][1]]) for k in range(n_objects): if check_same_identifier(objs[0][k][1], id_x): x = k if check_same_identifier(objs[0][k][1], id_y): y = k idx_rel_xy = x * n_objects + y idx_rel_yx = y * n_objects + x # print(x, y, n_objects) idx = i - (idx_frame - n_his * frame_offset) idx /= frame_offset Ra[idx_rel_xy, int(idx) * relation_dim] = 0.5 Ra[idx_rel_yx, int(idx) * relation_dim] = 0.5 if i == idx_frame + frame_offset: label_rel[idx_rel_xy] = 1 label_rel[idx_rel_yx] = 1 ''' print(feats[0, -state_dim]) print(feats[0, -state_dim+1]) print(feats[0, -state_dim+2]) print(feats[0, -state_dim+3]) print(feats[0, -state_dim+4]) ''' ''' ### change absolute pos to relative pos feats[:, state_dim+1::state_dim] = \ feats[:, state_dim+1::state_dim] - feats[:, 1:-state_dim:state_dim] # x feats[:, state_dim+2::state_dim] = \ feats[:, state_dim+2::state_dim] - feats[:, 2:-state_dim:state_dim] # y feats[:, 1] = 0 feats[:, 2] = 0 ''' x = feats[:, :-state_dim] label_obj = feats[:, -state_dim:] if self.args.box_only_flag: label_obj[:, 1] -= feats[:, -2 * state_dim + 1] label_obj[:, 2] -= feats[:, -2 * state_dim + 2] label_obj[:, 0] -= feats[:, -2 * state_dim + 0] label_obj[:, 3] -= feats[:, -2 * state_dim + 3] else: label_obj[:, 1] -= feats[:, -2 * state_dim + 1] label_obj[:, 2] -= feats[:, -2 * state_dim + 2] rel = prepare_relations(n_objects) rel.append(Ra[:, :-relation_dim]) ''' print(rel[-1][0, 0]) print(rel[-1][0, 1]) print(rel[-1][0, 2]) print(rel[-1][2, 3]) print(rel[-1][2, 4]) print(rel[-1][2, 5]) ''' # print("attr shape", attr.size()) # print("x shape", x.size()) # print("label_obj shape", label_obj.size()) # print("label_rel shape", label_rel.size()) ''' for i in range(n_objects): print(objs[0][i][1]) print(label_obj[i, 1]) time.sleep(10) ''' return attr, x, rel, label_obj, label_rel
def gen_valid_idx(self): print("Preprocessing valid idx ...") self.n_valid_idx = 0 self.valid_idx = [] self.metadata = [] fout = open(self.valid_idx_lst, 'w') n_his = self.args.n_his frame_offset = self.args.frame_offset for i in range(self.st_idx, self.st_idx + self.n_rollout): if i % 500 == 0: print("Preprocessing valid idx %d/%d" % (i, self.st_idx + self.n_rollout)) with open(os.path.join(self.label_dir, 'sim_%05d.json' % i)) as f: #with open(os.path.join(self.label_dir, 'annotation_%05d.json' % i)) as f: data = json.load(f) self.metadata.append(data) #pdb.set_trace() gt = data['ground_truth'] gt_ids = gt['objects'] gt_collisions = gt['collisions'] for j in range(n_his * frame_offset, len(data['frames']) - frame_offset): objects = data['frames'][j]['objects'] n_object_cur = len(objects) identifiers_cur = get_identifiers(objects) valid = True # check whether the current frame is valid: if check_duplicate_identifier(objects): valid = False ''' masks = get_masks(objects) if not check_valid_masks(masks): valid = False ''' # check whether history window is valid for k in range(n_his): idx = j - (k + 1) * frame_offset objects = data['frames'][idx]['objects'] n_object = len(objects) identifiers = get_identifiers(objects) # masks = get_masks(objects) if (not valid) or n_object != n_object_cur: valid = False break if not check_same_identifiers(identifiers, identifiers_cur): valid = False break if check_duplicate_identifier(objects): valid = False break ''' if not check_valid_masks(masks): valid = False break ''' # check whether the target is valid idx = j + frame_offset objects_nxt = data['frames'][idx]['objects'] n_object_nxt = len(objects_nxt) identifiers_nxt = get_identifiers(objects_nxt) if n_object_nxt != n_object_cur: valid = False elif not check_same_identifiers(identifiers_nxt, identifiers_cur): valid = False elif check_duplicate_identifier(objects_nxt): valid = False # check if detected the right objects for collision for k in range(len(gt_collisions)): if 0 <= gt_collisions[k]['frame'] - j < frame_offset: gt_obj = gt_collisions[k]['object'] id_0 = gt_obj[0] id_1 = gt_obj[1] for t in range(len(gt_ids)): if id_0 == gt_ids[t]['id']: id_x = get_identifier(gt_ids[t]) if id_1 == gt_ids[t]['id']: id_y = get_identifier(gt_ids[t]) # id_0 = get_identifier(gt_ids[gt_obj[0]]) # id_1 = get_identifier(gt_ids[gt_obj[1]]) if not check_contain_id(id_x, identifiers_cur): valid = False if not check_contain_id(id_y, identifiers_cur): valid = False ''' masks_nxt = get_masks(objects_nxt) if not check_valid_masks(masks_nxt): valid = False ''' if valid: self.valid_idx.append((i - self.st_idx, j)) fout.write('%d %d\n' % (i - self.st_idx, j)) self.n_valid_idx += 1 fout.close()
for i in range(0, len(data['frames']), frame_offset): objects = data['frames'][i]['objects'] frame_filename = data['frames'][i]['frame_filename'] n_objects = len(objects) img = cv2.imread(os.path.join(args.data_dir, frame_filename)) img = cv2.resize(img, (args.W, args.H), interpolation=cv2.INTER_AREA) / 255. frame_objs = [] frame_rels = [] ids_cur_frame = [] for j in range(n_objects): id = get_identifier(objects[j]) if check_contain_id(id, ids_filter) and not check_contain_id( id, ids_cur_frame): ids_cur_frame.append(id) material = objects[j]['material'] shape = objects[j]['shape'] attr = encode_attr(material, shape, bbox_size, args.attr_dim) mask_raw = decode(objects[j]['mask']) mask = cv2.resize(mask_raw, (W, H), interpolation=cv2.INTER_NEAREST) # cv2.imshow('mask', mask * 255) # cv2.waitKey(0) bbox, pos = convert_mask_to_bbox(mask_raw, H, W, bbox_size)
def create_order(): params = ['orderedItem', 'acceptedOffer', 'customer', 'broker'] variables, erroring_params, error = utils.request_variables(params) if error: if not variables['orderedItem']: return utils.error_response("incomplete_event_details") if not variables['acceptedOffer']: return utils.error_response("incomplete_offer_details") if not variables['customer']: return utils.error_response("incomplete_customer_details") if not variables['acceptedOffer']: return utils.error_response("incomplete_broker_details") else: event_id = utils.get_identifier( variables['orderedItem'][0]['orderedItem']['id']) offer_id = utils.get_identifier(variables['acceptedOffer'][0]['id']) utils.clean_expired_leases(event_id) event_data, event_error = models.Event(event_id).get() offer_data, offer_error = models.Offer(offer_id).get() if not event_error and not offer_error: quantity_of_order = int( variables['orderedItem'][0]['orderQuantity']) if utils.get_identifier( offer_data['itemOffered']['id']) != utils.get_identifier( event_data['id']): #EVENT DOES NOT MATCH OFFER return utils.error_response("offer_not_valid") if event_data['remainingAttendeeCapacity'] == 0: #EVENT IS FULL return utils.error_response("event_is_full") if event_data['remainingAttendeeCapacity'] < quantity_of_order: #EVENT HAS INSUFFICIENT SPACES return utils.error_response("event_has_insufficient_spaces") if utils.is_date_in_past( utils.from_datestring(event_data['startDate'])): # EVENT IS IN THE PAST return utils.error_response("unavailable_event") if utils.is_date_in_past( utils.from_datestring(offer_data['validThrough'])): # OFFER VALID THROUGH IS IN THE PAST return utils.error_response("offer_expired") if not utils.is_date_in_past( utils.from_datestring(offer_data['validFrom'])): # OFFER VALID FROM IS NOT YET IN THE PAST return utils.error_response("offer_not_yet_valid") order = models.Order() variables['orderDate'] = datetime.now() variables['paymentDueDate'] = utils.add_time( datetime.now(), 15, 'M') value_of_order = offer_data['price'] * quantity_of_order currency_of_offer = offer_data['priceCurrency'] variables['partOfInvoice'] = { "type": "Invoice", "paymentStatus": "https://schema.org/PaymentDue", "totalPaymentDue": { "type": "MonetaryAmount", "value": value_of_order, "currency": currency_of_offer } } order.create(variables) order_data, errors = order.get() order_id = order_data['identifier'] order_summary = { 'leaseExpiresAt': order_data['paymentDueDate'], 'places': quantity_of_order } event_data['remainingAttendeeCapacity'] = event_data[ 'remainingAttendeeCapacity'] - quantity_of_order event_data['orderLeases'][str(order_id)] = order_summary event = models.Event(event_id) event.update(event_data) return utils.json_response( order.as_json_ld(), created=True, created_key=order.as_json_ld()['id'].replace('$HOST$', '')) else: if event_error == 'resource_not_found': # EVENT NOT FOUND return utils.error_response("unavailable_event") if offer_error == 'resource_not_found': # OFFER NOT FOUND return utils.error_response("unavailable_offer")
def update_order(order_id): order_data, error = models.Order(order_id).get() if error: return utils.error_response(error) else: event_id = utils.get_identifier( order_data['orderedItem'][0]['orderedItem']['id']) params = ['payments', 'orderedItem'] variables, erroring_params, error = utils.request_variables(params) if params == erroring_params: return utils.error_response('insufficient_information') if [param for param in variables] == params: return utils.error_response('too_much_information') if 'payments' in variables and variables['payments'] is not None: # PAYMENT FLOW if utils.is_date_in_past( utils.from_datestring(order_data['paymentDueDate'])): # LEASE HAS EXPIRED # TODO delete the expired lease and release the places #utils.clean_expired_leases(event_id) return utils.error_response("anonymous_lease_expired") if variables['payments'][0]['totalPaidToProvider'][ 'value'] != order_data['partOfInvoice']['totalPaymentDue'][ 'value']: return utils.error_response("payment_amount_incorrect") if variables['payments'][0]['totalPaidToProvider'][ 'currency'] != order_data['partOfInvoice'][ 'totalPaymentDue']['currency']: return utils.error_response("currency_incorrect") if order_data[ 'orderStatus'] != "https://schema.org/OrderPaymentDue": return utils.error_response("order_cannot_be_completed") order_data['payments'] = variables['payments'] order_data['orderStatus'] = 'https://schema.org/OrderDelivered' order_data['potentialAction'] = [{ "type": "CancelAction", "name": "Cancel", "target": { "type": "EntryPoint", "urlTemplate": "https://example.com/orders/{order_id}", "encodingType": "application/vnd.openactive.v1.0+json", "httpMethod": "PATCH" } }] order_data['orderedItem'][0][ 'orderItemStatus'] = 'https://schema.org/OrderDelivered' order_data['partOfInvoice'][ 'paymentStatus'] = 'https://schema.org/PaymentComplete' order_data['payments'][0]['confirmationNumber'] = 'C' + str( random.randint(0, 100000)) order = models.Order(order_id) order.update(order_data) event_data, error = models.Event(event_id).get() # Remove used lease from Event order_summary = event_data['orderLeases'][str(order_id)] del event_data['orderLeases'][str(order_id)] del order_summary['leaseExpiresAt'] # Add completed order to event order_summary['orderCompletedAt'] = datetime.now().strftime( "%Y-%m-%dT%H:%M:%SZ") event_data['completedOrders'][str(order_id)] = order_summary event = models.Event(event_id) event.update(event_data) return utils.json_response(order.as_json_ld()) else: # CANCELLATION FLOW offer_id = utils.get_identifier( order_data['acceptedOffer'][0]['id']) offer_data, error = models.Offer(offer_id).get() if order_data['orderStatus'] != "https://schema.org/OrderDelivered": return utils.error_response("order_cannot_be_cancelled") if offer_data['isCancellable'] == False: return utils.error_response("order_is_uncancellable") if offer_data['isCancellable'] == True and utils.is_date_in_past( utils.from_datestring( offer_data['cancellationValidUntil'])): return utils.error_response( "order_cancellation_window_expired") order_data['orderedItem'][0][ 'orderItemStatus'] = 'https://schema.org/OrderCancelled' order_data['orderStatus'] = 'https://schema.org/OrderCancelled' order = models.Order(order_id) order.update(order_data) quantity_of_order = int( order_data['orderedItem'][0]['orderQuantity']) event_data, error = models.Event(event_id).get() event_data['remainingAttendeeCapacity'] = event_data[ 'remainingAttendeeCapacity'] + quantity_of_order del event_data['completedOrders'][str(order_id)] event = models.Event(event_id) event.update(event_data) return utils.json_response(order.as_json_ld())
def main(): """User interface for the Password manager""" utils.print_splash() # Menu that the user will see menu = '\nWhat would you like to do?\n\n' \ '1. Add Credentials\n' \ '2. Get Credentials\n' \ '3. Update Credentials\n' \ '4. Remove Credentials\n' \ '5. Quit\n\n' \ 'Please enter the integer of your choice: ' # user options options = ['1', '2', '3', '4', '5'] user_option = 0 try: datastore = 'storage.enc' # if there is no existing file get a new password for authentication if os.path.isfile('.eta'): # Get the password from the user utils.authenticate() else: print 'Welcome to ETA Password Manager!' if os.path.isfile(datastore): answer = raw_input('A prior instance of the password manager has been found.\n' \ 'If you are creating a new account, the previous data from the password manager will be overwritten.\n' \ 'Are you sure you would like to overwrite the previous data? [y/n] ').strip().lower() if answer == 'y': os.remove(datastore) else: raise authenticationError("[ERROR] Failed to authenticate. " \ "Previous data detected, and .eta file missing.") print 'Please enter a password you want to use to access your account. ' utils.new_passwd(utils.get_passwd()) # add init here encryptedFileEditor.init() path = os.getcwd() # Run until the user quits the program while user_option != '5': # Get the Users input option, input must be a valid integer 1, 2, 3, or 4 user_option = raw_input(menu) # User input must be valid to continue while user_option not in options: print '[ERROR] Improper input, please enter a valid integer (1 - 5).' user_option = raw_input(menu) # Perform the action based on the users choice if user_option == '1': print '********Add Credentials********' # add uacc object identifier = utils.get_identifier() username = utils.get_username() passwd = utils.get_passwd() user_account = UACC(identifier,username, passwd) print "\n" # Add the credentials using the given identifier, username, password combo useroptions.add_creds(user_account) # Remove sensitive information from memory del user_account del identifier del username del passwd # cause a pause after running. raw_input("Press enter to continue...") elif user_option == '2': print '********Get Credentials********' # Use the identifier to get the username and password for the authenticated user identifier = utils.get_identifier() # Call the get_creds function with the given identifier useroptions.get_creds(identifier) del identifier raw_input("Press enter to continue...") elif user_option == '3': print '********Update Credentials********' # Use the identifier and update the username and password for that identifier identifier = utils.get_identifier() # call update_creds function useroptions.update_creds(identifier) del identifier raw_input("Press enter to continue...") elif user_option == '4': print '********Remove Credentials********' # Use the identifier and remove the credentials from the datastore identifier = utils.get_identifier() # Call remove_creds function useroptions.remove_creds(identifier) del identifier raw_input("Press enter to continue...") except authenticationError, autherr: print autherr.message