-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
234 lines (179 loc) Β· 7.02 KB
/
app.py
File metadata and controls
234 lines (179 loc) Β· 7.02 KB
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
import traceback
import os
import cv2
import base64
import numpy as np
from uuid import uuid4
from multiprocessing import Process, Queue
from flask import Flask, request, jsonify
from flask_cors import CORS
from dotenv import load_dotenv
load_dotenv()
# λͺ¨λΈ import
from models.thumb_stt import find_best_thumbnail, analyze_video_content
from models.face_arrange import analyze_face_from_frame
from models.pet_daily import classify_media
from models.pet_shorts import find_pet_segments, compile_pet_shorts
# Worker queues
stt_q = Queue()
stt_res_q = Queue()
pet_q = Queue()
pet_res_q = Queue()
app = Flask(__name__)
CORS(app)
# ============================================================
# 1) μΌκ΅΄ μ λ ¬ (μ€μκ°)
# ============================================================
@app.route("/face_arrange", methods=["POST"])
def face_arrange_api():
print("\nπ [DEBUG] /face_arrange νΈμΆλ¨")
try:
# μ΄λ―Έμ§ μ½κΈ°
if "file" in request.files:
img_bytes = request.files["file"].read()
else:
data = request.get_json()
if not data or "image" not in data:
print("β [ERROR] image(base64) λλ file μμ")
return jsonify({"error": "image(base64) or file required"}), 400
try:
img_bytes = base64.b64decode(data["image"])
except:
print("β [ERROR] base64 decode μ€ν¨")
return jsonify({"error": "base64 decode failed"}), 400
np_arr = np.frombuffer(img_bytes, np.uint8)
frame = cv2.imdecode(np_arr, cv2.IMREAD_COLOR)
if frame is None:
print("β [ERROR] frame decode μ€ν¨")
return jsonify({"error": "image decode failed"}), 400
# μΌκ΅΄ λΆμ
result = analyze_face_from_frame(frame)
print(f"π [DEBUG] λΆμ κ²°κ³Ό: {result}")
return jsonify(result)
except Exception as e:
print("\nπ₯π₯π₯ [EXCEPTION in /face_arrange]")
traceback.print_exc()
return jsonify({"error": str(e)}), 500
# ============================================================
# 2) μΈλ€μΌ μΆμΆ
# ============================================================
@app.route("/thumbnail", methods=["POST"])
def thumbnail_api():
print("\nπ [DEBUG] /thumbnail νΈμΆλ¨")
if "video" not in request.files:
print("β [ERROR] video μμ")
return jsonify({"error": "No video provided"}), 400
temp_path = f"temp_{uuid4().hex}.mp4"
request.files["video"].save(temp_path)
print(f"π [DEBUG] μ μ₯λ νμΌ: {temp_path}")
try:
result = find_best_thumbnail(temp_path)
print(f"π [DEBUG] μΈλ€μΌ λΆμ κ²°κ³Ό: {result}")
os.remove(temp_path)
if not result:
print("β [ERROR] find_best_thumbnail() κ²°κ³Ό μμ")
return jsonify({"error": "No valid thumbnail"}), 500
return jsonify(result)
except Exception as e:
print("\nπ₯π₯π₯ [EXCEPTION in /thumbnail]")
traceback.print_exc()
if os.path.exists(temp_path):
os.remove(temp_path)
return jsonify({"error": str(e)}), 500
# ============================================================
# 3) STT + μμ½ + μ λͺ© μμ± β Worker
# ============================================================
@app.route("/stt", methods=["POST"])
def stt_api():
print("\nπ [DEBUG] /stt νΈμΆλ¨")
if "video" not in request.files:
print("β [ERROR] video μμ")
return jsonify({"error": "No video provided"}), 400
api_key = request.form.get("api_key")
if not api_key:
print("β [ERROR] API Key μμ")
return jsonify({"error": "Missing API Key"}), 400
file = request.files["video"]
filename = file.filename or "upload.webm"
# νμ₯μ μΆμΆ
if "." in filename:
ext = filename.rsplit(".", 1)[-1].lower()
else:
ext = "webm"
task_id = uuid4().hex
temp_path = f"temp_{task_id}.{ext}"
file.save(temp_path)
print(f"π [DEBUG] STT νμΌ μ μ₯: {temp_path}")
try:
stt_q.put({"id": task_id, "path": temp_path, "api_key": api_key})
print("π [DEBUG] STT μμ
νμ μ λ¬ μλ£")
result = stt_res_q.get()
print(f"π [DEBUG] STT κ²°κ³Ό: {result}")
return jsonify(result)
except Exception as e:
print("\nπ₯π₯π₯ [EXCEPTION in /stt]")
traceback.print_exc()
return jsonify({"error": str(e)}), 500
finally:
if os.path.exists(temp_path):
os.remove(temp_path)
# ============================================================
# 4) λ°λ €λλ¬Ό DAILY
# ============================================================
@app.route("/pet_daily", methods=["POST"])
def pet_daily_api():
print("\nπ [DEBUG] /pet_daily νΈμΆλ¨")
if "file" not in request.files:
print("β [ERROR] file μμ")
return jsonify({"error": "No file provided"}), 400
try:
file = request.files["file"]
ext = file.filename.split(".")[-1]
temp_path = f"temp_{uuid4().hex}.{ext}"
file.save(temp_path)
pet_q.put({"mode": "daily", "path": temp_path})
print("π [DEBUG] daily worker μ λ¬ μλ£")
result = pet_res_q.get()
print(f"π [DEBUG] daily κ²°κ³Ό: {result}")
os.remove(temp_path)
return jsonify(result)
except Exception as e:
print("\nπ₯π₯π₯ [EXCEPTION in /pet_daily]")
traceback.print_exc()
return jsonify({"error": str(e)}), 500
# ============================================================
# 5) λ°λ €λλ¬Ό μμΈ
# ============================================================
@app.route("/detect", methods=["POST"])
def detect_api():
print("\nπ [DEBUG] /detect νΈμΆλ¨")
if "video" not in request.files:
print("β [ERROR] video μμ")
return jsonify({"error": "No video provided"}), 400
try:
temp_path = f"temp_{uuid4().hex}.mp4"
request.files["video"].save(temp_path)
pet_q.put({"mode": "shorts", "path": temp_path})
print("π [DEBUG] shorts worker μ λ¬ μλ£")
result = pet_res_q.get()
print(f"π [DEBUG] shorts κ²°κ³Ό: {result}")
os.remove(temp_path)
return jsonify(result)
except Exception as e:
print("\nπ₯π₯π₯ [EXCEPTION in /detect]")
traceback.print_exc()
return jsonify({"error": str(e)}), 500
# ============================================================
# Worker μμ
# ============================================================
def start_workers():
from workers.stt_worker import run_stt_worker
from workers.pet_worker import run_pet_worker
print("π₯ STT Worker started.")
Process(target=run_stt_worker, args=(stt_q, stt_res_q)).start()
print("π₯ Pet Worker started.")
Process(target=run_pet_worker, args=(pet_q, pet_res_q)).start()
if __name__ == "__main__":
start_workers()
print("π App Started on port 8000")
app.run(host="0.0.0.0", port=8000)