121 lines
3.3 KiB
Python
121 lines
3.3 KiB
Python
|
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:7500"
|
||
|
class Restful_api():
|
||
|
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 Tongue_api():
|
||
|
|
||
|
def post_new_data(self,data):
|
||
|
API = "/api/Tongue_analysis_tw_1"
|
||
|
headers = {'Content-type': 'application/json'}
|
||
|
response =Restful_api(API, headers).post(data)
|
||
|
return response
|
||
|
|
||
|
def check_data(self,img_name):
|
||
|
API = "/api/Tongue_analysis_detail/img_name-"+img_name
|
||
|
headers = CaseInsensitiveDict();
|
||
|
headers['Accept'] = 'application/json'
|
||
|
data = Restful_api(API, headers).get()
|
||
|
return data
|
||
|
|
||
|
def get_data(self,img_name):
|
||
|
API = "/api/Tongue_analysis_detail/" + img_name
|
||
|
headers = CaseInsensitiveDict();
|
||
|
headers['Accept'] = 'application/json'
|
||
|
data = Restful_api(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 = Tongue_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)
|