def _generate_detections(self, folder, excludeBelowThreshold):
        detectionItems = list(Item().findWithPermissions(
            {"meta.detection": str(folder["_id"])},
            user=self.getCurrentUser()))
        detectionItems.sort(key=lambda d: d["created"], reverse=True)
        item = detectionItems[0]
        file = Item().childFiles(item)[0]

        # TODO: deprecated, remove after we migrate everyone to json
        if "csv" in file["exts"]:
            return File().download(file)

        filename = ".".join([file["name"].split(".")[:-1][0], "csv"])

        imageFiles = [
            f['name']
            for f in Folder().childItems(folder,
                                         filters={
                                             "lowerName": {
                                                 "$regex": safeImageRegex
                                             }
                                         }).sort("lowerName")
        ]
        thresholds = folder.get("meta", {}).get("confidenceFilters", {})

        track_dict = getTrackData(file)

        def downloadGenerator():
            for data in viame.export_tracks_as_csv(track_dict,
                                                   excludeBelowThreshold,
                                                   thresholds, imageFiles):
                yield data

        return filename, downloadGenerator
 def get_detection(self, folder):
     file = self._load_detections(folder)
     if file is None:
         return {}
     if "csv" in file["exts"]:
         return getTrackData(file)
     return File().download(file, contentDisposition="inline")
    def save_detection(self, folder, tracks):
        user = self.getCurrentUser()
        upsert: Dict[str, dict] = tracks.get('upsert', {})
        delete: List[str] = tracks.get('delete', [])
        track_dict = getTrackData(self._load_detections(folder))

        for track_id in delete:
            track_dict.pop(str(track_id), None)
        for track_id, track in upsert.items():
            validated: models.Track = models.Track(**track)
            track_dict[str(track_id)] = validated.dict(exclude_none=True)

        upserted_len = len(upsert.keys())
        deleted_len = len(delete)

        if upserted_len or deleted_len:
            saveTracks(folder, track_dict, user)

        return {
            "updated": upserted_len,
            "deleted": deleted_len,
        }