diff --git a/app/client.py b/app/client.py index 66fe5add..4dcbb293 100644 --- a/app/client.py +++ b/app/client.py @@ -60,6 +60,21 @@ def inference(model_name: str, media_filepath: str) -> str: st.session_state["inferenced"] = output_filepath +def get_inferenced_media(media_filepath: str) -> str: + log.info(f"inference(media_filepath={media_filepath!r})") + r = httpx.get( + BASE_URL + "/inference", + params={"media_filepath": media_filepath} + ) + + log.info(f"inference: r.json() = {r.json()!r}") + + if r.status_code == 200: + output_filepath = r.json() + log.info(f"inference: output_filepath = {output_filepath!r}") + st.session_state["inferenced"] = output_filepath + + def read_image(image_path: str) -> bytes: log.info(f"read_image(image_path={image_path!r})") with open(image_path, "rb") as f: @@ -97,8 +112,8 @@ def main(): st.button( "inference", - on_click=inference, - kwargs={"model_name": model_name, "media_filepath": video_path}, + on_click=get_inferenced_media, + kwargs={"media_filepath": video_path}, ) if st.session_state["inferenced"] is not None: diff --git a/app/config.yml b/app/config.yml index 5afcb75d..54ed93e1 100644 --- a/app/config.yml +++ b/app/config.yml @@ -20,6 +20,7 @@ model: # config: /opt/ml/final-project-level3-cv-17/mmyolo/configs/_emergency_/yolox/yolox_l_8xb8-300e_coco.py config: /opt/ml/final-project-level3-cv-17/mmyolo/work_dirs/yolox_l_8xb8-300e_coco/yolox_l_8xb8-300e_coco.py pth: /opt/ml/final-project-level3-cv-17/mmyolo/work_dirs/yolox_l_8xb8-300e_coco/epoch_300.pth +inferenced: _inferenced image: directory: /opt/ml/input/image/ format: diff --git a/app/server.py b/app/server.py index 3877fae7..4cf128f7 100644 --- a/app/server.py +++ b/app/server.py @@ -57,24 +57,29 @@ def get_model_by_name(model_name: str) -> ModelInfo: return model raise HTTPException(status_code=404, detail="모델을 찾을 수 없습니다") +def is_inferenced_media(filepath: Path) -> bool: + config = read_config() + return config['inferenced'] in filepath.stem -def _get_filepaths(key: str) -> List[FilePath]: +def _get_filepaths(key: str, inferenced: bool) -> List[FilePath]: config = read_config() directory = Path(config[key]["directory"]) formats = config[key]["format"] - return [_path for format in formats for _path in directory.glob(f"**/*{format}")] - + filepaths = [_path for format in formats for _path in directory.glob(f"**/*{format}")] + if inferenced: + return [_path for _path in filepaths if is_inferenced_media(_path)] + return [_path for _path in filepaths if not is_inferenced_media(_path)] @app.get("/images") def get_images() -> List[FilePath]: log.info("GET /images") - return _get_filepaths("image") + return _get_filepaths("image", inferenced=False) @app.get("/videos") def get_videos() -> List[FilePath]: log.info("GET /images") - return _get_filepaths("video") + return _get_filepaths("video", inferenced=False) def _is_image_file(filepath: FilePath) -> bool: @@ -177,6 +182,14 @@ async def inference(body: InferenceBody) -> FilePath: return _inference_video(model, body.media_filepath) +@app.get("/inference") +def get_inferenced_media(filepath: FilePath) -> FilePath: + log.info("GET /inference") + config = read_config() + inferenced_filename = filepath.stem + config['inferenced'] + filepath.suffix + return filepath.with_name(inferenced_filename) + + if __name__ == "__main__": config = read_config()