o /f @sddlZddlmZddlmZddlmZddlm Z m Z ddl Z ddl m Z ddlZddlZddlmZmZdd lmZdd lmZddlZGd d d ZdS) N)DeepFace)Image)BytesIO)UnionList) HTTPException) load_datasetRegNet)LogisticRegression)compile_torch_modelc@sheZdZddZdededdfddZdedefd d Zd ededefd d ZdededdfddZ dS)InferenceServicecCs d|_dS)N)model)selfr4/home/martin/ppai_team4/client/services/inference.py__init__s zInferenceService.__init__ video_contentuser_idreturnNc Csz4tt}tjd|ddtd|ddd}||WdWdS1s-wYWdStyI}z tdd |d d}~ww) zT Extract frames from the video content asynchronously using FFmpeg. temp/T)exist_ok /video.mp4wb)filemodeNizError saving video:  status_codedetail) struuiduuid4osmakedirsopenwrite Exceptionr)rrrZvideo_idferrr save_videos  &zInferenceService.save_videocCsd|d}tj|stdddt|}|\}}d}|rA|dkrAtd|d|||\}}|d 7}|rA|dks%d|d S) zk Save 10 random frames from the video content using cv2 and return the path to the frames. rrizVideo not foundrr z /frame%d.jpgr/)r"pathexistsrcv2Z VideoCapturereadZimwrite)rrZ video_pathZvidcapZsuccessimagecountrrr save_frames s        zInferenceService.save_frames frames_pathcCs dS)zO Train a model on the frames and return the path to the model. ztemp/model/fake_id.zipr)rr3rrrr train_model3szInferenceService.train_model model_pathcCsdS)z/ Push the model to the servrer Nr)rr5rrrr push_modelQszInferenceService.push_model) __name__ __module__ __qualname__rbytesrr)r2r4r6rrrrr s  r )numpynpZdeepfacerPILriortypingrrr"fastapirr r.Z preprocessr r Zsklearn.linear_modelr Zconcrete.ml.torch.compiler loggingr rrrrs