Spaces:
Sleeping
Sleeping
Rivalcoder
commited on
Commit
·
9775f6e
1
Parent(s):
a231c85
Add New Version
Browse files
app.py
CHANGED
|
@@ -10,7 +10,6 @@ from typing import Dict, Any
|
|
| 10 |
from fastapi import FastAPI, HTTPException, File, UploadFile
|
| 11 |
from pydantic import BaseModel
|
| 12 |
import gradio as gr
|
| 13 |
-
import shutil
|
| 14 |
import tempfile
|
| 15 |
|
| 16 |
app = FastAPI()
|
|
@@ -40,7 +39,7 @@ def load_emotion_model(model_path, device='cuda' if torch.cuda.is_available() el
|
|
| 40 |
return model
|
| 41 |
|
| 42 |
# Process the uploaded video (either MP4 or WebM)
|
| 43 |
-
def process_video(video_file: UploadFile) -> Dict[str, Any]:
|
| 44 |
global largest_face_detections
|
| 45 |
largest_face_detections = [] # Reset detections for new video
|
| 46 |
|
|
@@ -64,11 +63,13 @@ def process_video(video_file: UploadFile) -> Dict[str, Any]:
|
|
| 64 |
|
| 65 |
emotions = ['Angry', 'Disgust', 'Fear', 'Happy', 'Sad', 'Surprise', 'Neutral']
|
| 66 |
|
| 67 |
-
# Save the uploaded video file to a temporary directory
|
| 68 |
temp_dir = tempfile.mkdtemp()
|
| 69 |
video_path = os.path.join(temp_dir, "uploaded_video")
|
| 70 |
-
|
| 71 |
-
|
|
|
|
|
|
|
| 72 |
|
| 73 |
cap = cv2.VideoCapture(video_path)
|
| 74 |
if not cap.isOpened():
|
|
@@ -166,7 +167,7 @@ class VideoRequest(BaseModel):
|
|
| 166 |
@app.post("/process_video/")
|
| 167 |
async def process_video_request(file: UploadFile = File(...)):
|
| 168 |
try:
|
| 169 |
-
results = process_video(file)
|
| 170 |
return results
|
| 171 |
except Exception as e:
|
| 172 |
raise HTTPException(status_code=500, detail=str(e))
|
|
|
|
| 10 |
from fastapi import FastAPI, HTTPException, File, UploadFile
|
| 11 |
from pydantic import BaseModel
|
| 12 |
import gradio as gr
|
|
|
|
| 13 |
import tempfile
|
| 14 |
|
| 15 |
app = FastAPI()
|
|
|
|
| 39 |
return model
|
| 40 |
|
| 41 |
# Process the uploaded video (either MP4 or WebM)
|
| 42 |
+
async def process_video(video_file: UploadFile) -> Dict[str, Any]:
|
| 43 |
global largest_face_detections
|
| 44 |
largest_face_detections = [] # Reset detections for new video
|
| 45 |
|
|
|
|
| 63 |
|
| 64 |
emotions = ['Angry', 'Disgust', 'Fear', 'Happy', 'Sad', 'Surprise', 'Neutral']
|
| 65 |
|
| 66 |
+
# Save the uploaded video file to a temporary directory without using shutil
|
| 67 |
temp_dir = tempfile.mkdtemp()
|
| 68 |
video_path = os.path.join(temp_dir, "uploaded_video")
|
| 69 |
+
|
| 70 |
+
# Open the video file stream and save it as a local file
|
| 71 |
+
with open(video_path, "wb") as f:
|
| 72 |
+
f.write(await video_file.read())
|
| 73 |
|
| 74 |
cap = cv2.VideoCapture(video_path)
|
| 75 |
if not cap.isOpened():
|
|
|
|
| 167 |
@app.post("/process_video/")
|
| 168 |
async def process_video_request(file: UploadFile = File(...)):
|
| 169 |
try:
|
| 170 |
+
results = await process_video(file)
|
| 171 |
return results
|
| 172 |
except Exception as e:
|
| 173 |
raise HTTPException(status_code=500, detail=str(e))
|