def get_meanlines(in_file, out_file, roi_file, scale=1, debug_dir=False): from lib.debug import Debug if debug_dir: Debug.set_directory(debug_dir) from lib.timer import timeStart, timeEnd from lib.load_image import get_image from lib.geojson_io import get_features, save_features from lib.polygon_mask import mask_image from lib.meanline_detection import detect_meanlines, meanlines_to_geojson timeStart("get meanlines") timeStart("read image") image = get_image(in_file) timeEnd("read image") roi_polygon = get_features(roi_file)["geometry"]["coordinates"][0] timeStart("mask image") masked_image = mask_image(image, roi_polygon) timeEnd("mask image") meanlines = detect_meanlines(masked_image, scale=scale) timeStart("convert to geojson") meanlines_as_geojson = meanlines_to_geojson(meanlines) timeEnd("convert to geojson") timeStart("saving as geojson") save_features(meanlines_as_geojson, out_file) timeEnd("saving as geojson") timeEnd("get meanlines")
def debug_blocks(img, points, block_dims, threshold_function): ''' To be used for debugging. Saves images of blocks that throw errors, and an additional image showing how the blocks are distributed. ''' from debug import Debug from skimage.draw import line, circle from skimage.color import gray2rgb bad_block_points = [] for center in points: block = get_block(img, center, block_dims) try: if (type(block) is MaskedArray): threshold_function(block.compressed()) else: threshold_function(block) except Exception, e: print "threshold block error" print e bad_block_points.append(center) Debug.save_image("threshold", "bad_block_" + str(center), block)
def _start_conn(self): try: Debug().debug(Debug().OKBLUE+"Run Thread"+Debug().END) self._Set_Vars() if self._GetToken() == False: return False #self.stop() if self._SetUpSocket() == False: return False if self._Handshaked() == False: return False while self.StopMainWhile == False: # Если возврощает True значит все Ок и продолжаем иначе если False то произошла ошибка result = self._MainWhile() if result: if result == AOUTOR_ERROR: return True continue else: self.socket_o.close() break if self.chat_host == 'py-chat.tk': return True #self.quit() except KeyboardInterrupt: Debug().info("Exit by Keyboard Interrupt") sys.exit(2) return False
def create_image_cube(img, sigma_list, axis): gaussian_blurs = [gaussian_filter1d(img, s, axis=axis) for s in sigma_list] num_scales = len(gaussian_blurs) - 1 image_cube = np.zeros((img.shape[0], img.shape[1], num_scales)) for i in range(num_scales): image_cube[:, :, i] = ((gaussian_blurs[i] - gaussian_blurs[i + 1])) Debug.save_image("ridges", "image_cube-" + pad(i), image_cube[:, :, i]) return image_cube
def conn_Del(self): Debug().debug("Thread send del SIGNAL",Debug().RED) self.groupBox_captcha.hide() self.groupBox_OnConnected.hide() self.groupBox_Connect.show() self.groupBox_Connect.setDisabled(False) self.Button_Disconnect.setDisabled(False) self.Button_Connect.setDisabled(False)
def get_box_lines(boundary, image=None): height, width = boundary.shape [half_width, half_height] = np.floor([0.5 * width, 0.5 * height]).astype(int) timeStart("split image") image_regions = { "left": boundary[0:height, 0:half_width], "right": boundary[0:height, half_width:width], "top": boundary[0:half_height, 0:width], "bottom": boundary[half_height:height, 0:width] } timeEnd("split image") timeStart("get hough lines") hough_lines = { "left": np.array( get_hough_lines(image_regions["left"], min_angle=-10, max_angle=10)), "right": np.array( get_hough_lines(image_regions["right"], min_angle=-10, max_angle=10)), "top": np.array( get_hough_lines(image_regions["top"], min_angle=-120, max_angle=-70)), "bottom": np.array( get_hough_lines(image_regions["bottom"], min_angle=-120, max_angle=-70)) } timeEnd("get hough lines") hough_lines["bottom"] += [0, half_height] hough_lines["right"] += [half_width, 0] print "found these hough lines:" print hough_lines if Debug.active: image = gray2rgb(boundary) line_coords = [ skidraw.line(line[0][1], line[0][0], line[1][1], line[1][0]) for line in hough_lines.itervalues() ] for line in line_coords: rr, cc = line mask = (rr >= 0) & (rr < image.shape[0]) & (cc >= 0) & ( cc < image.shape[1]) image[rr[mask], cc[mask]] = [255, 0, 0] Debug.save_image("roi", "hough_lines", image) return hough_lines
class PluginHandler(object): events = {} Plugins = [] # Экземпляры загруженных плагинов def __init__(self): ev = {'test':[self.Event]} def AddEventHandler(self, EventName, EventFunction): #Debug().info(' Add Event Hendler: %s - call %s ' % (EventName, EventFunction)) if EventName in self.events: self.events[EventName].append(EventFunction) else: self.events[EventName] = [EventFunction,] def Event(self, event, argv, *args): if event in self.events: #print 'Event: %s **args: %s ' % (event,args) for func in self.events[event]: #print 'Call: %s' % (func) #_thread.start_new_thread(func, (argv,) ) func(argv) else: #Debug().err('Нет события для %s' % (event)) pass def LoadPlugins(self,Window_Main): if CFG.valueInt('DisablePlugins',1) == 1: Debug().info("Plugins are disabled") return #if '*' in G['config'].settings['skip_plugins']: return #from lib.plugin_kernel import KernelPlugin plugins_dir = './plugins' try: ss = os.listdir(plugins_dir) # Получаем список плагинов в /plugins sys.path.insert( 0, plugins_dir) # Добавляем папку плагинов в $PATH, чтобы __import__ мог их загрузить except Exception, err: Debug().err(err) Debug().info('Create dir "%s"' % (plugins_dir)) os.mkdir(plugins_dir) for s in ss: if s[-3:] != '.py': continue #if s in G['config'].settings['skip_plugins']: continue Debug().info('Found plugin: %s' % (s)) __import__(os.path.splitext(s)[ 0], None, None, ['']) # Импортируем исходник плагина #p = KernelPlugin() #print Plugin.__subclasses__() for plugin in Plugin.__subclasses__(): # так как Plugin произведен от object, мы используем __subclasses__, чтобы найти все плагины, произведенные от этого класса p = plugin() # Создаем экземпляр self.Plugins.append(p) Debug().info('Load Plugin: %s v%s' % (p.Name, p.Version)) p.OnLoad(Window_Main,self) # Вызываем событие загруки этого плагина return
def _GetToken(self): Debug().info('Get token from: %s' % (self.chat_token_page)) self._PrintGui('<font color="#0000ff">Get token...</font>') try: #data = urllib.request.urlopen(self.chat_token_page).read() data = urllib2.urlopen(self.chat_token_page).read() self.CurentToken = re.compile('token=([a-zA-Z0-9]{32})').search(data).group(1) except Exception , err: self._PrintGui('<font color="#800000">Get token error: %s</font>' % (err)) Debug().err('Get token error, '+str(err)) return False
def Load_and_Return(self): Debug().info('Read config...') qs = self.value('dic') s = qs.toPyObject() try: settings = pickle.loads(str(s)) except Exception, err: self.setValue('DisablePlugins', 1) Debug().err(err) Debug().warr( "Произошла ошибка при загрузке конфига, будут использованны настройки по умолчанию." ) settings = self._def_settings
def LoadPlugins(self,Window_Main): if CFG.valueInt('DisablePlugins',1) == 1: Debug().info("Plugins are disabled") return #if '*' in G['config'].settings['skip_plugins']: return #from lib.plugin_kernel import KernelPlugin plugins_dir = './plugins' try: ss = os.listdir(plugins_dir) # Получаем список плагинов в /plugins sys.path.insert( 0, plugins_dir) # Добавляем папку плагинов в $PATH, чтобы __import__ мог их загрузить except Exception, err: Debug().err(err) Debug().info('Create dir "%s"' % (plugins_dir)) os.mkdir(plugins_dir)
def ReadFile(self, path): Debug().info('Try read: %s' % (path)) file_h = open(path, 'r') data = file_h.read() file_h.close() #Debug().info(data) return data
def __init__(self, options={}): self.dbg = Debug('debug' in options and options['debug']) for o in ['progress', 'rtl']: self.options[o] = o in options and options[o] if self.options['rtl']: Panel.set_numbering('rtl') self.options[ 'min_panel_size_ratio'] = Panel.DEFAULT_MIN_PANEL_SIZE_RATIO if 'min_panel_size_ratio' in options and options[ 'min_panel_size_ratio']: self.options['min_panel_size_ratio'] = options[ 'min_panel_size_ratio']
def run(self): while self.StopMainWhile == False and self._start_conn() : pass self._PrintGui('<font color="#800000">Disconnected.</font>') Debug().info("STOP THREAD, quit main while.") self.emit(QtCore.SIGNAL('conn_del()')) self.deleteLater() del self.obj_tab.obj_conn
def _SetUpSocket(self): try: self.socket_o = socket.socket(socket.AF_INET, socket.SOCK_STREAM) except socket.error , err: Debug().err("Create socket error.") self._PrintGui('<font color="#800000">Create socket error: %s</font>' % (err)) self.stop()
def qStringToStr(s): if type(s) == QtCore.QString: try: if False: s = QtCore.QString() #s = unicode(s) #.encode('utf-8') #s = str(s.toUtf8()).decode('utf-8') s = str(s.toUtf8()).decode('utf-8') except Exception, err: Debug().err('Decode: ' + str(err))
def ParsePost(post_message): #[20:39:39] <b><<a href="event:insert,28792"><font color="#000000">28792</font></a></b><b>></b> TEst #re.I|re.U post_message = qStringToStr(post_message) try: post_message = post_message.strip() except Exception, err: Debug().err('Parse err: %s' % (err)) return post_message
def Settings_Load(self,index): sett = G['config'].settings current_index = sett['servers'][self.connIndex] Debug().info('Set %s' % current_index['host']) self.lineEdit_Name.setText(QtGui.QApplication.translate("MainWindow",current_index['user_nick'], None, QtGui.QApplication.UnicodeUTF8)) self.checkBox_Name.setChecked(bool(current_index['NamefagMode'])) self.lineEdit_Host.setText(current_index['host']) self.lineEdit_Port.setText(str(current_index['port'])) self.lineEdit_Token.setText(current_index['token_page']) self.tab_SetName(current_index['name'])
def stop(self): self.StopMainWhile = True if hasattr(self, 'socket_o'): try: self.socket_o.shutdown(socket.SHUT_RD) self.socket_o.close() except Exception , e: Debug().warr(e) self._PrintGui('<font color="#800000">%s</font>' % (e)) return False
def AddImagesThumb(Text, size=(150, 150)): #TODO: При клике на изображение разворачивать полную версию. #TODO: Когда в сообщении несколько ссылок на изображение через пробел или запятую то происходит кривой парсинг #TODO: Найти все ссылки, занести в в список, сделать замену. Text = re.sub(_reCompile_ImagesRGHostRe_F, 'http://rghost.ru/\\2.png', Text) Text_o = Text Text = re.sub( _reCompile_ImagesThumd_F, '''<a href="\\2" title="\\2" alt="\\2">\\2</a><br/><p><img class="image_thumb_re" src="http://imageflyresize.appspot.com/?q=\\2&width=''' + str(size[0]) + '&height=' + str(size[1]) + '" title="\\2" alt="http://imageflyresize.appspot.com/?q=\\2&width=150&height=150" onClick="expandimg();" /></p>', Text) if Text != Text_o: try: Debug().info(Text) except Exception, e: Debug().info(e) pass
def __init__(self, options: T.Optional[T.Dict[str, T.Any]] = None): self.options = {} if options is None: options = {} self.dbg = Debug("debug" in options and options["debug"]) for o in ["progress", "rtl"]: self.options[o] = o in options and options[o] if self.options["rtl"]: Panel.set_numbering("rtl") self.options[ "min_panel_size_ratio"] = Panel.DEFAULT_MIN_PANEL_SIZE_RATIO if "min_panel_size_ratio" in options and options[ "min_panel_size_ratio"]: self.options["min_panel_size_ratio"] = options[ "min_panel_size_ratio"]
class Main(object): def __init__(self): self.realpath = os.path.realpath(sys.argv[0]) self.iphakethe_working_dir = os.path.dirname(self.realpath) self.iphakethe_lib_dir = self.iphakethe_working_dir + "/lib" self.debug = Debug() def main(self): command_line_argument = "" try: if sys.argv[1]: command_line_argument = sys.argv[1] except: self.debug.log("Execução sem parâmetros de linha de comando") os.environ['IPHAKETHE_WORKING_DIR'] = self.iphakethe_working_dir os.environ['IPHAKETHE_LIB_DIR'] = self.iphakethe_lib_dir iphakethe = Iphakethe(command_line_argument) iphakethe.pack()
def get_boundary(grayscale_image, scale=1): timeStart("threshold image") black_and_white_image = otsu_threshold_image(grayscale_image) timeEnd("threshold image") Debug.save_image("roi", "black_and_white_image", black_and_white_image) timeStart("morphological open image") filter_element_opening = disk(PARAMS["trace-width"](scale)) opened_image = cv2.morphologyEx( 255 * black_and_white_image.astype(np.uint8), cv2.MORPH_OPEN, filter_element_opening) timeEnd("morphological open image") Debug.save_image("roi", "opened_image", opened_image) timeStart("invert image") opened_image = np.invert(opened_image) timeEnd("invert image") timeStart("segment image into connected regions") labeled_components, num_components = label(opened_image) timeEnd("segment image into connected regions") timeStart("calculate region areas") # Have to cast to np.intp with ndarray.astype because of a numpy bug # see: https://github.com/numpy/numpy/pull/4366 areas = np.bincount(labeled_components.flatten().astype(np.intp))[1:] timeEnd("calculate region areas") timeStart("calculate region boundaries") image_boundaries = find_boundaries(labeled_components, connectivity=1, mode="inner", background=0) timeEnd("calculate region boundaries") Debug.save_image("roi", "image_boundaries", image_boundaries) timeStart("mask region of interest") largest_component_id = np.argmax(areas) + 1 region_of_interest_mask = (labeled_components != largest_component_id) region_of_interest_boundary = np.copy(image_boundaries) region_of_interest_boundary[region_of_interest_mask] = 0 timeEnd("mask region of interest") Debug.save_image("roi", "region_of_interest_boundary", region_of_interest_boundary) return region_of_interest_boundary
def create_exclusion_cube(img, image_cube, dark_pixels, convex_pixels, axis, convex_threshold): timeStart("get slopes") slopes = get_slopes(img, axis=axis) timeEnd("get slopes") Debug.save_image("ridges", "slopes", slopes) exclusion_cube = np.zeros(image_cube.shape, dtype=bool) exclusion_cube[:,:,0] = dark_pixels | convex_pixels | slopes \ | (image_cube[:,:,0] < -convex_threshold) Debug.save_image("ridges", "exclusion_cube_base", exclusion_cube[:, :, 0]) num_scales = image_cube.shape[2] for i in range(1, num_scales): # each layer of the exclusion cube contains the previous layer # plus all convex pixels in the current image_cube layer exclusion_cube[:,:,i] = (exclusion_cube[:,:,i-1] \ | (image_cube[:,:,i] < -convex_threshold)) Debug.save_image("ridges", "exclusion_cube-" + pad(i), exclusion_cube[:, :, i]) return exclusion_cube
def _Handshaked(self): # Connecting self._PrintGui('<font color="#0000ff">Connecting...</font>') Debug().info('Connecting...') try: self.socket_o.connect( (self.chat_host, self.chat_port) ) self.SocketActive = True except Exception , err: #self._printLog("Error: Can\'t connect to server.") self._printLog("Error: %s" % err) self._PrintGui('<font color="#800000">Error: %s</font>' % err) return False
def CreateTab(self, connIndex=None, TabName='', conn_par=None, IsAutoConn=False): Debug().info('#open tab index %s' % (connIndex)) tab = TAB_Connection() self.chat_tabs.addTab(tab, TabName) tab.init_TWO(connIndex, conn_par, IsAutoConn) #thread.start_new_thread(tab.init_TWO,(connIndex,conn_par, self, IsAutoConn)) #self.tabs.append(tab) return tab
def writeSocket(self,data): if not hasattr(self, 'socket_o'): return False data = data.replace('\r\n',' ') data = data.replace('\n',' ') Debug().debug('Отправка[len: %s]: %s' % (len(data),data)) data_sended = 0 #data = bytes(data,'utf8') try: data_sended += self.socket_o.send(data) if data_sended != len(data): self._PrintGui( 'Ошибка отправки: отправленно %s байт из %s байт' % (data_sended,len(data)) ) return False #while data_sended <= len(data): # #add_size = data_size-len(data) # data_sended += self.socket_o.send(data) # print 'sended %s size %s' % (data_sended, len(data)) except socket.error , err: print (err) self._PrintGui(err) return False
def _MainWhile(self): # Комманды: 1 - сообщение чата, 2 - число онлайн, 3 - ошибка, 4 - заголовок с радио, 5 - верификация # Первыеее 2 байта это размер, а последующий 1 байт тип комманды cmd = b'' data_size = 0 data = b'' try: #TODO: Проверка соединения с чятом. (Для линукс систем больше это скорее всего) recv_tmp = self.socket_o.recv(3) if len(recv_tmp) < 1: return False if self.StopMainWhile: return False if recv_tmp is None: self._PrintGui('<font color="#800000">Ошибка: переменная recv_tmp равна none type.</font>') return False if len(recv_tmp) == 3: data_size = unpack(">H", recv_tmp[:2])[0] # Это почему то список cmd = recv_tmp[2:3] # Тут будет тип сообщения (тип комманды) else: return AOUTOR_ERROR self._PrintGui('<font color="#800000">Ошибка: recv_tmp содержит мение 3 байт!</font>') return False if data_size: pass while len(data) < data_size: add_size = data_size-len(data) data += self.socket_o.recv(add_size) except Exception , err: str_error = str(err) Debug().err("%s" % (str_error)) self._PrintGui('<font color="#800000">Error: %s</font>' % (str_error) ) self.SocketActive = False return False
def __init__(self): self.realpath = os.path.realpath(sys.argv[0]) self.iphakethe_working_dir = os.path.dirname(self.realpath) self.iphakethe_lib_dir = self.iphakethe_working_dir + "/lib" self.debug = Debug()
def analyze_image(in_file, out_dir, stats_file=False, scale=1, debug_dir=False, fix_seed=False): from lib.dir import ensure_dir_exists from lib.debug import Debug from lib.stats_recorder import Record if debug_dir: Debug.set_directory(debug_dir) if fix_seed: Debug.set_seed(1234567890) if stats_file: Record.activate() ensure_dir_exists(out_dir) from lib.timer import timeStart, timeEnd from lib.load_image import get_grayscale_image, image_as_float from skimage.morphology import medial_axis from lib.roi_detection import get_roi, corners_to_geojson from lib.polygon_mask import mask_image from lib.meanline_detection import detect_meanlines, meanlines_to_geojson from lib.threshold import flatten_background from lib.ridge_detection import find_ridges from lib.binarization import binary_image from lib.intersection_detection import find_intersections from lib.trace_segmentation import get_segments, segments_to_geojson from lib.geojson_io import save_features, save_json from lib.utilities import encode_labeled_image_as_rgb from scipy import misc import numpy as np paths = { "roi": out_dir + "/roi.json", "meanlines": out_dir + "/meanlines.json", "intersections": out_dir + "/intersections.json", "intersections_raster": out_dir + "/intersections_raster.png", "segments": out_dir + "/segments.json", "segment_regions": out_dir + "/segment_regions.png", "segment_assignments": out_dir + "/segment_assignments.json" } timeStart("get all metadata") timeStart("read image") img_gray = image_as_float(get_grayscale_image(in_file)) timeEnd("read image") print "\n--ROI--" timeStart("get region of interest") corners = get_roi(img_gray, scale=scale) timeEnd("get region of interest") timeStart("convert roi to geojson") corners_as_geojson = corners_to_geojson(corners) timeEnd("convert roi to geojson") timeStart("saving roi as geojson") save_features(corners_as_geojson, paths["roi"]) timeEnd("saving roi as geojson") print "\n--MASK IMAGE--" roi_polygon = corners_as_geojson["geometry"]["coordinates"][0] timeStart("mask image") masked_image = mask_image(img_gray, roi_polygon) timeEnd("mask image") Debug.save_image("main", "masked_image", masked_image.filled(0)) if Record.active: non_masked_values = 255 * masked_image.compressed() bins = np.arange(257) image_hist, _ = np.histogram(non_masked_values, bins=bins) Record.record("roi_intensity_hist", image_hist.tolist()) print "\n--MEANLINES--" meanlines = detect_meanlines(masked_image, corners, scale=scale) timeStart("convert meanlines to geojson") meanlines_as_geojson = meanlines_to_geojson(meanlines) timeEnd("convert meanlines to geojson") timeStart("saving meanlines as geojson") save_features(meanlines_as_geojson, paths["meanlines"]) timeEnd("saving meanlines as geojson") print "\n--FLATTEN BACKGROUND--" img_dark_removed, background = \ flatten_background(masked_image, prob_background=0.95, return_background=True, img_gray=img_gray) Debug.save_image("main", "flattened_background", img_dark_removed) masked_image = None print "\n--RIDGES--" timeStart("get horizontal and vertical ridges") ridges_h, ridges_v = find_ridges(img_dark_removed, background) ridges = ridges_h | ridges_v timeEnd("get horizontal and vertical ridges") print "\n--THRESHOLDING--" timeStart("get binary image") img_bin = binary_image(img_dark_removed, markers_trace=ridges, markers_background=background) timeEnd("get binary image") img_dark_removed = None background = None print "\n--SKELETONIZE--" timeStart("get medial axis skeleton and distance transform") img_skel, dist = medial_axis(img_bin, return_distance=True) timeEnd("get medial axis skeleton and distance transform") Debug.save_image("skeletonize", "skeleton", img_skel) print "\n--INTERSECTIONS--" intersections = find_intersections(img_bin, img_skel, dist, figure=False) timeStart("convert to geojson") intersection_json = intersections.asGeoJSON() timeEnd("convert to geojson") timeStart("saving intersections as geojson") save_features(intersection_json, paths["intersections"]) timeEnd("saving intersections as geojson") timeStart("convert to image") intersection_image = intersections.asImage() timeEnd("convert to image") Debug.save_image("intersections", "intersections", intersection_image) timeStart("save intersections raster") misc.imsave(paths["intersections_raster"], intersection_image) timeEnd("save intersections raster") print "\n--SEGMENTS--" timeStart("get segments") segments, labeled_regions = \ get_segments(img_gray, img_bin, img_skel, dist, intersection_image, ridges_h, ridges_v, figure=True) timeEnd("get segments") timeStart("encode labels as rgb values") rgb_segments = encode_labeled_image_as_rgb(labeled_regions) timeEnd("encode labels as rgb values") timeStart("save segment regions") misc.imsave(paths["segment_regions"], rgb_segments) timeEnd("save segment regions") timeStart("convert centerlines to geojson") segments_as_geojson = segments_to_geojson(segments) timeEnd("convert centerlines to geojson") timeStart("saving centerlines as geojson") save_features(segments_as_geojson, paths["segments"]) timeEnd("saving centerlines as geojson") #return (img_gray, ridges, img_bin, intersections, img_seg) # return segments # detect center lines # connect segments # output data time_elapsed = timeEnd("get all metadata") Record.record("time_elapsed", float("%.2f" % time_elapsed)) if (stats_file): Record.export_as_json(stats_file) # TODO: refactor this into some sort of status module. # For now, since this is our only problematic status, # it's hard to know what to generalize. Eventually # we might want to flag several different statuses # for specific conditions. max_segments_reasonable = 11000 if (len(segments) > max_segments_reasonable): print "STATUS>>>problematic<<<" else: print "STATUS>>>complete<<<"
class ObjectBase(object): # Creamos el objeto aquí, de esta forma sera el mismo objeto para todos los objetos que lo hereden. debug = Debug(True, DebugLevel.info)