-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain2.py
213 lines (169 loc) · 6.19 KB
/
main2.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
from fastapi import FastAPI, File, UploadFile, HTTPException, Body
import os
import io
import subprocess
import re
from utils.ocrresponse import response_data_with_binary_data
app = FastAPI()
# 指定文件保存的目录
DETECTION_SAVE_DIRECTORY = "/paddle/images/detection"
OCR_SAVE_DIRECTORY = "/paddle/images/ocr"
# 指定Detection项目目录
DETECTION_PATH = "/paddle/PaddleDetection/"
# 确保保存目录存在
os.makedirs(DETECTION_SAVE_DIRECTORY, exist_ok=True)
os.makedirs(OCR_SAVE_DIRECTORY, exist_ok=True)
# 用于存储内存中的文件对象
memory_files = {}
def get_image():
if memory_files == {}:
return None
else:
return memory_files
@app.get("/")
def read_root():
return {"Hello": "world"}
# 上传图片,保存到指定目录并保存到内存
@app.post("/upload/")
async def upload_image(file: UploadFile = File(...)):
# 确认文件是图片
if file.content_type not in ['image/jpeg', 'image/png', 'image/gif']:
raise HTTPException(status_code=400, detail="不支持的文件类型")
# 为文件生成唯一的文件名
filename = f"{hash(file.content_type)}_{file.filename}"
file_path = os.path.join(DETECTION_SAVE_DIRECTORY, filename)
# 将上传的文件内容读取到内存
file_content = await file.read()
memory_file = io.BytesIO(file_content)
# 保存到内存中,使用文件名作为键
memory_files[filename] = memory_file
# 将上传的文件保存到指定目录
with open(file_path, "wb") as f:
f.write(file_content)
img_file = "--image_file=" + file_path
infer_file = DETECTION_PATH + "deploy/python/infer.py"
# "--model_dir=./output_inference/picodet_lcnet_x2_5_640_mainbody"
model_dir = "--model_dir=" + DETECTION_PATH + "output_inference/picodet_lcnet_x2_5_640_mainbody"
command = [
"python",
infer_file,
model_dir,
img_file
]
# 使用subprocess.run执行命令
result = subprocess.run(command, capture_output=True, text=True)
# 获取命令的标准输出和标准错误
stdout = result.stdout
stderr = result.stderr
# 打印输出和错误(可选)
print("STDOUT:", stdout)
print("STDERR:", stderr)
pattern = r"class_id:(\d+), confidence:([0-9.]+), left_top:\[([0-9.]+),([0-9.]+)\],right_bottom:\[([0-9.]+),([0-9.]+)\]"
# 使用正则表达式查找所有匹配项
matches = re.findall(pattern, stdout)
# 将匹配项组装成字典列表
results = [
{
"class_id": int(class_id),
"confidence": float(confidence),
"bounding_box": {
"left_top": [float(x), float(y)],
"right_bottom": [float(w), float(h)]
}
}
for class_id, confidence, x, y, w, h in matches
]
# 检查命令是否成功执行
if result.returncode == 0:
return {"status": "success", "filename": filename, "logs": results}
else:
return {"status": "error", "error": stderr}
@app.post("/image-detection/")
async def upload_binary_data(file: UploadFile = File(...)):
# 检查文件是否为空
if not file.content_type:
raise HTTPException(status_code=400, detail="未提供文件")
# 为文件生成唯一的文件名
filename = f"{hash(file.content_type)}_{file.filename}"
print(f'filename = {filename}')
print(f'content_type = {file.content_type}')
file_path = os.path.join(DETECTION_SAVE_DIRECTORY, filename)
print(f'file_path = {file_path}')
# 读取文件的二进制数据
binary_data = await file.read()
#print(f'binary_data = {binary_data}')
memory_files[filename] = binary_data
# 将上传的文件保存到指定目录
with open(file_path, "wb") as f:
f.write(binary_data)
img_file = "--image_file=" + file_path
infer_file = DETECTION_PATH + "deploy/python/infer.py"
# "--model_dir=./output_inference/picodet_lcnet_x2_5_640_mainbody"
model_dir = "--model_dir=" + DETECTION_PATH + "output_inference/picodet_lcnet_x2_5_640_mainbody"
command = [
"python",
infer_file,
model_dir,
img_file
]
# 使用subprocess.run执行命令
result = subprocess.run(command, capture_output=True, text=True)
# 获取命令的标准输出和标准错误
stdout = result.stdout
stderr = result.stderr
# 打印输出和错误(可选)
print("STDOUT:", stdout)
print("STDERR:", stderr)
pattern = r"class_id:(\d+), confidence:([0-9.]+), left_top:\[([0-9.]+),([0-9.]+)\],right_bottom:\[([0-9.]+),([0-9.]+)\]"
# 使用正则表达式查找所有匹配项
matches = re.findall(pattern, stdout)
# 将匹配项组装成字典列表
results = [
{
"class_id": int(class_id),
"confidence": float(confidence),
"bounding_box": {
"left_top": [float(x), float(y)],
"right_bottom": [float(w), float(h)]
}
}
for class_id, confidence, x, y, w, h in matches
]
# 检查命令是否成功执行
if result.returncode == 0:
return {
"status": 200,
"result": {
"filename": filename,
"detections": results
}
}
else:
return {
"status": 0,
"result": {
"filename": filename,
"error": stderr
}
}
@app.post("/image-ocr/")
async def ocr_binary_data(file: UploadFile = File(...)):
# 检查文件是否为空
if not file.content_type:
raise HTTPException(status_code=400, detail="未提供文件")
# 为文件生成唯一的文件名
filename = f"{hash(file.content_type)}_{file.filename}"
# 读取文件的二进制数据
binary_data = await file.read()
# 将二进制数据保存到内存
memory_files[filename] = binary_data
final_result = response_data_with_binary_data(binary_data, filename)
return final_result
# 设置Uvicorn服务器的运行
if __name__ == "__main__":
import uvicorn
import sys
if len(sys.argv) > 1:
uvicorn.run(app, host="0.0.0.0", port=8000, reload=sys.argv[1])
else:
uvicorn.run(app, host="0.0.0.0", port=8000)