python远程调用摄像头_Python设置Socket代理及实现远程摄像头控制的例子
為python設(shè)置socket代理首先,你得下載SocksiPy這個(gè).解壓出來之后里面會(huì)有一個(gè)socks.py文件.然后你可以把這個(gè)文件復(fù)制到python安裝目錄里面的Lib\site-packages中.或者把這個(gè)文件復(fù)制到程序所在的目錄中.
然后就可以再程序中使用socket代理來編寫程序了.
下面是示例代碼
import socks
import socket
socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS5,"127.0.0.1",8088)
socket.socket =socks.socksocket
import urllib2
urllib2.urlopen('http://www.baidu.com').read()
通過socket通信實(shí)現(xiàn)遠(yuǎn)程攝像頭監(jiān)控通過python來實(shí)現(xiàn)攝像頭監(jiān)控,然后通過socket通信來將數(shù)據(jù)發(fā)送到遠(yuǎn)程服務(wù)器,這樣就可以實(shí)現(xiàn)遠(yuǎn)程監(jiān)控了.大概找了下資料,果然可以,下面貼出實(shí)現(xiàn)過程.
這個(gè)程序包括一個(gè)服務(wù)器和一個(gè)客戶端。需要的庫(kù)有 VideoCapture 和 pygame,一個(gè)用來得到攝像頭的視頻,一個(gè)用來顯示.
服務(wù)器端,主要實(shí)現(xiàn)監(jiān)聽客戶端所發(fā)送到指令,如果指令是startCam,則打開攝像頭,并向客戶端發(fā)送數(shù)據(jù).
from VideoCapture import Device
import ImageDraw, sys, pygame, time
from pygame.locals import *
import socket
import time
from PIL import ImageEnhance
from threading import Thread
import traceback
import threading
# 全局變量
is_sending = False
cli_address = ('', 0)
# 主機(jī)地址和端口
host = 'localhost'
port = 10218
# 初始化UDP socket
ser_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
ser_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
ser_socket.bind((host, port))
# 接收線程類,用于接收客戶端發(fā)送的消息
class UdpReceiver(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
self.thread_stop = False
def run(self):
while not self.thread_stop:
# 聲明全局變量,接收消息后更改
global cli_address
global is_sending
try:
message, address = ser_socket.recvfrom(2048)
except:
traceback.print_exc()
continue
print message,cli_address
cli_address = address
if message == 'startCam':
print 'start camera',
is_sending = True
ser_socket.sendto('startRcv', cli_address)
if message == 'quitCam':
is_sending = False
print 'quit camera',
def stop(self):
self.thread_stop = True
if __name__=='__main__':
res = (640,480)
cam = Device()
cam.setResolution(res[0],res[1])
brightness = 1.0
contrast = 1.0
shots = 0
receiveThread = UdpReceiver()
receiveThread.setDaemon(True) # 該選項(xiàng)設(shè)置后使得主線程退出后子線程同時(shí)退出
receiveThread.start()
while 1:
if is_sending:
camshot = ImageEnhance.Brightness(cam.getImage()).enhance(brightness)
camshot = ImageEnhance.Contrast(camshot).enhance(contrast)
clock = pygame.time.Clock()
img = cam.getImage().resize((160,120))
data = img.tostring()
ser_socket.sendto(data, cli_address)
time.sleep(0.05)
else:
time.sleep(1)
receiveThread.stop()
ser_socket.close()
客戶端:
主要功能是像服務(wù)器端發(fā)送指令,然后接受服務(wù)器所發(fā)送過來的數(shù)據(jù)并通過pygame模塊來顯示出來.
# -*- coding: UTF-8 -*-
import socket, time
import pygame
from pygame.locals import *
from sys import exit
# 服務(wù)器地址,初始化socket
ser_address = ('localhost', 10218)
cli_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
# 設(shè)置超時(shí)
cli_socket.settimeout(5)
# 向服務(wù)器發(fā)送消息,并判斷接收時(shí)是否超時(shí),若超時(shí)則重發(fā)
while 1:
cli_socket.sendto('startCam', ser_address)
try:
message, address = cli_socket.recvfrom(2048)
if message == 'startRcv':
print message
break
except socket.timeout:
continue
cli_socket.recvfrom(65536)
# 初始化視頻窗口
pygame.init()
screen = pygame.display.set_mode((640,480))
pygame.display.set_caption('Web Camera')
pygame.display.flip()
# 設(shè)置時(shí)間,可以用來控制幀率
clock = pygame.time.Clock()
# 主循環(huán),顯示視頻信息
while 1:
try:
data, address = cli_socket.recvfrom(65536)
except socket.timeout:
continue
camshot = pygame.image.frombuffer(data, (160,120), 'RGB')
camshot = pygame.transform.scale(camshot, (640, 480))
for event in pygame.event.get():
if event.type == pygame.QUIT:
cli_socket.sendto('quitCam', ser_address)
cli_socket.close()
pygame.quit()
exit()
screen.blit(camshot, (0,0))
pygame.display.update()
clock.tick(20)
客戶端就是簡(jiǎn)單地向服務(wù)器發(fā)送啟動(dòng)消息,接收到回復(fù)后開始進(jìn)入主循環(huán)開始接收視頻數(shù)據(jù)并顯示。
由于UDP協(xié)議不保證信息是否成功到達(dá),因此前面設(shè)置了個(gè)重發(fā)機(jī)制,只有當(dāng)客戶端收到服務(wù)器的回復(fù)后,才停止發(fā)送開啟消息并進(jìn)入主循環(huán).具體見注釋.
使用時(shí)將localhost改成服務(wù)器IP即可.
總結(jié)
以上是生活随笔為你收集整理的python远程调用摄像头_Python设置Socket代理及实现远程摄像头控制的例子的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: mybatisplus 操作另一个数据库
- 下一篇: python牛顿法计算平方根_常用的平方