前提・実現したいこと
ラズパイ側のUSBカメラで撮影した画像をシリアル通信にてPCに転送し、
PC側で画像として復元することを実現したい
発生している問題・エラーメッセージ
シリアル通信にて受信したものを画像として復元するためには、
もとの画像のサイズに関する情報もラズパイから受信しないと復元できない。
その送受信の方法をどのようにすれば良いか悩んでいます。
アドバイスをお願いします。
該当のソースコード
Python3
1ラズパイ側 2 3# -*- coding: utf-8 -*- 4import cv2 5from PIL import Image 6import serial 7 8def smile_recognition(): 9 # 笑顔を検出するとその画像を以下のpathに画像'smile_img.jpg'として保存 10 # 詳細省略 11 smile_img_path ="../images/" 12 13def convert_string(img_gray = "../images/smile_img.jpg"): 14 #img = np.array(Image.open(img_gray)) #画像をndarray化 15 img = Image.open(img_gray) 16 (width, height) = img.size 17 #print((width, height) ) 18 #img_string = img.tobytes() + b"\n" 19 img_string = img.tobytes() #バイト配列へ変換 20 return img_string 21 22def transfer_to_pc(string): 23 port = serial.Serial( 24 port = "/dev/ttyS0", 25 baudrate=115200, 26 parity = serial.PARITY_NONE, 27 stopbits = serial.STOPBITS_ONE, 28 bytesize = serial.EIGHTBITS, 29 timeout = 5 30 ) 31 port.write(string) 32 port.close() 33 34 35smile_recognition() 36img_gray = "../images/smile_img.jpg" 37img_string = convert_string(img_gray) 38 39print(img_string) #確認用 40transfer_to_pc(img_string) 41 42 43 44 45PC側 46# -*- coding: utf-8 -*- 47 48from PIL import Image 49import numpy as np 50import serial 51 52def bytes_receive(): 53 ser = serial.Serial( 54 port = "COM6", 55 baudrate=115200, 56 parity = serial.PARITY_NONE, 57 stopbits = serial.STOPBITS_ONE, 58 bytesize = serial.EIGHTBITS, 59 timeout = 5 60 ) 61 img_bytes_list = ser.readlines() 62 #print(img_bytes_list) 63 img_bytes = b"".join(img_bytes_list) #リスト → 文字列 64 print(img_bytes) 65 66 ser.close() 67 print("End") 68 return img_bytes 69 70def convert_img(img_bytes): 71 img_array = np.fromstring(img_bytes,dtype ='uint8') #バイトデータ→ndarray変換 72 img_array2 = np.reshape(img_array,(105, 105)) # ←(105,105)を変数として受け取りたい 73 print(img_array2.shape) 74 img = Image.fromarray(img_array2) 75 76 return img 77 78img_bytes = bytes_receive() 79img = convert_img(img_bytes) 80 81img.save('./images/decoded_img.jpg', quality=75) 82
試したこと
もとの画像サイズ(height,width)を
height_bytes = height.to_bytes(1, 'big') + b"\n"
width_bytes = width.to_bytes(1, 'big') + b"\n"
とバイト型にして
height_bytesとwidth_bytes をシリアルにてを送信することを試みました。
その時は画像とheight, width を3つに分けて送信したのですが、
どれかが欠けて受信され対応付けがうまくいきませんでした。
補足情報(FW/ツールのバージョンなど)
ここにより詳細な情報を記載してください。
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。