def follow_str(ns_path, string): debug.dbg('follow_module', ns_path, string) path = None if ns_path: path = ns_path elif self.import_stmt.relative_count: path = self.get_relative_path() if path and not isinstance(path, list): path = [path] global imports_processed imports_processed += 1 if path is not None: return imp.find_module(string, path) else: debug.dbg('search_module', string, self.file_path) # Override the sys.path. It works only good that way. # Injecting the path directly into `find_module` did not work. sys.path, temp = sys_path_mod, sys.path try: i = imp.find_module(string) except ImportError: sys.path = temp raise sys.path = temp return i
def complete(self): """ An auto completer for python files. :return: list of Completion objects. :rtype: list """ path = self.module.get_path_until_cursor() path, dot, like = self._get_completion_parts(path) try: scopes = list(self._prepare_goto(path, True)) except NotFoundError: scopes = [] scope_generator = evaluate.get_names_for_scope( self.parser.user_scope, self.pos) completions = [] for scope, name_list in scope_generator: for c in name_list: completions.append((c, scope)) else: completions = [] debug.dbg('possible scopes', scopes) for s in scopes: # TODO is this really the right way? just ignore the funcs? \ # do the magic functions first? and then recheck here? if not isinstance(s, evaluate.Function): if isinstance(s, imports.ImportPath): names = s.get_defined_names(on_import_stmt=True) else: names = s.get_defined_names() for c in names: completions.append((c, s)) if not dot: # named_params have no dots call_def = self.get_in_function_call() if call_def: if not call_def.module.is_builtin(): for p in call_def.params: completions.append((p.get_name(), p)) # Do the completion if there is no path before and no import stmt. if (not scopes or not isinstance(scopes[0], imports.ImportPath)) \ and not path: # add keywords bs = builtin.Builtin.scope completions += ((k, bs) for k in keywords.get_keywords( all=True)) completions = [(c, s) for c, s in completions if settings.case_insensitive_completion and c.names[-1].lower().startswith(like.lower()) or c.names[-1].startswith(like)] needs_dot = not dot and path completions = set(completions) c = [Completion(c, needs_dot, len(like), s) for c, s in completions] return c
def find_car(img): # touples: (scale, y_start, y_stop) cfg = [ #(1.5, 0.5, 0.60), (1, 0.55, 0.65), (0.75, 0.55, 0.80), (0.5, 0.55, 0.85), ] i = 0 img_dbg = img.copy() hmap = np.zeros(img.shape[:-1], 'int') for (scale, y_start, y_stop) in cfg: i += 1 y_start = int(y_start * img.shape[0]) y_stop = int(y_stop * img.shape[0]) cv2.line(img_dbg, (10 + i * 10, y_start), (10 + i * 10, y_stop), (200, 100, 100), 4) img_s = cv2.resize(img, None, fx=scale, fy=scale) img_s = img_s[int(y_start * scale):int(y_stop * scale)] bb = np.array(slide_window(img_s)) if bb.shape[0]: bb = bb // (scale, scale, scale, scale) bb += (0, y_start, 0, y_start) bb = bb.astype('int') plot_bb(img_dbg, bb, (i * 70 % 255, i * 150 % 255, i * 32 % 255)) for b in bb: hmap[b[1]:b[3], b[0]:b[2]] += 1 dbg("heat map", hmap.astype('uint8') * 25) dbg("find_car", img_dbg) return hmap
def follow_str(ns, string): debug.dbg('follow_module', ns, string) path = None if ns: path = ns[1] elif self.import_stmt.relative_count: module = self.import_stmt.get_parent_until() path = os.path.abspath(module.path) for i in range(self.import_stmt.relative_count): path = os.path.dirname(path) global imports_processed imports_processed += 1 if path is not None: return imp.find_module(string, [path]) else: debug.dbg('search_module', string, self.file_path) # Override the sys.path. It works only good that way. # Injecting the path directly into `find_module` did not work. sys.path, temp = sys_path_mod, sys.path try: i = imp.find_module(string) except ImportError: sys.path = temp raise sys.path = temp return i
def shell_with_input(command, in_text): dbg("shell_with_input: %s" %(' '.join(command))) p = Popen(command, shell=True, stdout=PIPE, stderr=PIPE, stdin=PIPE) output, stderr = p.communicate(input=in_text) if p.returncode: raise ReturnCode(p.returncode, stderr) return output
def shell_with_input(command, in_text): dbg("shell_with_input: %s" % (' '.join(command))) p = Popen(command, shell=True, stdout=PIPE, stderr=PIPE, stdin=PIPE) output, stderr = p.communicate(input=in_text) if p.returncode: raise ReturnCode(p.returncode, stderr) return output
def str_to_val(self, errors, pos, str): dbg('trying to convert "%s" to a boolean...' % str) if str == 'true': return True elif str == 'false': return False else: err_add(errors, pos, 'TYPE_VALUE', (str, self.definition, 'not a boolean')) return None
def shell(command): """Executes command in a shell, returns stdout; prints errors to stderr""" dbg("shell: %s" %(' '.join(command))) p = Popen(command, shell=True, stdout=PIPE, stderr=PIPE) output = "\n".join(p.stdout.readlines()) if p.returncode: raise ReturnCode(p.returncode, p.stderr.readlines()) return output
def shell(command): """Executes command in a shell, returns stdout; prints errors to stderr""" dbg("shell: %s" % (' '.join(command))) p = Popen(command, shell=True, stdout=PIPE, stderr=PIPE) output = "\n".join(p.stdout.readlines()) if p.returncode: raise ReturnCode(p.returncode, p.stderr.readlines()) return output
def str_to_val(self, errors, pos, str): dbg('trying to convert "%s" to a boolean...' % str) if str == 'true': return True; elif str == 'false': return False else: err_add(errors, pos, 'TYPE_VALUE', (str, self.definition, 'not a boolean')) return None
def __call__(self, execution, evaluate_generator=False): debug.dbg('Execution recursions: %s' % execution, self.recursion_level, self.execution_count, len(self.execution_funcs)) if self.check_recursion(execution, evaluate_generator): result = [] else: result = self.func(execution, evaluate_generator) self.cleanup() return result
def str_to_val(self, errors, pos, str): try: dbg('trying to convert "%s" to an int...' % str) if str in ['min', 'max']: return str return int(str, 0) except ValueError: err_add(errors, pos, 'TYPE_VALUE', (str, self.definition, 'not an integer')) return None
def hmap2bbox(hmap, th): hmap[hmap < th] = 0 la = scipy.ndimage.measurements.label(hmap) dbg('label', la[0].astype('uint8') * 50) ret = [] for car_number in range(1, la[1] + 1): nz = (la[0] == car_number).nonzero() nzy = nz[0] nzx = nz[1] bbox = (np.min(nzx), np.min(nzy), np.max(nzx), np.max(nzy)) if (bbox[2] - bbox[0]) * (bbox[3] - bbox[1]) > 40 * 40: # filter out small boxes ret.append(bbox) return ret
def get_nested_import(self, parent): """ See documentation of `self.is_nested_import`. Generates an Import statement, that can be used to fake nested imports. """ i = self.import_stmt # This is not an existing Import statement. Therefore, set position to # None. zero = (None, None) n = parsing.Name(i.namespace.names[1:], zero, zero) new = parsing.Import(zero, zero, n) new.parent = weakref.ref(parent) evaluate.faked_scopes.append(new) debug.dbg('Generated a nested import: %s' % new) return new
def search(self, url, checker, encoding=None): found = False try: req = urllib2.urlopen(url) for line in req.readlines(): if encoding: line = line.decode(encoding, 'replace') found = self.check(line, checker) if found: dbg(checker.name+' found: '+checker.collector) break req.close() except: pass return found
def get_nested_import(self, parent): """ See documentation of `self.is_nested_import`. Generates an Import statement, that can be used to fake nested imports. """ i = self.import_stmt # This is not an existing Import statement. Therefore, set position to # 0 (0 is not a valid line number). zero = (0, 0) names = i.namespace.names[1:] n = parsing.Name(i.module, names, zero, zero, self.import_stmt) new = parsing.Import(i.module, zero, zero, n) new.parent = parent debug.dbg('Generated a nested import: %s' % new) return new
def get_nested_import(self, parent): """ See documentation of `self.is_nested_import`. Generates an Import statement, that can be used to fake nested imports. """ i = self.import_stmt # This is not an existing Import statement. Therefore, set position to # 0 (0 is not a valid line number). zero = (0, 0) n = parsing.Name(i.namespace.names[1:], zero, zero, self.import_stmt) new = parsing.Import(zero, zero, n) new.parent = parent evaluate.faked_scopes.append(new) debug.dbg('Generated a nested import: %s' % new) return new
def load_module(self, path, original_changed_time): try: pickle_changed_time = self._index[self.py_version][path] except KeyError: return None if original_changed_time is not None and pickle_changed_time < original_changed_time: # the pickle file is outdated return None with open(self._get_hashed_path(path), "rb") as f: parser_cache_item = pickle.load(f) debug.dbg("pickle loaded", path) parser_cache[path] = parser_cache_item return parser_cache_item.parser
def load_module(self, path, original_changed_time): try: pickle_changed_time = self._index[self.py_version][path] except KeyError: return None if original_changed_time is not None \ and pickle_changed_time < original_changed_time: # the pickle file is outdated return None with open(self._get_hashed_path(path), 'rb') as f: parser_cache_item = pickle.load(f) debug.dbg('pickle loaded', path) parser_cache[path] = parser_cache_item return parser_cache_item.parser
def _prepare_goto(self, goto_path, is_like_search=False): debug.dbg('start: %s in %s' % (goto_path, self.parser.scope)) user_stmt = self.parser.user_stmt if not user_stmt and len(goto_path.split('\n')) > 1: # If the user_stmt is not defined and the goto_path is multi line, # something's strange. Most probably the backwards tokenizer # matched to much. return [] if isinstance(user_stmt, parsing.Import): scopes = [self._get_on_import_stmt(is_like_search)[0]] else: # just parse one statement, take it and evaluate it stmt = self._get_under_cursor_stmt(goto_path) scopes = evaluate.follow_statement(stmt) return scopes
def detect_django_path(module_path): """ Detects the path of the very well known Django library (if used) """ result = [] while True: new = os.path.dirname(module_path) # If the module_path doesn't change anymore, we're finished -> / if new == module_path: break else: module_path = new try: with open(module_path + os.path.sep + 'manage.py'): debug.dbg('Found django path: %s' % module_path) result.append(module_path) except IOError: pass return result
def execute(command, in_text=None): """Executes command, returns stdout; prints errors to stderr""" dbg("execute: `%s`" %(' '.join(command))) if in_text is None: p = Popen(command, shell=False, stdout=PIPE, stderr=PIPE) else: p = Popen(command, shell=False, stdout=PIPE, stderr=PIPE, stdin=PIPE) dbg("execute: polling (%s)..." %(in_text)) while p.poll() is None and p.stdin is not None: dbg("execute: Sending to process stdin") p.stdin.write(in_text) dbg("execute: sleeping") sleep(0.01) output = p.stdout.read() if p.returncode: dbg("Received return code %d" %(p.returncode)) raise ReturnCode(p.returncode, p.stderr.readlines()) return output
def execute(command, in_text=None): """Executes command, returns stdout; prints errors to stderr""" dbg("execute: `%s`" % (' '.join(command))) if in_text is None: p = Popen(command, shell=False, stdout=PIPE, stderr=PIPE) else: p = Popen(command, shell=False, stdout=PIPE, stderr=PIPE, stdin=PIPE) dbg("execute: polling (%s)..." % (in_text)) while p.poll() is None and p.stdin is not None: dbg("execute: Sending to process stdin") p.stdin.write(in_text) dbg("execute: sleeping") sleep(0.01) output = p.stdout.read() if p.returncode: dbg("Received return code %d" % (p.returncode)) raise ReturnCode(p.returncode, p.stderr.readlines()) return output
def follow(self, is_goto=False): """ Returns the imported modules. """ if evaluate.follow_statement.push_stmt(self.import_stmt): # check recursion return [] if self.import_path: try: scope, rest = self._follow_file_system() except ModuleNotFound: debug.warning('Module not found: ' + str(self.import_stmt)) evaluate.follow_statement.pop_stmt() return [] scopes = [scope] scopes += itertools.chain.from_iterable( remove_star_imports(s) for s in scopes) # follow the rest of the import (not FS -> classes, functions) if len(rest) > 1 or rest and self.is_like_search: scopes = [] elif rest: if is_goto: scopes = itertools.chain.from_iterable( evaluate.get_scopes_for_name(s, rest[0], is_goto=True) for s in scopes) else: scopes = itertools.chain.from_iterable( evaluate.follow_path(iter(rest), s, s) for s in scopes) scopes = list(scopes) if self.is_nested_import(): scopes.append(self.get_nested_import(scope)) else: scopes = [ImportPath.GlobalNamespace] debug.dbg('after import', scopes) evaluate.follow_statement.pop_stmt() return scopes
def _prepare_goto(self, goto_path, is_like_search=False): """ Base for complete, goto and get_definition. Basically it returns the resolved scopes under cursor. """ debug.dbg('start: %s in %s' % (goto_path, self.parser.scope)) user_stmt = self.parser.user_stmt debug.speed('parsed') if not user_stmt and len(goto_path.split('\n')) > 1: # If the user_stmt is not defined and the goto_path is multi line, # something's strange. Most probably the backwards tokenizer # matched to much. return [] if isinstance(user_stmt, parsing.Import): scopes = [self._get_on_import_stmt(is_like_search)[0]] else: # just parse one statement, take it and evaluate it stmt = self._get_under_cursor_stmt(goto_path) scopes = evaluate.follow_statement(stmt) return scopes
def check_module(module): try: possible_stmts = module.used_names['path'] except KeyError: return builtin.get_sys_path() sys_path = list(builtin.get_sys_path()) # copy for p in possible_stmts: try: call = p.get_assignment_calls().get_only_subelement() except AttributeError: continue n = call.name if not isinstance(n, parsing.Name) or len(n.names) != 3: continue if n.names[:2] != ('sys', 'path'): continue array_cmd = n.names[2] if call.execution is None: continue exe = call.execution if not (array_cmd == 'insert' and len(exe) == 2 or array_cmd == 'append' and len(exe) == 1): continue if array_cmd == 'insert': exe_type, exe.type = exe.type, parsing.Array.NOARRAY exe_pop = exe.values.pop(0) res = execute_code(exe.get_code()) if res is not None: sys_path.insert(0, res) debug.dbg('sys path inserted: %s' % res) exe.type = exe_type exe.values.insert(0, exe_pop) elif array_cmd == 'append': res = execute_code(exe.get_code()) if res is not None: sys_path.append(res) debug.dbg('sys path added: %s' % res) return sys_path
def parse_function_doc(func): """ Takes a function and returns the params and return value as a tuple. This is nothing more than a docstring parser. """ # TODO: things like utime(path, (atime, mtime)) and a(b [, b]) -> None doc = inspect.getdoc(func) # get full string, parse round parentheses: def func(a, (b,c)) try: count = 0 debug.dbg(func, func.__name__, doc) start = doc.index('(') for i, s in enumerate(doc[start:]): if s == '(': count += 1 elif s == ')': count -= 1 if count == 0: end = start + i break param_str = doc[start + 1:end] # remove square brackets, that show an optional param ( = None) def change_options(m): args = m.group(1).split(',') for i, a in enumerate(args): if a and '=' not in a: args[i] += '=None' return ','.join(args) while True: param_str, changes = re.subn(r' ?\[([^\[\]]+)\]', change_options, param_str) if changes == 0: break except (ValueError, AttributeError): debug.dbg('no brackets found - no param') end = 0 param_str = '' param_str = param_str.replace('-', '_') # see: isinstance.__doc__ if doc is not None: r = re.search('-[>-]* ', doc[end:end + 7]) if doc is None or r is None: ret = 'pass' else: index = end + r.end() # get result type, which can contain newlines pattern = re.compile(r'(,\n|[^\n-])+') ret_str = pattern.match(doc, index).group(0).strip() # New object -> object() ret_str = re.sub(r'[nN]ew (.*)', r'\1()', ret_str) ret = Parser.map_types.get(ret_str, ret_str) if ret == ret_str and ret not in ['None', 'object', 'tuple', 'set']: debug.dbg('not working', ret_str) if ret != 'pass': ret = ('return ' if 'return' not in ret else '') + ret return param_str, ret
def complete(self): """ An auto completer for python files. :return: list of Completion objects, sorted by name and __ comes last. :rtype: list """ def follow_imports_if_possible(name): # TODO remove this, or move to another place (not used) par = name.parent if isinstance(par, parsing.Import) and not \ isinstance(self.parser.user_stmt, parsing.Import): new = imports.ImportPath(par).follow(is_goto=True) # Only remove the old entry if a new one has been found. #print par, new, par.parent if new: try: return new except AttributeError: # .name undefined pass return [name] debug.speed('complete start') path = self.module.get_path_until_cursor() path, dot, like = self._get_completion_parts(path) try: scopes = list(self._prepare_goto(path, True)) except NotFoundError: scopes = [] scope_generator = evaluate.get_names_for_scope( self.parser.user_scope, self.pos) completions = [] for scope, name_list in scope_generator: for c in name_list: completions.append((c, scope)) else: completions = [] debug.dbg('possible scopes', scopes) for s in scopes: if s.isinstance(evaluate.Function): names = s.get_magic_method_names() else: if isinstance(s, imports.ImportPath): if like == 'import': l = self.module.get_line(self.pos[0])[:self.pos[1]] if not l.endswith('import import'): continue names = s.get_defined_names(on_import_stmt=True) else: names = s.get_defined_names() for c in names: completions.append((c, s)) if not dot: # named_params have no dots call_def = self.get_in_function_call() if call_def: if not call_def.module.is_builtin(): for p in call_def.params: completions.append((p.get_name(), p)) # Do the completion if there is no path before and no import stmt. if (not scopes or not isinstance(scopes[0], imports.ImportPath)) \ and not path: # add keywords bs = builtin.Builtin.scope completions += ((k, bs) for k in keywords.get_keywords(all=True)) needs_dot = not dot and path comps = [] for c, s in set(completions): n = c.names[-1] if settings.case_insensitive_completion \ and n.lower().startswith(like.lower()) \ or n.startswith(like): if not evaluate.filter_private_variable( s, self.parser.user_stmt, n): new = api_classes.Completion(c, needs_dot, len(like), s) comps.append(new) debug.speed('complete end') return sorted( comps, key=lambda x: (x.word.startswith('__'), x.word.startswith('_'), x.word.lower()))
parser.add_argument('-f', '--frame', type=int, default=0) parser.add_argument('images', help="test image", nargs='*') args = parser.parse_args() debug.setup(args.dbgdir, args.show, in_row=3) if args.model is not None: with open(args.model, 'rb') as f: svc, scaler = pickle.load(f) else: assert (0) for fname in args.images: img = cv2.imread(fname) print(img.dtype, np.max(img), np.min(img)) dbg.set_fname(fname) dbg("input", img) print(dbg) find_car(img) dbg.step_end() if args.videoin: print("video ", args.videoin) #di.set_active(0) vid_reader = imageio.get_reader(args.videoin) fps = vid_reader.get_meta_data()['fps'] cf = CarFinder(30) if args.videoout: vid_writer = imageio.get_writer(args.videoout, fps=fps)
def complete(self): """ An auto completer for python files. :return: list of Completion objects, sorted by name and __ comes last. :rtype: list """ def follow_imports_if_possible(name): # TODO remove this, or move to another place (not used) par = name.parent if isinstance(par, parsing.Import) and not \ isinstance(self.parser.user_stmt, parsing.Import): new = imports.ImportPath(par).follow(is_goto=True) # Only remove the old entry if a new one has been found. #print par, new, par.parent if new: try: return new except AttributeError: # .name undefined pass return [name] debug.speed('complete start') path = self.module.get_path_until_cursor() if re.search('^\.|\.\.$', path): return [] path, dot, like = self._get_completion_parts(path) try: scopes = list(self._prepare_goto(path, True)) except NotFoundError: scopes = [] scope_generator = evaluate.get_names_for_scope( self.parser.user_scope, self.pos) completions = [] for scope, name_list in scope_generator: for c in name_list: completions.append((c, scope)) else: completions = [] debug.dbg('possible scopes', scopes) for s in scopes: if s.isinstance(evaluate.Function): names = s.get_magic_method_names() else: if isinstance(s, imports.ImportPath): if like == 'import': l = self.module.get_line(self.pos[0])[:self.pos[1]] if not l.endswith('import import'): continue names = s.get_defined_names(on_import_stmt=True) else: names = s.get_defined_names() for c in names: completions.append((c, s)) if not dot: # named_params have no dots call_def = self.get_in_function_call() if call_def: if not call_def.module.is_builtin(): for p in call_def.params: completions.append((p.get_name(), p)) # Do the completion if there is no path before and no import stmt. if (not scopes or not isinstance(scopes[0], imports.ImportPath)) \ and not path: # add keywords bs = builtin.Builtin.scope completions += ((k, bs) for k in keywords.get_keywords( all=True)) needs_dot = not dot and path comps = [] for c, s in set(completions): n = c.names[-1] if settings.case_insensitive_completion \ and n.lower().startswith(like.lower()) \ or n.startswith(like): if not evaluate.filter_private_variable(s, self.parser.user_stmt, n): new = api_classes.Completion(c, needs_dot, len(like), s) comps.append(new) debug.speed('complete end') return sorted(comps, key=lambda x: (x.word.startswith('__'), x.word.startswith('_'), x.word.lower()))
def __init__(self, img, vis=False): self.img = img self.ppc = 8 self.ws = 64 # windows size self.hls = cv2.cvtColor(img, cv2.COLOR_BGR2HLS) hls = self.hls p = { 'block_norm': 'L2-Hys', 'orientations': 4, 'transform_sqrt': True, 'pixels_per_cell': (self.ppc, self.ppc), 'feature_vector': False } if vis: dbg("img H", hls[:, :, 0]) dbg("img L", hls[:, :, 1]) dbg("img S", hls[:, :, 2]) print(dbg) self.hog0, v = hog(hls[:, :, 0], **p, visualise=True) dbg("HOG(H)", v) self.hog1, v = hog(hls[:, :, 1], **p, visualise=True) dbg("HOG(L)", v) self.hog2, v = hog(hls[:, :, 2], **p, visualise=True) dbg("HOG(S)", v) else: self.hog0 = hog(hls[:, :, 0], **p) self.hog1 = hog(hls[:, :, 1], **p) self.hog2 = hog(hls[:, :, 2], **p)
f = np.concatenate([self.get_hog(xi, yi)]) return f if __name__ == "__main__": parser = argparse.ArgumentParser(description='train car classifier') parser.add_argument('images', nargs='+') parser.add_argument('-d', '--dbgdir', nargs='?') parser.add_argument('-s', '--show', help="show images", action='store_true') args = parser.parse_args() debug.setup(args.dbgdir, args.show, in_row=6) print("GOT DBG") print(dbg) print(debug.dbg) print(args) for fname in args.images: dbg.set_fname(fname) img = cv2.imread(fname) dbg("input", img) CarFrame(img, True) #print("STEP END") if dbg.step_end(): break