Archived
Дипломная работа
This commit is contained in:
@@ -0,0 +1,61 @@
|
||||
#!/usr/bin/python3
|
||||
import json
|
||||
|
||||
import cv2
|
||||
import uvicorn, asyncio
|
||||
import numpy as np
|
||||
|
||||
from fastapi import FastAPI, File, UploadFile, Form, HTTPException, Depends
|
||||
|
||||
from schemas.api_schemas import *
|
||||
|
||||
from workers.mongo_worker import MongoWorker
|
||||
from workers.face_rec_worker import FaceRecognition
|
||||
|
||||
app = FastAPI()
|
||||
mongo_worker = MongoWorker()
|
||||
face_recognition_worker = FaceRecognition()
|
||||
|
||||
@app.post('/add_person')
|
||||
async def add_person(json_data: str = Form(...),
|
||||
img_data: UploadFile = File(...),
|
||||
face_recognition: FaceRecognition = Depends(lambda: face_recognition_worker),
|
||||
mongo: MongoWorker = Depends(lambda: mongo_worker)) -> BaseResponse:
|
||||
json_validate = AddPersonJSON(**json.loads(json_data))
|
||||
if img_data.content_type not in ALLOWED_MIME_TYPES:
|
||||
raise HTTPException(400, detail="Invalid file type")
|
||||
|
||||
file_data = await img_data.read()
|
||||
face_img = cv2.imdecode(np.fromstring(file_data, np.uint8), cv2.IMREAD_COLOR)
|
||||
encodings = face_recognition.get_encodings(face_img)
|
||||
if not encodings:
|
||||
raise HTTPException(422, detail="Can't find human face")
|
||||
|
||||
result = mongo.add_person(json_validate.username, encodings[0], img_data.filename, face_img)
|
||||
if result.error:
|
||||
raise HTTPException(422, detail=str(result))
|
||||
return BaseResponse(result="Success")
|
||||
|
||||
@app.post('/find_by_img')
|
||||
async def find_by_img(img_data: UploadFile = File(...),
|
||||
face_recognition: FaceRecognition = Depends(lambda: face_recognition_worker),
|
||||
mongo: MongoWorker = Depends(lambda: mongo_worker)) -> BaseResponse:
|
||||
if img_data.content_type not in ALLOWED_MIME_TYPES:
|
||||
raise HTTPException(400, detail="Invalid file type")
|
||||
|
||||
file_data = await img_data.read()
|
||||
face_img = cv2.imdecode(np.fromstring(file_data, np.uint8), cv2.IMREAD_COLOR)
|
||||
encodings = face_recognition.get_encodings(face_img)
|
||||
if not encodings:
|
||||
raise HTTPException(422, detail="Can't find human face")
|
||||
res = face_recognition_worker.face_distance(encodings)
|
||||
print(res)
|
||||
return BaseResponse(result=res)
|
||||
|
||||
async def main():
|
||||
config = uvicorn.Config("server:app", host="0.0.0.0", port=5000, log_level="info")
|
||||
server = uvicorn.Server(config)
|
||||
await server.serve()
|
||||
|
||||
if __name__ == "__main__":
|
||||
asyncio.run(main())
|
||||
Reference in New Issue
Block a user