Advertisement
AnonKot

Server

Jul 25th, 2021
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.84 KB | None | 0 0
  1.  
  2. from kivy.uix.image import Image
  3. from kivy.clock import Clock
  4. from kivy.graphics.texture import Texture
  5. from kivy.app import App
  6. from kivy.uix.button import Button
  7. from kivy.uix.boxlayout import BoxLayout
  8. from kivy.uix.textinput import TextInput
  9. from kivy.uix.label import Label
  10. import pickle
  11. import socket
  12. import struct
  13. import cv2
  14. PORT = 8089
  15. class ScamPro(App):
  16. def __init__(self):
  17. super().__init__()
  18. self.HOST = "192.168.1.105"
  19. self.label = Label(text=' Сервер IP \n' + self.HOST)
  20.  
  21. def build(self):
  22. self.cam = False
  23. self.spam = False
  24. self.Demo = False
  25. self.s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  26. print('Socket created')
  27.  
  28. self.s.bind((self.HOST, PORT))
  29. print('Socket bind complete')
  30. self.s.listen(10)
  31. print('Socket now listening')
  32. self.conn, addr = self.s.accept()
  33. print(addr[0],'-Подключен')
  34. self.data = b'' ### CHANGED
  35. self.payload_size = struct.calcsize("L") ### CHANGED
  36. self.img1=Image()
  37. # Поле ввода
  38. main_layout = BoxLayout(orientation="vertical", padding=0, spacing=0)
  39. self.solution = TextInput(multiline=False, readonly=False, halign="right", font_size=55)
  40. main_layout.add_widget(self.solution)
  41. # Создание кнопок
  42. buttons = [["cam", "demo", "spam"], ]
  43. for row in buttons:
  44. h_layout = BoxLayout()
  45. for label in row:
  46. button = Button(text=label, pos_hint={"center_x": 0.5, "center_y": 0.5})
  47. button.bind(on_press=self.on_button_press)
  48. h_layout.add_widget(button)
  49. main_layout.add_widget(h_layout)
  50. h_layout.add_widget(self.label)
  51. main_layout.add_widget(self.img1)
  52. Clock.schedule_interval(self.update, 1.0/33.0)
  53. return main_layout
  54.  
  55. def update(self, dt):
  56. s=self.s
  57. def connect():
  58. self.conn, addr = s.accept()
  59. print(addr[0], '-Подключен')
  60. self.recv = self.conn.recv(4096)
  61. if self.cam:
  62. # Retrieve message size
  63. while len(self.data) < self.payload_size:
  64. try:
  65. self.recv=self.conn.recv(4096)
  66. except ConnectionResetError as error:
  67. print("Клиент отключен")
  68. connect()
  69. self.data += self.recv
  70.  
  71. packed_msg_size = self.data[:self.payload_size]
  72. self.data = self.data[self.payload_size:]
  73. msg_size = struct.unpack("L", packed_msg_size)[0] ### CHANGED
  74.  
  75. # Retrieve all data based on message size
  76. while len(self.data) < msg_size:
  77. self.data += self.conn.recv(4096)
  78.  
  79. frame_data = self.data[:msg_size]
  80. self.data = self.data[msg_size:]
  81.  
  82. # Extract frame
  83. frame = pickle.loads(frame_data)
  84. #cv2.imshow("CV2 Image", frame)
  85. # convert it to texture
  86. buf1 = cv2.flip(frame, 0)
  87. buf = buf1.tostring()
  88. texture1 = Texture.create(size=(frame.shape[1], frame.shape[0]), colorfmt='bgr')
  89. #if working on RASPBERRY PI, use colorfmt='rgba' here instead, but stick with "bgr" in blit_buffer.
  90. texture1.blit_buffer(buf, colorfmt='bgr', bufferfmt='ubyte')
  91. # display image from the texture
  92. self.img1.texture = texture1
  93.  
  94. def on_button_press(self, instance):
  95. if instance.text == "cam":
  96. self.cam=not self.cam
  97. if self.cam:
  98. instance.color = (173 / 255, 255 / 255, 47 / 255, 1)
  99.  
  100. else:
  101. instance.color = (255 / 255, 255 / 255, 255 / 255, 1)
  102.  
  103. ScamPro().run()
  104. cv2.destroyAllWindows()
  105.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement