Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import cv2
- import socket
- import pickle
- import struct
- import time
- import threading
- # import pyaudio
- from mss import mss
- import numpy as np
- e = True
- Status = b'CameraOFF'
- cap = None
- def CameraOFF():
- global cap
- global Status
- cap.release()
- print("CameraOFF")
- cap = None
- Status = b'CameraOFF'
- def connect():
- global cap
- global e
- global clientsocket
- time.sleep(10)
- print("TryConnect")
- clientsocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
- try:
- clientsocket.connect(('192.168.1.104', 8089))
- except socket.error as error:
- print("Error")
- if cap != None:
- CameraOFF()
- connect()
- else:
- print("RequestAccept")
- e = False
- def recv():
- while True:
- global clientsocket
- global e
- global Status
- global potok
- try:
- Status = clientsocket.recv(1024)
- except ConnectionResetError as error:
- print(1)
- e = True
- break
- potok = threading.Thread(target=recv)
- def cameraON():
- global cap
- if cap == None:
- print("CameraON")
- cap = cv2.VideoCapture(0, cv2.CAP_DSHOW)
- ret, frame = cap.read()
- frame = cv2.flip(frame, 0)
- data = pickle.dumps(frame)
- message_size = struct.pack("L", len(data))
- try:
- clientsocket.sendall(message_size + data)
- except ConnectionResetError as error:
- print("ServerError")
- CameraOFF()
- if Status == b'CameraOFF' and cap != None:
- CameraOFF()
- potok = threading.Thread(target=recv)
- while True:
- if e == True:
- connect()
- elif e == False and not potok.is_alive():
- potok.start()
- if Status == b'CameraOFF':
- time.sleep(1)
- if Status == b'CameraON':
- cameraON()
- cv2.destroyAllWindows()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement