はじめに
Pythonの主な開発環境には
- CPython(Pythonの基本パッケージ)
- Anaconda(CPython+数値計算ライブラリ+各種ツール)
- Miniconda(Anacondaから必要最小限のライブラリやツールを抜粋)
- Miniforge(Minicondaでdefaultリポジトリを削除してconda-forgeリポジトリを追加)
がありますが、このうちAnacondaとMinicondaはお使いの環境により有償となる場合があります。そこで本記事では、無償の環境構築を目的として、Minicondaと同じ操作で使える無償のMiniforgeを使って、ラズベリーパイ(Raspberry PI)にPythonの開発環境を構築します。画像処理はOpenCV、ディープラーニングにはKerasが使えるようにします。
やり方
-
MiniForge のダウンロード
wget https://github.com/conda-forge/miniforge/releases/latest/download/Miniforge3-Linux-aarch64.sh
-
Miniforge のインストール
bash Miniforge3-Linux-aarch64.sh
ライセンスに同意(Enter連打 + yes)をするとインストールが始まります。 -
condaの有効化
echo ". $HOME/miniforge3/etc/profile.d/conda.sh" >> ~/.bashrc
source ~/.bashrc
-
仮想環境の作成
conda create -n rpi-ai python=3.9
仮想環境名は「rpi-ai」としています。 -
仮想環境への移行
conda activate rpi-ai
-
OpenCV のインストール
conda install -c conda-forge opencv
動作確認
python -c "import cv2; print(cv2.__version__)"
-
pipで TensorFlow をインストール
pip install --upgrade pip
pip install tensorflow
動作確認
python -c "import tensorflow as tf; print(tf.keras.__version__)"
OpenCVの動作確認
-
OpenCVのサンプルプログラムのダウンロード
以下のサイトから、Code-Download ZIPでファイルを一式ダウンロードして、適当なフォルダに解凍します。
https://github.com/opencv/opencv -
デモプログラムの起動
opencv-master/samples/python/demo.py を実行します。次のようなアプリが起動します。
左側のファイルの一覧から選んでRunをクリックすると、デモプログラムが実行されます。
参考サイト
Kerasの動作確認
- サンプルプログラムの実行
以下サイトにあるPythonプログラムをThonnyに貼り付けて実行します。
https://github.com/keras-team/keras/blob/master/examples/demo_mnist_convnet.py
実行結果です。
-------------------------------------------
実行結果を表示(折りたたみ)
---------------------------------------------
x_train shape: (60000, 28, 28, 1)
60000 train samples
10000 test samples
Model: "sequential"
┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━┓
┃ Layer (type) ┃ Output Shape ┃ Param # ┃
┡━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━┩
│ conv2d (Conv2D) │ (None, 26, 26, 32) │ 320 │
├─────────────────────────────────┼────────────────────────┼───────────────┤
│ max_pooling2d (MaxPooling2D) │ (None, 13, 13, 32) │ 0 │
├─────────────────────────────────┼────────────────────────┼───────────────┤
│ conv2d_1 (Conv2D) │ (None, 11, 11, 64) │ 18,496 │
├─────────────────────────────────┼────────────────────────┼───────────────┤
│ max_pooling2d_1 (MaxPooling2D) │ (None, 5, 5, 64) │ 0 │
├─────────────────────────────────┼────────────────────────┼───────────────┤
│ flatten (Flatten) │ (None, 1600) │ 0 │
├─────────────────────────────────┼────────────────────────┼───────────────┤
│ dropout (Dropout) │ (None, 1600) │ 0 │
├─────────────────────────────────┼────────────────────────┼───────────────┤
│ dense (Dense) │ (None, 10) │ 16,010 │
└─────────────────────────────────┴────────────────────────┴───────────────┘
Total params: 34,826 (136.04 KB)
Trainable params: 34,826 (136.04 KB)
Non-trainable params: 0 (0.00 B)
2025-09-15 14:47:20.834363: W external/local_xla/xla/tsl/framework/cpu_allocator_impl.cc:84] Allocation of 169344000 exceeds 10% of free system memory.
Epoch 1/3
422/422 ━━━━━━━━━━━━━━━━━━━━ 47s 107ms/step - accuracy: 0.7739 - loss: 0.7486 - val_accuracy: 0.9777 - val_loss: 0.0855
Epoch 2/3
422/422 ━━━━━━━━━━━━━━━━━━━━ 44s 103ms/step - accuracy: 0.9619 - loss: 0.1212 - val_accuracy: 0.9842 - val_loss: 0.0558
Epoch 3/3
422/422 ━━━━━━━━━━━━━━━━━━━━ 51s 120ms/step - accuracy: 0.9723 - loss: 0.0871 - val_accuracy: 0.9868 - val_loss: 0.0496
Test loss: 0.048455528914928436
Test accuracy: 0.9843999743461609
作成した仮想環境です。
-------------------------------------------
environment.ymlを表示(折りたたみ)
---------------------------------------------
name: rpi-ai
channels:
- conda-forge
dependencies: - _openmp_mutex=4.5=2_gnu
- alsa-lib=1.2.14=h86ecc28_0
- aom=3.9.1=hcccb83c_0
- attr=2.5.1=h4e544f5_1
- bzip2=1.0.8=h4777abc_8
- c-ares=1.34.5=h86ecc28_0
- ca-certificates=2025.8.3=hbd8a1cb_0
- cairo=1.18.4=h83712da_0
- cyrus-sasl=2.1.28=h6c5dea3_0
- dav1d=1.2.1=h31becfc_0
- dbus=1.16.2=heda779d_0
- double-conversion=3.3.1=h5ad3122_0
- ffmpeg=7.1.1=gpl_h8d881e6_910
- font-ttf-dejavu-sans-mono=2.37=hab24e00_0
- font-ttf-inconsolata=3.000=h77eed37_0
- font-ttf-source-code-pro=2.038=h77eed37_0
- font-ttf-ubuntu=0.83=h77eed37_3
- fontconfig=2.15.0=h8dda3cd_1
- fonts-conda-ecosystem=1=0
- fonts-conda-forge=1=0
- freeglut=3.2.2=h5eeb66e_3
- freetype=2.14.0=h8af1aa0_1
- fribidi=1.0.16=he30d5cf_0
- gdk-pixbuf=2.42.12=h90308e0_3
- gettext=0.25.1=h5ad3122_0
- gettext-tools=0.25.1=h5ad3122_0
- gmp=6.3.0=h0a1ffab_2
- graphite2=1.3.14=hfae3067_2
- harfbuzz=11.4.5=he4899c9_0
- hdf5=1.14.6=nompi_h587839b_103
- icu=75.1=hf9b3779_0
- imath=3.2.1=h92288e7_0
- jasper=4.2.8=h27a9ab5_0
- keyutils=1.6.3=h86ecc28_0
- krb5=1.21.3=h50a48e9_0
- lame=3.100=h4e544f5_1003
- ld_impl_linux-aarch64=2.44=h5e2c951_1
- lerc=4.0.0=hfdc4d58_1
- libabseil=20250512.1=cxx17_h201e9ed_0
- libaec=1.1.4=h1e66f74_0
- libasprintf=0.25.1=h5e0f5ae_0
- libasprintf-devel=0.25.1=h5e0f5ae_0
- libass=0.17.4=hcfe818d_0
- libavif16=1.3.0=hfe54310_2
- libblas=3.9.0=35_haddc8a3_openblas
- libcap=2.76=h5706e9e_0
- libcblas=3.9.0=35_hd72aa62_openblas
- libclang-cpp20.1=20.1.8=default_he95a3c9_1
- libclang13=21.1.0=default_h94a09a5_1
- libcups=2.3.3=h5cdc715_5
- libcurl=8.14.1=h6702fde_0
- libdeflate=1.24=he377734_0
- libdrm=2.4.125=he30d5cf_1
- libedit=3.1.20250104=pl5321h976ea20_0
- libegl=1.7.0=hd24410f_2
- libev=4.33=h31becfc_2
- libexpat=2.7.1=hfae3067_0
- libffi=3.4.6=he21f813_1
- libflac=1.4.3=h2f0025b_0
- libfreetype=2.14.0=h8af1aa0_1
- libfreetype6=2.14.0=hdae7a39_1
- libgcc=15.1.0=he277a41_5
- libgcc-ng=15.1.0=he9431aa_5
- libgcrypt-lib=1.11.1=h86ecc28_0
- libgettextpo=0.25.1=h5ad3122_0
- libgettextpo-devel=0.25.1=h5ad3122_0
- libgfortran=15.1.0=he9431aa_5
- libgfortran5=15.1.0=hbc25352_5
- libgl=1.7.0=hd24410f_2
- libglib=2.84.3=h75d4a95_0
- libglu=9.0.3=h5ad3122_1
- libglvnd=1.7.0=hd24410f_2
- libglx=1.7.0=hd24410f_2
- libgomp=15.1.0=he277a41_5
- libgpg-error=1.55=h5ad3122_0
- libhwloc=2.12.1=default_h6f258fa_1000
- libiconv=1.18=h90929bb_2
- libjpeg-turbo=3.1.0=h86ecc28_0
- liblapack=3.9.0=35_h88aeb00_openblas
- liblapacke=3.9.0=35_hb558247_openblas
- libllvm20=20.1.8=h2b567e5_0
- libllvm21=21.1.0=h2b567e5_0
- liblzma=5.8.1=h86ecc28_2
- libnghttp2=1.67.0=ha888d0e_0
- libnsl=2.0.1=h86ecc28_1
- libntlm=1.4=hf897c2e_1002
- libogg=1.3.5=h86ecc28_1
- libopenblas=0.3.30=pthreads_h9d3fd7e_2
- libopencv=4.12.0=qt6_py39h5bf5aac_602
- libopengl=1.7.0=hd24410f_2
- libopenvino=2025.2.0=hcd21e76_1
- libopenvino-arm-cpu-plugin=2025.2.0=hcd21e76_1
- libopenvino-auto-batch-plugin=2025.2.0=h3890994_1
- libopenvino-auto-plugin=2025.2.0=h3890994_1
- libopenvino-hetero-plugin=2025.2.0=he07c6df_1
- libopenvino-ir-frontend=2025.2.0=he07c6df_1
- libopenvino-onnx-frontend=2025.2.0=h07d5dce_1
- libopenvino-paddle-frontend=2025.2.0=h07d5dce_1
- libopenvino-pytorch-frontend=2025.2.0=hfae3067_1
- libopenvino-tensorflow-frontend=2025.2.0=h38473e3_1
- libopenvino-tensorflow-lite-frontend=2025.2.0=hfae3067_1
- libopus=1.5.2=h86ecc28_0
- libpciaccess=0.18=h86ecc28_0
- libpng=1.6.50=h1abf092_1
- libpq=17.6=hb4b1422_1
- libprotobuf=6.31.1=h8bb3b26_1
- librsvg=2.58.4=h3ac5bce_3
- libsndfile=1.2.2=h79657aa_1
- libsqlite=3.50.4=h022381a_0
- libssh2=1.11.1=h18c354c_0
- libstdcxx=15.1.0=h3f4de04_5
- libstdcxx-ng=15.1.0=hf1166c9_5
- libsystemd0=257.9=hd926fa8_0
- libtiff=4.7.0=h7a57436_6
- libudev1=257.9=hf39d17c_0
- libunwind=1.8.3=h6470e1d_0
- liburing=2.12=hfefdfc9_0
- libusb=1.0.29=h06eaf92_0
- libuuid=2.41.1=h3e4203c_0
- libvorbis=1.3.7=h7ac5ae9_2
- libvpx=1.14.1=h0a1ffab_0
- libwebp-base=1.6.0=ha2e29f5_0
- libxcb=1.17.0=h262b8f6_0
- libxcrypt=4.4.36=h31becfc_1
- libxkbcommon=1.11.0=h95ca766_0
- libxml2=2.13.8=he58860d_1
- libzlib=1.3.1=h86ecc28_2
- lz4-c=1.10.0=h5ad3122_1
- mpg123=1.32.9=h65af167_0
- ncurses=6.5=ha32ae93_3
- numpy=2.0.2=py39h4a34e27_1
- opencv=4.12.0=qt6_py39h3bed1ee_602
- openexr=3.3.5=hf28b12f_1
- openh264=2.6.0=h0564a2a_0
- openldap=2.6.10=h30c48ee_0
- openssl=3.5.2=h8e36d6e_0
- pango=1.56.4=he55ef5b_0
- pcre2=10.45=hf4ec17f_0
- pip=25.2=pyh8b19718_0
- pixman=0.46.4=h7ac5ae9_1
- pthread-stubs=0.4=h86ecc28_1002
- pugixml=1.15=h6ef32b0_0
- pulseaudio-client=17.0=h2f84921_1
- py-opencv=4.12.0=qt6_py39hde81434_602
- python=3.9.23=h0819846_0_cpython
- python_abi=3.9=8_cp39
- qt6-main=6.9.2=h2f84684_0
- rav1e=0.7.1=ha3529ed_3
- readline=8.2=h8382b9d_2
- sdl2=2.32.56=h7ac5ae9_0
- sdl3=3.2.22=h506f210_0
- setuptools=80.9.0=pyhff2d567_0
- snappy=1.2.2=he774c54_0
- svt-av1=3.1.2=hfae3067_0
- tbb=2022.2.0=h8f856e4_1
- tk=8.6.13=noxft_h5688188_102
- tzdata=2025b=h78e105d_0
- wayland=1.24.0=h698ed42_0
- wheel=0.45.1=pyhd8ed1ab_1
- x264=1!164.3095=h4e544f5_2
- x265=3.5=hdd96247_3
- xcb-util=0.4.1=hca56bd8_2
- xcb-util-cursor=0.1.5=h86ecc28_0
- xcb-util-image=0.4.0=h5c728e9_2
- xcb-util-keysyms=0.4.1=h5c728e9_0
- xcb-util-renderutil=0.3.10=h5c728e9_0
- xcb-util-wm=0.4.2=h5c728e9_0
- xkeyboard-config=2.45=h86ecc28_0
- xorg-libice=1.1.2=h86ecc28_0
- xorg-libsm=1.2.6=h0808dbd_0
- xorg-libx11=1.8.12=hca56bd8_0
- xorg-libxau=1.0.12=h86ecc28_0
- xorg-libxcomposite=0.4.6=h86ecc28_2
- xorg-libxcursor=1.2.3=h86ecc28_0
- xorg-libxdamage=1.1.6=h86ecc28_0
- xorg-libxdmcp=1.1.5=h57736b2_0
- xorg-libxext=1.3.6=h57736b2_0
- xorg-libxfixes=6.0.1=h57736b2_0
- xorg-libxi=1.8.2=h57736b2_0
- xorg-libxrandr=1.5.4=h86ecc28_0
- xorg-libxrender=0.9.12=h86ecc28_0
- xorg-libxtst=1.2.5=h57736b2_3
- xorg-libxxf86vm=1.1.6=h86ecc28_0
- zstd=1.5.7=hbcf94c1_2
- pip:
- absl-py==2.3.1
- astunparse==1.6.3
- certifi==2025.8.3
- charset-normalizer==3.4.3
- flatbuffers==20181003210633
- gast==0.6.0
- google-pasta==0.2.0
- grpcio==1.74.0
- h5py==3.14.0
- idna==3.10
- importlib-metadata==8.7.0
- keras==3.10.0
- libclang==18.1.1
- markdown==3.9
- markdown-it-py==3.0.0
- markupsafe==3.0.2
- mdurl==0.1.2
- ml-dtypes==0.5.3
- namex==0.1.0
- opt-einsum==3.4.0
- optree==0.17.0
- packaging==25.0
- pillow==11.3.0
- protobuf==6.32.1
- pygments==2.19.2
- requests==2.32.5
- rich==14.1.0
- six==1.17.0
- tensorboard==2.20.0
- tensorboard-data-server==0.7.2
- tensorflow==2.20.0
- termcolor==3.1.0
- typing-extensions==4.15.0
- urllib3==2.5.0
- werkzeug==3.1.3
- wrapt==1.17.3
- zipp==3.23.0
prefix: /home/moony/miniforge3/envs/rpi-ai