11-2VideoServer.py

000: import socketserver
001: import cv2
002: import pickle
003:
004: HOST = ‘192.168.101.27’
005: PORT = 2000
006: BUFFER_SIZE = 4096
007: ADDRE = (HOST, PORT)
008: cap = None
009:
010: class MyTCPHandler(socketserver.BaseRequestHandler):
011:
012: def handle(self):
013: ret, img = cap.read()
014: if not ret: # キャプチャー画像取得に失敗したら終了
015: print(“Video Capture Err”)
016: exit()
017:
018: bData = pickle.dumps(img)
019: self.request.send(bData)
020:
021: if __name__ == “__main__”:
022:
023: cap = cv2.VideoCapture(0, cv2.CAP_V4L)
024: cap.set(cv2.CAP_PROP_FRAME_WIDTH, 1280)
025: cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 720)
026:
027: if not cap.isOpened():
028: print(“Not Opened Video Camera”)
029: exit()
030:
031: with socketserver.TCPServer((ADDRE), MyTCPHandler) as server:
032: server.serve_forever()