-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathviewer.py
More file actions
108 lines (83 loc) · 3.36 KB
/
viewer.py
File metadata and controls
108 lines (83 loc) · 3.36 KB
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import os
import tkinter as tk
from tkinter import ttk
from glob import glob
from PIL import Image, ImageTk
from config import *
def make_img_overlay(x, y):
x = x.convert("RGB")
y = y.convert("RGB")
result = x.copy()
pixels_result = result.load()
pixels_y = y.load()
for i in range(y.height):
for j in range(y.width):
if pixels_y[j, i] != (0, 0, 0):
r, g, b = pixels_result[j, i]
b = min(255, r + 60)
pixels_result[j, i] = (r, g, b)
return result
def on_mouse_wheel(event, canvas):
# Adjust scrolling for different platforms
delta = event.delta if event.delta else -event.num # Windows/Mac vs Linux
canvas.yview_scroll(int(-delta / 120), "units")
def main(test_images_dir=TEST_IMAGES_DIR):
# Find all test images
image_paths = sorted(glob(os.path.join(test_images_dir, '**/*.png'), recursive=True))
if not image_paths:
print("No test images found.")
return
# Create a Tkinter window
root = tk.Tk()
root.title("All Predictions Viewer")
root.geometry("1600x900")
# Create a frame that will contain the canvas and scrollbar
container = ttk.Frame(root)
container.pack(fill='both', expand=True)
# Create canvas and scrollbar
canvas = tk.Canvas(container)
scrollbar = ttk.Scrollbar(container, orient='vertical', command=canvas.yview)
scrollable_frame = ttk.Frame(canvas)
scrollable_frame.bind(
"<Configure>",
lambda e: canvas.configure(
scrollregion=canvas.bbox("all")
)
)
canvas.create_window((0, 0), window=scrollable_frame, anchor="nw")
canvas.configure(yscrollcommand=scrollbar.set)
scrollbar.pack(side="right", fill="y")
canvas.pack(side="left", fill="both", expand=True)
# Enable mouse wheel scrolling
root.bind_all("<MouseWheel>", lambda e: on_mouse_wheel(e, canvas)) # Windows/macOS
root.bind_all("<Button-4>", lambda e: on_mouse_wheel(e, canvas)) # Linux scroll up
root.bind_all("<Button-5>", lambda e: on_mouse_wheel(e, canvas)) # Linux scroll down
# Load all images and masks, create overlays, and display them
images_per_row = 3
thumbnail_size = (500, 500) # Adjust as needed
photo_images = [] # Keep references to PhotoImage objects to prevent garbage collection
for idx, img_path in enumerate(image_paths):
test_id = img_path.split('/')[-1].split('.')[0].split('_')[-1]
mask_path = f"{PREDICTED_GROUNDTRUTH_DIR}/test_{test_id}.png"
try:
img = Image.open(img_path)
mask = Image.open(mask_path)
except FileNotFoundError:
continue
overlay = make_img_overlay(img, mask)
overlay = overlay.resize(thumbnail_size, resample=Image.Resampling.LANCZOS)
photo = ImageTk.PhotoImage(overlay)
photo_images.append(photo)
# Determine grid position
row = idx // images_per_row
col = idx % images_per_row
# Create a frame for each image with a label and place it in the grid
frame = ttk.Frame(scrollable_frame, borderwidth=5, relief="groove")
frame.grid(row=row, column=col, padx=5, pady=5)
lbl = ttk.Label(frame, image=photo)
lbl.pack()
title_lbl = ttk.Label(frame, text=f"Test {test_id}")
title_lbl.pack()
root.mainloop()
if __name__ == '__main__':
main()