物理の駅 Physics station by 現役研究者

テクノロジーは共有されてこそ栄える

Ubuntu に Nvidiaのドライバーを入れたときの E: Unable to correct problems, you have held broken packages. エラー

$ sudo apt install --just-print nvidia-driver-440
Reading package lists... Done
Building dependency tree
Reading state information... Done
Some packages could not be installed. This may mean that you have
requested an impossible situation or if you are using the unstable
distribution that some required packages have not yet been created
or been moved out of Incoming.
The following information may help to resolve the situation:

The following packages have unmet dependencies:
 nvidia-driver-440 : Depends: libnvidia-compute-440 (= 440.33.01-0ubuntu1) but 440.59-0ubuntu0.18.04.1 is to be installed
                     Depends: libnvidia-decode-440 (= 440.33.01-0ubuntu1) but it is not going to be installed
                     Depends: libnvidia-encode-440 (= 440.33.01-0ubuntu1) but it is not going to be installed
                     Recommends: nvidia-settings but it is not going to be installed
                     Recommends: nvidia-prime (>= 0.8) but it is not going to be installed
                     Recommends: libnvidia-compute-440:i386 (= 440.33.01-0ubuntu1)
                     Recommends: libnvidia-decode-440:i386 (= 440.33.01-0ubuntu1)
                     Recommends: libnvidia-encode-440:i386 (= 440.33.01-0ubuntu1)
                     Recommends: libnvidia-ifr1-440:i386 (= 440.33.01-0ubuntu1)
                     Recommends: libnvidia-fbc1-440:i386 (= 440.33.01-0ubuntu1)
                     Recommends: libnvidia-gl-440:i386 (= 440.33.01-0ubuntu1)
E: Unable to correct problems, you have held broken packages.

と出たので、nvidiaとlibnvidiaをアンインストールした

$ sudo apt remove nvidia*
$ sudo apt remove libnvidia*

askubuntu.com

apt コマンドのチートシート

簡略化のため、パッケージ(済のみ)=インストール済みのパッケージ、パッケージ(未含む)=未インストールを含む全パッケージとしている。

コマンド 内容
sudo apt update 利用可能なパッケージリストを更新
作業前に実行
sudo apt upgrade インストール/アップグレード可能なパッケージでシステムを 更新
sudo apt full-upgrade upgrade更新 に加えて 削除 もする
sudo apt install {パッケージ名} パッケージを インストール
apt install --just-print {パッケージ名} 上のコマンドを試す
---- ----
sudo apt autoremove 依存されなくなった未インストールパッケージを 全て削除
apt autoremove --just-print 上のコマンドを試す
sudo apt clean キャッシュ済みローカルリポジトリを削除
sudo apt autoclean キャッシュ済み 未インストールローカルリポジトリを削除
sudo apt remove {パッケージ名} パッケージを削除
sudo apt purge {パッケージ名} パッケージを 設定ファイルとともに削除
---- ----
apt show {パッケージ名:完全} パッケージ(未含む)の詳細を表示
apt list {パッケージ名:完全} パッケージ(未含む)の概要を表示
apt search {パッケージ名:部分} パッケージ(未含む)を検索し概要を表示
apt list パッケージ(未含む)の概要一覧を表示
apt list --installed or
dpkg -l
パッケージ(済のみ)の概要一覧を表示
apt list --upgradeable アップグレード可能なパッケージ(済のみ)の概要を表示
cat /var/log/apt/history.log aptコマンドの使用履歴と詳細を表示
grep "^Commandline:" /var/log/apt/history.log aptコマンドの使用履歴を表示
zgrep "^Commandline:" /var/log/apt/history.log.1.gz 古いaptコマンドの使用履歴を表示
dpkg -L {パッケージ名:完全} パッケージ(済のみ)内のファイル一覧を表示
---- ----
-y Yes/NoをYesとみなしてコマンドを実行

apt-get(8) — apt — Debian unstable — Debian Manpages

aptコマンドチートシート - Qiita

「apt-get」はもう古い?新しい「apt」コマンドを使ったUbuntuのパッケージ管理 | LFI

CUDA Toolkitの各バージョンを対応する全OSに導入する公式情報

過去のアーカイブも含めて利用可能なものは以下にまとめられている

developer.nvidia.com

例えば、CUDA Toolkit 10.2を選んでみる

f:id:onsanai:20200208123713p:plain

そして、OS、CPU などの種類などを入れる

f:id:onsanai:20200208123753p:plain

するとコマンドが出てくるのでそのまま実行する

wget https://developer.download.nvidia.com/compute/cuda/repos/ubuntu1804/x86_64/cuda-ubuntu1804.pin
sudo mv cuda-ubuntu1804.pin /etc/apt/preferences.d/cuda-repository-pin-600
wget http://developer.download.nvidia.com/compute/cuda/10.2/Prod/local_installers/cuda-repo-ubuntu1804-10-2-local-10.2.89-440.33.01_1.0-1_amd64.deb
sudo dpkg -i cuda-repo-ubuntu1804-10-2-local-10.2.89-440.33.01_1.0-1_amd64.deb
sudo apt-key add /var/cuda-repo-10-2-local-10.2.89-440.33.01/7fa2af80.pub
sudo apt-get update
sudo apt-get -y install cuda

これでOK。なはず。

Python+OpenCV+pyzbarでQRコードを読み取ってみる

取り急ぎ、Windows PCでQRコードを読み込みたい需要があったので、書いてみた。QRコードをデコードするコードは下記のqiitaを丸パクリした。

qiita.com

import cv2
from pyzbar import pyzbar
import subprocess

# デバイス上でのウェブカメラを取得
cap = cv2.VideoCapture(1)

# Windows 10 の設定
chrome_path = r'"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe"'

while True:
    ret, frame = cap.read()
    height, width, channels = frame.shape 
    size = int(min(width,height) * 0.2)
    height = height // 2
    width = width // 2

    #赤線で囲む
    frame = cv2.rectangle(frame,(width - size,height - size),(width + size,height + size),(0,0,255),3)

    # 赤線内を切り抜く(トリミング)
    size -= 5
    new_frame = frame[height - size:height + size,width - size:width + size]

    cv2.imshow("QR1",frame)
    #デバッグ用
    #cv2.imshow("QR2",new_frame)
    decoded_objs = pyzbar.decode(new_frame)

    #データがあれば
    if decoded_objs != []:
        str_dec_obj = decoded_objs[0][0].decode('utf-8', 'ignore')
        print(f'QR cord: {str_dec_obj}')

        if len(str_dec_obj) > 4 and "http" == str_dec_obj[0:4]:
            #URLの場合はchromeで開く
            command = f'{chrome_path} {str_dec_obj}'
            print(command)
            result = subprocess.run(command, shell=True)
        else:
            #URL以外の場合は検索で開く
            command = f'{chrome_path} https://www.google.com/search?q={str_dec_obj}'
            print(command)
            result = subprocess.run(command, shell=True)

        #リソースのリリース
        cap.release()
        cv2.destroyAllWindows()
        break
    #100msecの待機
    cv2.waitKey(100)

自身のラップトップ(Surface book 2)だと、前面カメラ(0)ではコードが認識せず、背面カメラ(1)でのみ認識した。原因は深く追求していない。