2023/3/9

some notes on python tkinter

要寫一個開啟 jpg file,show 圖,並且在 status bar (bottom) 顯示 mouse 所在位置對應 jpg file 的 X, y (rgb) 值。
然後windows 要能resize.

結果寫好久...
一些注意事項。

tkinter 的 window 沒有 status bar 這個 option。
所以是用 label 來做。
show jpg 也沒有特別的 widget,所以也是用 label 來做。

windows 上 widget 的 layout 就是先放先贏 (先 pack 先放)。
所以都是 fill, expand 的話,先 pack 就佔掉全部的 window 了,後來 pack 的 widget 會被蓋住看不到。

2023/3/3

bookmark : Inverse Perspective Mapping

就是把照片反投影為上視圖

find real mount url in windows net-disk

因為 windows mount 完就看不到 server/path。所以linux 不知道要怎麼mount。
現在是用 ubuntu 的檔案總管 nautilus。
他的 "connect to",輸入:smb://ip 然後跳出 login dialog,要注意 domain name 要對。
登入後就可以 browse file,找到正確的 folder path。
然後就可以用
mount -t cifs -o username=myname,password=mypassword,iocharset=utf8,uid=myname,gid=myname //server-ip/folder-path mymountpoint

2023/3/2

bing 寫的,read input video resize and save to modified filename

主要是紀錄一下python 讀argument 和 filename 操作的code:
import cv2
import sys
import os

input_file = sys.argv[1]
path, filename = os.path.split(input_file)
filename, ext = os.path.splitext(filename)

output_file = filename + "s" + ext

cap = cv2.VideoCapture(input_file)
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))

fourcc = cv2.VideoWriter_fourcc(*'mp4v')
fps = cap.get(cv2.CAP_PROP_FPS)
out = cv2.VideoWriter(output_file, fourcc, fps, (width//2, height//2))

while cap.isOpened():
    ret, frame = cap.read()
    if ret:
        resized_frame = cv2.resize(frame, (width//2, height//2))
        out.write(resized_frame)
    else:
        break

cap.release()
out.release()
cv2.destroyAllWindows()

在多網卡機器上,指定使用某網卡

ref: 還有 bing 的回答:
有一些方法可以实现在多 nic 的 linux 机器上指定某程序使用某 nic,而不是依照 routing table。其中一种方法是使用 LD_PRELOAD 技巧,通过修改程序的网络绑定来实现¹。
另一种方法是使用网络命名空间(network namespaces)的特性,通过 ip 命令来创建和管理不同的网络环境¹²。
來源:
然後,name space 的方法:
如何使用网络命名空间来指定程序使用 eth1。可以按照以下步骤操作:

- 创建一个网络命名空间,比如叫 ns1:`ip netns add ns1`
- 将 eth1 接口移动到 ns1 命名空间中:`ip link set eth1 netns ns1`
- 在 ns1 命名空间中配置 eth1 的 IP 地址和路由:`ip netns exec ns1 ifconfig eth1 192.168.0.10/24 up` 和 `ip netns exec ns1 ip route add default via 192.168.0.1`
- 在 ns1 命名空间中运行您想要指定的程序,比如 ping:`ip netns exec ns1 ping 8.8.8.8`

这样,您就可以让 ping 程序使用 eth1 而不是默认的网络接口了。

(1) Network Namespaces — PF_RING dev documentation. https://www.ntop.org/guides/pf_ring/containers/namespaces.html
(2) How does `ip netns exec` command create mount namespace?. https://stackoverflow.com/questions/45629002/how-does-ip-netns-exec-command-create-mount-namespace
(3) Network Emulation using Network Namespaces and Mininet. https://www.inf.usi.ch/faculty/carzaniga/edu/adv-ntw/mininet.pdf

然後,用 LD_PRELOAD 的方法:
使用网络命名空间来指定程序使用 eth1。可以按照以下步骤操作:

- 创建一个网络命名空间,比如叫 ns1:`ip netns add ns1`
- 将 eth1 接口移动到 ns1 命名空间中:`ip link set eth1 netns ns1`
- 在 ns1 命名空间中配置 eth1 的 IP 地址和路由:`ip netns exec ns1 ifconfig eth1 192.168.0.10/24 up` 和 `ip netns exec ns1 ip route add default via 192.168.0.1`
- 在 ns1 命名空间中运行您想要指定的程序,比如 ping:`ip netns exec ns1 ping 8.8.8.8`

这样就可以让 ping 程序使用 eth1 而不是默认的网络接口了。

(1) Network Namespaces — PF_RING dev documentation. https://www.ntop.org/guides/pf_ring/containers/namespaces.html
(2) How does `ip netns exec` command create mount namespace?. https://stackoverflow.com/questions/45629002/how-does-ip-netns-exec-command-create-mount-namespace
(3) Network Emulation using Network Namespaces and Mininet. https://www.inf.usi.ch/faculty/carzaniga/edu/adv-ntw/mininet.pdf

2023/3/1

imshow error in remote X, and waitKey

ref: 因為server 是沒有 monitor 的,所以remote X 來跑,run plt.imshow( ) 就會出現上面的 error。

follow ref, install 一堆..
sudo apt-get install '^libxcb.*-dev' libx11-xcb-dev libglu1-mesa-dev libxrender-dev libxi-dev libxkbcommon-dev libxkbcommon-x11-dev
windows 端開啟 xserver 也用了下面 option (雖然覺得不是問題)
D:\Program Files\VcXsrv>vcxsrv.exe :0 -ac -terminate -lesspointer -multiwindow -clipboard -wgl -dpi auto
另外,cv2.imshow( ) 很奇怪,執行完並不會show 圖,要呼叫 cv2.waitKey(0) 之後才會。
即使是用 interactive mode,在python 的 prompt 下。
執行完 imshow( ) 之後,沒有反應。
要再執行 cv2.waitKey(0) 之後才會顯示 (無論是正確還是錯誤的 encoding)

imread 也是,雖然說 default 是 cv2.IMREAD_COLOR,但是有時候不寫,雖然沒有 Error,但是不會正常顯示。
雖然 mediainfo 讀出是 YUV420 jpg file( mediainfo 錯誤?),指定 cv2.IMREAD_COLOR 的話,imshow 還是會正常顯示。
不指定cv2.IMREAD_COLOR的話,imshow 就會顯示出不正常的 size and color (auto detect failed ?)

總之
cv2.imread 配合 cv2.imshow 真的莫名其妙的問題很多


ref: 原來 waitKey 是專門給 imshow 用的。
是用來決定要顯示多久用的 function。
所以 imshow 之後一定要配合 waitKey,才會顯示。
同時 waitKey 也不能單獨使用,一定要再 imshow 之後,才會真正讀到 keyboard 輸入。
單純的call waitKey( ) ,前面沒有 imshow 的話,waitKey 會直接 return -1

2023/2/22

VSCode. msys2. C++

ref: 結果要在windows 下 build/debug C++ 反而最麻煩。
因為 toolchain 要裝。
現在新的好像都用 msys2,這個裝了之後,用 pacman -S 來安裝 package,有比較方便。

VSCode 就麻煩了,要自己設定 vscode 需要的 json 文件。
結果這一篇說得比較清楚:
VSCode 作為一堆語言的 IDE,並不會自己偵測語言, build tool 和設置。
所以要 user 告訴他。

VSCode 是以 folder 為 project 單位,所以 folder 內要有 .vscode,裡面就是這格 project 相關的設定(build command, option... etc)
所以跟使用的 toolchain 有關,也跟安裝的位置有關。

有時候 VSCode 開啟 folder 時,如果沒看到 .vscode 目錄,會跳出設定讓使用者設。



updated:

最後還是官方的這一篇: 重點應該就是 msys2,新的msys2 用 pacman 安裝package。
pacman -S --needed base-devel mingw-w64-ucrt-x86_64-toolchain
所以follow instruction裝完 gcc (和gdb),設好環境變數(add msys2 /usr/bin/ to PATH),VSCode 安裝 C++ extension後,create new project,cpp file,之後run 就會自動 create task.json.
另外,在source code 設 bp,之後也會自動 invoke gdb



updated2:

上面的,用 msys2,build 出來的 exe 黨,要在 msys 的 shell 中執行才會正確。
可能是 library ? shell 環境 ?

如果要 build 在 windows command console 執行的,要用: download up 檔解開就可以。 (例如 D:\mingw64)
然後開啟command line 可以用command 設定環境變數
SET PATH=D:\mingw63\bin;%PATH%
然後就可以用了。
用這個 winlibs 的 gcc/g++ build 出來的 exe, 在 windows command console 下執行是正常的。