Compare commits
4 Commits
6fc17ccf64
...
3af7a0f805
| Author | SHA1 | Date | |
|---|---|---|---|
| 3af7a0f805 | |||
| cb46d12cfa | |||
| 123903950b | |||
| 2d5ada2909 |
26
api/roi.py
26
api/roi.py
@@ -29,9 +29,10 @@ class CreateROIRequest(BaseModel):
|
||||
rule_type: str
|
||||
direction: Optional[str] = None
|
||||
stay_time: Optional[int] = None
|
||||
threshold_sec: int = 360
|
||||
confirm_sec: int = 30
|
||||
return_sec: int = 5
|
||||
threshold_sec: int = 300
|
||||
confirm_sec: int = 10
|
||||
return_sec: int = 30
|
||||
working_hours: Optional[List[dict]] = None
|
||||
|
||||
|
||||
class UpdateROIRequest(BaseModel):
|
||||
@@ -45,6 +46,7 @@ class UpdateROIRequest(BaseModel):
|
||||
threshold_sec: Optional[int] = None
|
||||
confirm_sec: Optional[int] = None
|
||||
return_sec: Optional[int] = None
|
||||
working_hours: Optional[List[dict]] = None
|
||||
|
||||
|
||||
def _invalidate_roi_cache(camera_id: int):
|
||||
@@ -70,6 +72,7 @@ def list_rois(camera_id: int, db: Session = Depends(get_db)):
|
||||
"threshold_sec": roi.threshold_sec,
|
||||
"confirm_sec": roi.confirm_sec,
|
||||
"return_sec": roi.return_sec,
|
||||
"working_hours": json.loads(roi.working_hours) if roi.working_hours else None,
|
||||
}
|
||||
for roi in roi_configs
|
||||
]
|
||||
@@ -93,6 +96,7 @@ def get_roi(camera_id: int, roi_id: int, db: Session = Depends(get_db)):
|
||||
"threshold_sec": roi.threshold_sec,
|
||||
"confirm_sec": roi.confirm_sec,
|
||||
"return_sec": roi.return_sec,
|
||||
"working_hours": json.loads(roi.working_hours) if roi.working_hours else None,
|
||||
}
|
||||
|
||||
|
||||
@@ -102,6 +106,10 @@ def add_roi(
|
||||
request: CreateROIRequest,
|
||||
db: Session = Depends(get_db),
|
||||
):
|
||||
import json
|
||||
|
||||
working_hours_json = json.dumps(request.working_hours) if request.working_hours else None
|
||||
|
||||
roi = create_roi(
|
||||
db,
|
||||
camera_id=camera_id,
|
||||
@@ -115,6 +123,7 @@ def add_roi(
|
||||
threshold_sec=request.threshold_sec,
|
||||
confirm_sec=request.confirm_sec,
|
||||
return_sec=request.return_sec,
|
||||
working_hours=working_hours_json,
|
||||
)
|
||||
|
||||
_invalidate_roi_cache(camera_id)
|
||||
@@ -126,7 +135,13 @@ def add_roi(
|
||||
"type": roi.roi_type,
|
||||
"points": request.points,
|
||||
"rule": roi.rule_type,
|
||||
"direction": roi.direction,
|
||||
"stay_time": roi.stay_time,
|
||||
"enabled": roi.enabled,
|
||||
"threshold_sec": roi.threshold_sec,
|
||||
"confirm_sec": roi.confirm_sec,
|
||||
"return_sec": roi.return_sec,
|
||||
"working_hours": request.working_hours,
|
||||
}
|
||||
|
||||
|
||||
@@ -137,6 +152,9 @@ def modify_roi(
|
||||
request: UpdateROIRequest,
|
||||
db: Session = Depends(get_db),
|
||||
):
|
||||
import json
|
||||
working_hours_json = json.dumps(request.working_hours) if request.working_hours else None
|
||||
|
||||
roi = update_roi(
|
||||
db,
|
||||
roi_id=roi_id,
|
||||
@@ -149,6 +167,7 @@ def modify_roi(
|
||||
threshold_sec=request.threshold_sec,
|
||||
confirm_sec=request.confirm_sec,
|
||||
return_sec=request.return_sec,
|
||||
working_hours=working_hours_json,
|
||||
)
|
||||
if not roi:
|
||||
raise HTTPException(status_code=404, detail="ROI不存在")
|
||||
@@ -163,6 +182,7 @@ def modify_roi(
|
||||
"points": json.loads(roi.points),
|
||||
"rule": roi.rule_type,
|
||||
"enabled": roi.enabled,
|
||||
"working_hours": json.loads(roi.working_hours) if roi.working_hours else None,
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -66,9 +66,9 @@ class WorkingHours(BaseModel):
|
||||
|
||||
|
||||
class AlgorithmsConfig(BaseModel):
|
||||
leave_post_threshold_sec: int = 360
|
||||
leave_post_confirm_sec: int = 30
|
||||
leave_post_return_sec: int = 5
|
||||
leave_post_threshold_sec: int = 300
|
||||
leave_post_confirm_sec: int = 10
|
||||
leave_post_return_sec: int = 30
|
||||
intrusion_check_interval_sec: float = 1.0
|
||||
intrusion_direction_sensitive: bool = False
|
||||
|
||||
|
||||
10
config.yaml
10
config.yaml
@@ -70,9 +70,9 @@ working_hours:
|
||||
# 算法默认参数
|
||||
algorithms:
|
||||
leave_post:
|
||||
default_threshold_sec: 360 # 离岗超时(6分钟)
|
||||
confirm_sec: 30 # 离岗确认时间
|
||||
return_sec: 5 # 上岗确认时间
|
||||
threshold_sec: 300 # 离岗超时(5分钟)
|
||||
confirm_sec: 10 # 上岗确认时间(10秒)
|
||||
return_sec: 30 # 离岗缓冲时间(30秒)
|
||||
intrusion:
|
||||
check_interval_sec: 1.0 # 检测间隔
|
||||
direction_sensitive: false # 方向敏感
|
||||
@@ -94,7 +94,7 @@ monitoring:
|
||||
# 大模型配置(预留)
|
||||
llm:
|
||||
enabled: false
|
||||
api_key: ""
|
||||
base_url: ""
|
||||
api_key: "sk-21e61bef09074682b589da3bdbfe07a2"
|
||||
base_url: "https://dashscope.aliyuncs.com/compatible-mode/v1"
|
||||
model: "qwen3-vl-max"
|
||||
timeout: 30
|
||||
|
||||
@@ -139,6 +139,7 @@ def create_roi(
|
||||
threshold_sec: int = 300,
|
||||
confirm_sec: int = 10,
|
||||
return_sec: int = 30,
|
||||
working_hours: Optional[str] = None,
|
||||
) -> ROI:
|
||||
import json
|
||||
|
||||
@@ -154,6 +155,7 @@ def create_roi(
|
||||
threshold_sec=threshold_sec,
|
||||
confirm_sec=confirm_sec,
|
||||
return_sec=return_sec,
|
||||
working_hours=working_hours,
|
||||
)
|
||||
db.add(roi)
|
||||
db.commit()
|
||||
@@ -173,6 +175,7 @@ def update_roi(
|
||||
threshold_sec: Optional[int] = None,
|
||||
confirm_sec: Optional[int] = None,
|
||||
return_sec: Optional[int] = None,
|
||||
working_hours: Optional[str] = None,
|
||||
) -> Optional[ROI]:
|
||||
import json
|
||||
|
||||
@@ -198,6 +201,8 @@ def update_roi(
|
||||
roi.confirm_sec = confirm_sec
|
||||
if return_sec is not None:
|
||||
roi.return_sec = return_sec
|
||||
if working_hours is not None:
|
||||
roi.working_hours = working_hours
|
||||
|
||||
db.commit()
|
||||
db.refresh(roi)
|
||||
@@ -232,6 +237,7 @@ def get_roi_points(db: Session, camera_id: int) -> List[dict]:
|
||||
"threshold_sec": roi.threshold_sec,
|
||||
"confirm_sec": roi.confirm_sec,
|
||||
"return_sec": roi.return_sec,
|
||||
"working_hours": json.loads(roi.working_hours) if roi.working_hours else None,
|
||||
}
|
||||
for roi in rois
|
||||
]
|
||||
|
||||
@@ -93,6 +93,7 @@ class ROI(Base):
|
||||
threshold_sec: Mapped[int] = mapped_column(Integer, default=300)
|
||||
confirm_sec: Mapped[int] = mapped_column(Integer, default=10)
|
||||
return_sec: Mapped[int] = mapped_column(Integer, default=30)
|
||||
working_hours: Mapped[Optional[str]] = mapped_column(Text, nullable=True)
|
||||
pending_sync: Mapped[bool] = mapped_column(Boolean, default=False)
|
||||
sync_version: Mapped[int] = mapped_column(Integer, default=0)
|
||||
created_at: Mapped[datetime] = mapped_column(DateTime, default=datetime.utcnow)
|
||||
|
||||
@@ -38,14 +38,48 @@ const CameraManagement: React.FC = () => {
|
||||
const fetchCameras = async () => {
|
||||
setLoading(true);
|
||||
try {
|
||||
const [camerasRes, statusRes] = await Promise.all([
|
||||
const [camerasRes, statusRes, pipelineRes] = await Promise.all([
|
||||
axios.get('/api/cameras?enabled_only=false'),
|
||||
axios.get('/api/camera/status/all'),
|
||||
axios.get('/api/pipeline/status')
|
||||
]);
|
||||
setCameras(camerasRes.data);
|
||||
setCameraStatus(statusRes.data.cameras || {});
|
||||
|
||||
const statusMap: Record<number, CameraStatus> = {};
|
||||
|
||||
for (const cam of camerasRes.data) {
|
||||
const camId = cam.id;
|
||||
|
||||
let status: CameraStatus = {
|
||||
is_running: false,
|
||||
fps: 0,
|
||||
error_message: null,
|
||||
last_check_time: null,
|
||||
};
|
||||
|
||||
const pipelineStatus = pipelineRes.data.cameras?.[String(camId)];
|
||||
if (pipelineStatus) {
|
||||
status.is_running = pipelineStatus.is_running || false;
|
||||
status.fps = pipelineStatus.fps || 0;
|
||||
status.last_check_time = pipelineStatus.last_check_time;
|
||||
}
|
||||
|
||||
const dbStatus = statusRes.data.find((s: any) => s.camera_id === camId);
|
||||
if (dbStatus) {
|
||||
if (!status.is_running) {
|
||||
status.is_running = dbStatus.is_running || false;
|
||||
}
|
||||
status.fps = dbStatus.fps || status.fps;
|
||||
status.error_message = dbStatus.error_message;
|
||||
status.last_check_time = status.last_check_time || dbStatus.last_check_time;
|
||||
}
|
||||
|
||||
statusMap[camId] = status;
|
||||
}
|
||||
|
||||
setCameraStatus(statusMap);
|
||||
} catch (err) {
|
||||
message.error('获取摄像头列表失败');
|
||||
console.error('获取摄像头状态失败', err);
|
||||
} finally {
|
||||
setLoading(false);
|
||||
}
|
||||
@@ -59,8 +93,8 @@ const CameraManagement: React.FC = () => {
|
||||
|
||||
const extractIP = (url: string): string => {
|
||||
try {
|
||||
const match = url.match(/:\/\/([^:]+):?(\d+)?\//);
|
||||
return match ? match[1] : '未知';
|
||||
const ipMatch = url.match(/(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})/);
|
||||
return ipMatch ? ipMatch[1] : '未知';
|
||||
} catch {
|
||||
return '未知';
|
||||
}
|
||||
|
||||
@@ -1,7 +1,14 @@
|
||||
import React, { useEffect, useState, useRef } from 'react';
|
||||
import { Card, Button, Space, Select, message, Drawer, Form, Input, InputNumber, Switch } from 'antd';
|
||||
import { Card, Button, Space, Select, message, Drawer, Form, Input, InputNumber, Switch, TimePicker, Divider } from 'antd';
|
||||
import { Stage, Layer, Rect, Line, Circle, Text as KonvaText } from 'react-konva';
|
||||
import { RangePickerProps } from 'antd/es/date-picker';
|
||||
import axios from 'axios';
|
||||
import dayjs from 'dayjs';
|
||||
|
||||
interface WorkingHours {
|
||||
start: number[];
|
||||
end: number[];
|
||||
}
|
||||
|
||||
interface ROI {
|
||||
id: number;
|
||||
@@ -13,6 +20,7 @@ interface ROI {
|
||||
threshold_sec: number;
|
||||
confirm_sec: number;
|
||||
return_sec: number;
|
||||
working_hours: WorkingHours[] | null;
|
||||
}
|
||||
|
||||
interface Camera {
|
||||
@@ -29,12 +37,38 @@ const ROIEditor: React.FC = () => {
|
||||
const [selectedROI, setSelectedROI] = useState<ROI | null>(null);
|
||||
const [drawerVisible, setDrawerVisible] = useState(false);
|
||||
const [form] = Form.useForm();
|
||||
const [workingHoursList, setWorkingHoursList] = useState<{start: dayjs.Dayjs | null, end: dayjs.Dayjs | null}[]>([]);
|
||||
|
||||
const [isDrawing, setIsDrawing] = useState(false);
|
||||
const [tempPoints, setTempPoints] = useState<number[][]>([]);
|
||||
const [backgroundImage, setBackgroundImage] = useState<HTMLImageElement | null>(null);
|
||||
const stageRef = useRef<any>(null);
|
||||
|
||||
const addWorkingHours = () => {
|
||||
setWorkingHoursList([...workingHoursList, { start: null, end: null }]);
|
||||
};
|
||||
|
||||
const removeWorkingHours = (index: number) => {
|
||||
const newList = workingHoursList.filter((_, i) => i !== index);
|
||||
setWorkingHoursList(newList);
|
||||
};
|
||||
|
||||
const updateWorkingHours = (index: number, field: 'start' | 'end', value: dayjs.Dayjs | null) => {
|
||||
const newList = [...workingHoursList];
|
||||
newList[index] = { ...newList[index], [field]: value };
|
||||
setWorkingHoursList(newList);
|
||||
};
|
||||
|
||||
const updateWorkingHoursRange = (index: number, start: dayjs.Dayjs | null, end: dayjs.Dayjs | null) => {
|
||||
setWorkingHoursList(prev => {
|
||||
const newList = [...prev];
|
||||
if (newList[index]) {
|
||||
newList[index] = { start, end };
|
||||
}
|
||||
return newList;
|
||||
});
|
||||
};
|
||||
|
||||
const fetchCameras = async () => {
|
||||
try {
|
||||
const res = await axios.get('/api/cameras?enabled_only=true');
|
||||
@@ -95,16 +129,25 @@ const ROIEditor: React.FC = () => {
|
||||
const handleSaveROI = async (values: any) => {
|
||||
if (!selectedCamera || !selectedROI) return;
|
||||
try {
|
||||
const workingHours = workingHoursList
|
||||
.filter(item => item.start && item.end)
|
||||
.map(item => ({
|
||||
start: [item.start!.hour(), item.start!.minute()],
|
||||
end: [item.end!.hour(), item.end!.minute()],
|
||||
}));
|
||||
|
||||
await axios.put(`/api/camera/${selectedCamera}/roi/${selectedROI.id}`, {
|
||||
name: values.name,
|
||||
roi_type: values.roi_type,
|
||||
rule_type: values.rule_type,
|
||||
threshold_sec: values.threshold_sec,
|
||||
confirm_sec: values.confirm_sec,
|
||||
working_hours: workingHours,
|
||||
enabled: values.enabled,
|
||||
});
|
||||
message.success('保存成功');
|
||||
setDrawerVisible(false);
|
||||
setWorkingHoursList([]);
|
||||
fetchROIs();
|
||||
} catch (err: any) {
|
||||
message.error(`保存失败: ${err.response?.data?.detail || '未知错误'}`);
|
||||
@@ -150,6 +193,7 @@ const ROIEditor: React.FC = () => {
|
||||
threshold_sec: 60,
|
||||
confirm_sec: 5,
|
||||
return_sec: 5,
|
||||
working_hours: [],
|
||||
})
|
||||
.then(() => {
|
||||
message.success('ROI添加成功');
|
||||
@@ -212,6 +256,10 @@ const ROIEditor: React.FC = () => {
|
||||
confirm_sec: roi.confirm_sec,
|
||||
enabled: roi.enabled,
|
||||
});
|
||||
setWorkingHoursList(roi.working_hours?.map((wh: WorkingHours) => ({
|
||||
start: wh.start ? dayjs().hour(wh.start[0]).minute(wh.start[1]) : null,
|
||||
end: wh.end ? dayjs().hour(wh.end[0]).minute(wh.end[1]) : null,
|
||||
})) || []);
|
||||
setDrawerVisible(true);
|
||||
}}
|
||||
onMouseEnter={(e) => {
|
||||
@@ -369,6 +417,10 @@ const ROIEditor: React.FC = () => {
|
||||
confirm_sec: roi.confirm_sec,
|
||||
enabled: roi.enabled,
|
||||
});
|
||||
setWorkingHoursList(roi.working_hours?.map((wh: WorkingHours) => ({
|
||||
start: wh.start ? dayjs().hour(wh.start[0]).minute(wh.start[1]) : null,
|
||||
end: wh.end ? dayjs().hour(wh.end[0]).minute(wh.end[1]) : null,
|
||||
})) || []);
|
||||
setDrawerVisible(true);
|
||||
}}
|
||||
>
|
||||
@@ -403,6 +455,7 @@ const ROIEditor: React.FC = () => {
|
||||
onClose={() => {
|
||||
setDrawerVisible(false);
|
||||
setSelectedROI(null);
|
||||
setWorkingHoursList([]);
|
||||
}}
|
||||
width={400}
|
||||
>
|
||||
@@ -434,6 +487,39 @@ const ROIEditor: React.FC = () => {
|
||||
<Form.Item name="confirm_sec" label="确认时间(秒)" rules={[{ required: true }]}>
|
||||
<InputNumber min={5} style={{ width: '100%' }} />
|
||||
</Form.Item>
|
||||
<Divider>工作时间配置(可选)</Divider>
|
||||
<div>
|
||||
{workingHoursList.map((item, index) => (
|
||||
<Space key={index} align="baseline" style={{ display: 'flex', marginBottom: 8 }}>
|
||||
<Form.Item label={index === 0 ? '时间段' : ''} style={{ marginBottom: 0 }}>
|
||||
<TimePicker.RangePicker
|
||||
format="HH:mm"
|
||||
value={item.start && item.end ? [item.start, item.end] : null}
|
||||
onChange={(dates) => {
|
||||
if (dates && Array.isArray(dates) && dates.length >= 2 && dates[0] && dates[1]) {
|
||||
updateWorkingHoursRange(index, dates[0], dates[1]);
|
||||
} else {
|
||||
updateWorkingHoursRange(index, null, null);
|
||||
}
|
||||
}}
|
||||
/>
|
||||
</Form.Item>
|
||||
<Button
|
||||
type="link"
|
||||
danger
|
||||
onClick={() => removeWorkingHours(index)}
|
||||
>
|
||||
删除
|
||||
</Button>
|
||||
</Space>
|
||||
))}
|
||||
<Button type="dashed" onClick={addWorkingHours} block>
|
||||
添加时间段
|
||||
</Button>
|
||||
</div>
|
||||
<Form.Item style={{ fontSize: 12, color: '#999' }}>
|
||||
不配置工作时间则使用系统全局设置
|
||||
</Form.Item>
|
||||
</>
|
||||
)}
|
||||
<Form.Item name="enabled" label="启用状态" valuePropName="checked">
|
||||
@@ -447,6 +533,7 @@ const ROIEditor: React.FC = () => {
|
||||
<Button onClick={() => {
|
||||
setDrawerVisible(false);
|
||||
setSelectedROI(null);
|
||||
setWorkingHoursList([]);
|
||||
}}>
|
||||
取消
|
||||
</Button>
|
||||
|
||||
@@ -190,6 +190,7 @@ class InferencePipeline:
|
||||
"threshold_sec": roi_config.get("threshold_sec", 300),
|
||||
"confirm_sec": roi_config.get("confirm_sec", 10),
|
||||
"return_sec": roi_config.get("return_sec", 30),
|
||||
"working_hours": roi_config.get("working_hours"),
|
||||
},
|
||||
)
|
||||
|
||||
|
||||
@@ -205,6 +205,7 @@ class IntrusionAlgorithm:
|
||||
|
||||
def process(
|
||||
self,
|
||||
roi_id: str,
|
||||
camera_id: str,
|
||||
tracks: List[Dict],
|
||||
current_time: Optional[datetime] = None,
|
||||
@@ -236,7 +237,7 @@ class IntrusionAlgorithm:
|
||||
|
||||
for track_data in tracked:
|
||||
x1, y1, x2, y2, track_id = track_data
|
||||
cooldown_key = f"{camera_id}_{int(track_id)}"
|
||||
cooldown_key = f"{roi_id}_{int(track_id)}"
|
||||
|
||||
if cooldown_key not in self.alert_cooldowns or (
|
||||
now - self.alert_cooldowns[cooldown_key]
|
||||
@@ -292,11 +293,12 @@ class AlgorithmManager:
|
||||
algo_params.update(params)
|
||||
|
||||
if algorithm_type == "leave_post":
|
||||
roi_working_hours = algo_params.get("working_hours") or self.working_hours
|
||||
self.algorithms[roi_id]["leave_post"] = LeavePostAlgorithm(
|
||||
threshold_sec=algo_params.get("threshold_sec", 300),
|
||||
confirm_sec=algo_params.get("confirm_sec", 10),
|
||||
return_sec=algo_params.get("return_sec", 30),
|
||||
working_hours=self.working_hours,
|
||||
working_hours=roi_working_hours,
|
||||
)
|
||||
elif algorithm_type == "intrusion":
|
||||
self.algorithms[roi_id]["intrusion"] = IntrusionAlgorithm(
|
||||
|
||||
Binary file not shown.
Binary file not shown.
@@ -58,7 +58,7 @@ def test_leave_post_algorithm_process():
|
||||
{"bbox": [100, 100, 200, 200], "conf": 0.9, "cls": 0},
|
||||
]
|
||||
|
||||
alerts = algo.process("test_cam", tracks, datetime.now())
|
||||
alerts = algo.process("roi_1", "test_cam", tracks, datetime.now())
|
||||
assert isinstance(alerts, list)
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user