11import numpy as np
22import pyqtgraph as pg
3+ from PyQt5 import QtCore
34from matplotlib import cm
45from PyQt5 .QtWidgets import (
56 QDesktopWidget ,
@@ -53,21 +54,21 @@ def draw_user_bbox(self):
5354 def plot_bbox_roi (self ):
5455 # Plot bbox on GUI
5556 for i , bbox in enumerate (self .bbox ):
56- x1 , x2 , y1 , y2 = bbox
57+ y1 , y2 , x1 , x2 = bbox
5758 dy , dx = y2 - y1 , x2 - x1
58- xrange = np .arange (y1 + self .gui .sx [i ], y2 + self .gui .sx [i ]).astype (
59+ xrange = np .arange (x1 + self .gui .sx [i ], x2 + self .gui .sx [i ]).astype (
5960 np .int32
6061 )
61- yrange = np .arange (x1 + self .gui .sy [i ], x2 + self .gui .sy [i ]).astype (
62+ yrange = np .arange (y1 + self .gui .sy [i ], y2 + self .gui .sy [i ]).astype (
6263 np .int32
6364 )
64- x1 , y1 = yrange [0 ], xrange [0 ]
65+ y1 , x1 = yrange [0 ], xrange [0 ]
6566 self .gui .add_ROI (
6667 roitype = 4 + 1 ,
6768 roistr = "bbox_{}" .format (i ),
6869 moveable = False ,
6970 resizable = False ,
70- pos = (x1 , y1 , dx , dy ),
71+ pos = (y1 , x1 , dy , dx ),
7172 ivid = i ,
7273 yrange = yrange ,
7374 xrange = xrange ,
@@ -82,6 +83,10 @@ def adjust_bbox(self):
8283 dy , dx = y2 - y1 , x2 - x1
8384 if dx != dy : # If bbox is not square then add padding to image
8485 self .add_padding = True
86+ if dy != 256 or dx != 256 : # If bbox is not 256, 256 then resize image
87+ self .resize = True
88+ self .bbox [i ] = x1 , x2 , y1 , y2
89+ """
8590 larger_dim = max(dx, dy)
8691 if larger_dim < self.img_xy[0] or larger_dim < self.img_xy[1]:
8792 # If the largest dimension of the image is smaller than the minimum required dimension,
@@ -96,6 +101,7 @@ def adjust_bbox(self):
96101 y_dims=(y1, y2),
97102 )
98103 self.bbox[i] = x1, x2, y1, y2
104+ """
99105 print ("BBOX after adjustment:" , self .bbox )
100106 print ("RESIZE:" , self .resize )
101107 print ("PADDING:" , self .add_padding )
@@ -105,7 +111,7 @@ class ROI_popup(QDialog):
105111 def __init__ (self , frame , video_id , gui , pose , last_video ):
106112 super ().__init__ ()
107113 window_max_size = QDesktopWidget ().screenGeometry (- 1 )
108- fraction = 0.3
114+ fraction = 0.5
109115 aspect_ratio = 1.5
110116 self .resize (
111117 int (np .floor (window_max_size .width () * fraction )),
@@ -121,11 +127,19 @@ def __init__(self, frame, video_id, gui, pose, last_video):
121127 self .verticalLayout = QVBoxLayout (self )
122128 self .win = pg .GraphicsLayoutWidget ()
123129 self .win .setObjectName ("Dialog " + str (video_id + 1 ))
124- ROI_win = self .win .addViewBox (invertY = True )
130+ # fix image in ROI window
131+ ROI_win = self .win .addViewBox (invertY = True , lockAspect = True , enableMouse = False )
125132 self .img = pg .ImageItem (self .frame )
126133 ROI_win .addItem (self .img )
134+ shape_y , shape_x = self .frame .shape [0 ], self .frame .shape [1 ]
135+ ROI_win .setRange (xRange = [0 , shape_x ], yRange = [0 , shape_y ])
127136 self .roi = pg .RectROI (
128- [0 , 0 ], [100 , 100 ], pen = pg .mkPen ("r" , width = 2 ), movable = True , resizable = True
137+ [0 , 0 ],
138+ [int (np .floor (0.6 * shape_x )), int (np .floor (0.5 * shape_y ))],
139+ pen = pg .mkPen ("r" , width = 2 ),
140+ movable = True ,
141+ resizable = True ,
142+ maxBounds = QtCore .QRectF (0 , 0 , shape_x , shape_y ),
129143 )
130144 ROI_win .addItem (self .roi )
131145 self .win .show ()
@@ -162,7 +176,10 @@ def __init__(self, frame, video_id, gui, pose, last_video):
162176 self .exec_ ()
163177
164178 def get_coordinates (self ):
179+ print ("self.frame shape:" , self .frame .shape )
180+ print ("self.img shape:" , self .img .shape )
165181 roi_tuple , _ = self .roi .getArraySlice (self .frame , self .img , returnSlice = False )
182+ print ("roi_tuple:" , roi_tuple )
166183 (x1 , x2 ), (y1 , y2 ) = roi_tuple [0 ], roi_tuple [1 ]
167184 return (x1 , x2 ), (y1 , y2 )
168185
@@ -172,8 +189,8 @@ def skip_exec(self):
172189 self .close ()
173190
174191 def next_exec (self ):
175- (x1 , x2 ), (y1 , y2 ) = self .get_coordinates ()
176- self .pose .bbox .append ([x1 , x2 , y1 , y2 ])
192+ (y1 , y2 ), (x1 , x2 ) = self .get_coordinates ()
193+ self .pose .bbox .append ([y1 , y2 , x1 , x2 ])
177194 self .resize = False
178195 self .add_padding = False
179196 self .pose .adjust_bbox ()
@@ -185,8 +202,8 @@ def cancel_exec(self):
185202
186203 def done_exec (self ):
187204 # User finished drawing ROI
188- (x1 , x2 ), (y1 , y2 ) = self .get_coordinates ()
189- self .pose .bbox .append ([x1 , x2 , y1 , y2 ])
205+ (y1 , y2 ), (x1 , x2 ) = self .get_coordinates ()
206+ self .pose .bbox .append ([y1 , y2 , x1 , x2 ])
190207 self .resize = False
191208 self .add_padding = False
192209 self .pose .plot_bbox_roi ()
0 commit comments