Jump to content

[Source]Stream webcam Client/Server


newbeen
 Share

Recommended Posts

Estava aborrecido sem fazer nenhum, e encontrei um exemplo na net de como ler a câmara em linux a partir de python

então tive a ideia de fazer um client servidor a ver se funcionava 10minutos mais tarde apareceu isto B)

Client

import xmlrpclib
import pygame
import Image
from pygame.locals import *
import sys


def get_image():
    remote=xmlrpclib.ServerProxy("http://localhost:8000")
    remoteFrame=remote.get()

    return remoteFrame.decode("hex")


fps = 15.0
pygame.init()
window = pygame.display.set_mode((640,480))
pygame.display.set_caption("WebCam Demo")
screen = pygame.display.get_surface()

while True:
    events = pygame.event.get()
    for event in events:
        if event.type == QUIT or event.type == KEYDOWN:
            sys.exit(0)
    im = get_image()
    pg_img = pygame.image.frombuffer(im, (640,480), "RGB")
    screen.blit(pg_img, (0,0))
    pygame.display.flip()
    pygame.time.delay(int(1000 * 1.0/fps))

Servidor

import opencv
from opencv import highgui

from SimpleXMLRPCServer import SimpleXMLRPCServer
from SimpleXMLRPCServer import SimpleXMLRPCRequestHandler

class RequestHandler(SimpleXMLRPCRequestHandler):
    rpc_paths = ('/RPC2',)

server = SimpleXMLRPCServer(("localhost", 8000),requestHandler=RequestHandler)
server.register_introspection_functions()

camera = highgui.cvCreateCameraCapture(0)
def get_image():
    im = highgui.cvQueryFrame(camera)
    im = opencv.cvGetMat(im)
    frame=opencv.adaptors.Ipl2PIL(im).tostring()
    return frame.encode("hex")

server.register_function(get_image, 'get')
server.serve_forever()

Ps: O servidor so corre em linux mas o cliente deve correr em todo o lado

PS2: O stream é extremamente lento, vou tentar reescrever isto com sockets, em vez de xmlrpc, estava preguiçoso 😄

RHCE - 120-062-534

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.