Skip to content

Instantly share code, notes, and snippets.

@Zxilly
Last active May 22, 2022 11:03
Show Gist options
  • Save Zxilly/79e5cc0d7769eddcb1e1e893a20e4a28 to your computer and use it in GitHub Desktop.
Save Zxilly/79e5cc0d7769eddcb1e1e893a20e4a28 to your computer and use it in GitHub Desktop.
import subprocess
import uuid
import uvicorn
from fastapi import FastAPI, UploadFile, File
from fastapi.middleware.cors import CORSMiddleware
app = FastAPI()
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
@app.post('/handler')
async def handler(
file: UploadFile = File(...)
):
random_name = str(uuid.uuid4().int >> 64)[0:16]
error = ""
success = ""
res = await file.read()
filename = f"/tmp/{random_name}"
with open(filename, "wb") as f:
f.write(res)
binary = "/home/super2/caffe_soft/caffe-master/build/examples/cpp_classification/classification.bin"
deploy = "/home/super2/caffe_soft/caffe-master/my_file/crop_diseases/VGG16deploy.prototxt"
module = "/home/super2/caffe_soft/caffe-master/my_file/crop_diseases/tmp_vgg16/vgg16_caffenet_train_iter_3000.caffemodel"
proto = "/home/super2/caffe_soft/caffe-master/my_file/crop_diseases/mean.binaryproto"
word = "/home/super2/caffe_soft/caffe-master/my_file/crop_diseases/synset_words.txt"
process = subprocess.Popen([binary, deploy, module, proto, word, filename], stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
try:
returnCode = process.wait(timeout=15)
print(returnCode)
if returnCode != 0:
error = process.communicate()[1].decode()
else:
success = process.communicate()[0].decode()
except subprocess.TimeoutExpired:
# print("timeout")
process.terminate()
error = 'Recognize Timeout'
return {"success": success, "error": error}
if __name__ == '__main__':
uvicorn.run('main:app', host='0.0.0.0', port=26546, debug=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment