題名のとおり、画像データをサーバに送信中に非同期で撮影をさせたいです。
以下のように_shoot()が撮影したデータを_Send()に渡して送信させているのですが、非同期になりません。
await asyncio.Task()を複数なれべれば非同期になってくれるわけではないのですか?
Python3
1import asyncio,json,websockets,os,time,atexit,io,base64,socket,struct,serial 2import fcntl,struct,uuid,re,cv2 3import numpy as np 4import netifaces as ni 5import scapy.all as scapy 6import netifaces as ni 7from screeninfo import get_monitors 8from sys import argv 9 10from PIL import Image 11from jsongen import genjson 12 13monitor = get_monitors()[0] 14def imshow_fullscreen(winname, img): 15 cv2.namedWindow(winname, cv2.WINDOW_NORMAL) 16 cv2.setWindowProperty(winname, cv2.WND_PROP_FULLSCREEN, cv2.WINDOW_FULLSCREEN) 17 cv2.imshow(winname, img) 18 cv2.waitKey(300) 19def resize(img,re_length): 20 h, w = img.shape[:2] 21 re_h = re_w = re_length/min(h,w) 22 img2 = cv2.resize(img, dsize=None, fx=re_h , fy=re_w) 23 return img2 24def full_img(): 25 cap = cv2.VideoCapture (0) 26 WIDTH = 4056 27 HEIGHT = 3040 28 cap.set(cv2.CAP_PROP_FOURCC, cv2.VideoWriter_fourcc('Y','U','1','2')) 29 cap.set(cv2.CAP_PROP_FRAME_WIDTH, WIDTH) 30 cap.set(cv2.CAP_PROP_FRAME_HEIGHT, HEIGHT) 31 _, frame = cap.read() 32 imshow_fullscreen('screen', resize(frame,monitor.height)) 33 return cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) 34 35async def _Send(url,jlist): 36 async with websockets.connect(url,max_size=20000000) as websocket: 37 start = time.time() 38 sendj = json.dumps(jlist) 39 await websocket.send(sendj) 40 try: 41 recv_data = await websocket.recv() 42 except Exception as e: 43 print("websockets.exceptions.ConnectionClosedError:",e) 44 print('Reconnecting') 45 websocket = await websockets.connect(url,max_size=20000000) 46 await websocket.send(sendj) 47 else: 48 print("Send complete successfully:",time.time() - start,"[sec]") 49 print ("return:",recv_data) 50class CamPi: 51 @classmethod 52 async def _shoot(cls): 53 start = time.time() 54 png = io.BytesIO() 55 print("Please wait until the shooting is completed...",end="") 56 Image.fromarray(full_img()).save(png,"PNG") 57 print("\rshooting complete successfully:",time.time() - start,"[sec]") 58 b_frame = png.getvalue() 59 send_d = { 60 "in_data" : { 61 "db":"piscan", 62 "mac":"58:52:8a:d6:69:a1", 63 "jstr":genjson(), 64 "img":[base64.b64encode(b_frame).decode('utf-8'),base64.b64encode(b_frame).decode('utf-8')] 65 } 66 } 67 return send_d 68 @classmethod 69 async def Main(cls): 70 while(True): 71 start = time.time() 72 send_d = await asyncio.Task(cls._shoot()) 73 await asyncio.Task(_Send("ws://192.168.11.199:8080",send_d)) 74 print("End of loop time:",time.time() - start,"[sec]") 75 #time.sleep(10) 76 77 78if __name__ == '__main__': 79 if argv[1] == "-c": 80 asyncio.run(CamPi.Main())
追記
解決したわけではありませんが、結局multiProcessingでやりたいことは実現しました。でも非同期でのやりかたが結局わからないままなので質問は残しておきます。
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2021/01/22 04:55