文章目錄
- 一、遠程連接
- 1、通過putty連接
- 2、查看putty運行狀態
- 3、通過Puuty遠程登錄Ubuntu
- 4、添加新用戶
- 查看是否添加成功
- 5、用新用戶登錄遠程Ubuntu
- 6、使用VNC遠程登錄樹莓派
- 二、虛擬機上talk聊天
- 三、Opencv
- 1、簡單安裝版(適合新手安裝)
- 2、打開VScode
- 特效說明:
一、遠程連接
1、通過putty連接
sudo apt update
sudo apt install openssh-server
2、查看putty運行狀態
sudo systemctl status ssh
3、通過Puuty遠程登錄Ubuntu
4、添加新用戶
查看是否添加成功
cat /etc/passwd | grep -E 'classmate1|classmate2'
5、用新用戶登錄遠程Ubuntu
- X 窗口協議與 VNC-viewer 協議的差異
X 窗口協議:
用于在本地顯示遠程圖形界面,依賴于 X11 轉發。
適合運行單個圖形程序,性能較高。
VNC 協議:
提供完整的遠程桌面訪問,支持多用戶同時連接。
適合需要完整桌面環境的場景,但性能可能較低。
6、使用VNC遠程登錄樹莓派
二、虛擬機上talk聊天
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <signal.h>
#include <pwd.h>#define SOCKET_PATH "/tmp/talk_socket_"
#define MAX_CLIENTS 5
struct message {char sender[32];char text[256];
};
int server_fd;
char current_user[32] = {0};void cleanup() {char path[128];snprintf(path, sizeof(path), "%s%s", SOCKET_PATH, current_user);unlink(path); // 刪除套接字文件close(server_fd);
}void handle_signal(int sig) {printf("\nCleaning up before exit...\n");cleanup();exit(0);
}void run_server(const char *username) {struct sockaddr_un addr;int client_fd;struct message msg;// 設置套接字路徑char path[128];snprintf(path, sizeof(path), "%s%s", SOCKET_PATH, username);// 創建套接字if ((server_fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {perror("socket error");exit(EXIT_FAILURE);}// 綁定套接字memset(&addr, 0, sizeof(addr));addr.sun_family = AF_UNIX;strncpy(addr.sun_path, path, sizeof(addr.sun_path)-1);unlink(path); // 確保路徑可用if (bind(server_fd, (struct sockaddr*)&addr, sizeof(addr)) == -1) {perror("bind error");exit(EXIT_FAILURE);}// 監聽if (listen(server_fd, MAX_CLIENTS) == -1) {perror("listen error");exit(EXIT_FAILURE);}printf("Waiting for incoming messages as [%s]...\n", username);while (1) {// 接受連接if ((client_fd = accept(server_fd, NULL, NULL)) == -1) {perror("accept error");continue;}// 接收消息if (read(client_fd, &msg, sizeof(msg)) > 0) {printf("\n[Message from %s]: %s\n", msg.sender, msg.text);printf("Reply to %s> ", msg.sender);fflush(stdout);}close(client_fd);}
}void send_message(const char *target_user, const char *username) {struct sockaddr_un addr;int fd;struct message msg;void send_message(const char *target_user, const char *username) {struct sockaddr_un addr;int fd;struct message msg;// 設置目標套接字路徑char path[128];snprintf(path, sizeof(path), "%s%s", SOCKET_PATH, target_user);// 創建套接字if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {perror("socket error");exit(EXIT_FAILURE);}// 連接目標memset(&addr, 0, sizeof(addr));addr.sun_family = AF_UNIX;strncpy(addr.sun_path, path, sizeof(addr.sun_path)-1);if (connect(fd, (struct sockaddr*)&addr, sizeof(addr)) == -1) {perror("connect error");printf("User %s is not available or not running talk\n", target_user);exit(EXIT_FAILURE);}// 發送消息strncpy(msg.sender, username, sizeof(msg.sender)-1);printf("Enter message (Ctrl+D to end): ");fgets(msg.text, sizeof(msg.text), stdin);if (write(fd, &msg, sizeof(msg)) == -1) {perror("write error");}close(fd);
}int main(int argc, char *argv[]) {if (argc < 2) {fprintf(stderr, "Usage: %s <username> [target_user]\n", argv[0]);fprintf(stderr, "Server mode: %s yourname\n", argv[0]);fprintf(stderr, "Client mode: %s yourname target_user\n", argv[0]);exit(EXIT_FAILURE);}// 獲取當前用戶名strncpy(current_user, argv[1], sizeof(current_user)-1);signal(SIGINT, handle_signal);signal(SIGTERM, handle_signal);if (argc == 2) {// 服務端模式run_server(argv[1]);} else {// 客戶端模式send_message(argv[2], argv[1]);}return 0;
}
三、Opencv
1、簡單安裝版(適合新手安裝)
1、打開Terminal輸入以下命令
sudo apt update
sudo apt install python3-opencv libopencv-dev
2、驗證安裝是否成功
# 檢查Python環境
python3 -c "import cv2; print(cv2.__version__)"# 檢查C++開發環境
pkg-config --modversion opencv4
如圖所示,安裝的是4.2.0。這個方法安裝的Opencv版本較老,可以選擇去官網下載較新的版本安裝,只不過我這里嘗試了很久沒有安裝成功,并且每次最后都會導致虛擬機卡死
2、打開VScode
1、創建一個新的python文件
案例測試
import cv2# Load an image from file
image = cv2.imread('example.jpg')# Check if the image was successfully loaded
if image is None:print("Error: Could not load the image. Please check the file path.")
else:# Apply a grayscale effectgray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)# Apply a Gaussian blur effectblurred_image = cv2.GaussianBlur(image, (15, 15), 0)# Display the original imagecv2.imshow('Original Image', image)# Display the grayscale imagecv2.imshow('Grayscale Image', gray_image)# Display the blurred imagecv2.imshow('Blurred Image', blurred_image)# Wait for a key press and close the windowscv2.waitKey(0)cv2.destroyAllWindows()
以下是一個示例代碼,添加了一些簡單的圖像特效(如將圖像轉換為灰度圖像和應用高斯模糊):
import cv2# Load an image from file
image = cv2.imread('example.jpg')# Check if the image was successfully loaded
if image is None:print("Error: Could not load the image. Please check the file path.")
else:# Apply a grayscale effectgray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)# Apply a Gaussian blur effectblurred_image = cv2.GaussianBlur(image, (15, 15), 0)# Display the original imagecv2.imshow('Original Image', image)# Display the grayscale imagecv2.imshow('Grayscale Image', gray_image)# Display the blurred imagecv2.imshow('Blurred Image', blurred_image)# Wait for a key press and close the windowscv2.waitKey(0)cv2.destroyAllWindows()
特效說明:
- 灰度圖像:使用
cv2.cvtColor
將彩色圖像轉換為灰度圖像。 - 高斯模糊:使用
cv2.GaussianBlur
對圖像進行模糊處理,參數(15, 15)
控制模糊的程度。
如下圖所示
同樣這個也可以通過C++實現,但是我嘗試了很久,被各種配置困了很久還么解決,就用的Python