My boss was unable to visit the Teaching and Research Section frequently due to busy tasks. So I asked him to build a monitoring system so that he could see the number of students in the office. O (> others <) o |||
My idea was to go directly to the next software on the Internet, but it was not toxic or paid to find it, but the technology could not be cracked, so I had to find another method.
When there is no way, I can see a blog post. After reading this article, I feel that I can write it myself. I would like to thank James.
This program includes a server and a client. The required libraries include VideoCapture and pygame. One is used to get the video from the camera and the other is used to display the video. You can click here to download the Python Library :. Press ctrl + F to find the corresponding library and select the corresponding version. There are many other libraries available for download. If it cannot be run, it may be because the VideoCapture library references PIL, but you have not installed it. You can click to download the corresponding version.
The solution I came up with was to open a computer in the Teaching and Research Section, pick up a USB camera, and then start a server program, waiting for the boss to connect to the client, because it is real-time video transmission, use the UDP protocol. (The main transmission part uses James's Code ).
The server code is as follows:
VideoCapture is_sending = cli_address = ( host = port = 10218 ser_socket = ser_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1 threading.Thread. self.thread_stop = message, address = ser_socket.recvfrom(2048 cli_address = message == is_sending = ser_socket.sendto( message == is_sending = self.thread_stop = receiveThread = receiveThread.setDaemon(True) cam = cam.setResolution(320,240 1 img = cam.getImage().resize((160,120 data = time.sleep(0.05 time.sleep(1 ser_socket.close()
The server starts a subthread to listen to messages sent by the client. When a message exists, change is_sending to True, and the server sends video data to the client. For more information, see the code comments.
The client code is as follows:
pygame.locals * sys ser_address = (, 10218 cli_socket = cli_socket.settimeout(5 1 cli_socket.sendto( message, address = cli_socket.recvfrom(2048 message == cli_socket.recvfrom(65536 screen = pygame.display.set_mode((640,480 pygame.display.set_caption( clock = 1 data, address = cli_socket.recvfrom(65536 camshot = pygame.image.frombuffer(data, (160,120), camshot = pygame.transform.scale(camshot, (640, 480 event event.type == cli_socket.sendto( clock.tick(20)
The client simply sends a startup message to the server. After receiving the response, the client enters the main loop and receives and displays the video data.
The UDP protocol does not guarantee whether the information is successfully sent. Therefore, a re-sending mechanism is set up. Only when the client receives a response from the server, the sending of the message is stopped and the message is sent to the main loop. For more information, see annotations.
Change localhost to the server IP address during use. Currently, the test is only applicable to lan and campus network. Internet has not been tested yet. You can experiment with network programming.
When debugging, why can't the server receive the client message? The cause cannot be found all afternoon after debugging. In the evening, I went back and shut down the firewall and security software.
After the server starts a new thread, due to the strange Python setting, the Child thread will exit after the main thread exits. Here, the Child thread is an endless loop, therefore, you need to call setDaemon (True) to the sub-thread, so that the sub-thread will automatically exit when the main thread exits. If this method is not called, the second time after debugging may fail, because a subthread is running in the background.