class MainView(ZApplicationView): # Constructor def __init__(self, title, x, y, width, height, device=0): super(MainView, self).__init__(title, x, y, width, height) self.video_capture = ZOpenCVVideoCapture() self.video_capture.open(device) self.image_view = ZOpenCVImageView(self) self.add(self.image_view) title = str(device) + " - " + name self.setWindowTitle(title) self.show() # Read a frame from a video buffer of the video capture, and set it the image view to draw it on the imag view def render(self): if self.video_capture.is_opened(): frame = self.video_capture.read() if frame.any() != None: self.image_view.set_opencv_image(frame) self.image_view.update() return True else: # If the video_capture closed, return False return False def file_quit(self): self.terminated = True self.video_capture.close() self.close()
class MainView(ZApplicationView): # Constructor def __init__(self, title, x, y, width, height, device=0): super(MainView, self).__init__(title, x, y, width, height) self.video_capture = ZOpenCVVideoCapture() self.video_capture.open(device) self.image_view = ZOpenCVImageView(self) self.add(self.image_view) title = str(device) self.set_filenamed_title(title) self.show() # Read a frame from a video buffer of the video capture, and set it the image view to draw it on the imag view def render(self): if self.video_capture.is_opened(): frame = self.video_capture.read() if frame.any() != None: self.image_view.set_opencv_image(frame) self.image_view.update() return True else: # If the video_capture closed, return False return False # Show FileOpenDialog and select an image file. def file_open(self): options = QFileDialog.Options() filename, _ = QFileDialog.getOpenFileName( self, "FileOpenDialog", "", "All Files (*);;Image Files (*.png;*jpg;*.jpeg)", options=options) if filename: self.video_capture.close() self.video_capture.open(filename) self.set_filenamed_title(filename) def file_quit(self): self.terminated = True self.video_capture.close() self.close()
class MainView(ZApplicationView): class BinarizedImageView(ZOpenCVImageView): def __init__(self, parent): ZOpenCVImageView.__init__(self, parent) # Constructor def __init__(self, title, x, y, width, height): super(MainView, self).__init__(title, x, y, width, height) self.raw_video_queue = mp.Queue() self.to_binarize_video_queue = mp.Queue() self.binarized_video_queue = mp.Queue() # Binarize initial settings #self.block_size = 11 #self.adaptive_method_id = 0; #self.threshold_type_id = 0; self.binarize_video_settings_queue = mp.Queue() #binarize_video_settings_queue.put_nowait((self.block_size, \ # self.adaptive_method_id, \ # self.threshold_type_id)) self.time_snapshot_seconds = None # initialized to None . Used for rate limiting processing pipelines self.processing_rate_limit_hz = 20 # 30 Hz seems to be about the maximum if this camera self.raw_image_view = ZOpenCVImageView(self) self.binarized_image_view = self.BinarizedImageView(self) self.add(self.raw_image_view) self.add(self.binarized_image_view) title = name self.setWindowTitle(title) self.show() def add_control_pane(self, fixed_width=220): # Control pane widget self.vpane = ZVerticalPane(self, fixed_width) #""" self.block_size = 11 self.adaptive_method_id = 0 self.threshold_type_id = 0 #""" self.methods = { "ADAPTIVE_THRESH_MEAN_C": cv2.ADAPTIVE_THRESH_MEAN_C, "ADAPTIVE_THRESH_GAUSSIAN_C": cv2.ADAPTIVE_THRESH_GAUSSIAN_C } self.types = { "THRESH_BINARY": cv2.THRESH_BINARY, "THRESH_BINARY_INV": cv2.THRESH_BINARY_INV } self.adaptive_method = ZLabeledComboBox(self.vpane, "AdaptiveMethod") self.adaptive_method.add_items(list(self.methods.keys())) self.adaptive_method.add_activated_callback( self.adaptive_method_activated) self.threshold_type = ZLabeledComboBox(self.vpane, "ThresholdType") self.threshold_type.add_items(list(self.types.keys())) self.threshold_type.add_activated_callback( self.threshold_type_activated) self.labeled_slider = ZLabeledSlider(self.vpane, "BlockSize", take_odd=True, minimum=3, maximum=43, value=self.block_size, fixed_width=200) self.labeled_slider.add_value_changed_callback( self.slider_value_changed) self.vpane.add(self.adaptive_method) self.vpane.add(self.threshold_type) self.vpane.add(self.labeled_slider) self.set_right_dock(self.vpane) def slider_value_changed(self, value): self.block_size = int(value) if self.block_size % 2 == 0: # Block size should be odd. self.block_size = int((self.block_size * 2) / 2 + 1) #print("slider_value_changed:{}".format(block_size)) #self.binarized_image_view.binarize(self.adaptive_method_id, self.threshold_type_id, self.block_size) settings = (self.adaptive_method_id, self.threshold_type_id, self.block_size) self.binarize_video_settings_queue.put_nowait(settings) def adaptive_method_activated(self, text): #print("adaptive_method_activated:{}".format(text)) self.adaptive_method_id = self.methods[text] #self.binarized_image_view.binarize(self.adaptive_method_id, self.threshold_type_id, self.block_size) settings = (self.adaptive_method_id, self.threshold_type_id, self.block_size) self.binarize_video_settings_queue.put_nowait(settings) def threshold_type_activated(self, text): #print("threshold_type_activated:{}".format(text)) self.threshold_type_id = self.types[text] #self.binarized_image_view.binarize(self.adaptive_method_id, self.threshold_type_id, self.block_size) settings = (self.adaptive_method_id, self.threshold_type_id, self.block_size) self.binarize_video_settings_queue.put_nowait(settings) # Read a frame from a video buffer of the video capture, and set it the image view to draw it on the imag view def render(self): return_true = False #return_true = True #""" if not self.raw_video_queue.empty(): raw_frame = self.raw_video_queue.get_nowait() if raw_frame.any() != None: self.raw_image_view.set_opencv_image(raw_frame) self.raw_image_view.update() # Send frame to processing queue # rate limited current_time_seconds = time() if self.time_snapshot_seconds == None: self.time_snapshot_seconds = current_time_seconds if current_time_seconds > self.time_snapshot_seconds: # Avoid dividing by zero #print(1/(current_time_seconds-self.time_snapshot_seconds)) if ((1 / (current_time_seconds - self.time_snapshot_seconds)) <= self.processing_rate_limit_hz): # Rate limit #print("Sending raw frame for further processing!") self.to_binarize_video_queue.put_nowait(raw_frame) self.time_snapshot_seconds = current_time_seconds return_true = True if not self.binarized_video_queue.empty(): binarized_frame = self.binarized_video_queue.get_nowait() if binarized_frame.any() != None: self.binarized_image_view.set_opencv_image(binarized_frame) self.binarized_image_view.update() return_true = True #""" if return_true: return True else: return False def file_quit(self): self.terminated = True def closeEvent(self, ce): self.terminated = True
class MainView(ZApplicationView): class BinarizedImageView(ZOpenCVImageView): def __init__(self, parent): ZOpenCVImageView.__init__(self, parent) def load_source_image(self, source_image): image = cv2.GaussianBlur(source_image, (3,3), 0, 0, borderType = cv2.BORDER_DEFAULT ); #image = cv2.GaussianBlur(source_image, (3,3), 0, 0) self.gray_image = cv2.cvtColor(source_image, cv2.COLOR_RGB2GRAY) def binarize(self, adaptive_method_id, threshold_type_id, block_size): MAX_PIXEL_VALUE = 255 C = 9.0 binarized_image = cv2.adaptiveThreshold(self.gray_image, MAX_PIXEL_VALUE, adaptive_method_id, threshold_type_id, block_size, C); #return binarized_image self.set_opencv_image(binarized_image) self.update() # Constructor def __init__(self, title, x, y, width, height): super(MainView, self).__init__(title, x, y, width, height) self.raw_video_queue = mp.Queue() self.raw_image_view = ZOpenCVImageView(self) self.binarized_image_view = self.BinarizedImageView(self) self.add(self.raw_image_view) self.add(self.binarized_image_view) title = name self.setWindowTitle(title) self.show() def add_control_pane(self, fixed_width=220): # Control pane widget self.vpane = ZVerticalPane(self, fixed_width) self.block_size = 11 self.adaptive_method_id = 0; self.threshold_type_id = 0; self.methods = {"ADAPTIVE_THRESH_MEAN_C": cv2.ADAPTIVE_THRESH_MEAN_C, "ADAPTIVE_THRESH_GAUSSIAN_C": cv2.ADAPTIVE_THRESH_GAUSSIAN_C} self.types = {"THRESH_BINARY": cv2.THRESH_BINARY , "THRESH_BINARY_INV": cv2.THRESH_BINARY_INV } self.adaptive_method = ZLabeledComboBox(self.vpane, "AdaptiveMethod") self.adaptive_method.add_items(list(self.methods.keys() )) self.adaptive_method.add_activated_callback(self.adaptive_method_activated) self.threshold_type = ZLabeledComboBox(self.vpane, "ThresholdType") self.threshold_type.add_items(list(self.types.keys()) ) self.threshold_type.add_activated_callback(self.threshold_type_activated) self.labeled_slider = ZLabeledSlider(self.vpane, "BlockSize", take_odd =True, minimum=3, maximum=43, value=self.block_size, fixed_width=200) self.labeled_slider.add_value_changed_callback(self.slider_value_changed) self.vpane.add(self.adaptive_method) self.vpane.add(self.threshold_type) self.vpane.add(self.labeled_slider) self.set_right_dock(self.vpane) def slider_value_changed(self, value): self.block_size = int(value) if self.block_size % 2 == 0: # Block size should be odd. self.block_size = int((self.block_size * 2)/2 + 1) #print("slider_value_changed:{}".format(block_size)) self.binarized_image_view.binarize(self.adaptive_method_id, self.threshold_type_id, self.block_size) def adaptive_method_activated(self, text): #print("adaptive_method_activated:{}".format(text)) self.adaptive_method_id = self.methods[text] self.binarized_image_view.binarize(self.adaptive_method_id, self.threshold_type_id, self.block_size) def threshold_type_activated(self, text): #print("threshold_type_activated:{}".format(text)) self.threshold_type_id = self.types[text] self.binarized_image_view.binarize(self.adaptive_method_id, self.threshold_type_id, self.block_size) # Read a frame from a video buffer of the video capture, and set it the image view to draw it on the imag view def render(self): #return_true = False if not self.raw_video_queue.empty(): raw_frame = self.raw_video_queue.get_nowait() if raw_frame.any() != None: self.raw_image_view.set_opencv_image(raw_frame) self.raw_image_view.update() self.binarized_image_view.load_source_image(raw_frame) self.binarized_image_view.binarize(self.adaptive_method_id, self.threshold_type_id, self.block_size) return True """ if not self.binarized_video_queue.empty(): binarized_frame = self.binarized_video_queue.get_notwait() if binarized_frame.any() != None: self.binarized_image_view.set_opencv_image(binarized_frame) self.binarized_image_view.update() return_true = True if return_true: return True else: return False """ return False """ def load_file(self, filename): self.image_view_3.load(filename) self.set_filenamed_title(filename) """ def file_quit(self): self.terminated = True def closeEvent(self, ce): self.terminated = True
class MainView(ZApplicationView): class SourceImageView(ZOpenCVImageView): def __init__(self, parent): ZOpenCVImageView.__init__(self, parent) def load(self, filename): self.load_opencv_image(filename) self.update() """ class SourceImageView_1(ZOpenCVImageView): def __init__(self, parent): ZOpenCVImageView.__init__(self, parent) class SourceImageView_2(ZOpenCVImageView): def __init__(self, parent): ZOpenCVImageView.__init__(self, parent) """ # Constructor def __init__(self, title, x, y, width, height, device=0): super(MainView, self).__init__(title, x, y, width, height) self.raw_video_queue = mp.Queue() self.message_queue = mp.Queue() """ while queue_from_cam.empty(): pass print 'getting image' from_queue = queue_from_cam.get() print 'saving image' cv2.imwrite('temp.png', from_queue) print 'image saved' """ #self.video_capture = ZOpenCVVideoCapture() #self.video_capture.open(device) self.image_view = ZOpenCVImageView(self) #self.image_view = self.SourceImageView_1(self) #self.image_view = self.SourceImageView(self) #filename = "images/flower.png" #self.image_view_2 = self.SourceImageView(self) #self.image_view_2 = self.SourceImageView_2(self) self.image_view_2 = ZOpenCVImageView(self) #self.load_file(filename) filename = "images/flower.png" self.image_view_3 = self.SourceImageView(self) self.load_file(filename) self.add(self.image_view) self.add(self.image_view_2) self.add(self.image_view_3) title = str(device) + " - " + name self.setWindowTitle(title) self.show() # Read a frame from a video buffer of the video capture, and set it the image view to draw it on the imag view def render(self): if not self.raw_video_queue.empty(): #frame = self.raw_video_queue.get_nowait() frame = self.raw_video_queue.get() """ if self.video_capture.is_opened(): frame = self.video_capture.read() """ if frame.any() != None: self.image_view.set_opencv_image(frame) self.image_view.update() self.image_view_2.set_opencv_image(frame) self.image_view_2.update() return True else: # If the video_capture closed, return False return False def load_file(self, filename): self.image_view_3.load(filename) self.set_filenamed_title(filename) def file_quit(self): self.terminated = True #self.video_capture.close() #self.message_queue.put("exit") #self.message_queue.put_nowait("exit") print("2") #sleep(5) #while not self.raw_video_queue.empty(): # frame = self.raw_video_queue.get_nowait() #self.close() print("3") #self.terminated = True def closeEvent(self, ce): self.terminated = True #self.video_capture.close() #self.message_queue.put_nowait("exit") print("2") #sleep(5) #while not self.raw_video_queue.empty(): # frame = self.raw_video_queue.get_nowait() #self.close() print("3")
class MainView(ZApplicationView): # Constructor def __init__(self, title, x, y, width, height): super(MainView, self).__init__(title, x, y, width, height, layout=Z.Grid) self.raw_video_queue = Queue() self.to_dft_video_queue = Queue() self.dft_settings_queue = Queue() self.dft_settings_queue.put_nowait([30]) # temporary self.dft_magnitude_unfiltered_video_queue = Queue() self.dft_magnitude_filtered_video_queue = Queue() self.filtered_video_queue = Queue() self.time_snapshot_seconds = None # initialized to None . Used for rate limiting processing pipelines self.processing_rate_limit_hz = 5 self.raw_image_view = ZOpenCVImageView(self) self.dft_magnitude_unfiltered_image_view = ZOpenCVImageView(self) self.dft_magnitude_filtered_image_view = ZOpenCVImageView(self) self.filtered_image_view = ZOpenCVImageView(self) #self.grid = ZGridLayouter(self) self.main_layouter.add(self.raw_image_view, 0, 0) self.main_layouter.add(self.dft_magnitude_unfiltered_image_view, 0, 1) self.main_layouter.add(self.dft_magnitude_filtered_image_view, 1, 1) self.main_layouter.add(self.filtered_image_view, 1, 0) title = name self.setWindowTitle(title) self.show() #""" def add_control_pane(self, fixed_width=220): # Control pane widget self.vpane = ZVerticalPane(self, fixed_width) self.filter_width_div_two = 30 #self.adaptive_method_id = 0; #self.threshold_type_id = 0; """ self.methods = {"ADAPTIVE_THRESH_MEAN_C": cv2.ADAPTIVE_THRESH_MEAN_C, "ADAPTIVE_THRESH_GAUSSIAN_C": cv2.ADAPTIVE_THRESH_GAUSSIAN_C} self.types = {"THRESH_BINARY": cv2.THRESH_BINARY , "THRESH_BINARY_INV": cv2.THRESH_BINARY_INV } self.adaptive_method = ZLabeledComboBox(self.vpane, "AdaptiveMethod") self.adaptive_method.add_items(list(self.methods.keys() )) self.adaptive_method.add_activated_callback(self.adaptive_method_activated) self.threshold_type = ZLabeledComboBox(self.vpane, "ThresholdType") self.threshold_type.add_items(list(self.types.keys()) ) self.threshold_type.add_activated_callback(self.threshold_type_activated) """ self.labeled_slider = ZLabeledSlider(self.vpane, "Filter width // 2 ", take_odd=True, minimum=0, maximum=480 // 2, value=self.filter_width_div_two, fixed_width=200) self.labeled_slider.add_value_changed_callback( self.slider_value_changed) #self.vpane.add(self.adaptive_method) #self.vpane.add(self.threshold_type) self.vpane.add(self.labeled_slider) #self.set_right_dock(self.vpane) #self.grid = ZGridLayouter(self) self.main_layouter.add(self.vpane, 1, 2) def slider_value_changed(self, value): self.block_size = int(value) #if self.block_size % 2 == 0: # # Block size should be odd. # self.block_size = int((self.block_size * 2)/2 + 1) #print("slider_value_changed:{}".format(block_size)) #self.binarized_image_view.binarize(self.adaptive_method_id, self.threshold_type_id, self.block_size) """ # Send frame to processing queue rate limited current_time_seconds = time() if self.time_snapshot_seconds == None: self.time_snapshot_seconds = current_time_seconds if ((1/(current_time_seconds-self.time_snapshot_seconds)) <= self.processing_rate_limit_hz): # Rate limit """ settings = [self.block_size] self.dft_settings_queue.put_nowait(settings) #""" """ def adaptive_method_activated(self, text): #print("adaptive_method_activated:{}".format(text)) self.adaptive_method_id = self.methods[text] #self.binarized_image_view.binarize(self.adaptive_method_id, self.threshold_type_id, self.block_size) settings = (self.adaptive_method_id, self.threshold_type_id, self.block_size) self.binarize_video_settings_queue.put_nowait(settings) def threshold_type_activated(self, text): #print("threshold_type_activated:{}".format(text)) self.threshold_type_id = self.types[text] #self.binarized_image_view.binarize(self.adaptive_method_id, self.threshold_type_id, self.block_size) settings = (self.adaptive_method_id, self.threshold_type_id, self.block_size) self.binarize_video_settings_queue.put_nowait(settings) """ # Read a frame from a video buffer of the video capture, and set it the image view to draw it on the imag view def render(self): return_true = False if not self.raw_video_queue.empty(): raw_frame = self.raw_video_queue.get_nowait() if raw_frame.any() != None: self.raw_image_view.set_opencv_image(raw_frame) self.raw_image_view.update() # Send frame to processing queue rate limited current_time_seconds = time() if self.time_snapshot_seconds == None: self.time_snapshot_seconds = current_time_seconds if current_time_seconds > self.time_snapshot_seconds: # Avoid dividing by zero #print(1/(current_time_seconds-self.time_snapshot_seconds)) if ((1 / (current_time_seconds - self.time_snapshot_seconds)) <= self.processing_rate_limit_hz): # Rate limit #print("Sending raw frame for further processing!") self.to_dft_video_queue.put_nowait(raw_frame) self.time_snapshot_seconds = current_time_seconds return_true = True if not self.dft_magnitude_unfiltered_video_queue.empty(): dft_magnitude_unfiltered_frame = \ self.dft_magnitude_unfiltered_video_queue.get_nowait() if dft_magnitude_unfiltered_frame.any() != None: self.dft_magnitude_unfiltered_image_view.set_opencv_image( np.uint8(dft_magnitude_unfiltered_frame)) self.dft_magnitude_unfiltered_image_view.update() return_true = True if not self.dft_magnitude_filtered_video_queue.empty(): dft_magnitude_filtered_frame = \ self.dft_magnitude_filtered_video_queue.get_nowait() if dft_magnitude_filtered_frame.any() != None: self.dft_magnitude_filtered_image_view.set_opencv_image( np.uint8(dft_magnitude_filtered_frame)) self.dft_magnitude_filtered_image_view.update() return_true = True if not self.filtered_video_queue.empty(): filtered_video_frame = \ self.filtered_video_queue.get_nowait() if filtered_video_frame.any() != None: self.filtered_image_view.set_opencv_image( np.uint8(filtered_video_frame)) self.filtered_image_view.update() return_true = True if return_true: return True else: return False def file_quit(self): self.terminated = True def closeEvent(self, ce): self.terminated = True