This commit is contained in:
威勝 張 2024-10-19 11:43:43 +08:00
parent ecfe8bb354
commit b41bd63bba
3 changed files with 128 additions and 0 deletions

BIN
test_1019_api/img.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 43 KiB

128
test_1019_api/test_1019.py Normal file
View File

@ -0,0 +1,128 @@
import time
from datetime import datetime
from requests.structures import CaseInsensitiveDict
import os,base64
import threading
import json
import pandas as pd
import requests
import cv2
# 寫進WEBAPI
#request_url = "https://localhost:7205"
request_url = "http://211.22.135.143:7200"
class PostValues():
def __init__(self, Fun, headers, *args):
self.headers = headers
self.Fun = Fun
def post(self, data):
data_json = json.dumps(data)
response = requests.post(request_url + self.Fun, data=data_json, headers=self.headers, verify=False)
return response
def get(self):
response = requests.get(request_url + self.Fun, headers=self.headers, verify=False)
if response.status_code == 200:
data = response.json()
df = pd.DataFrame.from_records(data)
return df
else:
return
def put(self,data):
data_json = json.dumps(data)
response = requests.put(request_url + self.Fun, data=data_json, headers=self.headers, verify=False)
return response
def delete(self):
response = requests.delete(request_url + self.Fun, headers=self.headers, verify=False)
return response
def getSingleData(self):
response = requests.get(request_url + self.Fun, headers=self.headers, verify=False)
if response.status_code == 200:
data = response.json()
df = pd.DataFrame.from_records(data, index=[0])
return df
else:
return
class Posture_api():
def post_new_data(self,data):
API = "/api/Postural_Analysis_1"
headers = {'Content-type': 'application/json'}
response =PostValues(API, headers).post(data)
return response
def check_data(self,img_name):
API = "/api/Postural_Analysis_detail/img_name-"+img_name
headers = CaseInsensitiveDict();
headers['Accept'] = 'application/json'
data = PostValues(API, headers).get()
return data
def get_data(self,img_name):
API = "/api/Postural_Analysis_detail/" + img_name
headers = CaseInsensitiveDict();
headers['Accept'] = 'application/json'
data = PostValues(API, headers).getSingleData()
return data
#圖片 轉 base64
def img_to_base64(image):
if image is None:
return "None"
_, buffer = cv2.imencode('.jpg', image)
data = base64.b64encode(buffer).decode('utf-8')
data = "data:image/jpeg;base64," + data
return data
img = cv2.imread("img.jpg") # 讀取圖片
img_base64 = img_to_base64(img) # 轉base64
# 圖片名稱定義
img_name = f'1{datetime.now().strftime("%Y%m%d%H%M%S")}'
print(img_name)
# 上傳資料整理
up_load_data = {
"img_name": img_name,
"test_id": "1",
"test_analyst": " ",
"test_time": " ",
"test_date": " ",
"img": img_base64,
"data_creat_time": "2023-01-11T12:53:25.381Z",
}
API = Posture_api()
# 上傳資料
res = API.post_new_data(up_load_data)
if res.status_code >= 200 and res.status_code < 300:
# 等待運算結果
while True:
data = API.check_data(f'{img_name}.jpg')
if len(data)>0:
print(data)
break
time.sleep(0.5)
# 確認運算資料
img_data = API.get_data(f'{img_name}.jpg')
print(img_data)