Camera

class Camera(index: int, framerate: int, name: str)[source]

Bases: CameraBase

Controls a Picamera2 device, handles recording, and performs real-time detection.

width

Frame width.

Type:

int

height

Frame height.

Type:

int

index

Camera index.

Type:

int

name

Camera name (e.g., “BOX”, “CORRIDOR”).

Type:

str

framerate

Camera framerate.

Type:

int

encoder_quality

Quality setting for H264 encoder.

encoder

The video encoder instance.

Type:

H264Encoder

cam

The Picamera2 instance.

Type:

Picamera2

config

The camera configuration.

path_video

Path to the output video file.

Type:

str

path_csv

Path to the output CSV file.

Type:

str

path_picture

Path to save snapshots.

Type:

str

output

The output handler for recording.

Type:

FfmpegOutput

filename

Base filename for recordings.

Type:

str

color_areas

Colors for detection areas.

Type:

list

thickness_line

Line thickness for drawing.

Type:

int

detection_color

Color for detection indicators.

Type:

tuple

detection_size

Size of detection indicators.

Type:

int

color_rectangle

Color for the status rectangle.

Type:

tuple

color_text

Color for text overlays.

Type:

tuple

change

Flag to trigger property updates.

Type:

bool

annotation

Current annotation text.

Type:

str

trial

Current trial number.

Type:

int

tracking

Whether position tracking is enabled.

Type:

bool

x_position

Centroid X coordinate.

Type:

int

y_position

Centroid Y coordinate.

Type:

int

frames

List of frame numbers.

Type:

list[int]

timings

List of timings.

Type:

list[int]

trials

List of trial numbers.

Type:

list[int]

annotations

List of annotations.

Type:

list[str]

x_positions

List of X positions.

Type:

list[int]

y_positions

List of Y positions.

Type:

list[int]

camera_timestamps

List of camera sensor timestamps.

Type:

list[float]

pre_process_timestamps

List of preprocess timestamps.

Type:

list[float]

origin_rectangle

Coordinates for status bar background.

Type:

tuple

end_rectangle

Dimensions for status bar background.

Type:

tuple

origin_text1

Position for first text line.

Type:

tuple

origin_text2

Position for second text line.

Type:

tuple

origin_areas

Positions for area status text.

Type:

list

font

Font used for overlays.

scale

Font scale.

Type:

float

thickness_text

Font thickness.

Type:

int

frame_number

Current frame number.

Type:

int

chrono

Chronometer for timing.

Type:

time_utils.Chrono

masks

Detection masks for each area.

Type:

list

counts

Pixel counts for each area.

Type:

list[int]

error

Error message.

Type:

str

error_frame

Frame number where error occurred.

Type:

int

is_recording

Recording status.

Type:

bool

show_time_info

Whether to display time info on frame.

Type:

bool

two_mice_detections

Counter for multiple mouse detections.

Type:

int

prohibited_detections

Counter for prohibited area detections.

Type:

int

area4_alarm_timer

Timer for area 4 alarms.

Type:

time_utils.Timer

box_alarm_timer

Timer for box alarms.

Type:

time_utils.Timer

pre_process_timestamp

Timestamp of last preprocess call.

Type:

float

camera_timestamp

Timestamp of current frame.

Type:

float

watchdog_timer

Timer to restart camera if frozen.

Type:

QTimer

__init__(index: int, framerate: int, name: str) None[source]

Initializes the Camera.

Parameters:
  • index (int) – Camera index.

  • framerate (int) – Desired framerate.

  • name (str) – Name of the camera.

Methods

__init__(index, framerate, name)

Initializes the Camera.

area_1_empty()

Checks if area 1 is empty.

area_2_empty()

Checks if area 2 is empty.

area_3_empty()

Checks if area 3 is empty.

area_4_empty()

Checks if area 4 is empty.

areas_box_ok()

Checks box areas for allowed/prohibited detections and logs alarms if needed.

areas_corridor_ok()

Checks if the corridor areas are in valid states (no unexpected detections).

detect_and_trigger()

Performs detection based on color settings and position tracking.

detect_black()

Detects black objects in defined areas using thresholding.

detect_black_position_components()

Detects position of black mouse using connected components.

detect_black_position_contours()

Detects position of black mouse using contours.

detect_white()

Detects white objects in defined areas using thresholding.

detect_white_position_components()

Detects position of white mouse using connected components.

detect_white_position_contours()

Detects position of white mouse using contours.

draw_detection()

Draws detection results (thresholded views or position) on the frame.

draw_position()

Draws the current tracked position on the frame.

draw_rectangles()

Draws the status bar background and detection area rectangles.

draw_thresholded_black()

Draws the thresholded view for black detection on the frame.

draw_thresholded_white()

Draws the thresholded view for white detection on the frame.

get_gray_frame()

Converts the current frame to grayscale.

pre_process(request)

Callback for frame processing.

print_info_about_config()

Prints the current camera configuration to console.

reset_values()

Resets all tracking and recording variables to defaults.

restart_camera()

Restarts the camera subprocess and watchdog.

save_csv()

Saves the recorded data frames to a CSV file.

set_properties()

Updates camera detection properties from settings.

start_camera()

Starts the camera capture.

start_preview_window()

Starts a low-frequency preview window for the GUI.

start_recording([path_video, path_csv])

Starts recording video and data.

stop_camera()

Stops the camera capture.

stop_preview_window()

Stops the preview window and resets preview to NULL.

stop_recording()

Stops recording and saves CSV data.

take_picture()

Captures a snapshot from the camera.

trigger()

Checks if detection zones are triggered and notifies the manager.

watchdog_tick()

Checks if the camera is still producing frames, restarts if frozen.

write_csv()

Appends current frame data to internal lists for CSV export.

write_pixel_detection()

Writes pixel count for each area on the frame.

write_text(text)

Sets the annotation text to be displayed on the frame.

write_texts()

Writes status text (filename, trial, timing) onto the frame.

Attributes

path_picture: str = ''
annotation: str = ''
trial: int = -1
x_position: int = -1
y_position: int = -1
change: bool = False
chrono = <village.scripts.time_utils.TimeUtils.Chrono object>
error: str = 'Error connecting to the camera '
is_recording: bool = False
show_time_info: bool = False
set_properties() None[source]

Updates camera detection properties from settings.

start_camera() None[source]

Starts the camera capture.

stop_camera() None[source]

Stops the camera capture.

stop_preview_window() None[source]

Stops the preview window and resets preview to NULL.

start_recording(path_video: str = '', path_csv: str = '') None[source]

Starts recording video and data.

Parameters:
  • path_video (str) – Custom video path. Defaults to automatic naming based on settings.

  • path_csv (str) – Custom CSV path. Defaults to automatic naming based on settings.

stop_recording() None[source]

Stops recording and saves CSV data.

reset_values() None[source]

Resets all tracking and recording variables to defaults.

save_csv() None[source]

Saves the recorded data frames to a CSV file.

print_info_about_config() None[source]

Prints the current camera configuration to console.

watchdog_tick() None[source]

Checks if the camera is still producing frames, restarts if frozen.

restart_camera() None[source]

Restarts the camera subprocess and watchdog.

pre_process(request: Any) None[source]

Callback for frame processing. Handles detection, drawing, and recording.

Parameters:

request (Any) – The camera request containing the frame buffer.

get_gray_frame() None[source]

Converts the current frame to grayscale.

detect_and_trigger() None[source]

Performs detection based on color settings and position tracking.

trigger() None[source]

Checks if detection zones are triggered and notifies the manager.

detect_black() None[source]

Detects black objects in defined areas using thresholding.

detect_white() None[source]

Detects white objects in defined areas using thresholding.

detect_black_position_components() None[source]

Detects position of black mouse using connected components.

detect_white_position_components() None[source]

Detects position of white mouse using connected components.

detect_black_position_contours() None[source]

Detects position of black mouse using contours.

detect_white_position_contours() None[source]

Detects position of white mouse using contours.

draw_detection() None[source]

Draws detection results (thresholded views or position) on the frame.

draw_rectangles() None[source]

Draws the status bar background and detection area rectangles.

write_texts() None[source]

Writes status text (filename, trial, timing) onto the frame.

write_pixel_detection() None[source]

Writes pixel count for each area on the frame.

draw_thresholded_black() None[source]

Draws the thresholded view for black detection on the frame.

draw_thresholded_white() None[source]

Draws the thresholded view for white detection on the frame.

draw_position() None[source]

Draws the current tracked position on the frame.

write_csv() None[source]

Appends current frame data to internal lists for CSV export.

start_preview_window() <MagicMock name='mock.QWidget' id='140286155962896'>[source]

Starts a low-frequency preview window for the GUI.

Returns:

The preview widget.

Return type:

QWidget

write_text(text: str) None[source]

Sets the annotation text to be displayed on the frame.

Parameters:

text (str) – The annotation text.

areas_corridor_ok() bool[source]

Checks if the corridor areas are in valid states (no unexpected detections).

Returns:

True if areas are okay, False otherwise.

Return type:

bool

areas_box_ok() None[source]

Checks box areas for allowed/prohibited detections and logs alarms if needed.

area_1_empty() bool[source]

Checks if area 1 is empty.

area_2_empty() bool[source]

Checks if area 2 is empty.

area_3_empty() bool[source]

Checks if area 3 is empty.

area_4_empty() bool[source]

Checks if area 4 is empty.

take_picture() None[source]

Captures a snapshot from the camera.

area1: list[int] = []
area1_is_triggered: bool = False
area2: list[int] = []
area2_is_triggered: bool = False
area3: list[int] = []
area3_is_triggered: bool = False
area4: list[int] = []
area4_is_triggered: bool = False
areas: list[list[int]] = []