[英]Python webcam over socket
I got this script for socket screen share from server to client can somone tell me how I convert it from screen to webcam capture(stream)?我得到了这个脚本,用于从服务器到客户端的套接字屏幕共享,有人能告诉我如何将它从屏幕转换为网络摄像头捕获(流)吗? just how I convert the line of sct.grab to webcam and make it work.
我是如何将 sct.grab 行转换为网络摄像头并使其工作的。 I tried capturing a picture and then using open() on the image and send its pixels but pygame said:"String length does not equal format and resolution size"
我尝试捕获图片,然后在图像上使用 open() 并发送其像素,但 pygame 说:“字符串长度不等于格式和分辨率大小”
Server:服务器:
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()
Client:客户:
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()
Thanks谢谢
Well, I found a soulution I just tried it like the oriiginal insted of coming up with a new idea.好吧,我找到了一个我刚刚尝试过的解决方案,就像最初想出一个新想法一样。 I now use files(cache), trandfer the pixels, creating a new image file and uding the load() method in PyGame.image.
我现在使用文件(缓存),转移像素,创建一个新的图像文件并使用 PyGame.image 中的 load() 方法。 FYI, this is a base code, you can play with it your way and add more things to it, and make it better.
仅供参考,这是一个基本代码,您可以按照自己的方式使用它并向其添加更多内容,并使其变得更好。
Here is the code, Server:这是代码,服务器:
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()
Client:客户:
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.