<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Google Drive | Tomokatsu Yukishita | yre.jp</title><link>https://yre.jp/tag/google-drive/</link><atom:link href="https://yre.jp/tag/google-drive/index.xml" rel="self" type="application/rss+xml"/><description>Google Drive</description><generator>Hugo Blox Builder (https://hugoblox.com)</generator><language>ja-JP</language><lastBuildDate>Tue, 19 Jul 2022 00:00:00 +0000</lastBuildDate><image><url>https://yre.jp/media/icon_hufbc159bd6ce6a866189b19a79c0d0f51_12846_512x512_fill_lanczos_center_3.png</url><title>Google Drive</title><link>https://yre.jp/tag/google-drive/</link></image><item><title>Raspberry Pi + Google Drive でクラウド対応フォトフレームを自作する【rclone + fbi + Python】</title><link>https://yre.jp/post/raspi-photoframe/</link><pubDate>Tue, 19 Jul 2022 00:00:00 +0000</pubDate><guid>https://yre.jp/post/raspi-photoframe/</guid><description>&lt;p>写真が趣味で、2003年からデジタル一眼を使い始めて今に至ります。気づけば総撮影枚数は388,524枚。これだけ撮り溜めた写真を、スライドショー形式でリビングに飾りたいと思っていました。&lt;/p>
&lt;h2 id="市販のデジタルフォトフレームでは解決できなかった問題">市販のデジタルフォトフレームでは解決できなかった問題&lt;/h2>
&lt;p>市販のフォトフレームは、SDカードやUSBメモリにデータを入れて表示する仕組みが主流です。7〜10インチのものが多く、Amazonのランキングでも選択肢は豊富にあります。&lt;/p>
&lt;p>ただ、週1ペースで写真を撮り続ける身としては**「毎回SDカードに写真を入れ直す」運用が億劫**で、すぐに使わなくなりそうでした。&lt;/p>
&lt;p>
&lt;figure id="figure-クラウドから自動で写真を取得するフォトフレームのイメージ">
&lt;div class="d-flex justify-content-center">
&lt;div class="w-100" >&lt;img alt="Google DriveとRaspberry Piを組み合わせたクラウド対応フォトフレームのシステム構成図。クラウドから写真を自動取得してスライドショー表示する仕組みを示している" srcset="
/media/raspi-photoframe/5621deba8a18b839c7a4321764bb05e8_hub05f2d0d129c7ee7542169c2c963abde_86374_3f5a7eb7c7a059ffa19f361f5c9be38d.webp 400w,
/media/raspi-photoframe/5621deba8a18b839c7a4321764bb05e8_hub05f2d0d129c7ee7542169c2c963abde_86374_26bbc4e9cdb29660629bcf887c20be66.webp 760w,
/media/raspi-photoframe/5621deba8a18b839c7a4321764bb05e8_hub05f2d0d129c7ee7542169c2c963abde_86374_1200x1200_fit_q75_h2_lanczos_3.webp 1200w"
src="https://yre.jp/media/raspi-photoframe/5621deba8a18b839c7a4321764bb05e8_hub05f2d0d129c7ee7542169c2c963abde_86374_3f5a7eb7c7a059ffa19f361f5c9be38d.webp"
width="720"
height="405"
loading="lazy" data-zoomable />&lt;/div>
&lt;/div>&lt;figcaption>
クラウドから自動で写真を取得するフォトフレームのイメージ
&lt;/figcaption>&lt;/figure>
&lt;/p>
&lt;p>Google Driveなどのクラウドストレージから自動で写真を取得して表示するフォトフレームが欲しかったのですが、市販品では見つからなかったため、自分で作ることにしました。&lt;/p>
&lt;h2 id="用意したもの">用意したもの&lt;/h2>
&lt;h3 id="raspberry-pi-4">Raspberry Pi 4&lt;/h3>
&lt;p>常時通電するフォトフレーム用途には、消費電力が低く小型なRaspberry Piが適しています。&lt;/p>
&lt;a href="https://amzn.to/4mAJmpt" target="_blank" rel="noopener nofollow sponsored" class="amazon-card">
&lt;div class="amazon-card-image">
&lt;img src="https://m.media-amazon.com/images/I/51ETv8Le3nL._AC_.jpg" alt="【国内正規代理店品】Raspberry Pi4 ModelB 4GB ラズベリーパイ4 技適対応品">
&lt;/div>
&lt;div class="amazon-card-info">
&lt;div class="amazon-card-title">【国内正規代理店品】Raspberry Pi4 ModelB 4GB ラズベリーパイ4 技適対応品&lt;/div>
&lt;div class="amazon-card-meta">
&lt;span class="amazon-btn">Amazonで見る&lt;/span>
&lt;/div>
&lt;/div>
&lt;/a>
&lt;h3 id="モバイルモニター">モバイルモニター&lt;/h3>
&lt;p>写真を表示するためのモニターです。FullHD・IPSパネルのモバイルモニターを選びました。市販のフォトフレームは視野角が狭いパネルが多いですが、パーツを自分で選べるのが自作の醍醐味です。モバイルモニターは電源が小型で設置の自由度も高いです。&lt;/p>
&lt;a href="https://amzn.to/4vwkOSR" target="_blank" rel="noopener nofollow sponsored" class="amazon-card">
&lt;div class="amazon-card-image">
&lt;img src="https://m.media-amazon.com/images/I/71lbYPykVrL._AC_SL1500_.jpg" alt="モバイルモニター 15.6インチ タッチパネル モバイルディスプレイ">
&lt;/div>
&lt;div class="amazon-card-info">
&lt;div class="amazon-card-title">モバイルモニター 15.6インチ タッチパネル モバイルディスプレイ&lt;/div>
&lt;div class="amazon-card-meta">
&lt;span class="amazon-btn">Amazonで見る&lt;/span>
&lt;/div>
&lt;/div>
&lt;/a>
&lt;h3 id="microsdカード電源">microSDカード・電源&lt;/h3>
&lt;a href="https://amzn.to/4ch1OA3" target="_blank" rel="noopener nofollow sponsored" class="amazon-card">
&lt;div class="amazon-card-image">
&lt;img src="https://m.media-amazon.com/images/I/615exWhvZ0L._AC_SL1200_.jpg" alt="SanDisk 高耐久 ドライブレコーダー アクションカメラ対応 microSDHC 32GB">
&lt;/div>
&lt;div class="amazon-card-info">
&lt;div class="amazon-card-title">SanDisk 高耐久 ドライブレコーダー アクションカメラ対応 microSDHC 32GB&lt;/div>
&lt;div class="amazon-card-meta">
&lt;span class="amazon-btn">Amazonで見る&lt;/span>
&lt;/div>
&lt;/div>
&lt;/a>
&lt;a href="https://amzn.to/4cf3SbU" target="_blank" rel="noopener nofollow sponsored" class="amazon-card">
&lt;div class="amazon-card-image">
&lt;img src="https://m.media-amazon.com/images/I/61W8QsDXSYL._AC_SL1500_.jpg" alt="Miuzei Raspberry Pi 4 電源 USB-C 5.1V 3A スイッチ付き PSE認証">
&lt;/div>
&lt;div class="amazon-card-info">
&lt;div class="amazon-card-title">Miuzei Raspberry Pi 4 電源 USB-C 5.1V 3A スイッチ付き PSE認証&lt;/div>
&lt;div class="amazon-card-meta">
&lt;span class="amazon-btn">Amazonで見る&lt;/span>
&lt;/div>
&lt;/div>
&lt;/a>
&lt;h2 id="システム構成">システム構成&lt;/h2>
&lt;h3 id="osraspberry-pi-os">OS：Raspberry Pi OS&lt;/h3>
&lt;p>&lt;a href="https://www.raspberrypi.com/software/" target="_blank" rel="noopener">Raspberry Pi OS&lt;/a> をSDカードに書き込んで使います。フォトフレーム用途なのでデスクトップ環境は不要で、CLIのみの Lite 版でセットアップします。&lt;/p>
&lt;h3 id="画像表示fbi">画像表示：fbi&lt;/h3>
&lt;p>CLIから直接フレームバッファに画像を表示できる &lt;code>fbi&lt;/code> を使います。以前はUbuntu上のGUIで動かすバージョンも作りましたが動作が不安定だったため、CLIベースの構成に切り替えました。&lt;/p>
&lt;h3 id="クラウドストレージとの連携rclone">クラウドストレージとの連携：rclone&lt;/h3>
&lt;p>&lt;a href="https://rclone.org" target="_blank" rel="noopener">rclone&lt;/a> を使ってGoogle Drive上の写真を定期的にダウンロードします。Google Drive以外にもDropbox・S3など70以上のクラウドサービスに対応しています。&lt;/p>
&lt;p>rcloneで写真をダウンロード → fbiでスライドショー表示、という流れです。&lt;/p>
&lt;h2 id="セットアップ">セットアップ&lt;/h2>
&lt;h3 id="raspberry-pi-os-のインストール">Raspberry Pi OS のインストール&lt;/h3>
&lt;p>Raspberry Pi Imager を使ってSDカードにイメージを書き込みます。&lt;/p>
&lt;p>
&lt;figure id="figure-raspberry-pi-imager-でosを書き込む">
&lt;div class="d-flex justify-content-center">
&lt;div class="w-100" >&lt;img alt="Raspberry Pi ImagerでmicroSDカードにRaspberry Pi OSを書き込んでいる画面。OSとストレージを選択してWRITEボタンを押す操作画面" srcset="
/media/raspi-photoframe/8ffcb7bfb32b5079533396b7542ed9a1_hu8982ae701f5d02b9ab9d1e54062b4b24_153362_fa36f9d2badcc15fd6500bcdc35fea83.webp 400w,
/media/raspi-photoframe/8ffcb7bfb32b5079533396b7542ed9a1_hu8982ae701f5d02b9ab9d1e54062b4b24_153362_5221b7fcaf02876b6223f09d17408d34.webp 760w,
/media/raspi-photoframe/8ffcb7bfb32b5079533396b7542ed9a1_hu8982ae701f5d02b9ab9d1e54062b4b24_153362_1200x1200_fit_q75_h2_lanczos_3.webp 1200w"
src="https://yre.jp/media/raspi-photoframe/8ffcb7bfb32b5079533396b7542ed9a1_hu8982ae701f5d02b9ab9d1e54062b4b24_153362_fa36f9d2badcc15fd6500bcdc35fea83.webp"
width="760"
height="504"
loading="lazy" data-zoomable />&lt;/div>
&lt;/div>&lt;figcaption>
Raspberry Pi Imager でOSを書き込む
&lt;/figcaption>&lt;/figure>
&lt;/p>
&lt;p>32bit・64bitどちらでも問題なく動作します。&lt;/p>
&lt;h3 id="fbi-のインストール">fbi のインストール&lt;/h3>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-shell" data-lang="shell">&lt;span class="line">&lt;span class="cl">sudo apt-get -y install fbi
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;h4 id="パーミッションの付与">パーミッションの付与&lt;/h4>
&lt;p>デフォルトではスーパーユーザー以外は fbi で画像を表示できません。&lt;code>pi&lt;/code> ユーザーを &lt;code>video&lt;/code> グループに追加します。&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-shell" data-lang="shell">&lt;span class="line">&lt;span class="cl">pi@raspi3-photo:~ $ ls -Fla /dev/fb0
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">crw-rw---- &lt;span class="m">1&lt;/span> root video 29, &lt;span class="m">0&lt;/span> Apr &lt;span class="m">7&lt;/span> 10:33 /dev/fb0
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-shell" data-lang="shell">&lt;span class="line">&lt;span class="cl">sudo usermod -aG video pi
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;h3 id="rclone-のインストール">rclone のインストール&lt;/h3>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-shell" data-lang="shell">&lt;span class="line">&lt;span class="cl">curl https://rclone.org/install.sh &lt;span class="p">|&lt;/span> sudo bash
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>インストール後、&lt;code>rclone config&lt;/code> でGoogle Driveとの認証設定を行います。&lt;/p>
&lt;div class="alert alert-note">
&lt;div>
SSH接続のみのヘッドレス環境では、&lt;code>rclone config&lt;/code> 実行中にブラウザ認証用のURLが表示されます。そのURLをPC側のブラウザで開いてGoogleアカウントを認証してください。
&lt;/div>
&lt;/div>
&lt;h2 id="スライドショープログラムの実装">スライドショープログラムの実装&lt;/h2>
&lt;h3 id="アルゴリズム">アルゴリズム&lt;/h3>
&lt;p>以下の流れで動作します。&lt;/p>
&lt;ol>
&lt;li>&lt;code>rclone ls&lt;/code> でクラウドストレージ上の指定フォルダの画像一覧を取得&lt;/li>
&lt;li>一定数取得できた時点でランダムに画像のダウンロードを開始&lt;/li>
&lt;li>ダウンロードできた画像から順に &lt;code>fbi&lt;/code> で表示&lt;/li>
&lt;li>バックグラウンドで次の画像を取得し続ける&lt;/li>
&lt;li>指定時間が来たらランダムに次の画像へ切り替え&lt;/li>
&lt;li>重み付きランダム選択で、表示済み画像の優先度を下げ偏りを防ぐ&lt;/li>
&lt;li>週1回 &lt;code>rclone ls&lt;/code> でデータベースをマージして新着写真を反映&lt;/li>
&lt;li>データベースはファイルではなくメモリ上に保持（マージ速度改善）&lt;/li>
&lt;/ol>
&lt;h3 id="言語python">言語：Python&lt;/h3>
&lt;p>スライドショー制御スクリプトはPythonで実装しました。オブジェクト指向で設計し直した改良版では、起動から最初の画像表示までの時間を&lt;strong>旧バージョン比1700%高速化&lt;/strong>しています。&lt;/p>
&lt;p>Pythonの学習には &lt;a href="https://www.udemy.com/course/python-beginner/" target="_blank" rel="noopener">現役シリコンバレーエンジニアが教えるPython 3 入門 + 応用（Udemy）&lt;/a> が参考になりました。&lt;/p>
&lt;h2 id="ソースコード">ソースコード&lt;/h2>
&lt;p>作成したプログラムはGitHubで公開しています。&lt;/p>
&lt;p>&lt;a href="https://github.com/yukishita/photoView4" target="_blank" rel="noopener">GitHub - yukishita/photoView4：クラウド対応スライドショープログラム&lt;/a>&lt;/p>
&lt;h2 id="使い方">使い方&lt;/h2>
&lt;p>crontab に以下を追加して、起動時に自動実行します。&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-shell" data-lang="shell">&lt;span class="line">&lt;span class="cl">@reboot /home/pi/photoView4/photoView4.sh
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;h2 id="デモンストレーション">デモンストレーション&lt;/h2>
&lt;p>改良版（v4）のデモ動画です。旧バージョンと比べて起動から表示までの時間が大幅に短縮されています。&lt;/p>
&lt;div style="position: relative; padding-bottom: 56.25%; height: 0; overflow: hidden;">
&lt;iframe allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" allowfullscreen="allowfullscreen" loading="eager" referrerpolicy="strict-origin-when-cross-origin" src="https://www.youtube.com/embed/VP7y_E34EJA?autoplay=0&amp;controls=1&amp;end=0&amp;loop=0&amp;mute=0&amp;start=0" style="position: absolute; top: 0; left: 0; width: 100%; height: 100%; border:0;" title="YouTube video"
>&lt;/iframe>
&lt;/div>
&lt;h3 id="旧バージョン参考">旧バージョン（参考）&lt;/h3>
&lt;p>ファイルリストをすべて取得してから動作するため起動が遅い版です。&lt;/p>
&lt;div style="position: relative; padding-bottom: 56.25%; height: 0; overflow: hidden;">
&lt;iframe allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" allowfullscreen="allowfullscreen" loading="eager" referrerpolicy="strict-origin-when-cross-origin" src="https://www.youtube.com/embed/-dxP_aHSjIU?autoplay=0&amp;controls=1&amp;end=0&amp;loop=0&amp;mute=0&amp;start=0" style="position: absolute; top: 0; left: 0; width: 100%; height: 100%; border:0;" title="YouTube video"
>&lt;/iframe>
&lt;/div>
&lt;h2 id="tips指定時刻にディスプレイの電源を切る">Tips：指定時刻にディスプレイの電源を切る&lt;/h2>
&lt;p>24時間表示し続けると電気代がかさむため、HDMI接続時は &lt;code>vcgencmd&lt;/code> で電源スケジュールを設定できます。&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-shell" data-lang="shell">&lt;span class="line">&lt;span class="cl">&lt;span class="m">0&lt;/span> &lt;span class="m">0&lt;/span> * * * /usr/bin/vcgencmd display_power &lt;span class="m">0&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="m">30&lt;/span> &lt;span class="m">6&lt;/span> * * * /usr/bin/vcgencmd display_power &lt;span class="m">1&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>上記の例では午前0時にOFF・午前6時30分にONとなります。人感センサーや照度センサーと組み合わせてさらに自動化することも可能です。&lt;/p>
&lt;h2 id="まとめ">まとめ&lt;/h2>
&lt;table>
&lt;thead>
&lt;tr>
&lt;th>構成要素&lt;/th>
&lt;th>役割&lt;/th>
&lt;/tr>
&lt;/thead>
&lt;tbody>
&lt;tr>
&lt;td>Raspberry Pi 4&lt;/td>
&lt;td>メインコンピューター（低消費電力・常時稼働）&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>モバイルモニター&lt;/td>
&lt;td>FullHD・IPS・高視野角で写真を高品質表示&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>fbi&lt;/td>
&lt;td>CLIからフレームバッファに直接画像を表示&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>rclone&lt;/td>
&lt;td>Google Driveから写真を自動ダウンロード&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>Python&lt;/td>
&lt;td>スライドショー制御・重み付きランダム選択&lt;/td>
&lt;/tr>
&lt;/tbody>
&lt;/table>
&lt;p>Raspberry Pi 4を使いましたが、Raspberry Pi Zero 2 Wでも同様の構成で動作可能です。常時通電する用途なので、消費電力の小さい機種を選ぶとランニングコストを抑えられます。&lt;/p></description></item></channel></rss>