|
| 1 | +import sys |
| 2 | +import socket |
| 3 | +import threading |
| 4 | +import os |
| 5 | +from PyQt5.QtWidgets import QApplication, QMainWindow, QVBoxLayout, QPushButton, QLineEdit, QWidget, QLabel, QInputDialog |
| 6 | +from PyQt5.QtCore import Qt, QTimer |
| 7 | +import pyrealsense2 as rs |
| 8 | +import numpy as np |
| 9 | +import cv2 |
| 10 | + |
| 11 | +class Client(QMainWindow): |
| 12 | + def __init__(self): |
| 13 | + super().__init__() |
| 14 | + self.client_socket = None |
| 15 | + self.isConnected = False |
| 16 | + self.pipeline = None |
| 17 | + self.depth_video = None |
| 18 | + self.rgb_video = None |
| 19 | + self.initUI() |
| 20 | + |
| 21 | + def initUI(self): |
| 22 | + self.setWindowTitle('Client') |
| 23 | + self.setGeometry(100, 100, 300, 200) |
| 24 | + layout = QVBoxLayout() |
| 25 | + |
| 26 | + self.ipInput = QLineEdit(self) |
| 27 | + self.ipInput.setPlaceholderText('Server IP') |
| 28 | + self.ipInput.setText('192.168.1.1') # 设置默认IP地址 |
| 29 | + layout.addWidget(self.ipInput) |
| 30 | + |
| 31 | + self.portInput = QLineEdit(self) |
| 32 | + self.portInput.setPlaceholderText('Port') |
| 33 | + self.portInput.setText('9999') # 设置默认端口号 |
| 34 | + layout.addWidget(self.portInput) |
| 35 | + |
| 36 | + self.connectBtn = QPushButton('Connect', self) |
| 37 | + self.connectBtn.clicked.connect(self.start_connection) |
| 38 | + layout.addWidget(self.connectBtn) |
| 39 | + |
| 40 | + self.statusLabel = QLabel('Status: Disconnected', self) |
| 41 | + self.statusLabel.setAlignment(Qt.AlignCenter) |
| 42 | + layout.addWidget(self.statusLabel) |
| 43 | + |
| 44 | + container = QWidget() |
| 45 | + container.setLayout(layout) |
| 46 | + self.setCentralWidget(container) |
| 47 | + |
| 48 | + def start_connection(self): |
| 49 | + ip = self.ipInput.text() |
| 50 | + port = int(self.portInput.text()) |
| 51 | + try: |
| 52 | + self.client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
| 53 | + self.client_socket.connect((ip, port)) |
| 54 | + self.isConnected = True |
| 55 | + self.statusLabel.setText('Status: Connected') |
| 56 | + threading.Thread(target=self.receive_message, daemon=True).start() |
| 57 | + except Exception as e: |
| 58 | + self.statusLabel.setText(f'Connection Failed: {e}') |
| 59 | + |
| 60 | + def receive_message(self): |
| 61 | + while self.isConnected: |
| 62 | + try: |
| 63 | + message = self.client_socket.recv(1024).decode('utf-8') |
| 64 | + if message == "start_click": |
| 65 | + self.start_video_capture() |
| 66 | + elif message == "stop_click": |
| 67 | + self.stop_video_capture() |
| 68 | + else: |
| 69 | + directory = message |
| 70 | + if not os.path.exists(directory): |
| 71 | + os.makedirs(directory) |
| 72 | + self.save_directory = directory |
| 73 | + except Exception as e: |
| 74 | + self.statusLabel.setText(f'Error: {e}') |
| 75 | + self.isConnected = False |
| 76 | + break |
| 77 | + |
| 78 | + def start_video_capture(self): |
| 79 | + self.pipeline = rs.pipeline() |
| 80 | + config = rs.config() |
| 81 | + # config.enable_stream(rs.stream.depth, 640, 480, rs.format.z16, 30) |
| 82 | + # config.enable_stream(rs.stream.color, 640, 480, rs.format.bgr8, 30) |
| 83 | + config.enable_stream(rs.stream.depth, 640, 480, rs.format.z16, 90) |
| 84 | + config.enable_stream(rs.stream.color, 640, 480, rs.format.bgr8, 30) |
| 85 | + self.pipeline.start(config) |
| 86 | + self.depth_video = cv2.VideoWriter(os.path.join(self.save_directory, 'depth_video.avi'), |
| 87 | + cv2.VideoWriter_fourcc(*'XVID'), 90, (640, 480), False) |
| 88 | + self.rgb_video = cv2.VideoWriter(os.path.join(self.save_directory, 'rgb_video.avi'), |
| 89 | + cv2.VideoWriter_fourcc(*'XVID'), 30, (640, 480)) |
| 90 | + threading.Thread(target=self.capture_video, daemon=True).start() |
| 91 | + |
| 92 | + def capture_video(self): |
| 93 | + try: |
| 94 | + cnt = 0 |
| 95 | + while self.pipeline: |
| 96 | + frames = self.pipeline.wait_for_frames() |
| 97 | + depth_frame = frames.get_depth_frame() |
| 98 | + color_frame = frames.get_color_frame() |
| 99 | + if not depth_frame or not color_frame: |
| 100 | + continue |
| 101 | + |
| 102 | + depth_image = np.asanyarray(depth_frame.get_data()) |
| 103 | + color_image = np.asanyarray(color_frame.get_data()) |
| 104 | + depth_image_8bit = cv2.convertScaleAbs(depth_image, alpha=0.03) |
| 105 | + self.depth_video.write(depth_image_8bit) |
| 106 | + if cnt % 3 == 0: |
| 107 | + self.rgb_video.write(color_image) |
| 108 | + cnt += 1 |
| 109 | + except Exception as e: |
| 110 | + print(f"Error capturing video: {e}") |
| 111 | + |
| 112 | + def stop_video_capture(self): |
| 113 | + if self.pipeline: |
| 114 | + self.pipeline.stop() |
| 115 | + self.pipeline = None |
| 116 | + self.depth_video.release() |
| 117 | + self.rgb_video.release() |
| 118 | + self.depth_video = None |
| 119 | + self.rgb_video = None |
| 120 | + print("Video capture stopped and saved.") |
| 121 | + |
| 122 | +if __name__ == '__main__': |
| 123 | + app = QApplication(sys.argv) |
| 124 | + client = Client() |
| 125 | + client.show() |
| 126 | + sys.exit(app.exec_()) |
0 commit comments