@@ -27,55 +27,65 @@ class OfficialInterface:
2727 TEAM_ID = 117 # チームID
2828
2929 @classmethod
30- def upload_snap (cls , img_path : str ) -> bool :
30+ def upload_snap (cls , img_path : str , maxAttempts : int = 3 ) -> bool :
3131 """指定された画像をアップロードする.
3232
3333 Args:
3434 img_path (str): アップロードする画像のパス
35+ maxAttempts (int): 最大試行回数
3536
3637 Returns:
3738 success (bool): 通信が成功したか(成功:true/失敗:false)
3839 """
39- url = f"http://{ cls .SERVER_IP } /snap"
40- # リクエストヘッダー
41- headers = {
42- "Content-Type" : "image/jpeg"
43- }
44- # リクエストパラメータ
45- params = {
46- "id" : cls .TEAM_ID
47- }
48- try :
49- if not os .path .exists (img_path ):
50- print (f"画像ファイルが存在しません: { img_path } " )
51- return False
52- # サイズが正しくない場合はリサイズする
53- img = Image .open (img_path )
54- width , height = img .size
55- if not (width == 800 and height == 600 ):
56- img = img .resize ((800 , 600 ))
57- img .save (img_path , format = "JPEG" )
58- # bytes型で読み込み
59- with open (img_path , "rb" ) as image_file :
60- image_data = image_file .read ()
61- # APIにリクエストを送信
62- response = requests .post (url , headers = headers ,
63- data = image_data , params = params )
64- # レスポンスのステータスコードが201の場合、通信成功
65- print ("Response status code:" , response .status_code )
66- print ("Response text:" , response .text ) # 追加
67- if response .status_code != 201 :
68- raise ResponseError ("Failed to send upload image." )
69- success = True
70- print ("Image uploaded successfully." )
71- except Exception as e :
72- print (e )
73- success = False
40+ # 試行回数(attempts)が最大試行回数(maxAttempts)を超えるまで送信を試みる
41+ attempts = 0
42+ success = False
43+ while attempts < maxAttempts :
44+ url = f"http://{ cls .SERVER_IP } /snap"
45+ # リクエストヘッダー
46+ headers = {
47+ "Content-Type" : "image/jpeg"
48+ }
49+ # リクエストパラメータ
50+ params = {
51+ "id" : cls .TEAM_ID
52+ }
53+ try :
54+ if not os .path .exists (img_path ):
55+ print (f"画像ファイルが存在しません: { img_path } " )
56+ return False
57+ # サイズが正しくない場合はリサイズする
58+ img = Image .open (img_path )
59+ width , height = img .size
60+ if not (width == 800 and height == 600 ):
61+ img = img .resize ((800 , 600 ))
62+ img .save (img_path , format = "JPEG" )
63+ # bytes型で読み込み
64+ with open (img_path , "rb" ) as image_file :
65+ image_data = image_file .read ()
66+ # APIにリクエストを送信
67+ response = requests .post (url , headers = headers ,
68+ data = image_data , params = params )
69+ # レスポンスのステータスコードが201の場合、通信成功
70+ print ("Response status code:" , response .status_code )
71+ print ("Response text:" , response .text ) # 追加
72+ if response .status_code != 201 :
73+ raise ResponseError ("Failed to send upload image." )
74+ success = True
75+ print ("Image uploaded successfully." )
76+ return success
77+ except Exception as e :
78+ print (e )
79+ success = False
80+
81+ attempts += 1
82+
83+ print (f"Upload failed after { maxAttempts } attempts" )
7484 return success
7585
7686
7787if __name__ == "__main__" :
7888 print ("test-start" )
7989 print ("Current working directory:" , os .getcwd ())
80- OfficialInterface .upload_snap ("tests/testdata /img/Fig/Fig1-1.JPEG" )
90+ OfficialInterface .upload_snap ("tests/test_data /img/Fig/Fig1-1.JPEG" )
8191 print ("test-end" )
0 commit comments