def delete(self): transaction_id = str(uuid()) pk = int(request.args["id"]) label_id = int(request.args["label_id"]) filename = request.args["filename"] bus.push("file_deleted", transaction_id=transaction_id, id=pk, filename=filename, label_id=label_id) start = time() while True: if time() - start >= 20: log.debug( "Transaction closed by timeout, photo id={} not deleted". format(pk)) return {"success": False, "reason": "Timeout"}, 400 raw_msg = bus.client.lpop(transaction_id) if raw_msg: msg = bus._loads(raw_msg)[1] bus.client.delete(transaction_id) log.debug("new message: {}".format(msg)) if msg["deleted"]: return msg else: return msg, 400
def push_to_bus(bus): while True: if len(self.list_to_push) > 0: send_list = self.list_to_push.pop() bus.push('/pack_batches', [send_list]) else: time.sleep(0.01)
def alert_send(self, now, ts, camera_id, label, distance, l, t, r, b, filename, frame_number, frame): #log.info('sender dist {}'.format(distance)) if distance >= self.black_threshold: prev_alert = self.prev_alerts.get(label) if prev_alert is not None: if (ts - prev_alert) >= self.alert_inertia: self.prev_alerts.pop(label, None) else: self.prev_alerts[label] = now prev_alert = self.prev_alerts.get(label) if prev_alert is None: bus.push('/danger', ts, camera_id, label, distance, l, t, r, b, filename, frame_number, frame) self.prev_alerts[label] = now
def on_file_deleted(*args, **kwargs): pk = kwargs["id"] filename = kwargs["filename"] label_id = kwargs["label_id"] destination_topic = kwargs["transaction_id"] photos_dir = config.find('photos_dir') executor.execute_query(query, "delete_face_by_pk", pk=pk, commit=True) faces = executor.execute_query(query, 'select_faces', commit=True) labels_list = [row[2] for row in faces] features_list = [] for idx, row in enumerate(faces): file_path = os.path.join(photos_dir, row[1]) try: features = get_features_from_file(file_path) except AssertionError as e: log.warning('file "{}": {}'.format(file_path, e)) del labels_list[idx] continue except AttributeError as e: log.warning('failed to read from file "{}": {}'.format( file_path, e)) del labels_list[idx] continue except FaceExtractError as e: log.warning('failed to extract features from file "{}": {}'.format( file_path, e)) del labels_list[idx] continue features_list.append(features) try: os.remove(os.path.join(config.find('photos_dir'), filename)) except (FileNotFoundError, IsADirectoryError, OSError) as e: log.error("api File {} cannot be deleted: {}".format(filename, e)) bus.push(destination_topic, deleted=False, filename=filename, label_id=label_id, reason=str(e)) return features_set.rebuild(np.array(features_list), np.array(labels_list)) bus.push(destination_topic, deleted=True, filename=filename, label_id=label_id)
def post(self): """Add a new photo. Accepts ------- request.files: Form data binaries Returns ------- {"success": True}: JSON """ transaction_id = str(uuid()) filenames = [] for key in request.files: file = request.files[key] old_filename = file.filename filename = str(uuid()) + ".jpg" filenames.append(old_filename) output_path = os.path.join("/tmp/", filename) with open(output_path, "wb"): file.save(output_path) bus.push("file_added", filename=filename, old_filename=old_filename, transaction_id=transaction_id, is_tester=self.is_tester) processed = [] failed = {} start = time() while True: if time() - start >= 20: log.debug( "Transaction closed by timeout with {} files left".format( len(filenames))) break if not len(filenames): log.debug("all files processed in {} seconds".format(time() - start)) break raw_msg = bus.client.lpop(transaction_id) if raw_msg: msg = bus._loads(raw_msg)[1] log.debug("new message: {}".format(msg)) old_filename = msg.get("old_filename") if msg["processed"]: processed.append(old_filename) filenames.remove(old_filename) else: failed[old_filename] = msg.get("error_message") filenames.remove(old_filename) for filename in filenames: failed[filename] = "Timeout" res = {} status = 201 res["success"] = processed if len(failed): res["error"] = failed status = 207 log.debug("removing topic {}".format(transaction_id)) bus.client.delete(transaction_id) return res, status
def on_file_added(*args, **kwargs): assert (kwargs.get("filename")) assert (kwargs.get("old_filename")) assert (kwargs.get("transaction_id")) assert (kwargs.get("is_tester") is not None) log.debug("New file added: {} received".format(kwargs)) filename = kwargs["filename"] old_filename = kwargs["old_filename"] destination_topic = kwargs["transaction_id"] is_tester = kwargs["is_tester"] file_path = os.path.join("/tmp/", filename) photos_dir = config.find('photos_dir') destination_path = os.path.join(photos_dir, filename) try: new_face_features = get_features_from_file(file_path) except AssertionError as e: log.warning('file "{}": {}'.format(file_path, e)) bus.push(destination_topic, filename=filename, old_filename=old_filename, processed=False, error_message="Файл не найден") return except AttributeError as e: log.warning('failed to read from file "{}": {}'.format(file_path, e)) bus.push(destination_topic, filename=filename, old_filename=old_filename, processed=False, error_message= "Загруженный файл не является изображением в формате jpeg") return except FaceExtractError as e: bus.push(destination_topic, filename=filename, old_filename=old_filename, processed=False, error_message=str(e)) return try: os.rename(file_path, destination_path) except OSError as e: bus.push(destination_topic, filename=filename, old_filename=old_filename, processed=False, error_message=str(e)) return faces = executor.execute_query(query, 'select_faces', commit=True) labels_list = [row[2] for row in faces] new_face_label = max(labels_list) + 1 labels_list.append(new_face_label) features_list = [] for idx, row in enumerate(faces): file_path = os.path.join(photos_dir, row[1]) try: features = get_features_from_file(file_path) except AssertionError as e: log.warning('file "{}": {}'.format(file_path, e)) del labels_list[idx] continue except AttributeError as e: log.warning('failed to read from file "{}": {}'.format( file_path, e)) del labels_list[idx] continue except FaceExtractError as e: log.warning('failed to extract features from file "{}": {}'.format( file_path, e)) del labels_list[idx] continue features_list.append(features) features_list.append(new_face_features) features_set.rebuild(np.array(features_list), np.array(labels_list)) executor.execute_query(query, 'insert_face', filename=filename, label_id=new_face_label, is_tester=is_tester, commit=True) bus.push(destination_topic, filename=filename, old_filename=old_filename, processed=True)