def roi_callback(self): """ Called when a roi is drawn on an image_widget. Edit annotation if possible. """ if self.class_id == -1 or self.label == "": warning_dialog("Warning", "select label first") self.changes_done = True return if self.cur_annotation_index < 0: warning_dialog("Warning", "select annotation first") self.changes_done = True return # set bounding box x_center, y_center, width, height = self.sel_im_widget.get_normalized_roi( ) annotation = self.cur_annotated_image.annotation_list[ self.cur_annotation_index] annotation.bbox = utils.BoundingBox(x_center, y_center, width, height) # set selected label self.label = self.option_selector.currentText() if self.labels is None or len(self.labels) == 0: return self.class_id = [i[0] for i in self.labels].index(self.label) annotation.label = self.class_id self.set_annotation_list() self.show_image_from_workspace() self.sel_im_widget.clear() self.changes_done = True
def recognize_srv_call(self, roi_image): """ Method that calls the Recognize.srv :param roi_image: Selected roi_image by the user """ try: result = self._srv( image=self.bridge.cv2_to_imgmsg(roi_image, "bgr8")) except Exception as e: warning_dialog("Service Exception", str(e)) return print result for r in result.recognitions: text_array = [] best = CategoryProbability( label="unknown", probability=r.categorical_distribution.unknown_probability) for p in r.categorical_distribution.probabilities: text_array.append("%s: %.2f" % (p.label, p.probability)) if p.probability > best.probability: best = p self._image_widget.add_detection(r.roi.x_offset, r.roi.y_offset, r.roi.width, r.roi.height, best.label) if text_array: option_dialog( "Classification results (Unknown probability=%.2f)" % r.categorical_distribution.unknown_probability, text_array) # Show all results in a dropdown
def remove_current_annotation(self): """ Remove currently selected annotation """ if self.cur_annotation_index == -1: warning_dialog("Warning", "no annotation selected") return del (self.cur_annotated_image.annotation_list[ self.cur_annotation_index]) self.set_annotation_list()
def generate_augmented_data(self): if self.gen_dir is None: warning_dialog("Warning", "Set parameters first") return data_augmentation.multiply_dataset(self.workspace, self.gen_dir, None, 0, self.num_illuminate, self.num_scale, self.num_blur, 0) print("data augmentation done")
def get_persons_srv_call(self, roi_image): """ Method that calls the GetPersons.srv :param roi_image: Selected roi_image by the user """ try: result = self._srv(image=self.bridge.cv2_to_imgmsg(roi_image, "bgr8")) except Exception as e: warning_dialog("Service Exception", str(e)) return print result info_dialog("Persons", str(result))
def detect_srv_call(self, image): """ Method that calls the DetectObjects.srv :param roi_image: Selected roi_image by the user """ detectReq = Detect2DRequest() detectReq.image = self.bridge.cv2_to_imgmsg(image, "bgr8") try: result = self._srv_detect(detectReq) except Exception as e: warning_dialog("Service Exception", str(e)) return
def export_workspace_to_tf(self): """ Export workspace to training formats. """ if self.default_config_path is None or self.pretrained_graph is None: warning_dialog("Warning", "define default config and pretrained graph first") return tf_utils.export_data_to_tf(self.workspace, self.images_with_annotations, self.labels, self.p_test, self.default_config_path, self.batch_size, self.pretrained_graph, True) tf_utils.create_roi_images(self.workspace, self.images_with_annotations, self.labels) print("Export done")
def image_roi_callback(self, roi_image): """ Callback triggered when the user has drawn an ROI on the image :param roi_image: The opencv image in the ROI """ if not self.labels: warning_dialog("No labels specified!", "Please first specify some labels using the 'Edit labels' button") return height, width = roi_image.shape[:2] option = option_dialog("Label", self.labels) if option: self._image_widget.add_detection(0, 0, width, height, option) self._stage_recognition(self._image_widget.get_roi(), option)
def add_annotation(self): """ Add annotation to current image. Bounding box is just a dummy. Use current label. """ if self.class_id == -1 or self.label == "": warning_dialog("Warning", "select label first") return if self.cur_annotated_image is None: warning_dialog("Warning", "select image first") return label = self.option_selector.currentText() annotation = utils.AnnotationWithBbox(self.class_id, 1.0, 0.5, 0.5, 1, 1) self.cur_annotated_image.annotation_list.append(annotation) index = len(self.cur_annotated_image.annotation_list) - 1 self.add_annotation_to_list_widget(index, label, True) self.changes_done = True
def image_roi_callback(self, roi_image): if not self.labels: warning_dialog( "No labels specified!", "Please first specify some labels using the 'Edit labels' button" ) return self.roi_image = roi_image option = option_dialog("Label", self.labels) if option: self.label = option self._image_widget.set_text(option) self.store_image()
def grab_frame(self): """ Grab current frame, save to workspace and show it.""" if self.workspace is None or self.workspace == "": warning_dialog("Warning", "select workspace first") return cv_image = self.cur_im_widget.get_image() if cv_image is None: warning_dialog("warning", "no frame to grab") return file_name = "/images/{}.jpg".format( datetime.datetime.now().strftime("%Y-%m-%d-%H-%M-%S_%f")) new_image = utils.AnnotatedImage(self.workspace + file_name, []) self.cur_annotated_image = new_image self.images_with_annotations.append(new_image) cv2.imwrite(self.workspace + file_name, cv_image) self.add_image_to_list_widget(file_name, True) self.select_image()
def remove_current_image(self): """ Remove currently selected image. Remove ItemWidget and delete files """ if self.cur_annotated_image is None: warning_dialog("Warning", "no image selected") return image_file = self.cur_annotated_image.image_file label_file = image_file.replace("images", "labels").replace("jpg", "txt") self.image_list_widget.removeItemWidget( self.image_list_widget.currentItem()) self.images_with_annotations.remove(self.cur_annotated_image) os.remove(image_file) if os.path.isfile(label_file): os.remove(label_file) self.refresh_image_list_widget()
def add_label(self): """ If label doesn't exist yet, add to the list and combo box. """ new_label = str(self.label_edit.text()) if new_label is None or new_label == "": return for label in self.labels: if label[0] == new_label: warning_dialog("warning", "label\"" + label[0] + "\" already exists") return new_label = list((new_label, 0)) self.labels.append(new_label) self.option_selector.addItem(new_label[0]) label_file = self.workspace + "/labels.txt" utils.write_labels(label_file, self.labels)
def get_face_properties_srv_call(self, roi_image): """ Method that calls the GetFaceProperties.srv :param roi_image: Selected roi_image by the user """ try: result = self._srv(face_image_array=[self.bridge.cv2_to_imgmsg(roi_image, "bgr8")]) except Exception as e: warning_dialog("Service Exception", str(e)) return msg = "" for properties in result.properties_array: msg += "- FaceProperties(gender=%s, age=%s)" % \ ("male" if properties.gender == FaceProperties.MALE else "female", properties.age) info_dialog("Face Properties array", msg)
def annotate_srv(self, roi_image): """ Call the selected Annotate.srv :param roi_image: The full opencv image we want to annotate """ if roi_image is not None and self.label is not None and self._srv is not None: height, width = roi_image.shape[:2] try: self._srv(image=self.bridge.cv2_to_imgmsg(roi_image, "bgr8"), annotations=[ Annotation(label=self.label, roi=RegionOfInterest(x_offset=0, y_offset=0, width=width, height=height)) ]) except Exception as e: warning_dialog("Service Exception", str(e))
def image_roi_callback(self, roi_image): """ Callback triggered when the user has drawn an ROI on the image :param roi_image: The opencv image in the ROI """ if self._srv is None: warning_dialog( "No service specified!", "Please first specify a service via the options button (top-right gear wheel)" ) return if self._srv.service_class == Recognize: self.recognize_srv_call(roi_image) elif self._srv.service_class == GetFaceProperties: self.get_face_properties_srv_call(roi_image) else: warning_dialog("Unknown service class", "Service class is unkown!")
def image_roi_callback(self, roi_image): """ Callback from the image widget when the user has selected a ROI :param roi_image: The opencv image of the ROI """ if not self.labels: warning_dialog( "No labels specified!", "Please first specify some labels using the 'Edit labels' button" ) return height, width = roi_image.shape[:2] option = option_dialog("Label", self.labels) if option: self.label = option self._image_widget.add_detection(0, 0, width, height, option) self.annotate(roi_image)
def image_roi_callback(self, roi_image): if self._srv is None: warning_dialog( "No service specified!", "Please first specify a service via the options button (top-right gear wheel)" ) return try: result = self._srv( image=self.bridge.cv2_to_imgmsg(roi_image, "bgr8")) except Exception as e: warning_dialog("Service Exception", str(e)) return text_array = [ "%s: %.2f" % (r.label, r.probability) for r in result.recognitions ] if text_array: self._image_widget.set_text( text_array[0]) # Show first option in the image option_dialog("Classification results", text_array) # Show all results in a dropdown
def set_export_parameters(self): """ Set variables for export via QInputDialog and QFileDialog. """ # default config path config_path = QFileDialog.getOpenFileName(self.widget, "Select default config") config_path = str(config_path[0]) file, ext = os.path.splitext(config_path) if not ext == ".config": self.default_config_path = None warning_dialog("warning", "invalid file extension") return else: self.default_config_path = config_path # pretrained graph pretrained_graph_dir = QFileDialog.getExistingDirectory( self.widget, "Select pretrained graph directory") self.pretrained_graph = pretrained_graph_dir + "/model.ckpt" # batch size batch_size, ok = QInputDialog.getText(self.widget, "Set batch size", "12") if ok: try: self.batch_size = int(batch_size) except ValueError: pass # test percentage p_test, ok = QInputDialog.getText(self.widget, "Set test percentage", "0.2") if ok: try: self.p_test = float(p_test) except ValueError: pass
def classify_srv_call(self, roi_image): """ Method that calls the Classify2D.srv :param roi_image: Selected roi_image by the user """ try: images = [] image = self.bridge.cv2_to_imgmsg(roi_image, "bgr8") images.append(image) result = self._srv(images) except Exception as e: warning_dialog("Service Exception", str(e)) return # we send one image, so we get max. one result if len(result.classifications) == 0: return c = result.classifications[0] text_array = [] best = ObjectHypothesis(id=0, score=0.0) for r in c.results: if len(self.id_label_list) >= r.id: text_array.append("%s: %.2f" % (self.id_label_list.get(str(r.id)), r.score)) else: text_array.append("%s: %.2f" % ("no_label", r.score)) if r.score > best.score: best = r self._image_widget.add_detection(0, 0, 1, 1, str(best.id)) if text_array: option_dialog("Classification results", text_array) # Show all results in a dropdown
def close(self, doc=None): if not self.docs: return if doc is None: doc = self.current_doc if not self.mw.nb.page_num( doc.docarea) == self.mw.nb.get_current_page(): self.mw.set_active_tab(doc.docarea) if self.inspector.is_doc_not_saved(doc): first = _("Document '%s' has been modified.") % (doc.doc_name) second = _('Do you want to save your changes?') ret = dialogs.warning_dialog( self.mw, self.appdata.app_name, first, second, [( _("Don't save"), gtk.RESPONSE_NO, ), (gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL), (gtk.STOCK_SAVE, gtk.RESPONSE_OK)]) if ret == gtk.RESPONSE_OK: if not self.save(): return False elif ret == gtk.RESPONSE_NO: pass else: return False if doc in self.docs: self.docs.remove(doc) doc.close() gc.collect() events.emit(events.DOC_CLOSED) if not len(self.docs): self.current_doc = None events.emit(events.NO_DOCS) msg = _('To start create new or open existing document') events.emit(events.APP_STATUS, msg) return True
def image_roi_callback(self, roi_image): """ Callback triggered when the user has drawn an ROI on the image :param roi_image: The opencv image in the ROI """ if self._srv is None: warning_dialog( "No service specified!", "Please first specify a service via the options button (top-right gear wheel)" ) return height, width = roi_image.shape[:2] if (roi_image is None or height == 0 or width == 0): warning_dialog("ROI too small", "draw a larger ROI") return if self._srv.service_class == Classify2D: self.classify_srv_call(roi_image) else: warning_dialog("Unknown service class", "Service class is unknown!")
def execute(self, widget=None, data=None): def _req_pac_check(to_check, flag): to_do = [] try: pac = self.database.get_by_name(to_check) except NameError: dlg = error_dialog(self.gld.get_widget("main_win"), _("%(dep)s is not in the database. %(dep)s is required by %(pkg)s.\nThis maybe either an error in %(pkg)s packaging or a gtkpacman's bug.\nIf you think it's the first, contact the %(pkg)s maintainer, else fill a bug report for gtkpacman, please.") %{'dep': dep, "pkg": name}, self.icon) dlg.run() dlg.destroy() pacs_queues["add"].remove(pac) self.queues["add"].remove(name) return try: if not pac.prop_setted: self.database.set_pac_properties(pac) except: return pac, to_do if flag == "req": for req in pac.req_by.split(", "): if len(req) >= 1: to_do.append(req) else: if not pac.installed: for dep in pac.dependencies.split(", "): if len(dep) >= 1: to_do.append(dep) else: pac = None return pac, to_do pac = None return pac, to_do pacs_queues = { "add": [], "remove": [] } deps = [] for name in self.queues["add"]: try: pac = self.database.get_by_name(name) except NameError: dlg = error_dialog(self.gld.get_widget("main_win"), _("%s is not in the database.\nThis is probably a bug in gtkpacman. If you think it's so, please send me a bug report.") %name, self.icon) dlg.run() dlg.destroy() continue if not pac.prop_setted: self.database.set_pac_properties(pac) pacs_queues["add"].append(pac) if pac.dependencies: dep_todo_list = [] dep_black_list = [] deps = pac.dependencies.split(", ") for dep in deps: if not dep in self.queues["add"]: dep_todo_list.append(dep) while dep_todo_list: dep = dep_todo_list.pop(0) if dep.count(">="): dep = dep.split(">=")[0] if not (dep in self.queues["add"]): done, to_do = _req_pac_check(dep, "dep") if done and not done in pacs_queues["add"]: pacs_queues["add"].append(done) for add in to_do: if not add in dep_black_list: dep_todo_list.append(add) dep_black_list.append(add) for name in self.queues["remove"]: pac = self.database.get_by_name(name) if not pac.prop_setted: self.database.set_pac_properties(pac) pacs_queues["remove"].append(pac) if pac.req_by: req_pacs = [] req_todo_list = [] req_black_list = [] for req in pac.req_by.split(", "): if not (req in self.queues["remove"]): req_todo_list.append(req) while req_todo_list: req = req_todo_list.pop(0) if not (req in self.queues["remove"]): done, to_do = _req_pac_check(req, "req") if done and not done in req_pacs: req_pacs.append(done) for add in to_do: if not add in req_black_list: req_todo_list.append(add) req_black_list.append(add) continue if req_pacs: dlg = warning_dialog(self.gld.get_widget("main_win"), req_pacs, self.icon) if dlg.run() == RESPONSE_YES: pacs_queues["remove"].extend(req_pacs) else: self.queues["remove"].remove(name) pacs_queues["remove"].remove(pac) dlg.destroy() continue if not (pacs_queues["add"] or pacs_queues["remove"]): self._refresh_trees_and_queues() return retcode = self._confirm(pacs_queues) if retcode: self._statusbar(_("Executing queued operations...")) dlg = do_dialog(pacs_queues, self.icon) dlg.connect("destroy", self._refresh_trees_and_queues, pacs_queues) dlg.run() else: self.queues["add"] = [] self.queues["remove"] = [] self._refresh_trees_and_queues() return
def execute(self, widget=None, data=None): pacs_queues = { "add": [], "remove": [] } deps = [] req_pacs = [] if self.queues['add']: pacs_queues['add'] = self._execute_queue_add() # Check if packages are listed as ignorePkg b_list = self._blacklist( pacs_queues['add'], 0x01) if b_list: dlg = ignorepkg_dialog( b_list, self.icon) res = dlg.run() dlg.destroy() if res == RESPONSE_NO: self._done(None) return if self.queues['remove']: pacs_queues['remove'], req_pacs = self._execute_queue_remove() #pacs_queues["remove"] = self._execute_queue_remove() # Check if packages are listed as holdPkg b_list = self._blacklist( pacs_queues['remove'], 0x02) if b_list: dlg = holdpkg_dialog( b_list, self.icon) res = dlg.run() dlg.destroy() if res == RESPONSE_NO: self._done(None) return if not (pacs_queues["add"] or pacs_queues["remove"]): self._refresh_trees_and_queues() return self.gld.get_widget("main_win").set_sensitive(False) if req_pacs: dlg = warning_dialog(self.gld.get_widget("main_win"), req_pacs, self.icon) if dlg.run() == RESPONSE_YES: pacs_queues["remove"].extend(req_pacs) dlg.destroy() else: dlg.destroy() # Open new dialog and execute commands ie. install/remove packages do_dlg = do_dialog( self.gld.get_widget("main_win"), self.icon, pacs_queues) do_dlg.connect("destroy", self._refresh_trees_and_queues) resp = do_dlg.run() if resp == RESPONSE_YES: self._statusbar(_("Executing queued operations...")) do_dlg.set_sensitive(False) if self._passwd_dlg_init(do_dlg): do_dlg.install() do_dlg.set_sensitive(True) # User clicked cancel else: do_dlg.destroy() self.queues["add"] = [] self.queues["remove"] = [] self._statusbar(_("Upgrade canceled")) self.gld.get_widget("main_win").set_sensitive(True) elif resp == RESPONSE_NO: do_dlg.destroy() self.queues["add"] = [] self.queues["remove"] = [] self._statusbar(_("Upgrade canceled")) self.gld.get_widget("main_win").set_sensitive(True)
def _execute_queue_add(self): """ We convert names to pac (dict). Then we check if there are packages that need to be installed for our selected packages (dependecies). """ # queue is list of pacs objects and will be returned, # while queue_pac_name is only used for pac.name # to know what has been added in queue def add_to_install_queue(): done_pac.flag = 11 queue.append(done_pac) queue_pacs_names.append(done_pac.name) deps_todo_list.extend(queue_todo) if done_pac.conflict: conflicts[done_pac.name] = done_pac.conflict queue = [] queue_pacs_names = [] conflicts = {} # 1. we fetching pacs object for name in self.queues["add"]: pac = self.database.get_by_name(name) pac.flag = 0x12 self.database.set_pac_properties(pac) pac.flag = None queue.append(pac) queue_pacs_names.append(pac.name) # 2. iterate over pacs object for pac in queue: if pac.dependencies: deps_todo_list = [] deps_black_list = [] deps_todo_list.extend( pac.dependencies.split(",") ) while deps_todo_list: dep = deps_todo_list.pop(0) if not dep: continue # Split string if separator found if dep.count('>='): dep_name, dep_sep, dep_ver = dep.partition('>=') elif dep.count('<='): dep_name, dep_sep, dep_ver = dep.partition('<=') elif dep.count('<'): dep_name, dep_sep, dep_ver = dep.partition('<') elif dep.count('>'): dep_name, dep_sep, dep_ver = dep.partition('>') elif dep.count('='): dep_name, dep_sep, dep_ver = dep.partition('=') else: dep_name, dep_sep = dep, None dep_name = dep_name.strip() if ( dep_name not in self.queues["add"] and dep_name not in queue_pacs_names and dep_name not in deps_black_list ): done_pac, queue_todo = self._execute_dep_check(dep_name, "dep") if done_pac: # if separator found then compare versions if dep_sep: # compare versions if dep_sep == '>=' and dep_ver > done_pac.inst_ver or len(dep_ver) > len(done_pac.inst_ver): add_to_install_queue() elif dep_sep == '=': if not done_pac.inst_ver == dep_ver or not dep.installed: add_to_install_queue() elif (dep_sep == '>' and dep_ver > done_pac.inst_ver or len(dep_ver) > len(done_pac.inst_ver) ): add_to_install_queue() elif dep_sep == '<' and dep_ver < done_pac.inst_ver: print '!!!!!!!!<!!!!!!' else: if not done_pac.installed: add_to_install_queue() else: continue if not done_pac.installed and queue_todo: deps_todo_list.extend(queue_todo) deps_black_list.append(done_pac.name) # Resolve conflicts if conflicts: pacs = [] for pac in map( self.database.get_by_name, conflicts.itervalues() ): if pac and pac.installed and pac.name not in self.queues["remove"]: pacs.append(pac) if pacs: self.gld.get_widget("main_win").set_sensitive(False) dlg = warning_dialog(self.gld.get_widget("main_win"), pacs, self.icon, True) # Add pacs to remove queue if clicket on Yes button if dlg.run() == RESPONSE_YES: for pac in pacs: self.queues["remove"].append(pac.name) dlg.destroy() # Otherwise Cancel whole process else: self.queues['add'] = [] self.queues['remove'] = [] dlg.destroy() return return queue
def execute(self, widget=None, data=None): pacs_queues = { "add": [], "remove": [] } for name in self.queues["add"]: try: pac = self.database.get_by_name(name) except NameError: dlg = error_dialog(self.gld.get_widget("main_win"), _("%s is not in the database.\nThis is probably a bug in gtkpacman. If you think it's so, please send me a bug report.") %name, self.icon) dlg.run() dlg.destroy() continue if not pac.prop_setted: self.database.set_pac_properties(pac) pacs_queues["add"].append(pac) deps = pac.dependencies.split(", ") for dep in deps: if dep.count(">="): dep = dep.split(">=")[0] try: dep_pac = self.database.get_by_name(dep) except NameError: dlg = error_dialog(self.gld.get_widget("main_win"), _("%(dep)s is not in the database. %(dep)s is required by %(pkg)s.\nThis maybe either an error in %(pkg)s packaging or a gtkpacman's bug.\nIf you think it's the first, contact the %(pkg)s maintainer, else fill a bug report for gtkpacman, please.") %{'dep': dep, "pkg": name}, self.icon) dlg.run() dlg.destroy() pacs_queues["add"].remove(pac) self.queues["add"].remove(name) break if not dep_pac.installed: pacs_queues["add"].append(dep_pac) continue for name in self.queues["remove"]: pac = self.database.get_by_name(name) if not pac.prop_setted: self.database.set_pac_properties(pac) pacs_queues["remove"].append(pac) if pac.req_by: req_pacs = [] for req in pac.req_by.split(", "): if not (req in self.queues["remove"]): req_pac = self.database.get_by_name(req) req_pacs.append(req_pac) if req_pacs: dlg = warning_dialog(self.gld.get_widget("main_win"), req_pacs, self.icon) if dlg.run() == RESPONSE_YES: pacs_queues["remove"].extend(req_pacs) else: self.queues["remove"].remove(name) pacs_queues["remove"].remove(pac) dlg.destroy() continue if not (pacs_queues["add"] or pacs_queues["remove"]): self._refresh_trees_and_queues() return retcode = self._confirm(pacs_queues) if retcode: stat_bar = self.gld.get_widget("statusbar") stat_bar.pop(self.stat_id) stat_bar.push(self.stat_id, _("Executing queued operations")) dlg = do_dialog(pacs_queues, self.icon) dlg.connect("destroy", self._refresh_trees_and_queues, pacs_queues) dlg.run() else: self.queues["add"] = [] self.queues["remove"] = [] self._refresh_trees_and_queues() return