Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from kivy.uix.image import Image
- from kivy.clock import Clock
- from kivy.graphics.texture import Texture
- from kivy.app import App
- from kivy.uix.button import Button
- from kivy.uix.boxlayout import BoxLayout
- from kivy.uix.textinput import TextInput
- from kivy.uix.label import Label
- import pickle
- import socket
- import struct
- import cv2
- PORT = 8089
- class ScamPro(App):
- def __init__(self):
- super().__init__()
- self.HOST = "192.168.1.105"
- self.label = Label(text=' Сервер IP \n' + self.HOST)
- def build(self):
- self.cam = False
- self.spam = False
- self.Demo = False
- self.s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
- print('Socket created')
- self.s.bind((self.HOST, PORT))
- print('Socket bind complete')
- self.s.listen(10)
- print('Socket now listening')
- self.conn, addr = self.s.accept()
- print(addr[0],'-Подключен')
- self.data = b'' ### CHANGED
- self.payload_size = struct.calcsize("L") ### CHANGED
- self.img1=Image()
- # Поле ввода
- main_layout = BoxLayout(orientation="vertical", padding=0, spacing=0)
- self.solution = TextInput(multiline=False, readonly=False, halign="right", font_size=55)
- main_layout.add_widget(self.solution)
- # Создание кнопок
- buttons = [["cam", "demo", "spam"], ]
- for row in buttons:
- h_layout = BoxLayout()
- for label in row:
- button = Button(text=label, pos_hint={"center_x": 0.5, "center_y": 0.5})
- button.bind(on_press=self.on_button_press)
- h_layout.add_widget(button)
- main_layout.add_widget(h_layout)
- h_layout.add_widget(self.label)
- main_layout.add_widget(self.img1)
- Clock.schedule_interval(self.update, 1.0/33.0)
- return main_layout
- def update(self, dt):
- s=self.s
- def connect():
- self.conn, addr = s.accept()
- print(addr[0], '-Подключен')
- self.recv = self.conn.recv(4096)
- if self.cam:
- # Retrieve message size
- while len(self.data) < self.payload_size:
- try:
- self.recv=self.conn.recv(4096)
- except ConnectionResetError as error:
- print("Клиент отключен")
- connect()
- self.data += self.recv
- packed_msg_size = self.data[:self.payload_size]
- self.data = self.data[self.payload_size:]
- msg_size = struct.unpack("L", packed_msg_size)[0] ### CHANGED
- # Retrieve all data based on message size
- while len(self.data) < msg_size:
- self.data += self.conn.recv(4096)
- frame_data = self.data[:msg_size]
- self.data = self.data[msg_size:]
- # Extract frame
- frame = pickle.loads(frame_data)
- #cv2.imshow("CV2 Image", frame)
- # convert it to texture
- buf1 = cv2.flip(frame, 0)
- buf = buf1.tostring()
- texture1 = Texture.create(size=(frame.shape[1], frame.shape[0]), colorfmt='bgr')
- #if working on RASPBERRY PI, use colorfmt='rgba' here instead, but stick with "bgr" in blit_buffer.
- texture1.blit_buffer(buf, colorfmt='bgr', bufferfmt='ubyte')
- # display image from the texture
- self.img1.texture = texture1
- def on_button_press(self, instance):
- if instance.text == "cam":
- self.cam=not self.cam
- if self.cam:
- instance.color = (173 / 255, 255 / 255, 47 / 255, 1)
- else:
- instance.color = (255 / 255, 255 / 255, 255 / 255, 1)
- ScamPro().run()
- cv2.destroyAllWindows()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement