-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
157 lines (124 loc) · 3.86 KB
/
main.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
import base64
import cv2
import numpy as np
import uvicorn
from starlette.responses import RedirectResponse
from typing import Optional, Callable
from database import *
from fastapi.staticfiles import StaticFiles
from pydantic import BaseModel
from routers import contact
from fastapi.middleware.cors import CORSMiddleware
from fastapi import Body, FastAPI, Request, Response
from fastapi.routing import APIRoute
# from models.contact import *
app = FastAPI(title='Contact.ly', description='APIs for contact Apis', version='0.1')
# app.router.route_class = GzipRoute
origins = [
"http://localhost",
"http://localhost:8000",
"http://localhost:8080",
"https://0.0.0.0:8000",
"https://localhost:449",
"http://localhost:63342",
]
app.add_middleware(
CORSMiddleware,
allow_origins=origins,
allow_credentials=True,
allow_methods=["DELETE", "GET", "POST", "PUT"],
allow_headers=["*"],
)
app.mount("/static", StaticFiles(directory="static"), name="static")
class Contact(BaseModel):
contact_id: int
first_name: str
last_name: str
user_name: str
password: str
class Config:
schema_extra = {
"example": {
"contact_id": 1,
"first_name": "Jhon",
"last_name": "Doe",
"user_name": "jhon_123",
}
}
class Face(BaseModel):
face_id: Optional[str] = None
img_str: str
class ContactOut(BaseModel):
contact_id: int
first_name: str
last_name: str
user_name: str
@app.get("/")
def main():
return RedirectResponse(url="/docs/")
@app.post("/convert/{img_str}")
async def img_to_string(img_str: str):
with open("imgToString.jpeg", "wb") as new_file:
new_file.write(base64.b64decode(img_str))
return new_file
@app.post("/view/anhtostring2")
async def convert_str_img(face: Face):
img_str = face.img_str
img_name = face.face_id + '.jpeg'
img_path = 'images/' + img_name
with open(img_path, "wb") as new_file:
new_file.write(base64.b64decode(img_str))
return new_file
@app.get("/view/anhtostring")
async def convert_str_img(strvar: str):
return strvar
def chuyen_base64_sang_anh(anh_base64):
try:
anh_base64 = np.fromstring(base64.b64decode(anh_base64), dtype=np.uint8)
anh_base64 = cv2.imdecode(anh_base64, cv2.IMREAD_ANYCOLOR)
except:
return None
return anh_base64
def dem_so_mat(face):
# Khoi tao bo phat hien khuon mat
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
# Chuyen gray
gray = cv2.cvtColor(face, cv2.COLOR_BGR2GRAY)
# Phat hien khuon mat trong anh
faces = face_cascade.detectMultiScale(gray, 1.2, 10)
so_mat = len(faces)
return so_mat
@app.post("/view/nhandienkhuonmat")
async def nhandienkhuonmat(face: Face):
face_numbers = 0
# Doc anh tu client gui len
# facebase64 = await request.form()
# facebase64 = face.img_str
# Chuyen base 64 ve OpenCV Format
# face = chuyen_base64_sang_anh(facebase64)
face = cv2.imread('images/WIN_20210603_16_28_36_Pro.jpg')
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
# Chuyen gray
gray = cv2.cvtColor(face, cv2.COLOR_BGR2GRAY)
# Phat hien khuon mat trong anh
faces = face_cascade.detectMultiScale(gray, 1.2, 10)
so_mat = len(faces)
# Đếm số mặt trong ảnh
# face_numbers = dem_so_mat(face)
# Trả về
# return "Số mặt là = " + str(face_numbers)
return so_mat
#####################################################################
# app.include_router(contact.router_contacts)
# @app.on_event("startup")
# async def startup():
# print("Connecting...")
# if conn.is_closed():
# conn.connect()
#
#
# @app.on_event("shutdown")
# async def shutdown():
# print("Closing...")
# if not conn.is_closed():
# conn.close()