SSH remotely accesses the server Opencv and matplotlib through VSCode and cannot display images directly.

Description of Requirement:

When connecting to the server via SSH in VSCode, images cannot be displayed using cv2.imshow or plt.show().

The solution is as follows:

1. First check the network connection problem with the server (90% of the problems occur in the first step, hahaha)

Open cmd locally and use the command ping xxx.xxx.xxx.xxx, where (xxx.xxx.xxx.xxx is the IP address of the server)

Ping the local side from the server in VSCode or MobaXterm

Use the command ping xxx.xxx.xxx.xxx, where (xxx.xxx.xxx.xxx is the local IP address)

 At this step, the Windows 11 system cannot ping by default. This is because the firewall directly blocks it. Go to the system, turn off the firewall, and try again until you can ping.

Steps to turn off the firewall:

Control Panel Search Defender

 Then turn off the firewall

 2. On the server side

Add to the ~/.bashrc file:

export DISPLAY="172.22.205.229:0.0"

The content in quotation marks is the local IP address +:0.0.

Then run source ~/.bashrc in the terminal to take effect.

3. Install MobaXterm locally,

Click Settings-->Configurations-->X11 and set as follows:

Display offset is set to 0. If it is set to 1, the corresponding quotation mark in the second step is changed to the local IP address +:1.0, and so on.

X11 remote access is changed to full, which means that all remote access permissions are open.

4. With MobaXterm open, run xclock in VSCode, and a clock will be displayed.

 5. Opencv display test (MobaXterm needs to be kept open during the test)

Sample code:

import cv2 as cv
 
src = cv.imread("LaSOT/LaSOTBenchmark/bicycle/bicycle-1/img/00000001.jpg")
cv.namedWindow("test",0)
cv.imshow("test",src)
cv.waitKey(0)
cv.destroyAllWindows()

result:

6. matplotlib display test (MobaXterm needs to be kept open during the test)

 Sample code:

import matplotlib.pyplot as plt
import numpy as np
from mpl_toolkits.mplot3d import Axes3D
ax = plt.axes(projection='3d')
ax.scatter(np.random.rand(10),np.random.rand(10),np.random.rand(10))
plt.show()

result:

 

Guess you like

Origin blog.csdn.net/qq_17783559/article/details/131245475