2025-06-23 17:40:28 +03:00
|
|
|
import { baseUrl } from "@/api/baseUrl";
|
2025-10-22 16:36:09 +03:00
|
|
|
import ClassificationModelWizardDialog from "@/components/classification/ClassificationModelWizardDialog";
|
2025-06-05 02:09:55 +03:00
|
|
|
import ActivityIndicator from "@/components/indicators/activity-indicator";
|
2025-10-22 16:36:09 +03:00
|
|
|
import { ImageShadowOverlay } from "@/components/overlay/ImageShadowOverlay";
|
2025-11-01 17:11:24 +03:00
|
|
|
import { Button, buttonVariants } from "@/components/ui/button";
|
2025-10-22 16:36:09 +03:00
|
|
|
import { ToggleGroup, ToggleGroupItem } from "@/components/ui/toggle-group";
|
|
|
|
|
import useOptimisticState from "@/hooks/use-optimistic-state";
|
2025-06-05 02:09:55 +03:00
|
|
|
import { cn } from "@/lib/utils";
|
|
|
|
|
import {
|
|
|
|
|
CustomClassificationModelConfig,
|
|
|
|
|
FrigateConfig,
|
|
|
|
|
} from "@/types/frigateConfig";
|
2025-11-03 16:34:06 +03:00
|
|
|
import { useCallback, useEffect, useMemo, useState } from "react";
|
2025-10-22 16:36:09 +03:00
|
|
|
import { useTranslation } from "react-i18next";
|
|
|
|
|
import { FaFolderPlus } from "react-icons/fa";
|
2025-10-23 22:27:28 +03:00
|
|
|
import { MdModelTraining } from "react-icons/md";
|
2025-11-01 17:11:24 +03:00
|
|
|
import { LuTrash2 } from "react-icons/lu";
|
|
|
|
|
import { FiMoreVertical } from "react-icons/fi";
|
2025-06-05 02:09:55 +03:00
|
|
|
import useSWR from "swr";
|
2025-10-23 22:27:28 +03:00
|
|
|
import Heading from "@/components/ui/heading";
|
|
|
|
|
import { useOverlayState } from "@/hooks/use-overlay-state";
|
2025-11-01 17:11:24 +03:00
|
|
|
import axios from "axios";
|
|
|
|
|
import { toast } from "sonner";
|
|
|
|
|
import {
|
|
|
|
|
DropdownMenu,
|
|
|
|
|
DropdownMenuContent,
|
|
|
|
|
DropdownMenuItem,
|
|
|
|
|
DropdownMenuTrigger,
|
|
|
|
|
} from "@/components/ui/dropdown-menu";
|
|
|
|
|
import {
|
|
|
|
|
AlertDialog,
|
|
|
|
|
AlertDialogAction,
|
|
|
|
|
AlertDialogCancel,
|
|
|
|
|
AlertDialogContent,
|
|
|
|
|
AlertDialogDescription,
|
|
|
|
|
AlertDialogFooter,
|
|
|
|
|
AlertDialogHeader,
|
|
|
|
|
AlertDialogTitle,
|
|
|
|
|
} from "@/components/ui/alert-dialog";
|
|
|
|
|
import BlurredIconButton from "@/components/button/BlurredIconButton";
|
2025-06-05 02:09:55 +03:00
|
|
|
|
2025-10-22 16:36:09 +03:00
|
|
|
const allModelTypes = ["objects", "states"] as const;
|
|
|
|
|
type ModelType = (typeof allModelTypes)[number];
|
|
|
|
|
|
2025-06-05 02:09:55 +03:00
|
|
|
type ModelSelectionViewProps = {
|
|
|
|
|
onClick: (model: CustomClassificationModelConfig) => void;
|
|
|
|
|
};
|
|
|
|
|
export default function ModelSelectionView({
|
|
|
|
|
onClick,
|
|
|
|
|
}: ModelSelectionViewProps) {
|
2025-10-22 16:36:09 +03:00
|
|
|
const { t } = useTranslation(["views/classificationModel"]);
|
2025-10-23 22:27:28 +03:00
|
|
|
const [page, setPage] = useOverlayState<ModelType>("objects", "objects");
|
|
|
|
|
const [pageToggle, setPageToggle] = useOptimisticState(
|
|
|
|
|
page || "objects",
|
|
|
|
|
setPage,
|
|
|
|
|
100,
|
|
|
|
|
);
|
|
|
|
|
const { data: config, mutate: refreshConfig } = useSWR<FrigateConfig>(
|
|
|
|
|
"config",
|
|
|
|
|
{
|
|
|
|
|
revalidateOnFocus: false,
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// title
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
document.title = t("documentTitle");
|
|
|
|
|
}, [t]);
|
2025-06-05 02:09:55 +03:00
|
|
|
|
2025-10-22 16:36:09 +03:00
|
|
|
// data
|
|
|
|
|
|
2025-06-05 02:09:55 +03:00
|
|
|
const classificationConfigs = useMemo(() => {
|
|
|
|
|
if (!config) {
|
|
|
|
|
return [];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return Object.values(config.classification.custom);
|
|
|
|
|
}, [config]);
|
|
|
|
|
|
2025-10-22 16:36:09 +03:00
|
|
|
const selectedClassificationConfigs = useMemo(() => {
|
|
|
|
|
return classificationConfigs.filter((model) => {
|
|
|
|
|
if (pageToggle == "objects" && model.object_config != undefined) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (pageToggle == "states" && model.state_config != undefined) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
});
|
|
|
|
|
}, [classificationConfigs, pageToggle]);
|
|
|
|
|
|
|
|
|
|
// new model wizard
|
|
|
|
|
|
|
|
|
|
const [newModel, setNewModel] = useState(false);
|
|
|
|
|
|
2025-06-05 02:09:55 +03:00
|
|
|
if (!config) {
|
|
|
|
|
return <ActivityIndicator />;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return (
|
2025-10-22 16:36:09 +03:00
|
|
|
<div className="flex size-full flex-col p-2">
|
|
|
|
|
<ClassificationModelWizardDialog
|
|
|
|
|
open={newModel}
|
2025-10-23 22:27:28 +03:00
|
|
|
defaultModelType={pageToggle === "objects" ? "object" : "state"}
|
|
|
|
|
onClose={() => {
|
|
|
|
|
setNewModel(false);
|
|
|
|
|
refreshConfig();
|
|
|
|
|
}}
|
2025-10-22 16:36:09 +03:00
|
|
|
/>
|
|
|
|
|
|
|
|
|
|
<div className="flex h-12 w-full items-center justify-between">
|
|
|
|
|
<div className="flex flex-row items-center">
|
|
|
|
|
<ToggleGroup
|
|
|
|
|
className="*:rounded-md *:px-3 *:py-4"
|
|
|
|
|
type="single"
|
|
|
|
|
size="sm"
|
|
|
|
|
value={pageToggle}
|
|
|
|
|
onValueChange={(value: ModelType) => {
|
|
|
|
|
if (value) {
|
|
|
|
|
setPageToggle(value);
|
|
|
|
|
}
|
|
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
{allModelTypes.map((item) => (
|
|
|
|
|
<ToggleGroupItem
|
|
|
|
|
key={item}
|
|
|
|
|
className={`flex scroll-mx-10 items-center justify-between gap-2 ${pageToggle == item ? "" : "*:text-muted-foreground"}`}
|
|
|
|
|
value={item}
|
|
|
|
|
data-nav-item={item}
|
|
|
|
|
aria-label={t("selectItem", {
|
|
|
|
|
ns: "common",
|
|
|
|
|
item: t("menu." + item),
|
|
|
|
|
})}
|
|
|
|
|
>
|
|
|
|
|
<div className="smart-capitalize">{t("menu." + item)}</div>
|
|
|
|
|
</ToggleGroupItem>
|
|
|
|
|
))}
|
|
|
|
|
</ToggleGroup>
|
|
|
|
|
</div>
|
|
|
|
|
<div className="flex flex-row items-center">
|
|
|
|
|
<Button
|
|
|
|
|
className="flex flex-row items-center gap-2"
|
|
|
|
|
variant="select"
|
|
|
|
|
onClick={() => setNewModel(true)}
|
|
|
|
|
>
|
|
|
|
|
<FaFolderPlus />
|
2025-11-01 17:11:24 +03:00
|
|
|
{t("button.addClassification")}
|
2025-10-22 16:36:09 +03:00
|
|
|
</Button>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
2025-10-29 17:40:50 +03:00
|
|
|
{selectedClassificationConfigs.length === 0 ? (
|
|
|
|
|
<NoModelsView
|
|
|
|
|
onCreateModel={() => setNewModel(true)}
|
|
|
|
|
modelType={pageToggle}
|
|
|
|
|
/>
|
|
|
|
|
) : (
|
|
|
|
|
<div className="grid auto-rows-max grid-cols-2 gap-2 overflow-y-auto p-2 md:grid-cols-4 lg:grid-cols-5 xl:grid-cols-6 2xl:grid-cols-8 3xl:grid-cols-10">
|
|
|
|
|
{selectedClassificationConfigs.map((config) => (
|
2025-10-23 22:27:28 +03:00
|
|
|
<ModelCard
|
|
|
|
|
key={config.name}
|
|
|
|
|
config={config}
|
|
|
|
|
onClick={() => onClick(config)}
|
2025-11-01 17:11:24 +03:00
|
|
|
onDelete={() => refreshConfig()}
|
2025-10-23 22:27:28 +03:00
|
|
|
/>
|
2025-10-29 17:40:50 +03:00
|
|
|
))}
|
|
|
|
|
</div>
|
|
|
|
|
)}
|
2025-10-23 22:27:28 +03:00
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function NoModelsView({
|
|
|
|
|
onCreateModel,
|
|
|
|
|
modelType,
|
|
|
|
|
}: {
|
|
|
|
|
onCreateModel: () => void;
|
|
|
|
|
modelType: ModelType;
|
|
|
|
|
}) {
|
|
|
|
|
const { t } = useTranslation(["views/classificationModel"]);
|
|
|
|
|
const typeKey = modelType === "objects" ? "object" : "state";
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<div className="flex size-full items-center justify-center">
|
|
|
|
|
<div className="flex flex-col items-center gap-2">
|
|
|
|
|
<MdModelTraining className="size-8" />
|
|
|
|
|
<Heading as="h4">{t(`noModels.${typeKey}.title`)}</Heading>
|
|
|
|
|
<div className="mb-3 text-center text-secondary-foreground">
|
|
|
|
|
{t(`noModels.${typeKey}.description`)}
|
|
|
|
|
</div>
|
|
|
|
|
<Button size="sm" variant="select" onClick={onCreateModel}>
|
|
|
|
|
{t(`noModels.${typeKey}.buttonText`)}
|
|
|
|
|
</Button>
|
2025-10-22 16:36:09 +03:00
|
|
|
</div>
|
2025-06-05 02:09:55 +03:00
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|
2025-06-23 17:40:28 +03:00
|
|
|
|
|
|
|
|
type ModelCardProps = {
|
|
|
|
|
config: CustomClassificationModelConfig;
|
|
|
|
|
onClick: () => void;
|
2025-11-01 17:11:24 +03:00
|
|
|
onDelete: () => void;
|
2025-06-23 17:40:28 +03:00
|
|
|
};
|
2025-11-01 17:11:24 +03:00
|
|
|
function ModelCard({ config, onClick, onDelete }: ModelCardProps) {
|
|
|
|
|
const { t } = useTranslation(["views/classificationModel"]);
|
|
|
|
|
|
2025-06-23 17:40:28 +03:00
|
|
|
const { data: dataset } = useSWR<{
|
|
|
|
|
[id: string]: string[];
|
|
|
|
|
}>(`classification/${config.name}/dataset`, { revalidateOnFocus: false });
|
|
|
|
|
|
2025-11-01 17:11:24 +03:00
|
|
|
const [deleteDialogOpen, setDeleteDialogOpen] = useState(false);
|
|
|
|
|
|
|
|
|
|
const handleDelete = useCallback(async () => {
|
2025-11-03 18:01:30 +03:00
|
|
|
try {
|
|
|
|
|
// First, remove from config to stop the processor
|
|
|
|
|
await axios.put("/config/set", {
|
|
|
|
|
requires_restart: 0,
|
|
|
|
|
update_topic: `config/classification/custom/${config.name}`,
|
|
|
|
|
config_data: {
|
|
|
|
|
classification: {
|
|
|
|
|
custom: {
|
|
|
|
|
[config.name]: "",
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
2025-11-01 17:11:24 +03:00
|
|
|
});
|
2025-11-03 18:01:30 +03:00
|
|
|
|
|
|
|
|
// Then, delete the model data and files
|
|
|
|
|
await axios.delete(`classification/${config.name}`);
|
|
|
|
|
|
|
|
|
|
toast.success(t("toast.success.deletedModel", { count: 1 }), {
|
|
|
|
|
position: "top-center",
|
|
|
|
|
});
|
|
|
|
|
onDelete();
|
|
|
|
|
} catch (err) {
|
|
|
|
|
const error = err as {
|
|
|
|
|
response?: { data?: { message?: string; detail?: string } };
|
|
|
|
|
};
|
|
|
|
|
const errorMessage =
|
|
|
|
|
error.response?.data?.message ||
|
|
|
|
|
error.response?.data?.detail ||
|
|
|
|
|
"Unknown error";
|
|
|
|
|
toast.error(t("toast.error.deleteModelFailed", { errorMessage }), {
|
|
|
|
|
position: "top-center",
|
|
|
|
|
});
|
|
|
|
|
}
|
2025-11-01 17:11:24 +03:00
|
|
|
}, [config, onDelete, t]);
|
|
|
|
|
|
2025-11-03 16:34:06 +03:00
|
|
|
const handleDeleteClick = useCallback((e: React.MouseEvent) => {
|
|
|
|
|
e.stopPropagation();
|
|
|
|
|
setDeleteDialogOpen(true);
|
|
|
|
|
}, []);
|
2025-11-01 17:11:24 +03:00
|
|
|
|
2025-10-22 16:36:09 +03:00
|
|
|
const coverImage = useMemo(() => {
|
2025-10-23 22:27:28 +03:00
|
|
|
if (!dataset) {
|
2025-10-22 16:36:09 +03:00
|
|
|
return undefined;
|
2025-06-23 17:40:28 +03:00
|
|
|
}
|
|
|
|
|
|
2025-10-22 16:36:09 +03:00
|
|
|
const keys = Object.keys(dataset).filter((key) => key != "none");
|
|
|
|
|
const selectedKey = keys[0];
|
2025-06-23 17:40:28 +03:00
|
|
|
|
2025-10-23 22:27:28 +03:00
|
|
|
if (!dataset[selectedKey]) {
|
|
|
|
|
return undefined;
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-22 16:36:09 +03:00
|
|
|
return {
|
|
|
|
|
name: selectedKey,
|
|
|
|
|
img: dataset[selectedKey][0],
|
|
|
|
|
};
|
2025-06-23 17:40:28 +03:00
|
|
|
}, [dataset]);
|
|
|
|
|
|
|
|
|
|
return (
|
2025-11-01 17:11:24 +03:00
|
|
|
<>
|
|
|
|
|
<AlertDialog
|
|
|
|
|
open={deleteDialogOpen}
|
|
|
|
|
onOpenChange={() => setDeleteDialogOpen(!deleteDialogOpen)}
|
|
|
|
|
>
|
|
|
|
|
<AlertDialogContent>
|
|
|
|
|
<AlertDialogHeader>
|
|
|
|
|
<AlertDialogTitle>{t("deleteModel.title")}</AlertDialogTitle>
|
|
|
|
|
</AlertDialogHeader>
|
|
|
|
|
<AlertDialogDescription>
|
|
|
|
|
{t("deleteModel.single", { name: config.name })}
|
|
|
|
|
</AlertDialogDescription>
|
|
|
|
|
<AlertDialogFooter>
|
|
|
|
|
<AlertDialogCancel>
|
|
|
|
|
{t("button.cancel", { ns: "common" })}
|
|
|
|
|
</AlertDialogCancel>
|
|
|
|
|
<AlertDialogAction
|
|
|
|
|
className={buttonVariants({ variant: "destructive" })}
|
|
|
|
|
onClick={handleDelete}
|
|
|
|
|
>
|
|
|
|
|
{t("button.delete", { ns: "common" })}
|
|
|
|
|
</AlertDialogAction>
|
|
|
|
|
</AlertDialogFooter>
|
|
|
|
|
</AlertDialogContent>
|
|
|
|
|
</AlertDialog>
|
|
|
|
|
|
|
|
|
|
<div
|
|
|
|
|
className={cn(
|
|
|
|
|
"relative aspect-square w-full cursor-pointer overflow-hidden rounded-lg",
|
|
|
|
|
)}
|
|
|
|
|
onClick={onClick}
|
|
|
|
|
>
|
|
|
|
|
<img
|
|
|
|
|
className="size-full"
|
|
|
|
|
src={`${baseUrl}clips/${config.name}/dataset/${coverImage?.name}/${coverImage?.img}`}
|
|
|
|
|
/>
|
2025-11-03 16:34:06 +03:00
|
|
|
<ImageShadowOverlay lowerClassName="h-[30%] z-0" />
|
2025-11-01 17:11:24 +03:00
|
|
|
<div className="absolute bottom-2 left-3 text-lg text-white smart-capitalize">
|
|
|
|
|
{config.name}
|
|
|
|
|
</div>
|
|
|
|
|
<div className="absolute bottom-2 right-2 z-40">
|
|
|
|
|
<DropdownMenu>
|
|
|
|
|
<DropdownMenuTrigger asChild onClick={(e) => e.stopPropagation()}>
|
|
|
|
|
<BlurredIconButton>
|
|
|
|
|
<FiMoreVertical className="size-5 text-white" />
|
|
|
|
|
</BlurredIconButton>
|
|
|
|
|
</DropdownMenuTrigger>
|
2025-11-03 16:34:06 +03:00
|
|
|
<DropdownMenuContent
|
|
|
|
|
align="end"
|
|
|
|
|
onClick={(e) => e.stopPropagation()}
|
|
|
|
|
>
|
2025-11-01 17:11:24 +03:00
|
|
|
<DropdownMenuItem onClick={handleDeleteClick}>
|
|
|
|
|
<LuTrash2 className="mr-2 size-4" />
|
2025-11-03 16:34:06 +03:00
|
|
|
<span>{t("button.delete", { ns: "common" })}</span>
|
2025-11-01 17:11:24 +03:00
|
|
|
</DropdownMenuItem>
|
|
|
|
|
</DropdownMenuContent>
|
|
|
|
|
</DropdownMenu>
|
|
|
|
|
</div>
|
2025-06-23 17:40:28 +03:00
|
|
|
</div>
|
2025-11-01 17:11:24 +03:00
|
|
|
</>
|
2025-06-23 17:40:28 +03:00
|
|
|
);
|
|
|
|
|
}
|