kivy , opencv , socket

174 views
Skip to first unread message

AR

unread,
Dec 11, 2021, 7:02:22 AM12/11/21
to Kivy users support

i created a code for transfer video from raspiberry pi to laptop.
can transmit the code and receive it. but can’t show in my kivy app …i would like to know is there any mistake in my client code …(not kivy opencv)

Server code for transmitting video


import socket, cv2, pickle,struct # Socket Create server_socket = socket.socket(socket.AF_INET,socket.SOCK_STREAM) host_name = socket.gethostname() host_ip = socket.gethostbyname(host_name) print('HOST IP:',host_ip) port = 9998 socket_address = (host_ip,port) # Socket Bind server_socket.bind(socket_address) # Socket Listen server_socket.listen(5) print("LISTENING AT:",socket_address) # Socket Accept while True: client_socket,addr = server_socket.accept() print('GOT CONNECTION FROM:',addr) if client_socket: vid = cv2.VideoCapture(0) while(vid.isOpened()): img,frame = vid.read() a = pickle.dumps(frame) message = struct.pack("Q",len(a))+a client_socket.sendall(message) cv2.imshow('TRANSMITTING VIDEO',frame) key = cv2.waitKey(1) & 0xFF if key ==ord('q'): client_socket.close()

Client code

import socket,cv2, pickle,struct from kivy.app import App from kivy.uix.widget import Widget from kivy.uix.boxlayout import BoxLayout from kivy.uix.image import Image from kivy.clock import Clock from kivy.graphics.texture import Texture import cv2 client_socket = socket.socket(socket.AF_INET,socket.SOCK_STREAM) host_ip = '127.0.1.1' # paste your server ip address here port = 9998 client_socket.connect((host_ip,port)) # a tuple data = b"" payload_size = struct.calcsize("Q") class CamApp(App): def build(self): self.img1=Image() layout = BoxLayout() layout.add_widget(self.img1) #opencv2 stuffs #self.capture = cv2.VideoCapture(0) cv2.namedWindow("CV2 Image") Clock.schedule_interval(self.update, 1.0/33.0) return layout def update(self, *args): data = b"" while True: if len(data) < payload_size: packet = client_socket.recv(4*1024) # 4K if not packet: break data+=packet packed_msg_size = data[:payload_size] data = data[payload_size:] msg_size = struct.unpack("Q",packed_msg_size)[0] while len(data) < msg_size: data += client_socket.recv(4*1024) #print(data,"value: msg:", msg_szie) frame_data = data[:msg_size] data = data[msg_size:] frame = pickle.loads(frame_data) cv2.imshow("recived video", frame) cv2.waitKey(1) buf1 = cv2.flip(frame, 0) buf = buf1.tostring() texture1 = Texture.create(size=(frame.shape[1], frame.shape[0]), colorfmt='bgr') texture1.blit_buffer(buf, colorfmt='bgr', bufferfmt='ubyte') self.img1.texture = texture1 #cv2.imshow("RECEIVING VIDEO",frame) #cv2.waitKey(1) #key = cv2.waitKey(1) & 0xFF #if key == ord('q'): # break client_socket.close() #update() CamApp().run()

The opencv code that work fine with kivy

from kivy.app import App from kivy.uix.widget import Widget from kivy.uix.boxlayout import BoxLayout from kivy.uix.image import Image from kivy.clock import Clock from kivy.graphics.texture import Texture import cv2 class CamApp(App): def build(self): self.img1=Image() layout = BoxLayout() layout.add_widget(self.img1) #opencv2 stuffs self.capture = cv2.VideoCapture(0) cv2.namedWindow("CV2 Image") Clock.schedule_interval(self.update, 1.0/33.0) return layout def update(self, dt): # display image from cam in opencv window ret, frame = self.capture.read() 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 if __name__ == '__main__': CamApp().run() cv2.destroyAllWindows()


Elliot Garbus

unread,
Dec 11, 2021, 8:27:14 AM12/11/21
to kivy-...@googlegroups.com

The sockets library is blocking by default.  This means if you call recv, and there is no data in the buffer, the code will wait for data.  This is not compatible with kivy behavior.  You can set sockets to operate in non-blocking mode.

Here is a description: https://pymotw.com/3/socket/nonblocking.html

And https://docs.python.org/3/library/socket.html#notes-on-socket-timeouts

 

Kivy operates an event loop.  All code must return to the event loop.  The kivy event loop is described here: https://kivy.org/doc/stable/guide/events.html

 

You need to setup the sockets in the client sockets to run in non-blocking mode.  It also looks like you are calling socket.close() at the end of update.  This does not look correct.  I would think you would want to close the stock in the on_stop() method of app, when the program is closing.

--
You received this message because you are subscribed to the Google Groups "Kivy users support" group.
To unsubscribe from this group and stop receiving emails from it, send an email to kivy-users+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/55ac1701-e45d-4b72-a221-88c7ee76fa52n%40googlegroups.com.

 

Reply all
Reply to author
Forward
0 new messages