-
Notifications
You must be signed in to change notification settings - Fork 69
/
Copy pathpredict.py
56 lines (49 loc) · 1.63 KB
/
predict.py
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
import os
import tempfile
import shutil
from typing import List
from cog import BasePredictor, Path, Input, BaseModel
from openpsg.utils.utils import show_result
from mmdet.apis import init_detector, inference_detector
from mmcv import Config
import mmcv
class ModelOutput(BaseModel):
image: Path
class Predictor(BasePredictor):
def setup(self):
model_ckt = "epoch_60.pth"
cfg = Config.fromfile("configs/psgtr/psgtr_r50_psg_inference.py")
self.model = init_detector(cfg, model_ckt, device="cpu")
def predict(
self,
image: Path = Input(
description="Input image.",
),
num_rel: int = Input(
description="Number of Relations. Each relation will generate a scene graph",
default=5,
ge=1,
le=20,
),
) -> List[ModelOutput]:
input_image = mmcv.imread(str(image))
result = inference_detector(self.model, input_image)
out_path = Path(tempfile.mkdtemp()) / "output.png"
out_dir = "temp"
show_result(
str(image),
result,
is_one_stage=True,
num_rel=num_rel,
out_dir=out_dir,
out_file=str(out_path),
)
output = []
output.append(ModelOutput(image=out_path))
for i, img_path in enumerate(os.listdir(out_dir)):
img = mmcv.imread(os.path.join(out_dir, img_path))
out_path = Path(tempfile.mkdtemp()) / f"output_{i}.png"
mmcv.imwrite(img, str(out_path))
output.append(ModelOutput(image=out_path))
shutil.rmtree(out_dir)
return output