Compare commits

..

11 Commits

10 changed files with 123 additions and 41 deletions

View File

@@ -1,7 +1,7 @@
{ {
"name": "bayiq-ui", "name": "bayiq-ui",
"private": true, "private": true,
"version": "1.0.3", "version": "1.0.6",
"type": "module", "type": "module",
"scripts": { "scripts": {
"dev": "vite", "dev": "vite",

View File

@@ -14,16 +14,23 @@ const CameraZoomFetcher = ({ cameraId, dispatch }: CameraZoomFetcherProps) => {
useEffect(() => { useEffect(() => {
const fetchZoomLevel = async () => { const fetchZoomLevel = async () => {
const result = await cameraZoomQuery.refetch(); const result = await cameraZoomQuery.refetch();
if (result.data && typeof result.data.zoomLevel === "number") {
if (result?.data) {
const currentZoomLevel = result?.data["propPhysCurrent"].value;
const minLevel = result?.data["propPhysMin"].value;
const maxLevel = result?.data["propPhysMax"].value;
const normalizedZoomLevel = ((currentZoomLevel - minLevel) / (maxLevel - minLevel)).toFixed(2);
dispatch({ dispatch({
type: "SET_ZOOM_LEVEL", type: "SET_ZOOM_LEVEL",
payload: { cameraFeedID: cameraId, zoomLevel: result.data.zoomLevel }, payload: { cameraFeedID: cameraId, zoomLevel: parseFloat(normalizedZoomLevel) },
}); });
} }
}; };
fetchZoomLevel(); fetchZoomLevel();
}, [cameraId, cameraZoomQuery, dispatch]); }, [cameraId]);
return null; return null;
}; };

View File

@@ -3,6 +3,7 @@ import { CAMERA_IDS, DEFAULT_REGIONS, type CameraID } from "../config/cameraConf
export const initialState: CameraFeedState = { export const initialState: CameraFeedState = {
cameraFeedID: CAMERA_IDS[0], cameraFeedID: CAMERA_IDS[0],
brushSize: 1,
paintedCells: CAMERA_IDS.reduce( paintedCells: CAMERA_IDS.reduce(
(acc, id) => { (acc, id) => {
acc[id] = new Map<string, PaintedCell>(); acc[id] = new Map<string, PaintedCell>();
@@ -28,7 +29,7 @@ export const initialState: CameraFeedState = {
), ),
zoomLevel: CAMERA_IDS.reduce( zoomLevel: CAMERA_IDS.reduce(
(acc, id) => { (acc, id) => {
acc[id] = 1; acc[id] = 0;
return acc; return acc;
}, },
{} as Record<CameraID, number>, {} as Record<CameraID, number>,
@@ -110,6 +111,12 @@ export function reducer(state: CameraFeedState, action: CameraFeedAction) {
[action.payload.cameraFeedID]: action.payload.zoomLevel, [action.payload.cameraFeedID]: action.payload.zoomLevel,
}, },
}; };
case "SET_BRUSH_SIZE":
return {
...state,
brushSize: action.payload.brushSize,
};
default: default:
return state; return state;
} }

View File

@@ -1,5 +1,5 @@
import { Tabs, Tab, TabList, TabPanel } from "react-tabs"; import { Tabs, Tab, TabList, TabPanel } from "react-tabs";
import { useEffect } from "react"; import { useEffect, useState } from "react";
import { useCameraFeedContext } from "../../../../app/context/CameraFeedContext"; import { useCameraFeedContext } from "../../../../app/context/CameraFeedContext";
import RegionSelector from "./RegionSelector"; import RegionSelector from "./RegionSelector";
import CameraControls from "./cameraControls/CameraControls"; import CameraControls from "./cameraControls/CameraControls";
@@ -13,6 +13,7 @@ type CameraPanelProps = {
const CameraPanel = ({ tabIndex, isResetAllModalOpen, handleClose, setIsResetModalOpen }: CameraPanelProps) => { const CameraPanel = ({ tabIndex, isResetAllModalOpen, handleClose, setIsResetModalOpen }: CameraPanelProps) => {
const { state, dispatch } = useCameraFeedContext(); const { state, dispatch } = useCameraFeedContext();
const [subTabIndex, setSubTabIndex] = useState(0);
const cameraFeedID = state.cameraFeedID; const cameraFeedID = state.cameraFeedID;
const regions = state.regionsByCamera[cameraFeedID]; const regions = state.regionsByCamera[cameraFeedID];
@@ -39,7 +40,7 @@ const CameraPanel = ({ tabIndex, isResetAllModalOpen, handleClose, setIsResetMod
}, [dispatch, tabIndex]); }, [dispatch, tabIndex]);
return ( return (
<Tabs> <Tabs onSelect={(index) => setSubTabIndex(index)}>
<TabList> <TabList>
<Tab>Target Detection</Tab> <Tab>Target Detection</Tab>
<Tab>Camera Controls</Tab> <Tab>Camera Controls</Tab>
@@ -53,10 +54,11 @@ const CameraPanel = ({ tabIndex, isResetAllModalOpen, handleClose, setIsResetMod
isResetAllModalOpen={isResetAllModalOpen} isResetAllModalOpen={isResetAllModalOpen}
handleClose={handleClose} handleClose={handleClose}
setIsResetModalOpen={setIsResetModalOpen} setIsResetModalOpen={setIsResetModalOpen}
subTabIndex={subTabIndex}
/> />
</TabPanel> </TabPanel>
<TabPanel> <TabPanel>
<CameraControls cameraFeedID={cameraFeedID} /> <CameraControls cameraFeedID={cameraFeedID} subTabIndex={subTabIndex} />
</TabPanel> </TabPanel>
</Tabs> </Tabs>
); );

View File

@@ -6,6 +6,8 @@ import { useBlackBoard } from "../../../../hooks/useBlackBoard";
import { toast } from "sonner"; import { toast } from "sonner";
import { useCameraFeedSocket } from "../../../../app/context/WebSocketContext"; import { useCameraFeedSocket } from "../../../../app/context/WebSocketContext";
import type { CameraID } from "../../../../app/config/cameraConfig"; import type { CameraID } from "../../../../app/config/cameraConfig";
import { useEffect } from "react";
import SliderComponent from "../../../../ui/SliderComponent";
type RegionSelectorProps = { type RegionSelectorProps = {
regions: Region[]; regions: Region[];
@@ -15,6 +17,7 @@ type RegionSelectorProps = {
isResetAllModalOpen: boolean; isResetAllModalOpen: boolean;
handleClose: () => void; handleClose: () => void;
setIsResetModalOpen: React.Dispatch<React.SetStateAction<boolean>>; setIsResetModalOpen: React.Dispatch<React.SetStateAction<boolean>>;
subTabIndex?: number;
}; };
const RegionSelector = ({ const RegionSelector = ({
@@ -23,14 +26,20 @@ const RegionSelector = ({
mode, mode,
cameraFeedID, cameraFeedID,
isResetAllModalOpen, isResetAllModalOpen,
subTabIndex,
setIsResetModalOpen, setIsResetModalOpen,
}: RegionSelectorProps) => { }: RegionSelectorProps) => {
const { colourMutation } = useColourDectection(); const { colourMutation } = useColourDectection();
const { state, dispatch } = useCameraFeedContext(); const { state, dispatch } = useCameraFeedContext();
const { blackboardMutation } = useBlackBoard(); const { blackboardMutation } = useBlackBoard();
const paintedCells = state.paintedCells[cameraFeedID]; const paintedCells = state.paintedCells[cameraFeedID];
const brushSize = state.brushSize;
const cameraSocket = useCameraFeedSocket(); const cameraSocket = useCameraFeedSocket();
useEffect(() => {
if (subTabIndex === 0) {
dispatch({ type: "CHANGE_MODE", payload: { cameraFeedID, mode: "painter" } });
}
}, [cameraFeedID, dispatch, subTabIndex]);
const getCurrentSocket = () => { const getCurrentSocket = () => {
switch (cameraFeedID) { switch (cameraFeedID) {
@@ -164,7 +173,7 @@ const RegionSelector = ({
<div className="flex flex-col md:flex-row gap-3"> <div className="flex flex-col md:flex-row gap-3">
<div className="p-2 border border-gray-600 rounded-lg flex flex-col h-[10%] w-full"> <div className="p-2 border border-gray-600 rounded-lg flex flex-col h-[10%] w-full">
<h2 className="text-2xl mb-2">Tools</h2> <h2 className="text-2xl mb-2">Tools</h2>
<div className="flex flex-col"> <div className="flex flex-col gap-2">
<label <label
htmlFor="paintMode" htmlFor="paintMode"
className={`p-4 border rounded-lg mb-2 className={`p-4 border rounded-lg mb-2
@@ -197,6 +206,19 @@ const RegionSelector = ({
/> />
<span className="text-xl">Erase mode</span> <span className="text-xl">Erase mode</span>
</label> </label>
<div className="flex flex-col border border-gray-600 rounded-lg p-4">
<span className="text-lg mb-2">
{mode === "painter" ? "Brush" : "Eraser"} Size: {brushSize}
</span>
<SliderComponent
id="brushSize"
onChange={(value) => dispatch({ type: "SET_BRUSH_SIZE", payload: { brushSize: value as number } })}
value={brushSize}
min={1}
max={5}
step={1}
/>
</div>
<label <label
htmlFor="magnifyMode" htmlFor="magnifyMode"
className={`p-4 border rounded-lg mb-2 className={`p-4 border rounded-lg mb-2

View File

@@ -1,3 +1,4 @@
import { useEffect } from "react";
import type { CameraID } from "../../../../../app/config/cameraConfig"; import type { CameraID } from "../../../../../app/config/cameraConfig";
import { useCameraFeedContext } from "../../../../../app/context/CameraFeedContext"; import { useCameraFeedContext } from "../../../../../app/context/CameraFeedContext";
import SliderComponent from "../../../../../ui/SliderComponent"; import SliderComponent from "../../../../../ui/SliderComponent";
@@ -6,13 +7,25 @@ import { useDebouncedCallback } from "use-debounce";
type CameraControlsProps = { type CameraControlsProps = {
cameraFeedID: CameraID; cameraFeedID: CameraID;
subTabIndex?: number;
}; };
const CameraControls = ({ cameraFeedID }: CameraControlsProps) => { const CameraControls = ({ cameraFeedID, subTabIndex }: CameraControlsProps) => {
const { state, dispatch } = useCameraFeedContext(); const { state, dispatch } = useCameraFeedContext();
const { cameraZoomMutation } = useCameraZoom(cameraFeedID); const { cameraZoomMutation, cameraZoomQuery } = useCameraZoom(cameraFeedID);
const zoomLevel = state.zoomLevel ? state.zoomLevel[cameraFeedID] : 0;
const currentZoomLevel = cameraZoomQuery.data ? cameraZoomQuery.data["propPhysCurrent"].value : 0;
const minLevel = cameraZoomQuery.data ? cameraZoomQuery.data["propPhysMin"].value : 0;
const maxLevel = cameraZoomQuery.data ? cameraZoomQuery.data["propPhysMax"].value : 0;
const normalizedZoomLevel = ((currentZoomLevel - minLevel) / (maxLevel - minLevel)).toFixed(2);
useEffect(() => {
if (subTabIndex === 1) {
dispatch({ type: "CHANGE_MODE", payload: { cameraFeedID, mode: "controller" } });
}
}, [cameraFeedID, dispatch, subTabIndex]);
const zoomLevel = state.zoomLevel ? state.zoomLevel[cameraFeedID] : 1;
const debouncedMutation = useDebouncedCallback(async (value) => { const debouncedMutation = useDebouncedCallback(async (value) => {
await cameraZoomMutation.mutateAsync({ await cameraZoomMutation.mutateAsync({
cameraFeedID, cameraFeedID,
@@ -24,17 +37,34 @@ const CameraControls = ({ cameraFeedID }: CameraControlsProps) => {
const newZoom = value as number; const newZoom = value as number;
dispatch({ dispatch({
type: "SET_ZOOM_LEVEL", type: "SET_ZOOM_LEVEL",
payload: { cameraFeedID: cameraFeedID, zoomLevel: value as number }, payload: { cameraFeedID: cameraFeedID, zoomLevel: newZoom },
}); });
debouncedMutation(newZoom); debouncedMutation(newZoom);
}; };
const handleOneShotClick = () => {
debouncedMutation(normalizedZoomLevel);
};
return ( return (
<div className="p-2 border border-gray-600 rounded-lg flex flex-col w-full mt-[5%]"> <div>
<h2 className="text-2xl mb-4">Camera {cameraFeedID}</h2> <div className="p-2 border border-gray-600 rounded-lg flex flex-col w-full mt-[5%]">
<div className="w-[70%] "> <h2 className="text-2xl mb-4">Camera {cameraFeedID}</h2>
<label htmlFor="zoom">Zoom {zoomLevel} </label> <div className="w-[70%] ">
<SliderComponent id="zoom" onChange={handleChange} value={zoomLevel} min={1} max={3} step={0.1} /> <label htmlFor="zoom">Zoom {zoomLevel}</label>
<SliderComponent id="zoom" onChange={handleChange} value={zoomLevel} min={0} max={1} step={0.1} />
</div>
</div>
<div className="p-2 border border-gray-600 rounded-lg flex flex-col w-full mt-[5%]">
<h2>One Shot</h2>
<button
type="button"
className="p-2 bg-gray-500 rounded-xl w-[40%] mt-2 hover:bg-gray-700 cursor-pointer"
onClick={handleOneShotClick}
>
One Shot
</button>
</div> </div>
</div> </div>
); );

View File

@@ -23,6 +23,8 @@ const VideoFeedGridPainter = () => {
const regions = state.regionsByCamera[cameraFeedID]; const regions = state.regionsByCamera[cameraFeedID];
const selectedRegionIndex = state.selectedRegionIndex; const selectedRegionIndex = state.selectedRegionIndex;
const mode = state.modeByCamera[cameraFeedID]; const mode = state.modeByCamera[cameraFeedID];
const brushSize = state.brushSize;
const { latestBitmapRef, isloading } = useCreateVideoSnapshot(); const { latestBitmapRef, isloading } = useCreateVideoSnapshot();
const [stageSize, setStageSize] = useState({ width: BACKEND_WIDTH, height: BACKEND_HEIGHT }); const [stageSize, setStageSize] = useState({ width: BACKEND_WIDTH, height: BACKEND_HEIGHT });
const isDrawingRef = useRef(false); const isDrawingRef = useRef(false);
@@ -85,34 +87,37 @@ const VideoFeedGridPainter = () => {
const image = draw(latestBitmapRef); const image = draw(latestBitmapRef);
const paintCell = (x: number, y: number) => { const paintCell = (x: number, y: number, brushSize: number) => {
if (mode === "controller" || mode === "zoom" || mode === "magnify") return;
const col = Math.floor(x / (size + gap)); const col = Math.floor(x / (size + gap));
const row = Math.floor(y / (size + gap)); const row = Math.floor(y / (size + gap));
const radius = Math.floor(brushSize / 2);
if (row < 0 || row >= rows || col < 0 || col >= cols) return; if (row < 0 || row >= rows || col < 0 || col >= cols) return;
const activeRegion = regions[selectedRegionIndex]; const activeRegion = regions[selectedRegionIndex];
if (!activeRegion) return; if (!activeRegion) return;
const key = `${row}-${col}`;
const currentColour = regions[selectedRegionIndex].brushColour; const currentColour = regions[selectedRegionIndex].brushColour;
const map = paintedCells; for (let r = row - radius; r <= row + radius; r++) {
const existing = map.get(key); for (let c = col - radius; c <= col + radius; c++) {
if (r < 0 || r >= rows || c < 0 || c >= cols) continue;
if (mode === "eraser") { const key = `${r}-${c}`;
if (map.has(key)) { const map = paintedCells;
map.delete(key); const existing = map.get(key);
paintLayerRef.current?.batchDraw(); if (mode === "eraser") {
if (map.has(key)) {
map.delete(key);
paintLayerRef.current?.batchDraw();
}
continue;
}
if (existing && existing.colour === currentColour) continue;
map.set(key, { colour: currentColour, region: activeRegion });
} }
return;
} }
if (existing && existing.colour === currentColour) return;
map.set(key, { colour: currentColour, region: activeRegion });
paintLayerRef.current?.batchDraw(); paintLayerRef.current?.batchDraw();
}; };
@@ -120,14 +125,14 @@ const VideoFeedGridPainter = () => {
if (!regions[selectedRegionIndex] || mode === "magnify" || mode === "zoom") return; if (!regions[selectedRegionIndex] || mode === "magnify" || mode === "zoom") return;
isDrawingRef.current = true; isDrawingRef.current = true;
const pos = e.target.getStage()?.getPointerPosition(); const pos = e.target.getStage()?.getPointerPosition();
if (pos) paintCell(pos.x, pos.y); if (pos) paintCell(pos.x, pos.y, brushSize);
}; };
const handleStageMouseMove = (e: KonvaEventObject<MouseEvent>) => { const handleStageMouseMove = (e: KonvaEventObject<MouseEvent>) => {
if (!isDrawingRef.current || mode === "magnify") return; if (!isDrawingRef.current || mode === "magnify") return;
if (!regions[selectedRegionIndex]) return; if (!regions[selectedRegionIndex]) return;
const pos = e.target.getStage()?.getPointerPosition(); const pos = e.target.getStage()?.getPointerPosition();
if (pos) paintCell(pos.x, pos.y); if (pos) paintCell(pos.x, pos.y, brushSize);
}; };
const handleStageMouseUp = () => { const handleStageMouseUp = () => {
@@ -218,6 +223,7 @@ const VideoFeedGridPainter = () => {
height={stageSize.height} height={stageSize.height}
classname={"rounded-lg"} classname={"rounded-lg"}
onClick={(e) => handleZoomClick(e, cameraFeedID)} onClick={(e) => handleZoomClick(e, cameraFeedID)}
cornerRadius={10}
/> />
</Layer> </Layer>

View File

@@ -23,10 +23,13 @@ const postZoomLevel = async (zoomConfig: CameraZoomConfig) => {
id: `Camera${zoomConfig.cameraFeedID}-onvif-controller`, id: `Camera${zoomConfig.cameraFeedID}-onvif-controller`,
fields, fields,
}; };
const response = await fetch(`${CAMBASE}/api/update-config`, { const response = await fetch(
method: "POST", `${CAMBASE}/Camera${zoomConfig.cameraFeedID}-camera-control?command=setAbsoluteZoom&zoomLevel=${zoomConfig.zoomLevel}`,
body: JSON.stringify(zoomPayload), {
}); method: "POST",
body: JSON.stringify(zoomPayload),
},
);
if (!response.ok) { if (!response.ok) {
throw new Error("Network response was not ok"); throw new Error("Network response was not ok");

View File

@@ -39,7 +39,7 @@ const SystemHealth = ({ startTime, uptime, statuses, isLoading, isError, dateUpd
} }
return ( return (
<div className="relative h-100 md:h-75 overflow-y-auto flex flex-col gap-4"> <div className="relative h-100 md:h-75 overflow-y-auto flex flex-col gap-4">
<div className="p-2 border-b border-gray-600 grid grid-cols-1 md:grid-cols-2 gap-2 justify-between"> <div className="p-2 border-gray-600 grid grid-cols-1 md:grid-cols-2 gap-2 justify-between">
<div className="flex flex-col border border-gray-600 p-4 rounded-lg hover:bg-[#233241]"> <div className="flex flex-col border border-gray-600 p-4 rounded-lg hover:bg-[#233241]">
<h3 className="text-lg">Start Time</h3> <span className="text-slate-300">{startTime}</span> <h3 className="text-lg">Start Time</h3> <span className="text-slate-300">{startTime}</span>
</div> </div>

View File

@@ -175,6 +175,7 @@ export type OptionalBOF2LaneIDs = {
export type CameraFeedState = { export type CameraFeedState = {
cameraFeedID: CameraID; cameraFeedID: CameraID;
brushSize: number;
paintedCells: Record<CameraID, Map<string, PaintedCell>>; paintedCells: Record<CameraID, Map<string, PaintedCell>>;
regionsByCamera: Record<CameraID, Region[]>; regionsByCamera: Record<CameraID, Region[]>;
@@ -221,6 +222,10 @@ export type CameraFeedAction =
| { | {
type: "SET_ZOOM_LEVEL"; type: "SET_ZOOM_LEVEL";
payload: { cameraFeedID: CameraID; zoomLevel: number }; payload: { cameraFeedID: CameraID; zoomLevel: number };
}
| {
type: "SET_BRUSH_SIZE";
payload: { brushSize: number };
}; };
export type DecodeReading = { export type DecodeReading = {