Skip to content

How it works

OpenCV does the watching on every camera. The AI only checks the moments OpenCV flags, and never watches a stream.

Pehra overview: cameras feed ingest and OpenCV perception; watches in plain words and an agent with OpenCV tools turn flagged moments into incidents for the control room and Telegram; logs feed the gate log and the staff register; it runs on one box on AWS or any Linux machine.
Every piece at a glance. Open the picture for full size.

From a camera to an alert

  1. Pictures arrive from recorders and IP cameras over RTSP, the bridge, a phone or laptop, or an upload.
  2. Camera processes run the OpenCV pipeline at the analysis rate: detect, track, zones and lines, pose, faces at entries, plates, camera health.
  3. A watch in plain words is compiled once, by the AI, into general signals such as "a person inside the pool zone after 8 PM". A rule engine checks those signals on every analysed frame and flags a candidate.
  4. The agent checks the candidate with OpenCV tools and decides what happens.
  5. An incident opens, graded critical, warning or info, with a still and a clip, in the control room and on Telegram.
  6. Gate crossings and staff seen at entries are written to the logs.
Pehra architecture: the committee in a browser, a phone or laptop, the Pehra bridge and CCTV recorders over RTSP reach one EC2 Graviton4 instance with COOL in AWS Mumbai. On it the web app, the camera processes running OpenCV 5, the AI check with its OpenCV tools and the job worker; flagged moments go to the AI check, incidents to the job worker, and alerts to the society's Telegram group. S3, Bedrock, Parameter Store, CloudWatch, SNS and CloudFormation complete the stack.
The parts and where they run. Open the picture for full size.

What OpenCV does

StepOpenCVWhy
Read cameras and recordingscv2.VideoCapture with FFmpegRTSP streams, uploads and the demo clip
Pictures from phones and the bridgecv2.imdecode after a size checka tiny file cannot decode to gigabytes
Skip still scenescreateBackgroundSubtractorMOG2, morphologyEx, findContoursthe detector runs when something moves
Dark framescreateCLAHEbrightened before detection at night
People and vehiclesYOLOX nano on cv2.dnn, NMSBoxesBatchedevery analysed frame
Trackingcv2.KalmanFiltersteady ids for dwell time and line crossings
Zones and linespointPolygonTestinside, entering and crossing with a direction
Falls and postureMediaPipe pose on cv2.dnn, warpAffinetorso angle over time
Movement inside a boxDISOpticalFlow_createa still body in moving water
FacesFaceDetectorYN (YuNet), FaceRecognizerSF (SFace)blur everyone; match consenting staff at entries
PlatesLPD-YuNet and PP-OCR on cv2.dnn, warpPerspectivethe gate log's plate text and colour
Camera healthORB, BFMatcher, findHomography with USAC_MAGSAC, Laplacianturned, blurred or covered cameras
Gate open or closedGaussianBlur, CLAHE, Sobel, matchTemplatea zone's state against example pictures
Looks described in wordsSigLIP 2 on ONNX Runtime, or cv2.dnnthe image and text model; OpenCV prepares every crop
The agent's toolsresize and unsharp mask, CLAHE, fastNlMeansDenoisingColored, DIS flowa closer look at a flagged moment
Telegram stillsGaussianBlur on face boxes, imencodefaces of anyone not enrolled are blurred
The phone camera pageOpenCV.js FaceDetectorYNfaces blurred before a picture leaves the phone

The agent

Pehra agent workflow: OpenCV perceives each frame, the watch engine flags a moment or sends nothing, camera health and staff faces skip the AI, and the AI check calls OpenCV tools (more frames, zoom, brighten, denoise, flow, pose, path walked) in a loop until it alerts with a grade, asks a person, cannot judge, or dismisses. An incident goes to the control room and Telegram, and Acknowledge or False alarm comes back.
Perceive, decide, act: the AI check and its OpenCV tools. Open the picture for full size.

The agent is an LLM that sees only a flagged moment: a few stills and the watch's words. It can look closer with OpenCV tools (frames from the moment, a sharpened zoom, brighten, denoise or sharpen, optical flow inside the box, the person's pose over time, the path walked), then it must finish one way:

  • raise the alert, with a grade and a short reason;
  • dismiss it, because the pictures show it is not what the watch asks for;
  • ask a person, for a serious moment someone should decide;
  • or say it cannot judge, when the question needs someone's age, which it never judges.

What the AI does not decide:

  • A critical alert goes to Telegram at once, before the check. A critical watch is dismissed only when the pictures clearly show everyone is safe.
  • Past the daily AI limit, or when the AI cannot be reached, a moment is still filed, marked not checked by the AI.
  • Camera health and enrolled staff faces are measured by OpenCV; no AI looks at them.
  • It never names anyone. Only OpenCV recognises people, and only consenting adult staff at entries.

On AWS the AI is Claude Haiku 5.5 on Amazon Bedrock. The live demo uses an OpenRouter model. The same AI also compiles watches, answers questions about what happened, and searches footage by words.

The full design, the evaluation and the failure cases are in the technical report.

Next: Privacy and safety