Skip to content

Commit

Permalink
Merge pull request #72 from boostcampaitech3/feat/69-be
Browse files Browse the repository at this point in the history
[Feat] Add laughter-detection api
  • Loading branch information
sooya233 authored Jun 7, 2022
2 parents 792c98a + ed8da1c commit 9d571b2
Showing 1 changed file with 24 additions and 33 deletions.
57 changes: 24 additions & 33 deletions serving/backend/app_laughter/api/laughter.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,43 +16,13 @@
FILE_DIR = os.path.join(BASE_DIR, 'files/')
ML_DIR = os.path.join(BASE_DIR, 'ml/')

'''
class VideoTimeline(BaseModel):
id: UUID = Field(default_factory=uuid4)
file_name: str
laughter_timeline: Optional[List[Tuple]]
created_at : datetime = Field(default_factory=datetime.now)
@router.post("/timeline-laughter", description="비디오를 업로드하고 laughter detection을 수행합니다.")
async def get_laughter_timeline(file: UploadFile = File(...)):
# download the uploaded video
new_video_timeline = VideoTimeline(file_name=file.filename)
os.makedirs(os.path.join(FILE_DIR, str(new_video_timeline.id)))
server_path = os.path.join(FILE_DIR, str(new_video_timeline.id), ('original' + os.path.splitext(file.filename)[1]))
with open(server_path, "wb") as buffer:
shutil.copyfileobj(file.file, buffer)
# execute laughter detection
wav_path = os.path.join(FILE_DIR, str(new_video_timeline.id), 'for_laughter_detection')
ml_path = os.path.join(BASE_DIR, 'ml')
laughter_timeline = LaughterDetection(server_path, wav_path, ml_path)
new_video_timeline.laughter_timeline = laughter_timeline
# remove folder (laughter detection 서버에서는 일회성이므로)
shutil.rmtree(os.path.join(FILE_DIR, str(new_video_timeline.id)))
# TODO: timeline이 없을 때의 처리 추가 필요!
return new_video_timeline
'''


class Video(BaseModel):
id_laughter: UUID = Field(default_factory=uuid4)
file_name: str
created_at : datetime = Field(default_factory=datetime.now)


@router.post("/upload-video", description="비디오를 업로드합니다.")
async def create_video_file(file: UploadFile = File(...)):
# download the uploaded video
Expand Down Expand Up @@ -88,6 +58,27 @@ async def get_timeline_laughter(info: dict):
shutil.rmtree(os.path.join(FILE_DIR, str(new_video_timeline.id)))
return new_video_timeline

# TODO: /timeline-laughter (laughter detection을 통해 웃음 구간의 timeline 추출하기)

# TODO: app_laughter/files로 영상 저장하는 api / laughter-timeline 뽑는 api 분리
# TODO: /laughter-detection (기존의 /upload-video와 /timeline-laughter 통합)

@router.post("/laughter-detection", description="laughter timeline을 추출하는 전과정을 수행합니다.")
async def laughter_detection(file: UploadFile = File(...)):
# download the uploaded video
new_video = Video(file_name=file.filename)
os.makedirs(os.path.join(FILE_DIR, str(new_video.id_laughter)))
video_path = os.path.join(FILE_DIR, str(new_video.id_laughter), ('original' + os.path.splitext(file.filename)[1]))
with open(video_path, "wb") as buffer:
shutil.copyfileobj(file.file, buffer)

# extract laughter timeline
new_video_timeline = VideoTimeline(id=new_video.id_laughter)

# execute laughter detection
wav_path = os.path.join(FILE_DIR, str(new_video.id_laughter), 'for_laughter_detection')

laughter_timeline = LaughterDetection(video_path, wav_path, ML_DIR)
new_video_timeline.laugh = laughter_timeline

# remove folder (laughter detection 서버에서는 일회성이므로)
shutil.rmtree(os.path.join(FILE_DIR, str(new_video_timeline.id)))
return new_video_timeline

0 comments on commit 9d571b2

Please sign in to comment.