実現したいこと
pythonでWebsocketでjpgデータを受信してtkinter canvasに表示したい
クライアント側はUSBカメラOpenCVで毎秒、画像バイト数、jpeg画像(base64)で送信しています
サーバ側は受信したらjpeg画像をバイナリにしcanvasに表示する
前提
Websocketでデータ受信してファイルmyfile.jpgに出力しています
ファイルは正常に画像表示できています。
tk_image = ImageTk.PhotoImage(img1)
で以下のエラーメッセージが発生していると思われます。
この辺、分かる方がいれば教えてください。
発生している問題・エラーメッセージ
エラーメッセージ Exception occurred during processing of request from ('127.0.0.1', 53064) Traceback (most recent call last): File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.3056.0_x64__qbz5n2kfra8p0\lib\socketserver.py", line 683, in process_request_thread self.finish_request(request, client_address) File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.3056.0_x64__qbz5n2kfra8p0\lib\socketserver.py", line 360, in finish_request self.RequestHandlerClass(request, client_address, self) File "C:\Users\Arail\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\websocket_server\websocket_server.py", line 271, in __init__ StreamRequestHandler.__init__(self, socket, addr, server) File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.3056.0_x64__qbz5n2kfra8p0\lib\socketserver.py", line 747, in __init__ self.handle() File "C:\Users\Arail\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\websocket_server\websocket_server.py", line 284, in handle self.read_next_message() File "C:\Users\Arail\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\websocket_server\websocket_server.py", line 341, in read_next_message opcode_handler(self, message_bytes.decode('utf8')) File "C:\Users\Arail\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\websocket_server\websocket_server.py", line 163, in _message_received_ self.message_received(self.handler_to_client(handler), self, msg) File "E:\linux関係\TinkerBoard\pg\websocket\server2.py", line 78, in on_recieve tk_image = ImageTk.PhotoImage(img1) File "C:\Users\Arail\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\PIL\ImageTk.py", line 108, in __init__ mode = Image.getmodebase(mode) File "C:\Users\Arail\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\PIL\Image.py", line 267, in getmodebase return ImageMode.getmode(mode).basemode File "C:\Users\Arail\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\PIL\ImageMode.py", line 74, in getmode return _modes[mode] TypeError: unhashable type: 'numpy.ndarray' ---------------------------------------- Exception ignored in: <function PhotoImage.__del__ at 0x0000025706A58CA0> Traceback (most recent call last): File "C:\Users\Arail\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\PIL\ImageTk.py", line 118, in __del__ name = self.__photo.name AttributeError: 'PhotoImage' object has no attribute '_PhotoImage__photo'
該当のソースコード
python
1import tkinter as tk 2import threading 3import base64 4import io 5import numpy as np 6from PIL import Image 7from PIL import ImageTk 8from datetime import datetime 9from websocket_server import WebsocketServer 10 11class Application(tk.Frame): 12 thread1 = None 13 server = None 14 15 def __init__(self,master): 16 super().__init__(master) 17 18 try: 19 self.master.geometry("1024x720") 20 self.master.title("WebsocketServer") 21 #Frame_Camera 22 self.frame_cam = tk.Frame(self.master, relief= tk.FLAT) 23 self.frame_cam.place(x = 2, y = 2) 24 self.frame_cam.configure(width = 640, height = 480) 25 #Canvas 26 self.canvas1 = tk.Canvas(self.frame_cam, bg='#000000') 27 self.canvas1.configure( width= 640, height=480) 28 self.canvas1.place( x=1, y=2 ) 29 # Close 30 self.btn_close = tk.Button( self.master, text='閉じる') 31 self.btn_close.configure(command=self.press_close_button ) 32 self.btn_close.place( x=750, y=50, width=250, height=100 ) 33 34 self.thread1 = threading.Thread(target = self.thread1) 35 self.thread1.daemon = True 36 self.thread1.start() 37 38 except Exception as e: 39 print('=== エラー内容 __init__() ===') 40 print('type:' + str(type(e))) 41 print('e自身:' + str(e)) 42 43 def press_close_button(self): 44 self.master.destroy() 45 #websocketを終了させる 46 #self.server.shutdown_gracefully() 47 #self.server.shutdown_abruptly() 48 self.server.shutdown() 49 exit(-1) 50 51 52 def thread1(self): 53 try: 54 self.server = WebsocketServer(port=8765, host='127.0.0.1') 55 self.server.set_fn_new_client(self.new_client) 56 self.server.set_fn_message_received(self.on_recieve) 57 self.server.run_forever() 58 59 except Exception as e: 60 print('=== エラー内容 thread1() ===') 61 print('type:' + str(type(e))) 62 print('e自身:' + str(e)) 63 64 65 def new_client(self, client, server): 66 server.send_message_to_all(datetime.now().isoformat() + ": new client joined!") 67 68 def on_recieve(self, client, server, message): 69 flen = message[:6] 70 picb = base64.b64decode(message[6:]) 71 img1 = np.frombuffer(picb,dtype=np.uint8) 72 f = open('myfile.jpg', 'wb') 73 f.write(img1) 74 f.close() 75 tk_image = ImageTk.PhotoImage(img1) 76 self.canvas1.create_image(0,0, image = tk_image) 77 print(client["address"][0]+":" + str(client["address"][1]) + "から受信 : " + flen) 78 79 80if __name__ == "__main__": 81 root = tk.Tk() 82 app = Application(master = root) 83 app.mainloop()
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2023/04/26 10:08
2023/04/26 10:14
2023/04/26 12:30