[英]Python webcam over socket
我得到了這個腳本,用於從服務器到客戶端的套接字屏幕共享,有人能告訴我如何將它從屏幕轉換為網絡攝像頭捕獲(流)嗎? 我是如何將 sct.grab 行轉換為網絡攝像頭並使其工作的。 我嘗試捕獲圖片,然后在圖像上使用 open() 並發送其像素,但 pygame 說:“字符串長度不等於格式和分辨率大小”
服務器:
from socket import socket
from threading import Thread
from zlib import compress
from mss import mss
WIDTH = 1900
HEIGHT = 1000
def retreive_screenshot(conn):
with mss() as sct:
# The region to capture
rect = {'top': 0, 'left': 0, 'width': WIDTH, 'height': HEIGHT}
while 'recording':
# Capture the screen
img = sct.grab(rect)
data = compress(img.rgb, 6)
# Send the size of the pixels length
size = len(data)
size_len = (size.bit_length() + 7) // 8
conn.send(bytes([size_len]))
# Send the actual pixels length
size_bytes = size.to_bytes(size_len, 'big')
conn.send(size_bytes)
# Send pixels
conn.sendall(data)
def main(host='127.0.0.1', port=5555):
sock = socket()
sock.bind((host, port))
try:
sock.listen(5)
print('Server started.')
while 'connected':
conn, addr = sock.accept()
print('Client connected IP:', addr)
thread = Thread(target=retreive_screenshot, args=(conn,))
thread.start()
finally:
sock.close()
if __name__ == '__main__':
main()
客戶:
from socket import socket
from zlib import decompress
import pygame
WIDTH = 1900
HEIGHT = 1000
def recvall(conn, length):
""" Retreive all pixels. """
buffer = b''
while len(buffer) < length:
data = conn.recv(length - len(buffer))
if not data:
return data
buffer += data
return buffer
def main(host='127.0.0.1', port=5555):
pygame.init()
screen = pygame.display.set_mode((WIDTH, HEIGHT))
clock = pygame.time.Clock()
watching = True
s = socket()
s.connect((host, port))
try:
while watching:
for event in pygame.event.get():
if event.type == pygame.QUIT:
watching = False
break
# Retreive the size of the pixels length, the pixels length and pixels
size_len = int.from_bytes(s.recv(1), byteorder='big')
size = int.from_bytes(s.recv(size_len), byteorder='big')
pixels = decompress(recvall(s, size))
img = pygame.image.fromstring(pixels, (WIDTH, HEIGHT), 'RGB')
screen.blit(img, (0, 0))
pygame.display.flip()
clock.tick(60)
finally:
s.close()
if __name__ == '__main__':
main()
謝謝
好吧,我找到了一個我剛剛嘗試過的解決方案,就像最初想出一個新想法一樣。 我現在使用文件(緩存),轉移像素,創建一個新的圖像文件並使用 PyGame.image 中的 load() 方法。 僅供參考,這是一個基本代碼,您可以按照自己的方式使用它並向其添加更多內容,並使其變得更好。
這是代碼,服務器:
from socket import socket
from threading import Thread
from zlib import compress
from cv2 import *
WIDTH = 1900
HEIGHT = 1000
def retreive_screenshot(conn):
cam = VideoCapture(0)
while 'recording':
# initialize the camera
s, img = cam.read()
imwrite("filename.jpg", img) # save image
f = open('filename.jpg', 'rb')
data = compress(f.read(), 6)
f.close()
# Send the size of the pixels length
size = len(data)
size_len = (size.bit_length() + 7) // 8
conn.send(bytes([size_len]))
# Send the actual pixels length
size_bytes = size.to_bytes(size_len, 'big')
conn.send(size_bytes)
# Send pixels
conn.sendall(data)
def main(host='127.0.0.1', port=5555):
sock = socket()
sock.bind((host, port))
try:
sock.listen(5)
print('Server started.')
while 'connected':
conn, addr = sock.accept()
print('Client connected IP:', addr)
thread = Thread(target=retreive_screenshot, args=(conn,))
thread.start()
finally:
sock.close()
if __name__ == '__main__':
main()
客戶:
from socket import socket
from zlib import decompress
import pygame
WIDTH = 1900
HEIGHT = 1000
def recvall(conn, length):
""" Retreive all pixels. """
buffer = b''
while len(buffer) < length:
data = conn.recv(length - len(buffer))
if not data:
return data
buffer += data
return buffer
def main(host='127.0.0.1', port=5555):
pygame.init()
screen = pygame.display.set_mode((WIDTH, HEIGHT))
clock = pygame.time.Clock()
watching = True
s = socket()
s.connect((host, port))
try:
while watching:
for event in pygame.event.get():
if event.type == pygame.QUIT:
watching = False
break
# Retreive the size of the pixels length, the pixels length and pixels
size_len = int.from_bytes(s.recv(1), byteorder='big')
size = int.from_bytes(s.recv(size_len), byteorder='big')
pixels = decompress(recvall(s, size))
ff = open('filenam.jpg', 'wb')
ff.write(pixels)
ff.close()
img = pygame.image.load('filenam.jpg')
screen.blit(img, (0, 0))
pygame.display.flip()
clock.tick(60)
finally:
s.close()
if __name__ == '__main__':
main()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.