LoginSignup
0
0

More than 1 year has passed since last update.

M1 Mac の Jupyter 上で PixelLib を動かしたい!

Last updated at Posted at 2022-06-27

モチベーション

  1. 画像のセグメンテーションをしてみたかった
  2. カメラ画像がリアルタイム処理される様子をJupyter上で確認してみたかった

環境

  • Mac mini (M1, 2020)
    • macOS Monterey
  • python 3.9.1 (mambaforge)

環境構築

conda を用いて環境を構築し、jupyter も共にインストールを行った。

command
conda create -n qiita39 python=3.9.1 jupyter
conda activate qiita39

PixelLib READMEを参考にインストール。

shell
## https://pytorch.org/get-started/locally/
## PyTorch Build:       Stable 
## Your OS:             Mac 
## Package:             pip 
## Language:            Python
## Compute Platform:    Default
## 上記に沿って pytorch をインストール 
pip3 install torch torchvision torchaudio

## README.md より
pip3 install pycocotools
pip3 install pixellib

私の環境の場合は問題が発生したため、brew にて pyqt をインストールしシンボリックリンクをはり、その後に pixellib をインストールしました。
(問題が生じそうな場合はコメントを頂けますと幸いです。)

shell
## pixellib が pyqt が原因でインストールできなかった。(2022/06/27現在)
## brew を使って pyqt をインストール
brew install pyqt@5
ln -s /opt/homebrew/Cellar/pyqt@5/5.15.7/lib/python3.9/site-packages/* $HOME/mambaforge/envs/qiita39/lib/python3.9/site-packages
pip3 install pixellib

学習済みのモデル(pointrend_resnet50.pkl)を github からダウンロード。

プログラム

Python

参考資料 を元にカメラから画像を読み込み、セグメンテーション結果を jupyter notebook 上へ表示できた。

python
from pixellib.torchbackend.instance import instanceSegmentation
import IPython
from PIL import Image
import cv2
import io
import os


ins_seg = instanceSegmentation()
ins_seg.load_model('pointrend_resnet50.pkl')

def show(a, fmt='jpeg'):
    f = io.BytesIO()
    Image.fromarray(a).save(f, fmt)
    IPython.display.display(IPython.display.Image(data=f.getvalue()))

cap = cv2.VideoCapture(0)
assert cap.isOpened(), 'Could not open video device'

try:
    while(True):
        ret, frame = cap.read()

        if ret:
            image = ins_seg.segmentFrame(frame, show_bboxes = True)
            show(image[1])
            IPython.display.clear_output(wait=True)

except KeyboardInterrupt:
    cap.release()
    print('Stream stopped')

出力結果

キーボード.png

付録

参考資料

Jupyter notebook上でWebカメラから取得した映像をリアルタイム表示する (Python3)

問題

PixelLib が pyqt が原因でインストールできない。

command
% pip3 install pixellib
Collecting pixellib
  Using cached pixellib-0.7.1-py3-none-any.whl (430 kB)

-------------------- snip --------------------

Collecting pyQt5
  Using cached PyQt5-5.15.7.tar.gz (3.2 MB)
  Installing build dependencies ... done
  Getting requirements to build wheel ... done
  Preparing metadata (pyproject.toml) ... error
  error: subprocess-exited-with-error
  
-------------------- snip --------------------
  
  note: This error originates from a subprocess, and is likely not a problem with pip.
error: metadata-generation-failed

× Encountered error while generating package metadata.
╰─> See above for output.

note: This is an issue with the package mentioned above, not pip.hint: See above for details.
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0