blob: a2724822cb531ebb934df05ce57493700e368cea (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
|
#!/usr/bin/env bash
set -euo pipefail
# Kidcam Deployment Script for Ubuntu 20.04 (Jetson Nano)
readonly SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
readonly PROJECT_ROOT="$(cd "${SCRIPT_DIR}/../.." && pwd)"
readonly KIDCAM_USER="kidcam"
readonly INSTALL_DIR="/opt/kidcam"
readonly CONFIG_DIR="/etc/kidcam"
readonly VENV_DIR="${INSTALL_DIR}/venv"
log_info() {
echo "[INFO] $*" >&2
}
log_error() {
echo "[ERROR] $*" >&2
}
log_success() {
echo "[SUCCESS] $*" >&2
}
check_system() {
log_info "Checking system requirements..."
if [[ "$(uname -s)" != "Linux" ]]; then
log_error "This script requires Linux"
exit 1
fi
if [[ "$(uname -m)" != "aarch64" ]]; then
log_error "This script is designed for aarch64 (ARM64) architecture"
log_error "Found: $(uname -m)"
exit 1
fi
if ! grep -q "Ubuntu" /etc/os-release; then
log_error "This script is designed for Ubuntu"
log_error "Found: $(cat /etc/os-release | grep PRETTY_NAME)"
exit 1
fi
if [[ $EUID -ne 0 ]]; then
log_error "This script must be run as root (use sudo)"
exit 1
fi
log_success "System requirements verified"
}
create_user() {
if id "${KIDCAM_USER}" &>/dev/null; then
log_info "User '${KIDCAM_USER}' already exists"
else
log_info "Creating user '${KIDCAM_USER}'..."
useradd --system --no-create-home --shell /usr/sbin/nologin \
--comment "Kidcam service account" "${KIDCAM_USER}"
usermod -aG video "${KIDCAM_USER}"
log_success "User '${KIDCAM_USER}' created"
fi
}
install_system_dependencies() {
log_info "Installing system dependencies..."
apt-get update
apt-get install -y \
python3 \
python3-pip \
python3-venv \
python3-dev \
libopencv-dev \
python3-opencv \
gstreamer1.0-tools \
gstreamer1.0-plugins-base \
gstreamer1.0-plugins-good \
gstreamer1.0-plugins-bad \
gstreamer1.0-plugins-ugly \
gstreamer1.0-libav \
libgstreamer1.0-dev \
libgstreamer-plugins-base1.0-dev \
v4l-utils \
build-essential \
cmake \
pkg-config
log_success "System dependencies installed"
}
setup_directories() {
log_info "Setting up directories..."
mkdir -p "${INSTALL_DIR}"
mkdir -p "${CONFIG_DIR}"
mkdir -p "${INSTALL_DIR}/_/tmp"
log_info "Copying project files..."
rsync -av --exclude='_/' --exclude='.git' --exclude='*.pyc' \
"${PROJECT_ROOT}/" "${INSTALL_DIR}/"
chown -R "${KIDCAM_USER}:${KIDCAM_USER}" "${INSTALL_DIR}"
chown -R "${KIDCAM_USER}:${KIDCAM_USER}" "${CONFIG_DIR}"
chmod 755 "${INSTALL_DIR}"
chmod 750 "${CONFIG_DIR}"
log_success "Directories configured"
}
setup_python_environment() {
log_info "Setting up Python virtual environment..."
if [[ ! -d "${VENV_DIR}" ]]; then
sudo -u "${KIDCAM_USER}" python3 -m venv "${VENV_DIR}"
log_success "Virtual environment created"
else
log_info "Virtual environment already exists"
fi
log_info "Installing Python packages..."
sudo -u "${KIDCAM_USER}" "${VENV_DIR}/bin/pip" install --upgrade pip wheel setuptools
# Install packages with careful dependency management
sudo -u "${KIDCAM_USER}" "${VENV_DIR}/bin/pip" install \
ultralytics \
python-telegram-bot \
opencv-python \
torch torchvision --index-url https://download.pytorch.org/whl/cpu \
numpy \
pillow
log_success "Python packages installed"
}
create_config() {
local config_file="${CONFIG_DIR}/config.env"
if [[ -f "${config_file}" ]]; then
log_info "Config file already exists at ${config_file}"
log_info "Skipping config creation. Review existing configuration."
else
log_info "Creating example configuration..."
cat > "${config_file}" <<'EOF'
# Kidcam Configuration
# Copy this file to /etc/kidcam/config.env and edit with your values
# Camera device (default: /dev/video0)
CAMERA_DEVICE=/dev/video0
# RTSP stream port (default: 8554)
STREAM_PORT=8554
# Person detection confidence threshold (0.0-1.0, default: 0.5)
DETECTION_CONFIDENCE=0.5
# Cooldown period in minutes after detection (default: 5)
COOLDOWN_MINUTES=5
# Telegram Bot Configuration
# Get your bot token from @BotFather on Telegram
TELEGRAM_BOT_TOKEN=your-bot-token-here
# Get your chat ID by messaging your bot and checking:
# https://api.telegram.org/bot<YOUR_BOT_TOKEN>/getUpdates
TELEGRAM_CHAT_ID=your-chat-id-here
# Python path for imports
PYTHONPATH=/opt/kidcam
EOF
chown "${KIDCAM_USER}:${KIDCAM_USER}" "${config_file}"
chmod 640 "${config_file}"
log_success "Example config created at ${config_file}"
log_info "IMPORTANT: Edit ${config_file} with your Telegram credentials"
fi
}
install_systemd_service() {
log_info "Installing systemd service..."
cp "${SCRIPT_DIR}/kidcam.service" /etc/systemd/system/
chmod 644 /etc/systemd/system/kidcam.service
# Update ExecStart to use virtualenv python
sed -i "s|ExecStart=/usr/bin/python3|ExecStart=${VENV_DIR}/bin/python3|" \
/etc/systemd/system/kidcam.service
systemctl daemon-reload
systemctl enable kidcam.service
log_success "Systemd service installed and enabled"
}
main() {
log_info "Starting Kidcam deployment..."
check_system
create_user
install_system_dependencies
setup_directories
setup_python_environment
create_config
install_systemd_service
log_success "Deployment complete!"
echo ""
log_info "Next steps:"
echo " 1. Edit /etc/kidcam/config.env with your Telegram credentials"
echo " 2. Test camera access: v4l2-ctl --list-devices"
echo " 3. Start service: sudo systemctl start kidcam"
echo " 4. Check logs: sudo journalctl -u kidcam -f"
echo ""
log_info "For more information, see ${INSTALL_DIR}/Biz/Kidcam/README.md"
}
main "$@"
|