def anotate(img, out_path, wnd_height=800): img_height, img_width = img.shape[:2] wnd_width = int(wnd_height * img_width / img_height) frame = np.zeros((wnd_height + 200, wnd_width, 3), np.uint8) roi_size = [img_height // 10 * 5] roi_shiftx = [0] roi_shifty = [0] x1 = [0] x2 = [img_width // 2] y1 = [0] y2 = [img_height // 2] WINDOW_NAME = 'Roi Anotater' cvui.init(WINDOW_NAME) anchor = cvui.Point() while (True): y = cvui.mouse().y x = cvui.mouse().x if (x > 0 and x < wnd_width and y > 0 and y < wnd_height): if cvui.mouse(cvui.DOWN): anchor.y = y anchor.x = x if cvui.mouse(cvui.IS_DOWN): if x > anchor.x and y > anchor.y: x1[0] = anchor.x * img_width // wnd_width x2[0] = x * img_width // wnd_width y1[0] = anchor.y * img_height // wnd_height y2[0] = y * img_height // wnd_height frame[:] = (49, 52, 49) img_tmp = img.copy() img_tmp = cv2.rectangle(img_tmp, (x1[0], y1[0]), (x2[0], y2[0]), (0, 0, 255), 1) cvui.image(frame, 0, 0, cv2.resize(img_tmp, (wnd_width, wnd_height))) param(frame, 50, wnd_height + 10, wnd_width - 400, x1, 0, x2[0], 'x1', 1) param(frame, 50, wnd_height + 50, wnd_width - 400, x2, x1[0], img_width, 'x2', 1) param(frame, 50, wnd_height + 90, wnd_width - 400, y1, 0, y2[0], 'y1', 1) param(frame, 50, wnd_height + 130, wnd_width - 400, y2, y1[0], img_height, 'y2', 1) cvui.update() cv2.imshow(WINDOW_NAME, frame) if cv2.waitKey(20) == 27: np.savez(out_path, x1=x1[0] / img_width, x2=x2[0] / img_width, y1=y1[0] / img_height, y2=y2[0] / img_height) break
def reset_target_selection(): target_pos = np.array((cvui.mouse().x / p.DISPLAY_RESCALE_SPOTTER, cvui.mouse().y / p.DISPLAY_RESCALE_SPOTTER)) feature_pos = np.array((cvui.mouse().x / p.DISPLAY_RESCALE_SPOTTER, cvui.mouse().y / p.DISPLAY_RESCALE_SPOTTER)) feature_delta = feature_pos - target_pos return target_pos, feature_delta
def wellOrganisation(self, controller, circular, rectangular, roi, other, multipleROIs): if multipleROIs: controller.show_frame("NbRegionsOfInterest") else: if rectangular: self.shape = 'rectangular' self.configFile["wellsAreRectangles"] = 1 controller.show_frame("CircularOrRectangularWells") else: if circular and self.organism != 'drosoorrodent': # should remove the self.organism != 'drosoorrodent' at some point self.shape = 'circular' controller.show_frame("CircularOrRectangularWells") else: self.shape = 'other' if roi: cap = cv2.VideoCapture(self.videoToCreateConfigFileFor) cap.set(1, 10) ret, frame = cap.read() frame2 = frame.copy() [ frame2, getRealValueCoefX, getRealValueCoefY, horizontal, vertical ] = resizeImageTooLarge(frame2, True, 0.85) WINDOW_NAME = "Click on the top left of the region of interest" cvui.init(WINDOW_NAME) cv2.moveWindow(WINDOW_NAME, 0, 0) cvui.imshow(WINDOW_NAME, frame2) while not (cvui.mouse(cvui.CLICK)): cursor = cvui.mouse() if cv2.waitKey(20) == 27: break self.configFile["oneWellManuallyChosenTopLeft"] = [ int(getRealValueCoefX * cursor.x), int(getRealValueCoefY * cursor.y) ] cv2.destroyAllWindows() WINDOW_NAME = "Click on the bottom right of the region of interest" cvui.init(WINDOW_NAME) cv2.moveWindow(WINDOW_NAME, 0, 0) cvui.imshow(WINDOW_NAME, frame2) while not (cvui.mouse(cvui.CLICK)): cursor = cvui.mouse() if cv2.waitKey(20) == 27: break self.configFile["oneWellManuallyChosenBottomRight"] = [ int(getRealValueCoefX * cursor.x), int(getRealValueCoefY * cursor.y) ] cv2.destroyAllWindows() self.configFile["nbWells"] = 1 chooseBeginningAndEndOfVideo(self, controller) else: self.configFile["noWellDetection"] = 1 self.configFile["nbWells"] = 1 chooseBeginningAndEndOfVideo(self, controller)
def mouse_check(self): self.mouse_y = cvui.mouse().y self.mouse_x = cvui.mouse().x if self.mouse_in_range(): if cvui.mouse(cvui.DOWN): self.mouse_down() else: self.mouse_move()
def main(camera_toml_path, enable_distortion_correction, scale_val = 0.65): camera_config = get_config(camera_toml_path) camera = Camera(camera_config) print(camera) scaling = partial(scaling_int, scale=scale_val) if camera_config.roi_size != 4: sys.exit('This script is only supported on "camera_config.roi_size == 4" ') if camera_config.auto_exposure != "roi": sys.exit('This script is only supported on "camera_config.auto_exposure == roi" ') image_width = camera.image_width image_height = camera.image_height roi = cvui.Rect(0, 0, 0, 0) WINDOW_NAME = "Capture" cvui.init(WINDOW_NAME) click_pos_x = image_width // 2 click_pos_y = image_height // 2 while True: key = cv2.waitKey(10) frame = np.zeros((scaling(image_height), scaling(image_width), 3), np.uint8) frame[:] = (49, 52, 49) status = camera.update() if status: # WARNING:If distortion correction is enabled, the rectangle on windows doesn't indicate actual RoI area for auto exposure. see3cam_rgb_image = camera.remap_image if enable_distortion_correction else camera.image scaled_width = scaling(image_width) scaled_height = scaling(image_height) see3cam_rgb_image_resized = cv2.resize(see3cam_rgb_image, (scaled_width, scaled_height)) frame[:scaled_height, :scaled_width, :] = see3cam_rgb_image_resized window_w = image_width // 2 window_h = image_height // 2 if cvui.mouse(cvui.DOWN): click_pos_x = int(cvui.mouse().x / scale_val) click_pos_y = int(cvui.mouse().y / scale_val) camera.set_roi_properties(click_pos_x, click_pos_y, win_size=4) roi = cvui.Rect(scaling(click_pos_x - image_width // 4), scaling(click_pos_y - image_height // 4), scaling(window_w), scaling(window_h)) # Ensure ROI is within bounds roi.x = 0 if roi.x < 0 else roi.x roi.y = 0 if roi.y < 0 else roi.y roi.width = roi.width + scaled_width - (roi.x + roi.width) if roi.x + roi.width > scaled_width else roi.width roi.height = roi.height + scaled_height - (roi.y + roi.height) if roi.y + roi.height > scaled_height else roi.height cvui.rect(frame, roi.x, roi.y, roi.width, roi.height, 0xFF0000) if key == 27 or key == ord("q"): break cvui.update() cvui.imshow(WINDOW_NAME, frame) cv2.destroyAllWindows()
def findTailTipByUserInput(frame, frameNumber, videoPath, hyperparameters): # global ix # ix = -1 # img = np.zeros((512,512,3), np.uint8) # cv2.namedWindow('Click on tail tip') # cv2.setMouseCallback('Click on tail tip',getXYCoordinates) # print("ix:", ix) # while(ix == -1): # cv2.imshow('Click on tail tip',frame) # k = cv2.waitKey(20) & 0xFF # if k == 27: # break # elif k == ord('a'): # print("yeah:",ix,iy) # cv2.destroyAllWindows() # return [ix,iy] WINDOW_NAME = "Click on tail tip" cvui.init(WINDOW_NAME) cv2.moveWindow(WINDOW_NAME, 0, 0) font = cv2.FONT_HERSHEY_SIMPLEX frame = cv2.rectangle(frame, (0, 0), (250, 29), (255, 255, 255), -1) cv2.putText(frame, 'Click any key if the tail is', (1, 10), font, 0.5, (0, 150, 0), 1, cv2.LINE_AA) cv2.putText(frame, 'not straight on this image', (1, 22), font, 0.5, (0, 150, 0), 1, cv2.LINE_AA) cvui.imshow(WINDOW_NAME, frame) plus = 1 while not (cvui.mouse(WINDOW_NAME, cvui.CLICK)): cursor = cvui.mouse(WINDOW_NAME) if cv2.waitKey(20) != -1: [frame, thresh1] = headEmbededFrame(videoPath, frameNumber + plus, hyperparameters) frame = cv2.rectangle(frame, (0, 0), (250, 29), (255, 255, 255), -1) cv2.putText(frame, 'Click any key if the tail is', (1, 10), font, 0.5, (0, 150, 0), 1, cv2.LINE_AA) cv2.putText(frame, 'not straight on this image', (1, 22), font, 0.5, (0, 150, 0), 1, cv2.LINE_AA) cvui.imshow(WINDOW_NAME, frame) plus = plus + 1 # cv2.destroyAllWindows() cv2.destroyWindow(WINDOW_NAME) return [cursor.x, cursor.y]
def begin(self, frame): mouseInsideTitleArea = cvui.mouse().inside(cvui.Rect(self.__x, self.__y, self.__width, 20)) self.__height = 20 if self.__minimized else self.__heightNotMinimized if self.__isMoving == False and cvui.mouse(cvui.DOWN) and mouseInsideTitleArea: self.__deltaX = cvui.mouse().x - self.__x self.__deltaY = cvui.mouse().y - self.__y self.__isMoving = True elif self.__isMoving and cvui.mouse(cvui.IS_DOWN): self.__x = cvui.mouse().x - self.__deltaX self.__y = cvui.mouse().y - self.__deltaY else: frameRows,frameCols,frameChannels = frame.shape self.__isMoving = False self.__x = max(0, self.__x) self.__y = max(0, self.__y) self.__x = min(frameCols - self.__width, self.__x) self.__y = min(frameRows - 20, self.__y) cvui.window(frame, self.__x, self.__y, self.__width, self.__height, self.__title) if self.__minimizable and cvui.button(frame, self.__x + self.__width - 20, self.__y + 1, 18, 18, '+' if self.__minimized else '-'): self.__minimized = not self.__minimized cvui.beginRow(frame, self.__x + 10, self.__y + 30, self.__width - 20, self.__height - 20) cvui.beginColumn(self.__width - 10, self.__height - 20)
def begin(self, frame): mouseInsideTitleArea = cvui.mouse().inside(cvui.Rect(self.__x, self.__y, self.__width, 20)) self.__height = 20 if self.__minimized else self.__heightNotMinimized if self.__isMoving == False and cvui.mouse(cvui.DOWN) and mouseInsideTitleArea: self.__deltaX = cvui.mouse().x - self.__x self.__deltaY = cvui.mouse().y - self.__y self.__isMoving = True elif self.__isMoving and cvui.mouse(cvui.IS_DOWN): self.__x = cvui.mouse().x - self.__deltaX self.__y = cvui.mouse().y - self.__deltaY else: frameRows,frameCols,frameChannels = frame.shape self.__isMoving = False self.__x = max(0, self.__x) self.__y = max(0, self.__y) self.__x = min(frameCols - self.__width, self.__x) self.__y = min(frameRows - 20, self.__y) cvui.window(frame, self.__x, self.__y, self.__width, self.__height, self.__title) if self.__minimizable and cvui.button(frame, self.__x + self.__width - 20, self.__y + 1, 18, 18, '+' if self.__minimized else '-'): self.__minimized = not self.__minimized cvui.beginRow(frame, self.__x + 10, self.__y + 30, self.__width - 20, self.__height - 20) cvui.beginColumn(self.__width - 10, self.__height - 20)
def main(): frame = np.zeros((300, 600, 3), np.uint8) # Init cvui and tell it to create a OpenCV window, i.e. cv::namedWindow(WINDOW_NAME). cvui.init(WINDOW_NAME) while (True): # Fill the frame with a nice color frame[:] = (49, 52, 49) # Render a rectangle on the screen. rectangle = cvui.Rect(50, 50, 100, 100) cvui.rect(frame, rectangle.x, rectangle.y, rectangle.width, rectangle.height, 0xff0000) # Check what is the current status of the mouse cursor # regarding the previously rendered rectangle. status = cvui.iarea(rectangle.x, rectangle.y, rectangle.width, rectangle.height) # cvui::iarea() will return the current mouse status: # CLICK: mouse just clicked the interaction are # DOWN: mouse button was pressed on the interaction area, but not released yet. # OVER: mouse cursor is over the interaction area # OUT: mouse cursor is outside the interaction area if status == cvui.CLICK: print('Rectangle was clicked!') if status == cvui.DOWN: cvui.printf(frame, 240, 70, "Mouse is: DOWN") if status == cvui.OVER: cvui.printf(frame, 240, 70, "Mouse is: OVER") if status == cvui.OUT: cvui.printf(frame, 240, 70, "Mouse is: OUT") # Show the coordinates of the mouse pointer on the screen cvui.printf(frame, 240, 50, "Mouse pointer is at (%d,%d)", cvui.mouse().x, cvui.mouse().y) # This function must be called *AFTER* all UI components. It does # all the behind the scenes magic to handle mouse clicks, etc. cvui.update() # Show everything on the screen cv2.imshow(WINDOW_NAME, frame) # Check if ESC key was pressed if cv2.waitKey(20) == 27: break
def main(): frame = np.zeros((300, 600, 3), np.uint8) # Init cvui and tell it to create a OpenCV window, i.e. cv::namedWindow(WINDOW_NAME). cvui.init(WINDOW_NAME) while (True): # Fill the frame with a nice color frame[:] = (49, 52, 49) # Render a rectangle on the screen. rectangle = cvui.Rect(50, 50, 100, 100) cvui.rect(frame, rectangle.x, rectangle.y, rectangle.width, rectangle.height, 0xff0000) # Check what is the current status of the mouse cursor # regarding the previously rendered rectangle. status = cvui.iarea(rectangle.x, rectangle.y, rectangle.width, rectangle.height); # cvui::iarea() will return the current mouse status: # CLICK: mouse just clicked the interaction are # DOWN: mouse button was pressed on the interaction area, but not released yet. # OVER: mouse cursor is over the interaction area # OUT: mouse cursor is outside the interaction area if status == cvui.CLICK: print('Rectangle was clicked!') if status == cvui.DOWN: cvui.printf(frame, 240, 70, "Mouse is: DOWN") if status == cvui.OVER: cvui.printf(frame, 240, 70, "Mouse is: OVER") if status == cvui.OUT: cvui.printf(frame, 240, 70, "Mouse is: OUT") # Show the coordinates of the mouse pointer on the screen cvui.printf(frame, 240, 50, "Mouse pointer is at (%d,%d)", cvui.mouse().x, cvui.mouse().y) # This function must be called *AFTER* all UI components. It does # all the behind the scenes magic to handle mouse clicks, etc. cvui.update() # Show everything on the screen cv2.imshow(WINDOW_NAME, frame) # Check if ESC key was pressed if cv2.waitKey(20) == 27: break
def findRectangularWellsArea(frame, videoPath, hyperparameters): frame2 = frame.copy() root = tk.Tk() horizontal = root.winfo_screenwidth() vertical = root.winfo_screenheight() getRealValueCoefX = 1 getRealValueCoefY = 1 if len(frame2[0]) > horizontal or len(frame2) > vertical: getRealValueCoefX = len(frame2[0]) / int(horizontal*0.8) getRealValueCoefY = len(frame2) / int(vertical*0.8) frame2 = cv2.resize(frame2, (int(horizontal*0.8), int(vertical*0.8))) root.destroy() WINDOW_NAME = "Click on the top left of one of the wells" cvui.init(WINDOW_NAME) cv2.moveWindow(WINDOW_NAME, 0,0) cvui.imshow(WINDOW_NAME, frame2) while not(cvui.mouse(cvui.CLICK)): cursor = cvui.mouse() if cv2.waitKey(20) == 27: break topLeft = [cursor.x, cursor.y] cv2.destroyAllWindows() WINDOW_NAME = "Click on the bottom right of the same well" cvui.init(WINDOW_NAME) cv2.moveWindow(WINDOW_NAME, 0,0) cvui.imshow(WINDOW_NAME, frame2) while not(cvui.mouse(cvui.CLICK)): cursor = cvui.mouse() if cv2.waitKey(20) == 27: break bottomRight = [cursor.x, cursor.y] cv2.destroyAllWindows() rectangularWellsArea = int(abs((topLeft[0] - bottomRight[0]) * getRealValueCoefX) * abs((topLeft[1] - bottomRight[1]) * getRealValueCoefY)) return rectangularWellsArea
def main(): lena = cv2.imread('lena.jpg') frame = np.zeros(lena.shape, np.uint8) anchors = [cvui.Point() for i in range(3)] # one anchor for each mouse button rois = [cvui.Rect() for i in range(3)] # one ROI for each mouse button colors = [0xff0000, 0x00ff00, 0x0000ff] # Init cvui and tell it to create a OpenCV window, i.e. cv.namedWindow(WINDOW_NAME). cvui.init(WINDOW_NAME) while (True): # Fill the frame with Lena's image frame[:] = lena[:] # Show the coordinates of the mouse pointer on the screen cvui.text( frame, 10, 10, 'Click (any) mouse button then drag the pointer around to select a ROI.' ) cvui.text( frame, 10, 25, 'Use different mouse buttons (right, middle and left) to select different ROIs.' ) # Iterate all mouse buttons (left, middle and right button) button = cvui.LEFT_BUTTON while button <= cvui.RIGHT_BUTTON: # Get the anchor, ROI and color associated with the mouse button anchor = anchors[button] roi = rois[button] color = colors[button] # The function 'bool cvui.mouse(int button, int query)' allows you to query a particular mouse button for events. # E.g. cvui.mouse(cvui.RIGHT_BUTTON, cvui.DOWN) # # Available queries: # - cvui.DOWN: mouse button was pressed. cvui.mouse() returns true for single frame only. # - cvui.UP: mouse button was released. cvui.mouse() returns true for single frame only. # - cvui.CLICK: mouse button was clicked (went down then up, no matter the amount of frames in between). cvui.mouse() returns true for single frame only. # - cvui.IS_DOWN: mouse button is currently pressed. cvui.mouse() returns true for as long as the button is down/pressed. # Did the mouse button go down? if cvui.mouse(button, cvui.DOWN): # Position the anchor at the mouse pointer. anchor.x = cvui.mouse().x anchor.y = cvui.mouse().y # Is any mouse button down (pressed)? if cvui.mouse(button, cvui.IS_DOWN): # Adjust roi dimensions according to mouse pointer width = cvui.mouse().x - anchor.x height = cvui.mouse().y - anchor.y roi.x = anchor.x + width if width < 0 else anchor.x roi.y = anchor.y + height if height < 0 else anchor.y roi.width = abs(width) roi.height = abs(height) # Show the roi coordinates and size cvui.printf(frame, roi.x + 5, roi.y + 5, 0.3, color, '(%d,%d)', roi.x, roi.y) cvui.printf(frame, cvui.mouse().x + 5, cvui.mouse().y + 5, 0.3, color, 'w:%d, h:%d', roi.width, roi.height) # Ensure ROI is within bounds lenaRows, lenaCols, lenaChannels = lena.shape roi.x = 0 if roi.x < 0 else roi.x roi.y = 0 if roi.y < 0 else roi.y roi.width = roi.width + lenaCols - ( roi.x + roi.width) if roi.x + roi.width > lenaCols else roi.width roi.height = roi.height + lenaRows - ( roi.y + roi.height) if roi.y + roi.height > lenaRows else roi.height # If the ROI is valid, render it in the frame and show in a window. if roi.area() > 0: cvui.rect(frame, roi.x, roi.y, roi.width, roi.height, color) cvui.printf(frame, roi.x + 5, roi.y - 10, 0.3, color, 'ROI %d', button) lenaRoi = lena[roi.y:roi.y + roi.height, roi.x:roi.x + roi.width] cv2.imshow('ROI button' + str(button), lenaRoi) button += 1 # This function must be called *AFTER* all UI components. It does # all the behind the scenes magic to handle mouse clicks, etc. cvui.update() # Show everything on the screen cv2.imshow(WINDOW_NAME, frame) # Check if ESC key was pressed if cv2.waitKey(20) == 27: break
def main(): lena = cv2.imread('lena.jpg') frame = np.zeros(lena.shape, np.uint8) anchors = [cvui.Point() for i in range(3)] # one anchor for each mouse button rois = [cvui.Rect() for i in range(3)] # one ROI for each mouse button colors = [0xff0000, 0x00ff00, 0x0000ff] # Init cvui and tell it to create a OpenCV window, i.e. cv.namedWindow(WINDOW_NAME). cvui.init(WINDOW_NAME) while (True): # Fill the frame with Lena's image frame[:] = lena[:] # Show the coordinates of the mouse pointer on the screen cvui.text(frame, 10, 10, 'Click (any) mouse button then drag the pointer around to select a ROI.') cvui.text(frame, 10, 25, 'Use different mouse buttons (right, middle and left) to select different ROIs.') # Iterate all mouse buttons (left, middle and right button) button = cvui.LEFT_BUTTON while button <= cvui.RIGHT_BUTTON: # Get the anchor, ROI and color associated with the mouse button anchor = anchors[button] roi = rois[button] color = colors[button] # The function 'bool cvui.mouse(int button, int query)' allows you to query a particular mouse button for events. # E.g. cvui.mouse(cvui.RIGHT_BUTTON, cvui.DOWN) # # Available queries: # - cvui.DOWN: mouse button was pressed. cvui.mouse() returns true for single frame only. # - cvui.UP: mouse button was released. cvui.mouse() returns true for single frame only. # - cvui.CLICK: mouse button was clicked (went down then up, no matter the amount of frames in between). cvui.mouse() returns true for single frame only. # - cvui.IS_DOWN: mouse button is currently pressed. cvui.mouse() returns true for as long as the button is down/pressed. # Did the mouse button go down? if cvui.mouse(button, cvui.DOWN): # Position the anchor at the mouse pointer. anchor.x = cvui.mouse().x anchor.y = cvui.mouse().y # Is any mouse button down (pressed)? if cvui.mouse(button, cvui.IS_DOWN): # Adjust roi dimensions according to mouse pointer width = cvui.mouse().x - anchor.x height = cvui.mouse().y - anchor.y roi.x = anchor.x + width if width < 0 else anchor.x roi.y = anchor.y + height if height < 0 else anchor.y roi.width = abs(width) roi.height = abs(height) # Show the roi coordinates and size cvui.printf(frame, roi.x + 5, roi.y + 5, 0.3, color, '(%d,%d)', roi.x, roi.y) cvui.printf(frame, cvui.mouse().x + 5, cvui.mouse().y + 5, 0.3, color, 'w:%d, h:%d', roi.width, roi.height) # Ensure ROI is within bounds lenaRows, lenaCols, lenaChannels = lena.shape roi.x = 0 if roi.x < 0 else roi.x roi.y = 0 if roi.y < 0 else roi.y roi.width = roi.width + lenaCols - (roi.x + roi.width) if roi.x + roi.width > lenaCols else roi.width roi.height = roi.height + lenaRows - (roi.y + roi.height) if roi.y + roi.height > lenaRows else roi.height # If the ROI is valid, render it in the frame and show in a window. if roi.area() > 0: cvui.rect(frame, roi.x, roi.y, roi.width, roi.height, color) cvui.printf(frame, roi.x + 5, roi.y - 10, 0.3, color, 'ROI %d', button) lenaRoi = lena[roi.y : roi.y + roi.height, roi.x : roi.x + roi.width] cv2.imshow('ROI button' + str(button), lenaRoi) button += 1 # This function must be called *AFTER* all UI components. It does # all the behind the scenes magic to handle mouse clicks, etc. cvui.update() # Show everything on the screen cv2.imshow(WINDOW_NAME, frame) # Check if ESC key was pressed if cv2.waitKey(20) == 27: break
def main(): lena = cv2.imread('lena.jpg') frame = np.zeros(lena.shape, np.uint8) anchor = cvui.Point() roi = cvui.Rect(0, 0, 0, 0) working = False # Init cvui and tell it to create a OpenCV window, i.e. cv.namedWindow(WINDOW_NAME). cvui.init(WINDOW_NAME) while (True): # Fill the frame with Lena's image frame[:] = lena[:] # Show the coordinates of the mouse pointer on the screen cvui.text(frame, 10, 10, 'Click (any) mouse button and drag the pointer around to select a ROI.') # The function 'bool cvui.mouse(int query)' allows you to query the mouse for events. # E.g. cvui.mouse(cvui.DOWN) # # Available queries: # - cvui.DOWN: any mouse button was pressed. cvui.mouse() returns true for single frame only. # - cvui.UP: any mouse button was released. cvui.mouse() returns true for single frame only. # - cvui.CLICK: any mouse button was clicked (went down then up, no matter the amount of frames in between). cvui.mouse() returns true for single frame only. # - cvui.IS_DOWN: any mouse button is currently pressed. cvui.mouse() returns true for as long as the button is down/pressed. # Did any mouse button go down? if cvui.mouse(cvui.DOWN): # Position the anchor at the mouse pointer. anchor.x = cvui.mouse().x anchor.y = cvui.mouse().y # Inform we are working, so the ROI window is not updated every frame working = True # Is any mouse button down (pressed)? if cvui.mouse(cvui.IS_DOWN): # Adjust roi dimensions according to mouse pointer width = cvui.mouse().x - anchor.x height = cvui.mouse().y - anchor.y roi.x = anchor.x + width if width < 0 else anchor.x roi.y = anchor.y + height if height < 0 else anchor.y roi.width = abs(width) roi.height = abs(height) # Show the roi coordinates and size cvui.printf(frame, roi.x + 5, roi.y + 5, 0.3, 0xff0000, '(%d,%d)', roi.x, roi.y) cvui.printf(frame, cvui.mouse().x + 5, cvui.mouse().y + 5, 0.3, 0xff0000, 'w:%d, h:%d', roi.width, roi.height) # Was the mouse clicked (any button went down then up)? if cvui.mouse(cvui.UP): # We are done working with the ROI. working = False # Ensure ROI is within bounds lenaRows, lenaCols, lenaChannels = lena.shape roi.x = 0 if roi.x < 0 else roi.x roi.y = 0 if roi.y < 0 else roi.y roi.width = roi.width + lena.cols - (roi.x + roi.width) if roi.x + roi.width > lenaCols else roi.width roi.height = roi.height + lena.rows - (roi.y + roi.height) if roi.y + roi.height > lenaRows else roi.height # Render the roi cvui.rect(frame, roi.x, roi.y, roi.width, roi.height, 0xff0000) # This function must be called *AFTER* all UI components. It does # all the behind the scenes magic to handle mouse clicks, etc. cvui.update() # Show everything on the screen cv2.imshow(WINDOW_NAME, frame) # If the ROI is valid, show it. if roi.area() > 0 and working == False: lenaRoi = lena[roi.y : roi.y + roi.height, roi.x : roi.x + roi.width] cv2.imshow(ROI_WINDOW, lenaRoi) # Check if ESC key was pressed if cv2.waitKey(20) == 27: break
def main(): lena = cv2.imread('lena.jpg') frame = np.zeros(lena.shape, np.uint8) anchor = cvui.Point() roi = cvui.Rect(0, 0, 0, 0) working = False # Init cvui and tell it to create a OpenCV window, i.e. cv.namedWindow(WINDOW_NAME). cvui.init(WINDOW_NAME) while (True): # Fill the frame with Lena's image frame[:] = lena[:] # Show the coordinates of the mouse pointer on the screen cvui.text( frame, 10, 10, 'Click (any) mouse button and drag the pointer around to select a ROI.' ) # The function 'bool cvui.mouse(int query)' allows you to query the mouse for events. # E.g. cvui.mouse(cvui.DOWN) # # Available queries: # - cvui.DOWN: any mouse button was pressed. cvui.mouse() returns true for single frame only. # - cvui.UP: any mouse button was released. cvui.mouse() returns true for single frame only. # - cvui.CLICK: any mouse button was clicked (went down then up, no matter the amount of frames in between). cvui.mouse() returns true for single frame only. # - cvui.IS_DOWN: any mouse button is currently pressed. cvui.mouse() returns true for as long as the button is down/pressed. # Did any mouse button go down? if cvui.mouse(cvui.DOWN): # Position the anchor at the mouse pointer. anchor.x = cvui.mouse().x anchor.y = cvui.mouse().y # Inform we are working, so the ROI window is not updated every frame working = True # Is any mouse button down (pressed)? if cvui.mouse(cvui.IS_DOWN): # Adjust roi dimensions according to mouse pointer width = cvui.mouse().x - anchor.x height = cvui.mouse().y - anchor.y roi.x = anchor.x + width if width < 0 else anchor.x roi.y = anchor.y + height if height < 0 else anchor.y roi.width = abs(width) roi.height = abs(height) # Show the roi coordinates and size cvui.printf(frame, roi.x + 5, roi.y + 5, 0.3, 0xff0000, '(%d,%d)', roi.x, roi.y) cvui.printf(frame, cvui.mouse().x + 5, cvui.mouse().y + 5, 0.3, 0xff0000, 'w:%d, h:%d', roi.width, roi.height) # Was the mouse clicked (any button went down then up)? if cvui.mouse(cvui.UP): # We are done working with the ROI. working = False # Ensure ROI is within bounds lenaRows, lenaCols, lenaChannels = lena.shape roi.x = 0 if roi.x < 0 else roi.x roi.y = 0 if roi.y < 0 else roi.y roi.width = roi.width + lena.cols - ( roi.x + roi.width) if roi.x + roi.width > lenaCols else roi.width roi.height = roi.height + lena.rows - ( roi.y + roi.height) if roi.y + roi.height > lenaRows else roi.height # Render the roi cvui.rect(frame, roi.x, roi.y, roi.width, roi.height, 0xff0000) # This function must be called *AFTER* all UI components. It does # all the behind the scenes magic to handle mouse clicks, etc. cvui.update() # Show everything on the screen cv2.imshow(WINDOW_NAME, frame) # If the ROI is valid, show it. if roi.area() > 0 and working == False: lenaRoi = lena[roi.y:roi.y + roi.height, roi.x:roi.x + roi.width] cv2.imshow(ROI_WINDOW, lenaRoi) # Check if ESC key was pressed if cv2.waitKey(20) == 27: break
def onWork(self): self.frame[:] = (49, 52, 49) char = cv.waitKey(1) doOverw = False writeOnName = False writeOnSubject = False writeOnProf = False stringCopy = '' stringCopyColor = 0x10dca1 while True: cursor = cvui.mouse(WINDOW_NAME) click = cvui.mouse(cvui.CLICK) # (self, x, y, w, h, title, char, writeOn, cursor, click, writeOnBool): writeOnName, self.folderName = self.textInput( 5, 5, 305, 40, "Folder Name:", char, self.folderName, cursor, click, writeOnName) writeOnSubject, self.subjectName = self.textInput( 5 + X_SCREEN / 2, 5, 305, 40, "Subject Name:", char, self.subjectName, cursor, click, writeOnSubject) writeOnProf, self.tempName = self.textInput( 5 + X_SCREEN / 2, 105, 305, 40, "Professors:", char, self.tempName, cursor, click, writeOnProf) cvui.rect(self.frame, 6 + X_SCREEN / 2, 155, X_SCREEN / 2 - 63, 235 - 165, 0x8d9797, 0x3c4747) cvui.rect(self.frame, X_SCREEN - 52, 155, 47, 235 - 165, 0x8d9797, 0x293939) if cvui.button( self.frame, X_SCREEN - 49, 157, "+") and not self.tempName == '' and len(self.names) < 4: self.names.append(self.tempName) self.tempName = '' if cvui.button(self.frame, X_SCREEN - 49, 196, "-") and not len(self.names) == 0: del self.names[-1] for i in range(len(self.names)): cvui.printf(self.frame, 10 + X_SCREEN / 2, 160 + 15 * i, 0.4, 0xdd97fb, self.names[i]) xp = int((X_SCREEN - 20) / 4) cvui.window(self.frame, 5, 235, X_SCREEN - 10, Y_SCREEN - 235 - 5, "Premade:") if cvui.button(self.frame, 10 + xp - 10 * 11, 260, "Lab. Micro."): self.subjectName = '22.99 Laboratorio De Microprocesadores' self.names.clear() self.names.append('Jacoby, Daniel Andres') self.names.append('Magliola, Nicolas') self.names.append('Ismirlian, Diego Matias') self.group[0] = 3 if cvui.button(self.frame, 10 + xp * 2 - 10 * 7, 260, "Control"): self.subjectName = '22.85 Sistemas de Control' self.names.clear() self.names.append('Nasini, Victor Gustavo') self.names.append('Zujew, Cristian Alejo') if cvui.button(self.frame, 10 + xp * 3 - 10 * 9, 260, "Transinfo"): self.subjectName = '22.61 Transmision de la Informacion' self.names.clear() self.names.append('Bertucci, Eduardo Adolfo') self.names.append('Vila Krause, Luis Gustavo') if cvui.button(self.frame, 10 + xp * 4 - 10 * 11, 260, "Electromag."): self.subjectName = '22.37 Electromagnetismo' self.names.clear() self.names.append('Munoz, Claudio Marcelo') self.names.append('Dobrusin, Pablo') cvui.printf(self.frame, 5, 55, 0.4, 0xdd97fb, f'N Exercises:') cvui.counter(self.frame, 5, 70, self.numberFolder) if self.numberFolder[0] <= 0: cvui.rect(self.frame, 5 + 25, 72, 40, 17, 0x292929, 0x292929) self.numberFolder[0] = 1 cvui.printf(self.frame, 5 + 41, 76, 0.4, 0x9C9C9C, '1') cvui.printf(self.frame, 5 + X_SCREEN / 6, 55, 0.4, 0xdd97fb, f'Day:') cvui.counter(self.frame, 5 + X_SCREEN / 6, 70, self.day) if self.day[0] <= 0: cvui.rect(self.frame, 5 + X_SCREEN / 6 + 25, 72, 40, 17, 0x292929, 0x292929) self.day[0] = 1 cvui.printf(self.frame, 5 + X_SCREEN / 6 + 41, 76, 0.4, 0x9C9C9C, '1') elif self.day[0] >= 31: self.day[0] = 31 cvui.printf(self.frame, 5 + X_SCREEN * 2 / 6, 55, 0.4, 0xdd97fb, f'Month:') cvui.counter(self.frame, 5 + X_SCREEN * 2 / 6, 70, self.month) if self.month[0] <= 0: cvui.rect(self.frame, 5 + X_SCREEN * 2 / 6 + 25, 72, 40, 17, 0x292929, 0x292929) self.month[0] = 1 cvui.printf(self.frame, 5 + X_SCREEN * 2 / 6 + 41, 76, 0.4, 0x9C9C9C, '1') elif self.month[0] >= 12: self.month[0] = 12 cvui.printf(self.frame, 5 + X_SCREEN * 3 / 6, 55, 0.4, 0xdd97fb, f'Year:') cvui.counter(self.frame, 5 + X_SCREEN * 3 / 6, 70, self.year) if self.year[0] <= 19: cvui.rect(self.frame, 5 + X_SCREEN * 3 / 6 + 25, 72, 40, 17, 0x292929, 0x292929) self.year[0] = 20 cvui.printf(self.frame, 5 + X_SCREEN * 3 / 6 + 37, 76, 0.4, 0x9C9C9C, '20') elif self.year[0] >= 22: self.year[0] = 22 cvui.printf(self.frame, 5 + X_SCREEN * 4 / 6, 55, 0.4, 0xdd97fb, f'N Group:') cvui.counter(self.frame, 5 + X_SCREEN * 4 / 6, 70, self.group) if self.group[0] <= 0: cvui.rect(self.frame, 5 + X_SCREEN * 4 / 6 + 25, 72, 40, 17, 0x292929, 0x292929) self.group[0] = 1 cvui.printf(self.frame, 5 + X_SCREEN * 4 / 6 + 41, 76, 0.4, 0x9C9C9C, '1') elif self.group[0] >= 7: self.group[0] = 7 cvui.printf(self.frame, 5 + X_SCREEN * 5 / 6, 55, 0.4, 0xdd97fb, f'N TP:') cvui.counter(self.frame, 5 + X_SCREEN * 5 / 6, 70, self.tp) if self.tp[0] <= 0: cvui.rect(self.frame, 5 + X_SCREEN * 5 / 6 + 25, 72, 40, 17, 0x292929, 0x292929) self.tp[0] = 1 cvui.printf(self.frame, 5 + X_SCREEN * 5 / 6 + 41, 76, 0.4, 0x9C9C9C, '1') if cvui.button(self.frame, X_SCREEN / 4 - 10 * 16 / 2, 110, "Load Folder Path") and not doOverw: self.folderPath = self.getPath() cvui.window(self.frame, 5, 155, X_SCREEN / 2 - 10, 40, "Folder Path Selected") if len(self.folderPath) < MAXCHAR + 3: cvui.printf(self.frame, 10, 180, 0.4, 0xdd97fb, self.folderPath) else: cvui.printf(self.frame, 10, 180, 0.4, 0xdd97fb, self.folderPath[0:MAXCHAR + 2] + '...') if (not self.folderPath == '') and (not self.folderName == '') and (not self.subjectName == ''): if cvui.button(self.frame, 120, 200, "Start Copy"): self.startCopy = True stringCopy = "Wait While Copying..." stringCopyColor = 0xdc1076 cvui.printf(self.frame, 5, 205, 0.4, stringCopyColor, stringCopy) if self.startCopy: self.startCopy = False while self.folderName[-1] == ' ': #32: self.folderName = self.folderName[:-1] finalPath = self.folderPath + '/' + self.folderName coping = self.copyFolders(self.numberFolder[0], finalPath, True) if not coping == None: if not coping: doOverw = True else: stringCopy = "Copy Done!" stringCopyColor = 0x10dca1 else: stringCopy = "Template Not Found!!" stringCopyColor = 0xdc1076 if doOverw: check = self.overWrite(finalPath) if not check == -1: if check == None: stringCopy = "Template Not Found!!" stringCopyColor = 0xdc1076 else: if check == True: stringCopy = "Copy Done!" stringCopyColor = 0x10dca1 else: stringCopy = "Unable To Copy" stringCopyColor = 0xdc1076 doOverw = False cvui.imshow(WINDOW_NAME, self.frame) char = cv.waitKey(1) self.frame[:] = (49, 52, 49) if (char == 27) or not cv.getWindowProperty( WINDOW_NAME, cv.WND_PROP_VISIBLE): break
def main(): context = zmq.Context() # Receive video frames from camera video_socket = context.socket(zmq.SUB) video_socket.setsockopt(zmq.CONFLATE, 1) video_socket.setsockopt(zmq.RCVTIMEO, 1000) video_socket.connect('tcp://%s:%d' % (p.VIDEO_SPOTTER_IP, p.VIDEO_SPOTTER_PORT)) topicfilter = '' video_socket.setsockopt_string(zmq.SUBSCRIBE, topicfilter) # Receive updates from stage stage_sub = context.socket(zmq.SUB) stage_sub.setsockopt(zmq.CONFLATE, 1) stage_sub.setsockopt(zmq.RCVTIMEO, 0) stage_sub.connect('tcp://%s:%d' % (p.STAGE_POSITION_IP, p.STAGE_POSITION_PORT)) topicfilter = '' stage_sub.setsockopt_string(zmq.SUBSCRIBE, topicfilter) # TODO: We need to connect here instead of binding if we want to use this at the same time as CameraDisplaySpotter # Publish tracking deltas track_socket = context.socket(zmq.PUB) track_socket.bind('tcp://*:%s' % p.TRACK_PORT) intrinsic = np.load('intrinsic_calibration/ll_65/intrinsic.npy') cvui.init('MaskWindow1') # Need to wait for stage to be up and running: while 1: try: stage_msg = stage_sub.recv_string() (stage_x, stage_y, stage_z) = [float(x) for x in stage_msg.split(' ')] break except zmq.Again: print('Waiting for stage controller node') time.sleep(1) stage_zero_offset = np.array([stage_x, stage_y, -stage_z], ndmin=2).T np.save('tank_corners_offset.npy', stage_zero_offset) while keep_running: MODE = '1' # phase 1: First position corner_points = np.zeros( (3, 8)) # first 4 for front, second 4 for back corner_ix = 0 print('Click on corners at front of tank') while MODE == '1': try: frame = recv_img(video_socket) except zmq.Again: print('No new frame!') continue for pt_ix in range(corner_ix): center = (int(corner_points[0, pt_ix]), int(corner_points[1, pt_ix])) if pt_ix < 4: color = (0, 0, 255) else: color = (255, 0, 0) cv2.circle(frame, center, 5, color, -1) cvui.update('MaskWindow1') cv2.imshow('MaskWindow1', frame) cv2.waitKey(1) cvui.context('MaskWindow1') if cvui.mouse(cvui.CLICK): print('CLICK') corner_points[0, corner_ix] = cvui.mouse().x corner_points[1, corner_ix] = cvui.mouse().y corner_points[2, corner_ix] = 1 corner_ix += 1 if corner_ix == 4: print('Click on corners at back of tank') if corner_ix == 8: MODE = '2' # Move stage # TODO: Add a REP/REQ instead of PUB/SUB for one-off reliably stage movements stage_delta = 15.0 track_socket.send_string('%f 0 0' % (stage_delta * p.X_MOVE_SCALE)) print('Waiting for stage motion') time.sleep(2) E = np.array([[0, 0, 0], [0, 0, -stage_delta], [0, stage_delta, 0]]) # essential matrix - only X motion F = np.linalg.inv(intrinsic).T @ E @ np.linalg.inv( intrinsic) # Fundamental Matrix # phase 2: Click corners in second position corner_points2 = np.zeros((3, 8)) corner_ix2 = 0 print('Click on corners at front of tank') while MODE == '2': try: frame = recv_img(video_socket) except zmq.Again: print('No new frame!') # Plot the epipolar line for the current point # i.e. p_r^T * F * p_l^T = 0 where F is fundamental matrix and p_r and p_l are points in (homogeneous) image space # y0 and y1 are the y coordinates of this line for x=0 and x=img_width im1_pt = corner_points[:, corner_ix2:corner_ix2 + 1] d = im1_pt.T @ F y0 = int(-d[0, 2] / d[0, 1]) y1 = int((-d[0, 0] * p.IMG_WIDTH_SPOTTER - d[0, 2]) / d[0, 1]) cv2.line(frame, (0, y0), (640, y1), (0, 255, 0)) for pt_ix in range(corner_ix2): center = (int(corner_points2[0, pt_ix]), int(corner_points2[1, pt_ix])) if pt_ix < 4: color = (0, 0, 255) else: color = (255, 0, 0) cv2.circle(frame, center, 5, color, -1) cvui.update('MaskWindow1') cv2.imshow('MaskWindow1', frame) cv2.waitKey(1) cvui.context('MaskWindow1') if cvui.mouse(cvui.CLICK): corner_points2[0, corner_ix2] = cvui.mouse().x corner_points2[1, corner_ix2] = cvui.mouse().y corner_points2[2, corner_ix2] = 1 corner_ix2 += 1 if corner_ix2 == 4: print('Click on corners at back of tank') if corner_ix2 == 8: MODE = '3' # Intermezzo: Compute the 3d location of the tank corners world_rays1 = np.linalg.inv(intrinsic) @ corner_points world_rays2 = np.linalg.inv(intrinsic) @ corner_points2 world_points = np.zeros((3, 8)) A = np.zeros((2, 2)) B = np.zeros((2, 1)) for point_ix in range(8): p1 = np.zeros((3, 1)) p2 = np.zeros((3, 1)) v1 = world_rays1[:, point_ix:point_ix + 1] v2 = world_rays2[:, point_ix:point_ix + 1] p2[0] = -stage_delta A[0, 0] = -np.inner(v1.T, v1.T) A[0, 1] = np.inner(v1.T, v2.T) B[0, 0] = -(np.inner(p2.T, v1.T) - np.inner(p1.T, v1.T)) A[1, 0] = -np.inner(v1.T, v2.T) A[1, 1] = np.inner(v2.T, v2.T) B[1, 0] = -(np.inner(p2.T, v2.T) - np.inner(p1.T, v2.T)) sol = np.linalg.solve(A, B) point1 = p1 + sol[0] * v1 point2 = p2 + sol[1] * v2 mp = (point2 - point1) / 2.0 + point1 world_points[:, point_ix] = mp.squeeze() print(world_points) np.save('tank_corners.npy', world_points) # phase 3: Compute world-frame corners and update as stage moves stage_pos_cur = np.zeros((3, 1)) while MODE == '3': try: frame = recv_img(video_socket) except zmq.Again: print('No new frame!') try: stage_msg = stage_sub.recv_string() (stage_x, stage_y, stage_z) = [float(x) for x in stage_msg.split(' ')] stage_pos_cur[0] = stage_x stage_pos_cur[1] = stage_y stage_pos_cur[2] = -stage_z except zmq.Again: pass stage_delta = stage_pos_cur - stage_zero_offset corners_translated = world_points - stage_delta full_corners_img = np.zeros((2, 8)) for pt_ix in range(8): corner_img = intrinsic @ corners_translated[:, pt_ix] corner_img = corner_img / corner_img[2] full_corners_img[0, pt_ix] = corner_img[0] full_corners_img[1, pt_ix] = corner_img[1] center = (int(corner_img[0]), int(corner_img[1])) if pt_ix < 4: color = (0, 0, 255) else: color = (255, 0, 0) cv2.circle(frame, center, 5, color, -1) cv2.imshow('MaskWindow1', frame) print([ np.array([ full_corners_img[:, 0], full_corners_img[:, 1], full_corners_img[:, 2], full_corners_img[:, 3] ]) ]) poly_frame_front = np.zeros(frame.shape, dtype=np.uint8) poly_frame_back = np.zeros(frame.shape, dtype=np.uint8) poly_frame_front = cv2.fillPoly(poly_frame_front, [ np.array([ full_corners_img[:, 0], full_corners_img[:, 1], full_corners_img[:, 2], full_corners_img[:, 3] ], dtype='int32') ], (255, 255, 255)) poly_frame_back = cv2.fillPoly(poly_frame_back, [ np.array([ full_corners_img[:, 4], full_corners_img[:, 5], full_corners_img[:, 6], full_corners_img[:, 7] ], dtype='int32') ], (255, 255, 255)) clipped_frame = cv2.bitwise_and(frame, poly_frame_front) clipped_frame = cv2.bitwise_and(clipped_frame, poly_frame_back) cv2.imshow('MaskWindow2', clipped_frame) cv2.waitKey(1)
def main(): # We have one mat for each window. frame1 = np.zeros((200, 500, 3), np.uint8) frame2 = np.zeros((200, 500, 3), np.uint8) frame3 = np.zeros((200, 500, 3), np.uint8) # Init cvui, instructing it to create 3 OpenCV windows. windows = [WINDOW1_NAME, WINDOW2_NAME, WINDOW3_NAME] cvui.init(windows, 3) while (True): # clear all frames frame1[:] = (49, 52, 49) frame2[:] = (49, 52, 49) frame3[:] = (49, 52, 49) # Inform cvui that all subsequent component calls and events are related to window 1. # We do that by calling cvui.context(). cvui.context(WINDOW1_NAME) cvui.printf( frame1, 10, 10, 'In window1, mouse is at: %d,%d (obtained from window name)', cvui.mouse(WINDOW1_NAME).x, cvui.mouse(WINDOW1_NAME).y) if cvui.mouse(WINDOW1_NAME, cvui.LEFT_BUTTON, cvui.IS_DOWN): cvui.printf(frame1, 10, 30, 'In window1, mouse LEFT_BUTTON is DOWN') cvui.imshow(WINDOW1_NAME, frame1) # From this point on, we are going to render the second window. We need to inform cvui # that all updates and components from now on are connected to window 2. cvui.context(WINDOW2_NAME) cvui.printf(frame2, 10, 10, 'In window2, mouse is at: %d,%d (obtained from context)', cvui.mouse().x, cvui.mouse().y) if cvui.mouse(cvui.LEFT_BUTTON, cvui.IS_DOWN): cvui.printf(frame2, 10, 30, 'In window2, mouse LEFT_BUTTON is DOWN') cvui.imshow(WINDOW2_NAME, frame2) # Finally we are going to render the thrid window. Again we need to inform cvui # that all updates and components from now on are connected to window 3. cvui.context(WINDOW3_NAME) cvui.printf(frame3, 10, 10, 'In window1, mouse is at: %d,%d', cvui.mouse(WINDOW1_NAME).x, cvui.mouse(WINDOW1_NAME).y) cvui.printf(frame3, 10, 30, 'In window2, mouse is at: %d,%d', cvui.mouse(WINDOW2_NAME).x, cvui.mouse(WINDOW2_NAME).y) cvui.printf(frame3, 10, 50, 'In window3, mouse is at: %d,%d', cvui.mouse(WINDOW3_NAME).x, cvui.mouse(WINDOW3_NAME).y) if cvui.mouse(WINDOW1_NAME, cvui.LEFT_BUTTON, cvui.IS_DOWN): cvui.printf(frame3, 10, 90, 'window1: LEFT_BUTTON is DOWN') if cvui.mouse(WINDOW2_NAME, cvui.LEFT_BUTTON, cvui.IS_DOWN): cvui.printf(frame3, 10, 110, 'window2: LEFT_BUTTON is DOWN') if cvui.mouse(WINDOW3_NAME, cvui.LEFT_BUTTON, cvui.IS_DOWN): cvui.printf(frame3, 10, 130, 'window3: LEFT_BUTTON is DOWN') cvui.imshow(WINDOW3_NAME, frame3) # Check if ESC key was pressed if cv2.waitKey(20) == 27: break
import cv2 import cvui import cateyesperseption ##main monitor display out put to tv ## cam = frame cvui.init('screen') while True: success, current_cam = cam.read() dim = (width, height) Full_frame = cv2.resize(current_cam, dim, interpolation=cv2.INTER_AREA) cv2.namedWindow('screen', cv2.WINDOW_NORMAL) cv2.setWindowProperty('screen', cv2.WND_PROP_FULLSCREEN, cv2.WINDOW_FULLSCREEN) if (cvui.button(Full_frame, width - 100, height - 40, "Next") and cvui.mouse(cvui.CLICK)): print("Next Button Pressed") cvui.init('screen') cam_no = cam_no+1 if (cam_no>4): cam_no=1 del cam cam = create_camera(str(cam_no)) if (cvui.button(Full_frame, width - 200, height - 40, "Previous") and cvui.mouse(cvui.CLICK)): print("Previous Button Pressed") cvui.init('screen') cam_no = cam_no - 1 if (cam_no<1): cam_no=4 del cam cam = create_camera(str(cam_no)) cv2.imshow('screen', Full_frame)
def mouse_click(): if (cvui.mouse(cvui.LEFT_BUTTON, cvui.CLICK)): if (cvui.mouse().x >= layout_space_video and cvui.mouse().x <= layout_space_video + video_size_x and cvui.mouse().y >= layout_space_video and cvui.mouse().y <= layout_space_video + video_size_y): print("Adding points ", (cvui.mouse().x - layout_space_video, cvui.mouse().y - layout_space_video)) new_points.append((cvui.mouse().x - layout_space_video, cvui.mouse().y - layout_space_video)) if (cvui.mouse(cvui.RIGHT_BUTTON, cvui.CLICK)): if (cvui.mouse().x >= layout_space_video and cvui.mouse().x <= layout_space_video + video_size_x and cvui.mouse().y >= layout_space_video and cvui.mouse().y <= layout_space_video + video_size_y and len(points) > 0): if (len(new_points) >= 1): print("Deleting points") del new_points[-1] fill_poligon = False if (new_stringLabel): # erase stringLabel new_stringLabel.clear()
def main(): global keep_running # This is for saving video *with* detection boxes on it # To save raw video, use the CameraSaver.py script save_video = True if save_video: sz = (p.IMG_WIDTH_SPOTTER, p.IMG_HEIGHT_SPOTTER) fourcc = cv2.VideoWriter_fourcc(*'mp4v') vout = cv2.VideoWriter() vout.open('track_output.mp4', fourcc, p.FPS_SPOTTER, sz, False) signal.signal(signal.SIGINT, sigint_handler) control_panes = ControlPanes() control_panes.stage_control_pane = EnhancedWindow(0, 0, 300, 500, 'Stage Control') control_panes.focus_control_pane = EnhancedWindow(0, 20, 300, 500, 'Focus Control') control_panes.tracker_select_pane = EnhancedWindow(0, 40, 300, 500, 'Tracker Select') control_panes.canny_settings_pane = EnhancedWindow(0, 60, 300, 500, 'Canny Tuning') control_panes.threshold_setting_pane = EnhancedWindow( 0, 80, 300, 500, 'Threshold Tuning') cvui.init(p.CTRL_WINDOW_NAME) cvui.init(p.VIDEO_WINDOW_NAME) context = zmq.Context() (video_socket, focus_sub, stage_sub, focus_state_sub, macro_sharpness_sub, track_socket, roi_socket, af_pub) = setup_zmq(context) stage_zero_offset = np.load('tank_corners_offset.npy') world_points = np.load('tank_corners.npy') intrinsic = np.load('intrinsic_calibration/ll_65/intrinsic.npy') stage_x = None stage_y = None stage_z = None z_moving = True current_ll_focus = None object_distance_ll = 0 target_pos_obs = None target_pos = np.array([1, 1]) target_pos_slow = target_pos.copy() feature_delta = np.array([0, 0]) target_track_init = False STAGE_MODE = 'PAUSED' FOCUS_MODE = 'MANUAL' tracker_type = 'KCF' # options are KCF or CANNY # These three structs store the state information necessary for the trackers canny_tracker_state = CannyTracker() canny_tracker_state.canny_low = [50] canny_tracker_state.canny_high = [150] kcf_tracker_state = KCFTracker() kcf_tracker_state.kcf_box_anchor = cvui.Point() kcf_tracker_state.kcf_roi = cvui.Rect(0, 0, 0, 0) kcf_tracker_state.kcf_tracker_init = False threshold_tracker_state = ThresholdTracker() threshold_tracker_state.threshold = [30] threshold_tracker_state.roi = cvui.Rect(0, 0, 0, 0) threshold_tracker_state.box_anchor = cvui.Point threshold_tracker_state.show_binary = [False] sharpness_focus_state = SharpnessFocusState() sharpness_focus_state.mode = 'COARSE' macro_sharpness = 0 while keep_running: ctrl_frame = np.zeros((700, 300, 3), np.uint8) # Receive stage position updates try: stage_pos = stage_sub.recv_string() (stage_x, stage_y, stage_z_new) = [float(x) for x in stage_pos.split(' ')] if stage_z_new == stage_z: z_moving = False else: z_moving = True stage_z = stage_z_new except zmq.Again: # the stage publisher only publishes at ~10hz, so not having an update is common pass # Receive macro sharpness try: macro_sharpness_last = macro_sharpness macro_sharpness = float(macro_sharpness_sub.recv_string()) except zmq.Again: # no sharpness value, which is unexpected print('No Macro Image Sharpness!') # receive next frame try: frame = recv_img(video_socket) except zmq.Again: print('Timed Out!') time.sleep(1) continue cvui.context(p.VIDEO_WINDOW_NAME) if cvui.mouse(cvui.IS_DOWN): (target_pos, feature_delta) = reset_target_selection() target_pos_slow = target_pos.copy() target_track_init = True feature_delta += get_feature_2delta() if stage_x is not None: stage_pos = np.array([stage_x, stage_y, -stage_z], ndmin=2).T frame = tank_corners_clip(frame, stage_pos, stage_zero_offset, world_points, intrinsic) # This is where the tracking happens. tracker_type is controlled by a button on the interface # Adding a new tracker is as simple as adding another case to this if/else and adding a button in # the UI to switch into the new tracking mode if tracker_type == 'CANNY': canny_tracker_state.target_pos = target_pos (target_pos_obs, roi, canny_tracker_state) = update_canny_tracker( frame, canny_tracker_state) elif tracker_type == 'KCF': cvui.context(p.VIDEO_WINDOW_NAME) (target_pos_obs, roi, kcf_tracker_state) = update_kcf_tracker(frame, kcf_tracker_state) elif tracker_type == 'THRESHOLD': cvui.context(p.VIDEO_WINDOW_NAME) threshold_tracker_state.target_pos = target_pos (target_pos_obs, roi, threshold_tracker_state) = update_threshold_tracker( frame, threshold_tracker_state) else: print('Invalid tracker mode: %s' % tracker_type) roi = None keep_running = False # This roi_msg takes an roi that may have been identified around the animal and sends it over zmq # This enables any cameras trying to autofocus to know which roi to keep in focus # if no autofocusing is happening, then these messages don't do anything if roi is not None: roi_msg = m.SetFocusROI(roi[0], roi[1]) else: roi_msg = m.SetFocusROI(None, None) roi_socket.send_pyobj( roi_msg ) # tell the LL camera (or anything else I guess) which ROI to focus (target_track_ok, target_pos, target_pos_slow) = filter_target_position(target_pos, target_pos_slow, target_pos_obs) # This is probably where we want to use the other camera to estimate depth # Now we have a giant state machine. We need to structure the code this way, because we want 2D tracking and # user interaction to update even when we are waiting on some slower action to occur related to object depth # and focusing. The state machine provides a mechanism to handle these slower processes while not impeding the # rest of the tracking process. # STAGE_MODE = {MANUAL | AUTO | PAUSED} # -- In MANUAL mode, dx,dy,dz all set by keyboard input. # -- In AUTO mode, dx and dy are set by tracker. dz is set by autofocus if FOCUS_MODE is set to AUTO # -- In PAUSED mode, dx = dy = dz = 0. The tracker will keep tracking, but the stage won't move # # FOCUS_MODE = {MANUAL | SHARPNESS | DEPTH} # -- In MANUAL mode, dz is set by keyboard input # -- In SHARPNESS mode, dz is set by trying to maximize sharpness, although the final position can be tweaked # by user input. SHARPNESS mode does nothing if STAGE_MODE is MANUAL # -- In DEPTH mode, dz is set by a target depth measurement that is estimated from a second camera # (stereo or perpendicular) # Determine dx and dy if STAGE_MODE == 'PAUSED': # -> Stage Control track_socket.send_string('0 0 0') dx = 0 dy = 0 dz = 0 elif STAGE_MODE == 'MANUAL': # TODO: Probably tune this better (dx, dy) = get_feature_2delta() dx = 10 * dx dy = 10 * dy print('FULL_MANUAL %f, %f' % (dx, dy)) dz = manual_focus_update() elif STAGE_MODE == 'AUTO': # The tracker makes a determination in pixel space, then we may decide to filter it. We then determine the # dx and dy based on the distance between the feature of interest and the macro lens center # how much do we need to move in pixel-space? # Note dx and dy are 0 if there are no target tracks if stage_z is None: print('Waiting on stage node') dx = 0 dy = 0 dz = 0 else: if target_pos_obs is not None: if target_track_ok: (dx, dy) = calculate_movement_offsets( frame, target_pos, target_pos_slow, feature_delta) else: dx = 0 dy = 0 else: dx = 0 dy = 0 target_track_ok = False # When STAGE_MODE == 'AUTO', we need to determine how to handle the focusing if FOCUS_MODE == 'MANUAL': dz = manual_focus_update() elif FOCUS_MODE == 'SHARPNESS': sharpness_focus_state.stage_z = stage_z sharpness_focus_state.macro_sharpness = macro_sharpness sharpness_focus_state.z_moving = z_moving dz, sharpness_focus_state = sharpness_focus( sharpness_focus_state, af_pub, focus_state_sub, video_socket, focus_sub) elif FOCUS_MODE == 'DEPTH': # this is the mode when we have a second camera to estimate depth dz = 0 else: # invalid focus mode print('Invalid focus mode %s' % FOCUS_MODE) sys.exit(1) else: print('Unknown stage mode: %s' % STAGE_MODE) dx = 0 dy = 0 dz = 0 print(dx, dy, dz) track_socket.send_string( '%f %f %f' % (dx, dy, dz)) # 'wasteful', but easier debugging for now frame = cv2.resize( frame, (p.IMG_DISP_WIDTH_SPOTTER, p.IMG_DISP_HEIGHT_SPOTTER)) # draw dots on frame centers cv2.circle(frame, (int( p.IMG_DISP_WIDTH_SPOTTER / 2), int(p.IMG_DISP_HEIGHT_SPOTTER / 2)), 5, (0, 0, 255), -1) # center of frame cv2.circle(frame, (p.MACRO_LL_CENTER[0], p.MACRO_LL_CENTER[1]), 5, (255, 0, 255), -1) # center of macro frame frame cvui.update(p.VIDEO_WINDOW_NAME) cv2.imshow(p.VIDEO_WINDOW_NAME, frame) if save_video: vout.write(frame) cvui.context(p.CTRL_WINDOW_NAME) STAGE_MODE, FOCUS_MODE, tracker_type, macro_resweep, ll_resweep = draw_settings( ctrl_frame, control_panes, canny_tracker_state, threshold_tracker_state, STAGE_MODE, FOCUS_MODE, tracker_type) if macro_resweep: p.BYPASS_LL_ESTIMATE = True sharpness_focus_state.mode = 'FINE_UNINITIALIZED' if ll_resweep: if stage_z is not None: print('Liquid Lens Refocus!') dist_to_tank = (300 - stage_z) + p.STAGE_TANK_OFFSET ll_max = 2953.5 * dist_to_tank**-0.729 ll_min = 2953.5 * (dist_to_tank + p.TANK_DEPTH_MM)**-0.729 print('llmin, llmax: (%f, %f)' % (ll_min, ll_max)) af_pub.send_pyobj(m.AutofocusMessage(ll_min, ll_max, 1)) else: print('Cannot refocus liquid lens until stage node is running') cvui.update(p.CTRL_WINDOW_NAME) cv2.imshow(p.CTRL_WINDOW_NAME, ctrl_frame) cv2.waitKey(1) if save_video: vout.release()
def main(): frame = np.zeros((300, 600, 3), np.uint8) # Init cvui and tell it to create a OpenCV window, i.e. cv::namedWindow(WINDOW_NAME). cvui.init(WINDOW_NAME) # Rectangle to be rendered according to mouse interactions. rectangle = cvui.Rect(0, 0, 0, 0) while (True): # Fill the frame with a nice color frame[:] = (49, 52, 49) # Show the coordinates of the mouse pointer on the screen cvui.text( frame, 10, 30, 'Click (any) mouse button and drag the pointer around to select an area.' ) cvui.printf(frame, 10, 50, 'Mouse pointer is at (%d,%d)', cvui.mouse().x, cvui.mouse().y) # The function "bool cvui.mouse(int query)" allows you to query the mouse for events. # E.g. cvui.mouse(cvui.DOWN) # # Available queries: # - cvui.DOWN: any mouse button was pressed. cvui.mouse() returns true for a single frame only. # - cvui.UP: any mouse button was released. cvui.mouse() returns true for a single frame only. # - cvui.CLICK: any mouse button was clicked (went down then up, no matter the amount of frames in between). cvui.mouse() returns true for a single frame only. # - cvui.IS_DOWN: any mouse button is currently pressed. cvui.mouse() returns true for as long as the button is down/pressed. # Did any mouse button go down? if cvui.mouse(cvui.DOWN): # Position the rectangle at the mouse pointer. rectangle.x = cvui.mouse().x rectangle.y = cvui.mouse().y # Is any mouse button down (pressed)? if cvui.mouse(cvui.IS_DOWN): # Adjust rectangle dimensions according to mouse pointer rectangle.width = cvui.mouse().x - rectangle.x rectangle.height = cvui.mouse().y - rectangle.y # Show the rectangle coordinates and size cvui.printf(frame, rectangle.x + 5, rectangle.y + 5, 0.3, 0xff0000, '(%d,%d)', rectangle.x, rectangle.y) cvui.printf(frame, cvui.mouse().x + 5, cvui.mouse().y + 5, 0.3, 0xff0000, 'w:%d, h:%d', rectangle.width, rectangle.height) # Did any mouse button go up? if cvui.mouse(cvui.UP): # Hide the rectangle rectangle.x = 0 rectangle.y = 0 rectangle.width = 0 rectangle.height = 0 # Was the mouse clicked (any button went down then up)? if cvui.mouse(cvui.CLICK): cvui.text(frame, 10, 70, 'Mouse was clicked!') # Render the rectangle cvui.rect(frame, rectangle.x, rectangle.y, rectangle.width, rectangle.height, 0xff0000) # This function must be called *AFTER* all UI components. It does # all the behind the scenes magic to handle mouse clicks, etc, then # shows the frame in a window like cv2.imshow() does. cvui.imshow(WINDOW_NAME, frame) # Check if ESC key was pressed if cv2.waitKey(20) == 27: break
def main(): frame = np.zeros((300, 600, 3), np.uint8) # Init cvui and tell it to create a OpenCV window, i.e. cv::namedWindow(WINDOW_NAME). cvui.init(WINDOW_NAME); # Rectangle to be rendered according to mouse interactions. rectangle = cvui.Rect(0, 0, 0, 0) while (True): # Fill the frame with a nice color frame[:] = (49, 52, 49) # Show the coordinates of the mouse pointer on the screen cvui.text(frame, 10, 30, 'Click (any) mouse button and drag the pointer around to select an area.') cvui.printf(frame, 10, 50, 'Mouse pointer is at (%d,%d)', cvui.mouse().x, cvui.mouse().y) # The function "bool cvui.mouse(int query)" allows you to query the mouse for events. # E.g. cvui.mouse(cvui.DOWN) # # Available queries: # - cvui.DOWN: any mouse button was pressed. cvui.mouse() returns true for a single frame only. # - cvui.UP: any mouse button was released. cvui.mouse() returns true for a single frame only. # - cvui.CLICK: any mouse button was clicked (went down then up, no matter the amount of frames in between). cvui.mouse() returns true for a single frame only. # - cvui.IS_DOWN: any mouse button is currently pressed. cvui.mouse() returns true for as long as the button is down/pressed. # Did any mouse button go down? if cvui.mouse(cvui.DOWN): # Position the rectangle at the mouse pointer. rectangle.x = cvui.mouse().x rectangle.y = cvui.mouse().y # Is any mouse button down (pressed)? if cvui.mouse(cvui.IS_DOWN): # Adjust rectangle dimensions according to mouse pointer rectangle.width = cvui.mouse().x - rectangle.x rectangle.height = cvui.mouse().y - rectangle.y # Show the rectangle coordinates and size cvui.printf(frame, rectangle.x + 5, rectangle.y + 5, 0.3, 0xff0000, '(%d,%d)', rectangle.x, rectangle.y) cvui.printf(frame, cvui.mouse().x + 5, cvui.mouse().y + 5, 0.3, 0xff0000, 'w:%d, h:%d', rectangle.width, rectangle.height) # Did any mouse button go up? if cvui.mouse(cvui.UP): # Hide the rectangle rectangle.x = 0 rectangle.y = 0 rectangle.width = 0 rectangle.height = 0 # Was the mouse clicked (any button went down then up)? if cvui.mouse(cvui.CLICK): cvui.text(frame, 10, 70, 'Mouse was clicked!') # Render the rectangle cvui.rect(frame, rectangle.x, rectangle.y, rectangle.width, rectangle.height, 0xff0000) # This function must be called *AFTER* all UI components. It does # all the behind the scenes magic to handle mouse clicks, etc, then # shows the frame in a window like cv2.imshow() does. cvui.imshow(WINDOW_NAME, frame) # Check if ESC key was pressed if cv2.waitKey(20) == 27: break
def mouse_click_entryexit(): if (cvui.mouse(cvui.LEFT_BUTTON, cvui.CLICK)): if (cvui.mouse().x >= layout_space_video and cvui.mouse().x <= layout_space_video + video_size_x and cvui.mouse().y >= layout_space_video and cvui.mouse().y <= layout_space_video + video_size_y): print("Adding entry/exit points ", (cvui.mouse().x - layout_space_video, cvui.mouse().y - layout_space_video)) new_epoints.append((cvui.mouse().x - layout_space_video, cvui.mouse().y - layout_space_video)) if (cvui.mouse(cvui.RIGHT_BUTTON, cvui.CLICK)): if (cvui.mouse().x >= layout_space_video and cvui.mouse().x <= layout_space_video + video_size_x and cvui.mouse().y >= layout_space_video and cvui.mouse().y <= layout_space_video + video_size_y and len(epoints) > 0): if (len(new_epoints) >= 1): print("Deleting entry/exit points") del new_epoints[-1] fill_poligon = False if (new_estringLabel): # erase stringLabel new_estringLabel.clear() #if(len(new_epoints)>2): #set direction #draw arrow #elif(len(new_epoints)>4): if (len(new_epoints) > 4): setEntryExit = 0
def findWells(videoPath, hyperparameters): if hyperparameters["noWellDetection"]: cap = cv2.VideoCapture(videoPath) if (cap.isOpened()== False): print("Error opening video stream or file") frame_width = int(cap.get(3)) frame_height = int(cap.get(4)) l = [] well = { 'topLeftX' : 0 , 'topLeftY' : 0 , 'lengthX' : frame_width , 'lengthY': frame_height } l.append(well) return l # Multiple ROIs defined by user during the execution if int(hyperparameters["multipleROIsDefinedDuringExecution"]): l = [] cap = cv2.VideoCapture(videoPath) if (cap.isOpened()== False): print("Error opening video stream or file") ret, frame = cap.read() [frame, getRealValueCoefX, getRealValueCoefY, horizontal, vertical] = resizeImageTooLarge(frame, True, 0.85) cv2.waitKey(500) for i in range(0, int(hyperparameters["nbWells"])): WINDOW_NAME = "Click on the top left of one of the regions of interest" cvui.init(WINDOW_NAME) cv2.moveWindow(WINDOW_NAME, 0,0) cvui.imshow(WINDOW_NAME, frame) while not(cvui.mouse(cvui.CLICK)): cursor = cvui.mouse() cv2.waitKey(20) topLeft = [cursor.x, cursor.y] cv2.destroyWindow(WINDOW_NAME) WINDOW_NAME = "Click on the bottom right of the same region of interest" cvui.init(WINDOW_NAME) cv2.moveWindow(WINDOW_NAME, 0,0) cvui.imshow(WINDOW_NAME, frame) while not(cvui.mouse(cvui.CLICK)): cursor = cvui.mouse() cv2.waitKey(20) bottomRight = [cursor.x, cursor.y] frame = cv2.rectangle(frame, (topLeft[0], topLeft[1]), (bottomRight[0], bottomRight[1]), (255, 0, 0), 1) cv2.destroyWindow(WINDOW_NAME) frame_width = int(getRealValueCoefX * (bottomRight[0] - topLeft[0])) frame_height = int(getRealValueCoefY * (bottomRight[1] - topLeft[1])) well = {'topLeftX' : int(getRealValueCoefX * topLeft[0]), 'topLeftY' : int(getRealValueCoefY * topLeft[1]), 'lengthX' : frame_width, 'lengthY': frame_height} l.append(well) cap.release() return l # One ROI definied in the configuration file if len(hyperparameters["oneWellManuallyChosenTopLeft"]): l = [] topLeft_X = hyperparameters["oneWellManuallyChosenTopLeft"][0] topLeft_Y = hyperparameters["oneWellManuallyChosenTopLeft"][1] bottomRight_X = hyperparameters["oneWellManuallyChosenBottomRight"][0] bottomRight_Y = hyperparameters["oneWellManuallyChosenBottomRight"][1] frame_width = bottomRight_X - topLeft_X frame_height = bottomRight_Y - topLeft_Y well = { 'topLeftX' : topLeft_X , 'topLeftY' : topLeft_Y , 'lengthX' : frame_width , 'lengthY': frame_height } l.append(well) return l # Circular or rectangular wells cap = cv2.VideoCapture(videoPath) if (cap.isOpened()== False): print("Error opening video stream or file") ret, frame = cap.read() if hyperparameters["invertBlackWhiteOnImages"]: frame = 255 - frame if hyperparameters["wellsAreRectangles"]: if hyperparameters["adjustRectangularWellsDetect"]: rectangularWellsArea = findRectangularWellsArea(frame, videoPath, hyperparameters) initializeAdjustHyperparametersWindows("Rectangular Wells Detection: Adjust parameters until you get the right number of wells detected (on the right side)") else: rectangularWellsArea = hyperparameters["findRectangleWellArea"] wellPositions = findRectangularWells(frame, videoPath, hyperparameters, rectangularWellsArea) else: wellPositions = findCircularWells(frame, videoPath, hyperparameters) cap.release() # Sorting wells nbWellsPerRows = hyperparameters["nbWellsPerRows"] nbRowsOfWells = hyperparameters["nbRowsOfWells"] if len(wellPositions) >= nbWellsPerRows * nbRowsOfWells: for i in range(0, len(wellPositions)): for j in range(0, len(wellPositions)-1): if wellPositions[j]['topLeftY'] > wellPositions[j+1]['topLeftY']: aux = wellPositions[j] wellPositions[j] = wellPositions[j+1] wellPositions[j+1] = aux if (nbRowsOfWells == 0): for i in range(0, len(wellPositions)): for j in range(0, len(wellPositions)-1): if wellPositions[j]['topLeftX'] > wellPositions[j+1]['topLeftX']: aux = wellPositions[j] wellPositions[j] = wellPositions[j+1] wellPositions[j+1] = aux else: for k in range(0, nbRowsOfWells): for i in range(nbWellsPerRows*k, nbWellsPerRows*(k+1)): for j in range(nbWellsPerRows*k, nbWellsPerRows*(k+1)-1): if wellPositions[j]['topLeftX'] > wellPositions[j+1]['topLeftX']: aux = wellPositions[j] wellPositions[j] = wellPositions[j+1] wellPositions[j+1] = aux else: print("Not enough wells detected, please adjust your configuration file") # Creating validation image rectangleTickness = 2 lengthY = len(frame) lengthX = len(frame[0]) if len(wellPositions): perm1 = np.random.permutation(len(wellPositions)) * int(255/len(wellPositions)) perm2 = np.random.permutation(len(wellPositions)) * int(255/len(wellPositions)) perm3 = np.random.permutation(len(wellPositions)) * int(255/len(wellPositions)) for i in range(0, len(wellPositions)): if hyperparameters["wellsAreRectangles"]: topLeft = (wellPositions[i]['topLeftX'], wellPositions[i]['topLeftY']) bottomRight = (wellPositions[i]['topLeftX'] + wellPositions[i]['lengthX'], wellPositions[i]['topLeftY'] + wellPositions[i]['lengthY']) color = (int(perm1[i]), int(perm2[i]), int(perm3[i])) frame = cv2.rectangle(frame, topLeft, bottomRight, color, rectangleTickness) else: cv2.circle(frame, (int(wellPositions[i]['topLeftX'] + wellPositions[i]['lengthX'] / 2), int(wellPositions[i]['topLeftY'] + wellPositions[i]['lengthY'] / 2)), 170, (0,0,255), 2) cv2.putText(frame, str(i), (int(wellPositions[i]['topLeftX'] + wellPositions[i]['lengthX'] / 2), int(wellPositions[i]['topLeftY'] + wellPositions[i]['lengthY'] / 2)), cv2.FONT_HERSHEY_SIMPLEX, 4,(255,255,255),2,cv2.LINE_AA) frame = cv2.resize(frame, (int(lengthX/2), int(lengthY/2))) # ??? cv2.imwrite(os.path.join(os.path.join(hyperparameters["outputFolder"], hyperparameters["videoName"]), "repartition.jpg"), frame ) if hyperparameters["debugFindWells"]: frame2 = frame.copy() [frame2, getRealValueCoefX, getRealValueCoefY, horizontal, vertical] = resizeImageTooLarge(frame2) cv2.imshow('Wells Detection', frame2) if hyperparameters["exitAfterBackgroundExtraction"]: cv2.waitKey(3000) else: cv2.waitKey(0) cv2.destroyWindow('Wells Detection') print("Wells found") if hyperparameters["popUpAlgoFollow"]: popUpAlgoFollow.prepend("Wells found") return wellPositions