-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidationROIv2.py
More file actions
199 lines (152 loc) · 6.17 KB
/
validationROIv2.py
File metadata and controls
199 lines (152 loc) · 6.17 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
import tkinter as tk
from tkinter import ttk
from PIL import Image, ImageTk
import cv2
import decord
import numpy as np
class ROIValidator:
def __init__(self, video_path, rois, significant_frame):
self.root = tk.Tk()
self.root.title("ROI Validation Tool")
self.root.geometry("1200x800")
self.video_path = video_path
self.rois = rois
self.significant_frame = significant_frame
self.current_roi_idx = 0
self.create_widgets()
self.update_display()
def create_widgets(self):
# Main frame
main_frame = ttk.Frame(self.root)
main_frame.pack(fill=tk.BOTH, expand=True, padx=20, pady=20)
# Display area
self.image_frame = ttk.Frame(main_frame)
self.image_frame.pack(fill=tk.BOTH, expand=True)
# Labels for images
self.original_label = ttk.Label(self.image_frame)
self.original_label.pack(side=tk.LEFT, expand=True)
self.roi_label = ttk.Label(self.image_frame)
self.roi_label.pack(side=tk.RIGHT, expand=True)
# Controls
control_frame = ttk.Frame(main_frame)
control_frame.pack(fill=tk.X, pady=20)
self.btn_prev = ttk.Button(
control_frame,
text="← Previous",
command=self.prev_roi,
style='TButton',
state=tk.DISABLED
)
self.btn_prev.pack(side=tk.LEFT, padx=10)
self.btn_accept = ttk.Button(
control_frame,
text="Accept ROI ✓",
command=self.accept_roi,
style='Success.TButton'
)
self.btn_accept.pack(side=tk.LEFT, padx=10)
self.btn_reject = ttk.Button(
control_frame,
text="Reject ✗",
command=self.reject_roi,
style='Danger.TButton'
)
self.btn_reject.pack(side=tk.LEFT, padx=10)
self.btn_next = ttk.Button(
control_frame,
text="Next ROI →",
command=self.next_roi,
style='TButton'
)
self.btn_next.pack(side=tk.RIGHT, padx=10)
# Style configuration
style = ttk.Style()
style.configure('TButton', font=('Helvetica', 16), padding=15)
style.configure('Success.TButton', foreground='green', font=('Helvetica', 16, 'bold'))
style.configure('Danger.TButton', foreground='red', font=('Helvetica', 16, 'bold'))
def load_images(self):
def reduce_roi_centered(x, y, w, h, reduction_factor=0.8):
# Centro da ROI
cx = x + w // 2
cy = y + h // 2
# Nova largura e altura reduzidas
new_w = int(w * reduction_factor)
new_h = int(h * reduction_factor)
# Novo canto superior esquerdo para manter centro fixo
new_x = cx - new_w // 2
new_y = cy - new_h // 2
return new_x, new_y, new_w, new_h
vr = decord.VideoReader(self.video_path)
frame = vr[self.significant_frame].asnumpy()
x, y, w, h = self.current_roi
x, y, w, h = reduce_roi_centered(x, y, w, h, reduction_factor=0.8) # reduz 20%
# Original image with ROI highlighted
frame_with_roi = cv2.rectangle(
frame.copy(),
(x, y),
(x + w, y + h),
(0, 255, 0),
3
)
# Cropped ROI image
roi_image = frame[y:y+h, x:x+w]
return frame_with_roi, roi_image
def update_display(self):
original_img, roi_img = self.load_images()
# Process images for display
original_img = cv2.resize(original_img, (800, 600))
roi_img = cv2.resize(roi_img, (600, 600)) if roi_img.size > 0 else np.zeros((600,600,3), dtype=np.uint8)
# Convert to Tkinter format
self.original_photo = ImageTk.PhotoImage(Image.fromarray(original_img))
self.roi_photo = ImageTk.PhotoImage(Image.fromarray(roi_img))
# Update labels
self.original_label.config(image=self.original_photo)
self.roi_label.config(image=self.roi_photo)
# Update button states
self.btn_prev["state"] = tk.NORMAL if self.current_roi_idx > 0 else tk.DISABLED
self.btn_next["state"] = tk.NORMAL if self.current_roi_idx < len(self.rois)-1 else tk.DISABLED
@property
def current_roi(self):
return self.rois[self.current_roi_idx]
def prev_roi(self):
if self.current_roi_idx > 0:
self.current_roi_idx -= 1
self.update_display()
def next_roi(self):
if self.current_roi_idx < len(self.rois)-1:
self.current_roi_idx += 1
self.update_display()
def accept_roi(self):
self.selected_roi = self.current_roi
self.root.destroy()
def reject_roi(self):
self.rois.pop(self.current_roi_idx)
if self.current_roi_idx >= len(self.rois):
self.current_roi_idx = len(self.rois)-1
self.update_display()
def run(self):
self.root.mainloop()
return getattr(self, 'selected_roi', None)
# Modified main function
def validateROI(video_name, video_path, rois, significant_frame, scale_factor):
"""
Validate ROIs considering scaled coordinates
Parameters:
scale_factor (float): Fator de escalonamento usado no pré-processamento
(ex: 0.5 para redução de 50%)
"""
# Converter ROIs para coordenadas originais
original_rois = [(int(x/scale_factor),
int(y/scale_factor),
int(w/scale_factor),
int(h/scale_factor))
for (x,y,w,h) in rois]
# Validar com resolução original
validator = ROIValidator(video_path, original_rois, significant_frame)
selected_roi = validator.run()
if selected_roi:
with open(f'SaveRois/{video_name}_validated.txt', 'w') as f:
f.write(str(selected_roi))
return selected_roi
print("No valid ROI selected.")
return None