スマホの画面をキャプチャーする環境構築（下書き）


scrcpyが動作するWSL環境を構築する

https://leng-yue.github.io/py-scrcpy-client/guide.html


# スマホ側の準備

USB接続してadbで次のコマンドを実行

```
C:\>adb tcpip 5555
restarting in TCP mode port: 5555
```

# Python3をインストール

sudo apt install python3.10-venv
python3 -m venv scrcpy
source scrcpy/bin/activate
pip install scrcpy-client[ui]

テストプログラムを実行してみる
```
> python test.py
```

スマホはPCと同じネットワークのWifiに接続する。スマホのIPアドレスを調べて、プログラム中のadb.connectのところに入れる。

```Python:test.py
import scrcpy
# If you already know the device serial
# client = scrcpy.Client(device="DEVICE SERIAL")
# You can also pass an ADBClient instance to it
from adbutils import adb
adb.connect("192.168.0.25:5555")
client = scrcpy.Client(device=adb.device_list()[0])

import cv2

def on_frame(frame):
    # If you set non-blocking (default) in constructor, the frame event receiver 
    # may receive None to avoid blocking event.
    print ("a")
    if frame is not None:
        # frame is an bgr numpy ndarray (cv2' default format)
        cv2.imshow("viz", frame)
    cv2.waitKey(10)

client.add_listener(scrcpy.EVENT_FRAME, on_frame)

def on_init():
    # Print device name
    print(client.device_name)
client.add_listener(scrcpy.EVENT_INIT, on_init)

client.start()

```

エラーになる

```
(scrcpy) masa@wsl-ubuntu:~/test$ python test.py
Traceback (most recent call last):
  File "/home/masa/test/test.py", line 1, in <module>
    import scrcpy
  File "/home/masa/scrcpy/lib/python3.10/site-packages/scrcpy/__init__.py", line 6, in <module>
    from .core import Client
  File "/home/masa/scrcpy/lib/python3.10/site-packages/scrcpy/core.py", line 9, in <module>
    import cv2
  File "/home/masa/scrcpy/lib/python3.10/site-packages/cv2/__init__.py", line 181, in <module>
    bootstrap()
  File "/home/masa/scrcpy/lib/python3.10/site-packages/cv2/__init__.py", line 153, in bootstrap
    native_module = importlib.import_module("cv2")
  File "/usr/lib/python3.10/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
ImportError: libGL.so.1: cannot open shared object file: No such file or directory
```

xtermをいれてみる
sudo apt-get install xterm
xtermを起動できることを確認。

test.pyを実行すると、デバイス名が表示される。
なにも応答がない。
画面も表示されない。

WSLイメージを保存　d:\ubuntu2204-bak.tar



```
(scrcpy) masa@wsl-ubuntu:~/test$ pip list opencv-python
Package            Version
------------------ ---------
adbutils           1.2.9
apkutils2          1.0.0
av                 9.2.0
certifi            2022.12.7
charset-normalizer 3.1.0
cigam              0.0.3
decorator          5.1.1
deprecation        2.1.0
idna               3.4
numpy              1.24.2
opencv-python      4.7.0.72
packaging          23.0
Pillow             9.4.0
pip                22.0.2
py                 1.11.0
pyelftools         0.29
PySide6            6.4.2
PySide6-Addons     6.4.2
PySide6-Essentials 6.4.2
requests           2.28.2
retry              0.9.2
scrcpy-client      0.4.7
setuptools         59.6.0
shiboken6          6.4.2
urllib3            1.26.15
whichcraft         0.6.1
xmltodict          0.13.0
(scrcpy) masa@wsl-ubuntu:~/test$ pip uninstall opencv-python
Found existing installation: opencv-python 4.7.0.72
Uninstalling opencv-python-4.7.0.72:
  Would remove:
    /home/masa/scrcpy/lib/python3.10/site-packages/cv2/*
    /home/masa/scrcpy/lib/python3.10/site-packages/opencv_python-4.7.0.72.dist-info/*
    /home/masa/scrcpy/lib/python3.10/site-packages/opencv_python.libs/libQt5Core-39545cc7.so.5.15.0
    /home/masa/scrcpy/lib/python3.10/site-packages/opencv_python.libs/libQt5Gui-48e93776.so.5.15.0
    /home/masa/scrcpy/lib/python3.10/site-packages/opencv_python.libs/libQt5Test-c38a5234.so.5.15.0
    /home/masa/scrcpy/lib/python3.10/site-packages/opencv_python.libs/libQt5Widgets-e69d94fb.so.5.15.0
    /home/masa/scrcpy/lib/python3.10/site-packages/opencv_python.libs/libQt5XcbQpa-c112ba75.so.5.15.0
    /home/masa/scrcpy/lib/python3.10/site-packages/opencv_python.libs/libX11-xcb-69166bdf.so.1.0.0
    /home/masa/scrcpy/lib/python3.10/site-packages/opencv_python.libs/libXau-00ec42fe.so.6.0.0
    /home/masa/scrcpy/lib/python3.10/site-packages/opencv_python.libs/libavcodec-16a334ab.so.59.37.100
    /home/masa/scrcpy/lib/python3.10/site-packages/opencv_python.libs/libavformat-e0b1067c.so.59.27.100
    /home/masa/scrcpy/lib/python3.10/site-packages/opencv_python.libs/libavutil-82c407cb.so.57.28.100
    /home/masa/scrcpy/lib/python3.10/site-packages/opencv_python.libs/libcrypto-47343492.so.1.1
    /home/masa/scrcpy/lib/python3.10/site-packages/opencv_python.libs/libgfortran-91cc3cb1.so.3.0.0
    /home/masa/scrcpy/lib/python3.10/site-packages/opencv_python.libs/libopenblas-r0-f650aae0.3.3.so
    /home/masa/scrcpy/lib/python3.10/site-packages/opencv_python.libs/libpng16-57e5e0a0.so.16.37.0
    /home/masa/scrcpy/lib/python3.10/site-packages/opencv_python.libs/libquadmath-96973f99.so.0.0.0
    /home/masa/scrcpy/lib/python3.10/site-packages/opencv_python.libs/libssl-16e42f2f.so.1.1
    /home/masa/scrcpy/lib/python3.10/site-packages/opencv_python.libs/libswresample-d02fa90a.so.4.7.100
    /home/masa/scrcpy/lib/python3.10/site-packages/opencv_python.libs/libswscale-9b504c0d.so.6.7.100
    /home/masa/scrcpy/lib/python3.10/site-packages/opencv_python.libs/libvpx-e95aadfe.so.7.1.0
    /home/masa/scrcpy/lib/python3.10/site-packages/opencv_python.libs/libxcb-icccm-413c9f41.so.4.0.0
    /home/masa/scrcpy/lib/python3.10/site-packages/opencv_python.libs/libxcb-image-e82a276d.so.0.0.0
    /home/masa/scrcpy/lib/python3.10/site-packages/opencv_python.libs/libxcb-keysyms-21015570.so.1.0.0
    /home/masa/scrcpy/lib/python3.10/site-packages/opencv_python.libs/libxcb-randr-a96a5a87.so.0.1.0
    /home/masa/scrcpy/lib/python3.10/site-packages/opencv_python.libs/libxcb-render-637b984a.so.0.0.0
    /home/masa/scrcpy/lib/python3.10/site-packages/opencv_python.libs/libxcb-render-util-43ce00f5.so.0.0.0
    /home/masa/scrcpy/lib/python3.10/site-packages/opencv_python.libs/libxcb-shape-25c2b258.so.0.0.0
    /home/masa/scrcpy/lib/python3.10/site-packages/opencv_python.libs/libxcb-shm-7a199f70.so.0.0.0
    /home/masa/scrcpy/lib/python3.10/site-packages/opencv_python.libs/libxcb-sync-89374f40.so.1.0.0
    /home/masa/scrcpy/lib/python3.10/site-packages/opencv_python.libs/libxcb-util-4d666913.so.1.0.0
    /home/masa/scrcpy/lib/python3.10/site-packages/opencv_python.libs/libxcb-xfixes-9be3ba6f.so.0.0.0
    /home/masa/scrcpy/lib/python3.10/site-packages/opencv_python.libs/libxcb-xinerama-ae147f87.so.0.0.0
    /home/masa/scrcpy/lib/python3.10/site-packages/opencv_python.libs/libxcb-xkb-9ba31ab3.so.1.0.0
    /home/masa/scrcpy/lib/python3.10/site-packages/opencv_python.libs/libxkbcommon-71ae2972.so.0.0.0
    /home/masa/scrcpy/lib/python3.10/site-packages/opencv_python.libs/libxkbcommon-x11-c65ed502.so.0.0.0
Proceed (Y/n)? y
  Successfully uninstalled opencv-python-4.7.0.72
```

$ sudo apt-get install python3-opencv

test.pyを実行すると`ModuleNotFoundError: No module named 'cv2'`エラーとなる

```
$ pip install opencv-python
```

応答がなくなる現状は変わらない。

ubuntu20で動作している環境のPIP
```
(venv) masa@DESKTOP-HP:~/autopjsekai$ pip list
Package               Version
--------------------- ---------
adbutils              0.11.0
apkutils2             1.0.0
av                    8.1.0
bleach                4.1.0
certifi               2021.10.8
cffi                  1.15.0
charset-normalizer    2.0.12
cigam                 0.0.3
click                 7.1.2
colorama              0.4.4
cryptography          36.0.1
cycler                0.11.0
decorator             5.1.1
deprecation           2.1.0
docutils              0.18.1
fonttools             4.29.1
idna                  3.3
importlib-metadata    4.11.2
jeepney               0.7.1
keyring               23.5.0
kiwisolver            1.3.2
matplot               0.1.9
matplotlib            3.5.1
numpy                 1.22.2
opencv-python         4.5.5.64
packaging             21.3
Pillow                9.0.1
pip                   20.0.2
pkg-resources         0.0.0
pkginfo               1.8.2
py                    1.11.0
pycparser             2.21
pyelftools            0.28
Pygments              2.11.2
pyloco                0.0.139
pyparsing             3.0.7
PyQt5                 5.15.6
PyQt5-Qt5             5.15.2
PyQt5-sip             12.9.1
PySide6               6.2.3
python-dateutil       2.8.2
readme-renderer       33.0
requests              2.27.1
requests-toolbelt     0.9.1
retry                 0.9.2
rfc3986               2.0.0
scrcpy-client         0.3.7
SecretStorage         3.3.1
setuptools            44.0.0
shiboken6             6.2.3
SimpleWebSocketServer 0.1.1
six                   1.16.0
tqdm                  4.63.0
twine                 3.8.0
typing                3.7.4.3
urllib3               1.26.8
ushlex                0.99.1
webencodings          0.5.1
websocket-client      1.3.1
whichcraft            0.6.1
xmltodict             0.12.0
zipp                  3.7.0
```

```
(scrcpy) masa@wsl-ubuntu:~/test$ pip install opencv-python==4.5.5.64
Collecting opencv-python==4.5.5.64
  Downloading opencv_python-4.5.5.64-cp36-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (60.5 MB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 60.5/60.5 MB 26.1 MB/s eta 0:00:00
Requirement already satisfied: numpy>=1.21.2 in /home/masa/scrcpy/lib/python3.10/site-packages (from opencv-python==4.5.5.64) (1.24.2)
Installing collected packages: opencv-python
  Attempting uninstall: opencv-python
    Found existing installation: opencv-python 4.7.0.72
    Uninstalling opencv-python-4.7.0.72:
      Successfully uninstalled opencv-python-4.7.0.72
Successfully installed opencv-python-4.5.5.64
```



# Ubuntu20.04でやってみる

```
PS C:\Users\masat> wsl --import scrcpy d:\wsl\scrcpy D:\ubuntu-20.04.tar
```

$ sudo apt-get update
$ sudo apt install python3.8-venv

python3 -m venv scrcpy
source scrcpy/bin/activate
pip install scrcpy-client[ui]

test.pyを実行するとscrcpをimportするとimshowが動作しなくなる（2204の場合と同じ現象となった）
動作している環境と同じscrcpy-clientをいれてみる

$ pip install scrcpy-client==0.3.7

imshowが動いた。
adbが使えない

RuntimeError: No adb exe could be found. Install adb on your system

$ pip install adb

gccが無いためエラーになる

$ sudo apt-get install gcc
$ pip install adb
$ sudo apt-get install python3.8-dev
$ pip install adb

$ sudo apt-get install adb

動作した

# ubuntu2204に戻る

```
$ pip install scrcpy-client==0.3.7
ERROR: Could not find a version that satisfies the requirement scrcpy-client==0.3.7 (from versions: 0.1.0, 0.1.1, 0.1.2, 0.2.0, 0.2.1, 0.2.2, 0.4.0, 0.4.1, 0.4.2, 0.4.3, 0.4.4, 0.4.5, 0.4.6, 0.4.7)
ERROR: No matching distribution found for scrcpy-client==0.3.7
$ pip install scrcpy-client==0.4.0
```

0.4.0では、scrcpyをimportするとimshowが動作しない

$ pip install scrcpy-client==0.2.2

$ sudo apt-get install adb






参考
https://github.com/Genymobile/scrcpy



https://stackoverflow.com/questions/65939167/problem-using-opencv-in-wsl-when-opening-windows