File size: 1,346 Bytes
d866b52
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from typing import *
from pathlib import Path

import cv2
from inference.openvino import OpenVINOInferencer
import numpy as np

inferencer = None
last_model_path: str = None

def predict_openvino(
        image: Union[str, Path, np.ndarray],
        model_path: Union[str, Path],
        device: str,
        threshold: float) -> Dict[str, np.ndarray]:
    global inferencer, last_model_path
    if not isinstance(inferencer, OpenVINOInferencer) or last_model_path != str(model_path):
        inferencer = OpenVINOInferencer(
            path = model_path,
            device=device.upper()
        )
        last_model_path = str(model_path)
    inferencer.metadata["pixel_threshold"] = threshold
    return inferencer(image)

def draw_contour(image, mask, color=(255,0,0), thickness=3):
    contours, _ = cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
    return cv2.drawContours(image.copy(), contours, -1, color, thickness)

def convert_to_heatmap(heatmap):
    heatmap = cv2.applyColorMap((heatmap*255).astype("uint8"), cv2.COLORMAP_HSV)
    return heatmap

def predict_fn(image, threshold, device, model_dir):
    outputs = predict_openvino(image, model_dir, device, threshold)
    out_image = draw_contour(image, outputs["pred_mask"])
    heatmap = convert_to_heatmap(outputs["anomaly_map"])
    return out_image, heatmap