def _draw_labels(self, image, track, markersize=0.20): nframes = len(track) size = int(round(image.shape[1]/nframes, 0)), image.shape[0] msize = int(round(size[0]*markersize, 0)) image[size[1]-int(msize/4):size[1], :] = hex2rgb("#FFFFFF") for i, label in enumerate(track): name = self.classdef.class_names[label] color = np.array(hex2rgb(self.classdef.hexcolors[name], mpl=False), dtype=int) image[size[1]-msize:size[1], i*size[0]:i*size[0]+msize] = color return image
def _draw_labels(self, image, track, markersize=0.20): nframes = len(track) size = int(round(image.shape[1] / nframes, 0)), image.shape[0] msize = int(round(size[0] * markersize, 0)) image[size[1] - int(msize / 4):size[1], :] = hex2rgb("#FFFFFF") for i, label in enumerate(track): name = self.classdef.class_names[label] color = np.array(hex2rgb(self.classdef.hexcolors[name], mpl=False), dtype=int) image[size[1] - msize:size[1], i * size[0]:i * size[0] + msize] = color return image
def draw_annotation_images(self, plate, training_set, container, learner, rid=""): cldir = dict([(cname, join(learner.samples_dir, cname)) \ for cname in learner.class_names.values()]) # create dir per class name for dir_ in cldir.values(): makedirs(dir_) for obj in training_set.itervalues(): rgb_value = ccore.RGBValue(*hex2rgb(obj.strHexColor)) file_ = 'PL%s___P%s___T%05d___X%04d___Y%04d' \ %(plate, self.P, self._iT, obj.oCenterAbs[0], obj.oCenterAbs[1]) obj.file = file_ file_ = join(cldir[obj.strClassName], '%s___%s.png' % (file_, rid + "_%s")) container.exportObject(obj.iId, file_ % "img", file_ % "msk") container.markObjects([obj.iId], rgb_value, False, True) ccore.drawFilledCircle(ccore.Diff2D(*obj.oCenterAbs), 3, container.img_rgb, rgb_value)
def _activate_object(self, item, point, class_name, state=True): if state: color = \ QColor(*hex2rgb(self._learner.hexcolors[class_name])) # color.setAlphaF(1.0) label = self._learner.class_labels[class_name] # item2 = QGraphicsEllipseItem(point.x(), point.y(), 3, 3,item) # item2.setPen(QPen(color)) # item2.setBrush(QBrush(color)) # item2.show() item2 = QGraphicsSimpleTextItem(str(label), item) rect = item2.boundingRect() # center the text item at the annotated point item2.setPos(point - QPointF(rect.width() / 2, rect.height() / 2)) item2.setPen(QPen(color)) item2.setBrush(QBrush(color)) item2.show() else: color = self.browser.image_viewer.contour_color scene = item.scene() for item2 in item.childItems(): scene.removeItem(item2) item.set_pen_color(color) obj_id = item.data(0) return obj_id
def _activate_object(self, item, point, class_name, state=True): if state: color = \ QColor(*hex2rgb(self._learner.hexcolors[class_name])) # color.setAlphaF(1.0) label = self._learner.class_labels[class_name] # item2 = QGraphicsEllipseItem(point.x(), point.y(), 3, 3,item) # item2.setPen(QPen(color)) # item2.setBrush(QBrush(color)) # item2.show() item2 = QGraphicsSimpleTextItem(str(label), item) rect = item2.boundingRect() # center the text item at the annotated point item2.setPos(point - QPointF(rect.width()/2, rect.height()/2)) item2.setPen(QPen(color)) item2.setBrush(QBrush(color)) item2.show() else: color = self.browser.image_viewer.contour_color scene = item.scene() for item2 in item.childItems(): scene.removeItem(item2) item.set_pen_color(color) obj_id = item.data(0) return obj_id
def draw_annotation_images(self, plate, training_set, container, learner, rid=""): cldir = dict([(cname, join(learner.samples_dir, cname)) \ for cname in learner.class_names.values()]) # create dir per class name for dir_ in cldir.values(): makedirs(dir_) for obj in training_set.itervalues(): rgb_value = ccore.RGBValue(*hex2rgb(obj.strHexColor)) file_ = 'PL%s___P%s___T%05d___X%04d___Y%04d' \ %(plate, self.P, self._iT, obj.oCenterAbs[0], obj.oCenterAbs[1]) obj.file = file_ file_ = join(cldir[obj.strClassName], '%s___%s.png' %(file_, rid+"_%s")) container.exportObject(obj.iId, file_ %"img", file_ %"msk") container.markObjects([obj.iId], rgb_value, False, True) ccore.drawFilledCircle(ccore.Diff2D(*obj.oCenterAbs), 3, container.img_rgb, rgb_value)
def render(self, dctRenderInfo=None, images=None): lstImages = [] if not images is None: lstImages += images if dctRenderInfo is None: for name, oChannel in self._channel_registry.iteritems(): for strRegion, oContainer in oChannel.containers.iteritems(): strHexColor, fAlpha = oChannel.dctAreaRendering[strRegion] imgRaw = oChannel.meta_image.image imgCon = ccore.Image(imgRaw.width, imgRaw.height) ccore.drawContour(oContainer.getBinary(), imgCon, 255, False) lstImages.append((imgRaw, strHexColor, 1.0)) lstImages.append((imgCon, strHexColor, fAlpha)) else: for channel_name, dctChannelInfo in dctRenderInfo.iteritems(): if channel_name in self._channel_registry: oChannel = self._channel_registry[channel_name] if 'raw' in dctChannelInfo: strHexColor, fAlpha = dctChannelInfo['raw'] # special casing for virtual channel to mix # raw images together if oChannel.is_virtual(): lstImages.extend(oChannel.meta_images(fAlpha)) else: lstImages.append( (oChannel.meta_image.image, strHexColor, 1.0)) if 'contours' in dctChannelInfo: # transform the old dict-style to the new tuple-style, # which allows multiple definitions for one region if isinstance(dctChannelInfo['contours'], dict): lstContourInfos = [ (k, ) + v for k, v in dctChannelInfo['contours'].iteritems() ] else: lstContourInfos = dctChannelInfo['contours'] for tplData in lstContourInfos: strRegion, strNameOrColor, fAlpha, bShowLabels = tplData[: 4] # draw contours only if region is present if oChannel.has_region(strRegion): if len(tplData) > 4: bThickContours = tplData[4] else: bThickContours = False imgRaw = oChannel.meta_image.image if strNameOrColor == 'class_label': oContainer = oChannel.containers[strRegion] oRegion = oChannel.get_region(strRegion) dctLabels = {} dctColors = {} for iObjId, oObj in oRegion.iteritems(): iLabel = oObj.iLabel if not iLabel is None: if not iLabel in dctLabels: dctLabels[iLabel] = [] dctLabels[iLabel].append(iObjId) dctColors[ iLabel] = oObj.strHexColor imgCon2 = ccore.Image( imgRaw.width, imgRaw.height) for iLabel, lstObjIds in dctLabels.iteritems( ): imgCon = ccore.Image( imgRaw.width, imgRaw.height) # Flip this and use drawContours with fill option enables to get black background oContainer.drawContoursByIds( lstObjIds, 255, imgCon, bThickContours, False) lstImages.append( (imgCon, dctColors[iLabel], fAlpha)) if isinstance(bShowLabels, bool) and bShowLabels: oContainer.drawTextsByIds( lstObjIds, [str(iLabel)] * len(lstObjIds), imgCon2) lstImages.append((imgCon2, '#FFFFFF', 1.0)) else: oContainer = oChannel.containers[strRegion] oRegion = oChannel.get_region(strRegion) lstObjIds = oRegion.keys() imgCon = ccore.Image( imgRaw.width, imgRaw.height) if not strNameOrColor is None: oContainer.drawContoursByIds( lstObjIds, 255, imgCon, bThickContours, False) else: strNameOrColor = '#FFFFFF' lstImages.append( (imgCon, strNameOrColor, fAlpha)) if bShowLabels: imgCon2 = ccore.Image( imgRaw.width, imgRaw.height) oContainer.drawLabelsByIds( lstObjIds, imgCon2) lstImages.append( (imgCon2, '#FFFFFF', 1.0)) if len(lstImages) > 0: imgRgb = ccore.makeRGBImage( [x[0].getView() for x in lstImages], [ccore.RGBValue(*hex2rgb(x[1])) for x in lstImages], [x[2] for x in lstImages]) return imgRgb
def render(self, dctRenderInfo=None, images=None): lstImages = [] if not images is None: lstImages += images if dctRenderInfo is None: for name, oChannel in self._channel_registry.iteritems(): for strRegion, oContainer in oChannel.containers.iteritems(): strHexColor, fAlpha = oChannel.dctAreaRendering[strRegion] imgRaw = oChannel.meta_image.image imgCon = ccore.Image(imgRaw.width, imgRaw.height) ccore.drawContour(oContainer.getBinary(), imgCon, 255, False) lstImages.append((imgRaw, strHexColor, 1.0)) lstImages.append((imgCon, strHexColor, fAlpha)) else: for channel_name, dctChannelInfo in dctRenderInfo.iteritems(): if channel_name in self._channel_registry: oChannel = self._channel_registry[channel_name] if 'raw' in dctChannelInfo: strHexColor, fAlpha = dctChannelInfo['raw'] # special casing for virtual channel to mix # raw images together if oChannel.is_virtual(): lstImages.extend(oChannel.meta_images(fAlpha)) else: lstImages.append((oChannel.meta_image.image, strHexColor, 1.0)) if 'contours' in dctChannelInfo: # transform the old dict-style to the new tuple-style, # which allows multiple definitions for one region if isinstance(dctChannelInfo['contours'], dict): lstContourInfos = [(k,)+v for k,v in dctChannelInfo['contours'].iteritems()] else: lstContourInfos = dctChannelInfo['contours'] for tplData in lstContourInfos: strRegion, strNameOrColor, fAlpha, bShowLabels = tplData[:4] # draw contours only if region is present if oChannel.has_region(strRegion): if len(tplData) > 4: bThickContours = tplData[4] else: bThickContours = False imgRaw = oChannel.meta_image.image if strNameOrColor == 'class_label': oContainer = oChannel.containers[strRegion] oRegion = oChannel.get_region(strRegion) dctLabels = {} dctColors = {} for iObjId, oObj in oRegion.iteritems(): iLabel = oObj.iLabel if not iLabel is None: if not iLabel in dctLabels: dctLabels[iLabel] = [] dctLabels[iLabel].append(iObjId) dctColors[iLabel] = oObj.strHexColor imgCon2 = ccore.Image(imgRaw.width, imgRaw.height) for iLabel, lstObjIds in dctLabels.iteritems(): imgCon = ccore.Image(imgRaw.width, imgRaw.height) # Flip this and use drawContours with fill option enables to get black background oContainer.drawContoursByIds(lstObjIds, 255, imgCon, bThickContours, False) lstImages.append((imgCon, dctColors[iLabel], fAlpha)) if isinstance(bShowLabels, bool) and bShowLabels: oContainer.drawTextsByIds(lstObjIds, [str(iLabel)]*len(lstObjIds), imgCon2) lstImages.append((imgCon2, '#FFFFFF', 1.0)) else: oContainer = oChannel.containers[strRegion] oRegion = oChannel.get_region(strRegion) lstObjIds = oRegion.keys() imgCon = ccore.Image(imgRaw.width, imgRaw.height) if not strNameOrColor is None: oContainer.drawContoursByIds(lstObjIds, 255, imgCon, bThickContours, False) else: strNameOrColor = '#FFFFFF' lstImages.append((imgCon, strNameOrColor, fAlpha)) if bShowLabels: imgCon2 = ccore.Image(imgRaw.width, imgRaw.height) oContainer.drawLabelsByIds(lstObjIds, imgCon2) lstImages.append((imgCon2, '#FFFFFF', 1.0)) if len(lstImages) > 0: imgRgb = ccore.makeRGBImage([x[0].getView() for x in lstImages], [ccore.RGBValue(*hex2rgb(x[1])) for x in lstImages], [x[2] for x in lstImages]) return imgRgb
def _set_info_table(self, conf): rows = len(self._learner.class_labels) self._table_info.clear() names_horizontal = [('Name', 'class name'), ('Samples', 'class samples'), ('Color', 'class color'), ('%PR', 'class precision in %'), ('%SE', 'class sensitivity in %')] names_vertical = [str(k) for k in self._learner.class_names.keys()] + \ ['', '#'] self._table_info.setColumnCount(len(names_horizontal)) self._table_info.setRowCount(len(names_vertical)) self._table_info.setVerticalHeaderLabels(names_vertical) self._table_info.setColumnWidth(1, 20) for c, (name, info) in enumerate(names_horizontal): item = QTableWidgetItem(name) item.setToolTip(info) self._table_info.setHorizontalHeaderItem(c, item) for r, label in enumerate(self._learner.class_names.keys()): self._table_info.setRowHeight(r, 20) name = self._learner.class_names[label] samples = self._learner.names2samples[name] self._table_info.setItem(r, 0, QTableWidgetItem(name)) self._table_info.setItem(r, 1, QTableWidgetItem(str(samples))) item = QTableWidgetItem(' ') item.setBackground(QBrush(\ QColor(*hex2rgb(self._learner.hexcolors[name])))) self._table_info.setItem(r, 2, item) if not conf is None and r < len(conf): item = QTableWidgetItem('%.1f' % (conf.ppv[r] * 100.)) item.setToolTip('"%s" precision' % name) self._table_info.setItem(r, 3, item) item = QTableWidgetItem('%.1f' % (conf.se[r] * 100.)) item.setToolTip('"%s" sensitivity' % name) self._table_info.setItem(r, 4, item) if not conf is None: self._table_info.setRowHeight(r+1, 20) r += 2 self._table_info.setRowHeight(r, 20) name = "overal" samples = sum(self._learner.names2samples.values()) self._table_info.setItem(r, 0, QTableWidgetItem(name)) self._table_info.setItem(r, 1, QTableWidgetItem(str(samples))) item = QTableWidgetItem(' ') item.setBackground(QBrush(QColor(*hex2rgb('#FFFFFF')))) self._table_info.setItem(r, 2, item) item = QTableWidgetItem('%.1f' % (conf.wav_ppv * 100.)) item.setToolTip('%s per class precision' % name) self._table_info.setItem(r, 3, item) item = QTableWidgetItem('%.1f' % (conf.wav_se * 100.)) item.setToolTip('%s per class sensitivity' % name) self._table_info.setItem(r, 4, item) self._table_info.resizeColumnsToContents()
def _set_info_table(self, conf): rows = len(self._learner.class_labels) self._table_info.clear() names_horizontal = [('Name', 'class name'), ('Samples', 'class samples'), ('Color', 'class color'), ('%PR', 'class precision in %'), ('%SE', 'class sensitivity in %')] names_vertical = [str(k) for k in self._learner.class_names.keys()] + \ ['', '#'] self._table_info.setColumnCount(len(names_horizontal)) self._table_info.setRowCount(len(names_vertical)) self._table_info.setVerticalHeaderLabels(names_vertical) self._table_info.setColumnWidth(1, 20) for c, (name, info) in enumerate(names_horizontal): item = QTableWidgetItem(name) item.setToolTip(info) self._table_info.setHorizontalHeaderItem(c, item) for r, label in enumerate(self._learner.class_names.keys()): self._table_info.setRowHeight(r, 20) name = self._learner.class_names[label] samples = self._learner.names2samples[name] self._table_info.setItem(r, 0, QTableWidgetItem(name)) self._table_info.setItem(r, 1, QTableWidgetItem(str(samples))) item = QTableWidgetItem(' ') item.setBackground(QBrush(\ QColor(*hex2rgb(self._learner.hexcolors[name])))) self._table_info.setItem(r, 2, item) if not conf is None and r < len(conf): item = QTableWidgetItem('%.1f' % (conf.ppv[r] * 100.)) item.setToolTip('"%s" precision' % name) self._table_info.setItem(r, 3, item) item = QTableWidgetItem('%.1f' % (conf.se[r] * 100.)) item.setToolTip('"%s" sensitivity' % name) self._table_info.setItem(r, 4, item) if not conf is None: self._table_info.setRowHeight(r + 1, 20) r += 2 self._table_info.setRowHeight(r, 20) name = "overal" samples = sum(self._learner.names2samples.values()) self._table_info.setItem(r, 0, QTableWidgetItem(name)) self._table_info.setItem(r, 1, QTableWidgetItem(str(samples))) item = QTableWidgetItem(' ') item.setBackground(QBrush(QColor(*hex2rgb('#FFFFFF')))) self._table_info.setItem(r, 2, item) item = QTableWidgetItem('%.1f' % (conf.wav_ppv * 100.)) item.setToolTip('%s per class precision' % name) self._table_info.setItem(r, 3, item) item = QTableWidgetItem('%.1f' % (conf.wav_se * 100.)) item.setToolTip('%s per class sensitivity' % name) self._table_info.setItem(r, 4, item) self._table_info.resizeColumnsToContents()
def from_hex_color(cls, name, string): return cls(name, hex2rgb(string))