-
Notifications
You must be signed in to change notification settings - Fork 13
/
PyApiServer.py
142 lines (106 loc) · 3.97 KB
/
PyApiServer.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
import asyncio
import jiagu
from fastapi import FastAPI, status, Form
from fastapi.exceptions import RequestValidationError
from fastapi.responses import JSONResponse
from paddleocr import PaddleOCR
from PIL import Image
from sklearn.cluster import KMeans
from skimage import color
import torch
import numpy as np
import cv2
import BpmDetector
import vits_process
# Do initialization here
api = FastAPI()
ocr = PaddleOCR(use_angle_cls=True, lang='ch')
img_common = torch.hub.load('ultralytics/yolov5', 'custom', path='yolo/common.pt')
anime_face = torch.hub.load('ultralytics/yolov5', 'custom', path='yolo/yolov5s_anime.pt')
# Api begin
@api.exception_handler(RequestValidationError)
async def doHandleValidationError(_request, _exc):
return JSONResponse(
status_code=status.HTTP_422_UNPROCESSABLE_ENTITY,
content={'status': False, 'message': 'field required'}
)
@api.post('/bpm')
async def getBPM(audio: str = Form(...)):
loop = asyncio.get_event_loop()
bpm = await loop.run_in_executor(None, BpmDetector.detectWav, audio)
return {'status': True, 'data': bpm}
@api.post('/sentiment')
async def doDetectSentiment(text: str = Form(...)):
return {'status': True, 'data': jiagu.sentiment(text)[0] == 'positive'}
@api.post('/tts')
async def getTTS(text: str = Form(...)):
loop = asyncio.get_event_loop()
path = await loop.run_in_executor(None, vits_process.doTTS, text, 'ja')
return {'status': True, 'data': path}
@api.post('/tts_cn')
async def getTTSCN(text: str = Form(...)):
loop = asyncio.get_event_loop()
path = await loop.run_in_executor(None, vits_process.doTTS, text, 'zh')
return {'status': True, 'data': path}
@api.post('/ocr')
async def getOCR(path: str = Form(...)):
loop = asyncio.get_event_loop()
result = await loop.run_in_executor(None, doOCR, path)
return {'status': True, 'data': result}
@api.post('/lt')
async def isLt(path: str = Form(...)):
loop = asyncio.get_event_loop()
result = await loop.run_in_executor(None, detectLt, path)
return {'status': True, 'data': result}
@api.post('/blonde')
async def isBlonde(path: str = Form(...)):
loop = asyncio.get_event_loop()
result = await loop.run_in_executor(None, detectBlonde, path)
return {'status': True, 'data': result}
# Api end
def doOCR(path):
return '\n'.join([line[1][0] for line in ocr.ocr(path, cls=True)])
def detectLt(path):
img = Image.open(path)
if img.size[0] < 100 or img.size[1] < 100:
return False
for area in img_common(img).xyxy[0]:
if area[5] == 0 and area[4] > 0.8:
return True
return False
blondeColors = [
[90.73948779100502, -4.477169141775095, 42.932090784625785],
[96.01817010098311, -7.205140542378363, 28.57951059226316],
[81.3990919790363, 10.1113281133971, 23.247659861636638],
[95.85821436038806, -19.323659509977777, 67.48143654894507]
]
def detectBlonde(path):
img = cv2.imread(path, cv2.IMREAD_COLOR)
for (x1, y1, x2, y2, p, _) in anime_face(img).xyxy[0]:
if p < 0.7:
continue
if y1 <= 20:
y1 = 1
else:
y1 -= 20
now = img[y1:y1+(y2-y1)//4, x1:x2]
now = now.reshape((now.shape[0] * now.shape[1], 3))
clt = KMeans(n_clusters=6)
clt.fit(now)
fringe = color.rgb2lab(getMainColor(centroidHistogram(clt), clt.cluster_centers_))
for c in blondeColors:
if color.deltaE_cie76(fringe, c) < 100:
return True
return False
# Based on https://www.pyimagesearch.com/2014/05/26/opencv-python-k-means-color-clustering/
def centroidHistogram(clt):
numLabels = np.arange(0, len(np.unique(clt.labels_)) + 1)
(hist, _) = np.histogram(clt.labels_, bins=numLabels)
hist = hist.astype("float")
hist /= hist.sum()
return hist
def getMainColor(hist, centroids):
return sorted(zip(hist, centroids), key=lambda x: -x[0])[0][1]
if __name__ == '__main__':
import uvicorn
uvicorn.run(app=api, host="0.0.0.0", port=10090)