Skip to content

Commit

Permalink
Use config attribute map instead of hard coded (#14387)
Browse files Browse the repository at this point in the history
  • Loading branch information
NickM-27 authored Oct 16, 2024
1 parent eda52a3 commit 06f47f2
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 19 deletions.
1 change: 1 addition & 0 deletions docker/main/install_deps.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

#!/bin/bash

set -euxo pipefail
Expand Down
17 changes: 13 additions & 4 deletions web/src/components/settings/ObjectMaskEditPane.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import {
FormMessage,
} from "@/components/ui/form";
import { useCallback, useEffect, useMemo } from "react";
import { ATTRIBUTE_LABELS, FrigateConfig } from "@/types/frigateConfig";
import { FrigateConfig } from "@/types/frigateConfig";
import useSWR from "swr";
import { zodResolver } from "@hookform/resolvers/zod";
import { useForm } from "react-hook-form";
Expand All @@ -37,6 +37,7 @@ import axios from "axios";
import { toast } from "sonner";
import { Toaster } from "../ui/sonner";
import ActivityIndicator from "../indicators/activity-indicator";
import { getAttributeLabels } from "@/utils/iconUtil";

type ObjectMaskEditPaneProps = {
polygons?: Polygon[];
Expand Down Expand Up @@ -367,6 +368,14 @@ type ZoneObjectSelectorProps = {
export function ZoneObjectSelector({ camera }: ZoneObjectSelectorProps) {
const { data: config } = useSWR<FrigateConfig>("config");

const attributeLabels = useMemo(() => {
if (!config) {
return [];
}

return getAttributeLabels(config);
}, [config]);

const cameraConfig = useMemo(() => {
if (config && camera) {
return config.cameras[camera];
Expand All @@ -382,20 +391,20 @@ export function ZoneObjectSelector({ camera }: ZoneObjectSelectorProps) {

Object.values(config.cameras).forEach((camera) => {
camera.objects.track.forEach((label) => {
if (!ATTRIBUTE_LABELS.includes(label)) {
if (!attributeLabels.includes(label)) {
labels.add(label);
}
});
});

cameraConfig.objects.track.forEach((label) => {
if (!ATTRIBUTE_LABELS.includes(label)) {
if (!attributeLabels.includes(label)) {
labels.add(label);
}
});

return [...labels].sort();
}, [config, cameraConfig]);
}, [config, cameraConfig, attributeLabels]);

return (
<>
Expand Down
17 changes: 13 additions & 4 deletions web/src/components/settings/ZoneEditPane.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
} from "@/components/ui/form";
import { Input } from "@/components/ui/input";
import { useCallback, useEffect, useMemo, useState } from "react";
import { ATTRIBUTE_LABELS, FrigateConfig } from "@/types/frigateConfig";
import { FrigateConfig } from "@/types/frigateConfig";
import useSWR from "swr";
import { zodResolver } from "@hookform/resolvers/zod";
import { useForm } from "react-hook-form";
Expand All @@ -28,6 +28,7 @@ import { Toaster } from "@/components/ui/sonner";
import { toast } from "sonner";
import { flattenPoints, interpolatePoints } from "@/utils/canvasUtil";
import ActivityIndicator from "../indicators/activity-indicator";
import { getAttributeLabels } from "@/utils/iconUtil";

type ZoneEditPaneProps = {
polygons?: Polygon[];
Expand Down Expand Up @@ -505,6 +506,14 @@ export function ZoneObjectSelector({
}: ZoneObjectSelectorProps) {
const { data: config } = useSWR<FrigateConfig>("config");

const attributeLabels = useMemo(() => {
if (!config) {
return [];
}

return getAttributeLabels(config);
}, [config]);

const cameraConfig = useMemo(() => {
if (config && camera) {
return config.cameras[camera];
Expand All @@ -519,23 +528,23 @@ export function ZoneObjectSelector({
const labels = new Set<string>();

cameraConfig.objects.track.forEach((label) => {
if (!ATTRIBUTE_LABELS.includes(label)) {
if (!attributeLabels.includes(label)) {
labels.add(label);
}
});

if (zoneName) {
if (cameraConfig.zones[zoneName]) {
cameraConfig.zones[zoneName].objects.forEach((label) => {
if (!ATTRIBUTE_LABELS.includes(label)) {
if (!attributeLabels.includes(label)) {
labels.add(label);
}
});
}
}

return [...labels].sort() || [];
}, [config, cameraConfig, zoneName]);
}, [config, cameraConfig, attributeLabels, zoneName]);

const [currentLabels, setCurrentLabels] = useState<string[] | undefined>(
selectedLabels,
Expand Down
18 changes: 15 additions & 3 deletions web/src/hooks/use-camera-activity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@ import {
useInitialCameraState,
useMotionActivity,
} from "@/api/ws";
import { ATTRIBUTE_LABELS, CameraConfig } from "@/types/frigateConfig";
import { CameraConfig, FrigateConfig } from "@/types/frigateConfig";
import { MotionData, ReviewSegment } from "@/types/review";
import { useCallback, useEffect, useMemo, useState } from "react";
import { useTimelineUtils } from "./use-timeline-utils";
import { ObjectType } from "@/types/ws";
import useDeepMemo from "./use-deep-memo";
import { isEqual } from "lodash";
import { useAutoFrigateStats } from "./use-stats";
import useSWR from "swr";
import { getAttributeLabels } from "@/utils/iconUtil";

type useCameraActivityReturn = {
activeTracking: boolean;
Expand All @@ -23,6 +25,16 @@ export function useCameraActivity(
camera: CameraConfig,
revalidateOnFocus: boolean = true,
): useCameraActivityReturn {
const { data: config } = useSWR<FrigateConfig>("config", {
revalidateOnFocus: false,
});
const attributeLabels = useMemo(() => {
if (!config) {
return [];
}

return getAttributeLabels(config);
}, [config]);
const [objects, setObjects] = useState<ObjectType[]>([]);

// init camera activity
Expand Down Expand Up @@ -99,7 +111,7 @@ export function useCameraActivity(
if (updatedEvent.after.sub_label) {
const sub_label = updatedEvent.after.sub_label[0];

if (ATTRIBUTE_LABELS.includes(sub_label)) {
if (attributeLabels.includes(sub_label)) {
label = sub_label;
} else {
label = `${label}-verified`;
Expand All @@ -113,7 +125,7 @@ export function useCameraActivity(
}

handleSetObjects(newObjects);
}, [camera, updatedEvent, objects, handleSetObjects]);
}, [attributeLabels, camera, updatedEvent, objects, handleSetObjects]);

// determine if camera is offline

Expand Down
8 changes: 0 additions & 8 deletions web/src/types/frigateConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,6 @@ export interface BirdseyeConfig {
width: number;
}

export const ATTRIBUTE_LABELS = [
"amazon",
"face",
"fedex",
"license_plate",
"ups",
];

export type SearchModelSize = "small" | "large";

export interface CameraConfig {
Expand Down
14 changes: 14 additions & 0 deletions web/src/utils/iconUtil.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { IconName } from "@/components/icons/IconPicker";
import { FrigateConfig } from "@/types/frigateConfig";
import { BsPersonWalking } from "react-icons/bs";
import {
FaAmazon,
Expand Down Expand Up @@ -36,6 +37,19 @@ import { LuBox, LuLassoSelect } from "react-icons/lu";
import * as LuIcons from "react-icons/lu";
import { MdRecordVoiceOver } from "react-icons/md";

export function getAttributeLabels(config?: FrigateConfig) {
if (!config) {
return [];
}

const labels = new Set();

Object.values(config.model.attributes_map).forEach((values) =>
values.forEach((label) => labels.add(label)),
);
return [...labels];
}

export function isValidIconName(value: string): value is IconName {
return Object.keys(LuIcons).includes(value as IconName);
}
Expand Down

0 comments on commit 06f47f2

Please sign in to comment.