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";
|
|
|
|
|
import { Button } from "@/components/ui/button";
|
|
|
|
|
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-10-23 22:27:28 +03:00
|
|
|
import { useEffect, useMemo, useState } from "react";
|
2025-06-05 02:09:55 +03:00
|
|
|
import { isMobile } from "react-device-detect";
|
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-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-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 />
|
|
|
|
|
Add Classification
|
|
|
|
|
</Button>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
<div className="flex size-full gap-2 p-2">
|
2025-10-23 22:27:28 +03:00
|
|
|
{selectedClassificationConfigs.length === 0 ? (
|
|
|
|
|
<NoModelsView
|
|
|
|
|
onCreateModel={() => setNewModel(true)}
|
|
|
|
|
modelType={pageToggle}
|
2025-10-22 16:36:09 +03:00
|
|
|
/>
|
2025-10-23 22:27:28 +03:00
|
|
|
) : (
|
|
|
|
|
selectedClassificationConfigs.map((config) => (
|
|
|
|
|
<ModelCard
|
|
|
|
|
key={config.name}
|
|
|
|
|
config={config}
|
|
|
|
|
onClick={() => onClick(config)}
|
|
|
|
|
/>
|
|
|
|
|
))
|
|
|
|
|
)}
|
|
|
|
|
</div>
|
|
|
|
|
</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;
|
|
|
|
|
};
|
|
|
|
|
function ModelCard({ config, onClick }: ModelCardProps) {
|
|
|
|
|
const { data: dataset } = useSWR<{
|
|
|
|
|
[id: string]: string[];
|
|
|
|
|
}>(`classification/${config.name}/dataset`, { revalidateOnFocus: false });
|
|
|
|
|
|
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 (
|
|
|
|
|
<div
|
|
|
|
|
key={config.name}
|
|
|
|
|
className={cn(
|
2025-10-22 16:36:09 +03:00
|
|
|
"relative size-60 cursor-pointer overflow-hidden rounded-lg",
|
2025-06-23 17:40:28 +03:00
|
|
|
"outline-transparent duration-500",
|
|
|
|
|
isMobile && "w-full",
|
|
|
|
|
)}
|
|
|
|
|
onClick={() => onClick()}
|
|
|
|
|
>
|
2025-10-22 16:36:09 +03:00
|
|
|
<img
|
|
|
|
|
className={cn("size-full", isMobile && "w-full")}
|
|
|
|
|
src={`${baseUrl}clips/${config.name}/dataset/${coverImage?.name}/${coverImage?.img}`}
|
|
|
|
|
/>
|
|
|
|
|
<ImageShadowOverlay />
|
|
|
|
|
<div className="absolute bottom-2 left-3 text-lg smart-capitalize">
|
|
|
|
|
{config.name}
|
2025-06-23 17:40:28 +03:00
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|