File size: 1,046 Bytes
df0d128
3979c37
1e9ebb3
3979c37
 
df0d128
 
 
 
4567417
1e9ebb3
 
 
df0d128
 
 
 
 
4567417
df0d128
 
 
1e9ebb3
 
 
df0d128
 
 
1e9ebb3
 
 
 
 
 
4567417
be8f1b6
3979c37
 
 
df0d128
 
 
 
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
import threading
import cv2
import os


buffer = []


def worker():
    global buffer
    try:
        while True:
            cv2.waitKey(50)
            if len(buffer) > 0:
                task = buffer.pop(0)
                if task is None:
                    cv2.destroyAllWindows()
                else:
                    flag, img, title = task
                    cv2.imshow(flag, img)
                    cv2.setWindowTitle(flag, title)
                    cv2.setWindowProperty(flag, cv2.WND_PROP_TOPMOST, 1)
    except Exception as e:
        print('Failed to open preview window. You are not using a local device with GUI support.')
        print(e)
    pass


def save_image(path, img):
    os.makedirs(os.path.dirname(path), exist_ok=True)
    cv2.imwrite(path, img[..., ::-1].copy())
    print(f'Image saved: {path}')


def show_preview(flag, img, title='preview'):
    buffer.append((flag, img[..., ::-1].copy(), title))


def close_all_preview():
    buffer.append(None)


threading.Thread(target=worker, daemon=True).start()