File size: 2,479 Bytes
f2f112a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import numpy as np
import cv2
from PIL import Image
from collections import OrderedDict
import os
from pathlib import Path

def crop_image(ocvimg, bbox_list):
    H, W = ocvimg.shape[:2]
    res = OrderedDict()

    for idx, (x0, y0, x1, y1) in enumerate(bbox_list):
        # clamp to image bounds
        x0 = max(0, min(W, int(x0)))
        y0 = max(0, min(H, int(y0)))
        x1 = max(0, min(W, int(x1)))
        y1 = max(0, min(H, int(y1)))

        # skip invalid/empty crops
        if x1 <= x0 or y1 <= y0:
            # print(f"Invalid bbox after clamp: {(x0,y0,x1,y1)}")
            continue

        crop_bgr = ocvimg[y0:y1, x0:x1]
        crop_rgb = cv2.cvtColor(crop_bgr, cv2.COLOR_BGR2RGB)
        res[idx] = Image.fromarray(crop_rgb)

    return res

def draw_bboxes(ocvimg, bbox_list, color=(0, 255, 0), thickness=4, clamp=True):
    img = ocvimg.copy()
    H, W = img.shape[:2]

    for bbox in bbox_list:
        x0, y0, x1, y1 = [int(round(v)) for v in bbox]

        if clamp:
            x0 = max(0, min(W - 1, x0))
            y0 = max(0, min(H - 1, y0))
            x1 = max(0, min(W - 1, x1))
            y1 = max(0, min(H - 1, y1))

        # skip invalid boxes
        if x1 <= x0 or y1 <= y0:
            continue

        cv2.rectangle(img, (x0, y0), (x1, y1), color, thickness)

    return img


def save_image(img, save_path):
    os.makedirs(os.path.dirname(save_path) or ".", exist_ok=True)
    ok = cv2.imwrite(save_path, img)
    if not ok:
        raise IOError(f"cv2.imwrite failed for: {save_path}")
    return save_path

def find_same_class(predict_res, score, visited, index, List_class, List_score, threshold):
    target_class = List_class[index]
    for i in range(len(score)):
        if List_class[i] == target_class:
            if score[i] > threshold:
                predict_res[i]["score"] = score[i]
                visited[i] = 1
                predict_res[i]["category_id"] = 1
            else:    
                # predict_res[i]["score"] = float(List_score[i])*float(score[i])
                if List_score[index] > 0.8 and List_score[i] > 0.8:
                    predict_res[i]["score"] = float(score[i])
                    visited[i] = 1
                    predict_res[i]["category_id"] = 1

def open_image_follow_symlink(path: str):
    p = Path(path)

    real = p.resolve(strict=True)

    if not real.is_file():
        raise FileNotFoundError(f"Resolved path is not a file: {real}")

    return Image.open(real)