def __init__(self, parent): Gtk.Box.__init__(self, False, 0) self.parent = parent try: current_locale, encoding = locale.getdefaultlocale() locale_path = os.path.join(os.path.abspath(os.path.dirname(__file__)), 'locale') translate = gettext.translation (cn.App.application_shortname, locale_path, [current_locale] ) _ = translate.gettext except FileNotFoundError: _ = str chart = ch.Chart() self.function = fn.Function() algo = al.Algorithm() self.algo_parameters = apm.AlgorithmParameters(self.function, algo, chart) self.func_parameters = fpm.FunctionParameters(self.function, algo, chart) self.result = rsl.Result(self.function, algo) hpaned = Gtk.Paned() hpaned.set_position(800) hpaned.add1(chart.sw) vbox = Gtk.VBox() vbox.add(self.algo_parameters.frame) vbox.add(self.func_parameters.frame) vbox.add(self.result.frame) hpaned.add2(vbox) self.pack_start(hpaned, True, True, 0)
def _parse_api_info(api_info): """ Breaks up API info into a more useable form. :param api_info: A list of dictionaries as returned by Galah. :returns: A dictionary such that each value is a :class:`function.Function` object, and each key is the name of the function. """ result = {} for command in api_info: parameters = [] for i in command.get("args", []): if i.get("takes_file", False): parameter_type = file else: parameter_type = str parameters.append( function.Function.Parameter( name=i["name"], default_value=i.get("default_value"), param_type=parameter_type)) result[command["name"]] = function.Function(name=command["name"], params=parameters) return result
def _set_current_function(self, ctxt, func): try: if func: func = function.Function(binaryview.BinaryView(handle = core.BNGetFunctionData(func)), core.BNNewFunctionReference(func)) else: func = None self.perform_set_current_function(func) except: log.log.log_error(traceback.format_exc())
def function(self): """Basic block function (read-only)""" if self._func is not None: return self._func func = core.BNGetBasicBlockFunction(self.handle) if func is None: return None self._func = function.Function(self.view, func) return self._func
def _get_incoming_flag_value(self, ctxt, reg, func, result): try: func_obj = function.Function(binaryview.BinaryView(handle = core.BNGetFunctionData(func)), core.BNNewFunctionReference(func)) reg_name = self.arch.get_reg_name(reg) api_obj = self.perform_get_incoming_flag_value(reg_name, func_obj)._to_api_object() except: log.log_error(traceback.format_exc()) api_obj = function.RegisterValue()._to_api_object() result[0].state = api_obj.state result[0].value = api_obj.value
def _function_action(cls, view, func, action): try: file_metadata = filemetadata.FileMetadata( handle=core.BNGetFileForView(view)) view_obj = binaryview.BinaryView( file_metadata=file_metadata, handle=core.BNNewViewReference(view)) func_obj = function.Function(view_obj, core.BNNewFunctionReference(func)) action(view_obj, func_obj) except: log.log_error(traceback.format_exc())
def visit_FuncDefNode(self, node, context): res = RTResult() func_name = node.var_name_tok.value if node.var_name_tok else None body_node = node.body_node arg_names = [arg_name.value for arg_name in node.arg_name_tok] func_value = fn.Function(func_name, body_node, arg_names).set_context(context).set_pos(node.pos_start, node.pos_end) if node.var_name_tok: context.symbol_table.set(func_name, func_value) return res.success(func_value)
def optimize_callback(req): # rospy.loginfo("I heard %s",req) global dyn_tuner if dyn_tuner != None: return False values = req.observation_values start = json.loads(req.start_signals) end = json.loads(req.end_signals) params = json.loads(req.experiments[0].parameters) func = req.experiments[0].objective funcs = function.load_functions() tasks = [(funcs[task.function], task.args, task.return_var, task.has_script, task.script) if not task.has_script else (None, [], None, task.has_script, task.script) for task in func.tasks] obj_func = function.Function(tasks=tasks, retvar=func.return_var, types=func.arg_types) obj_func = bypasser(obj_func) # src_topic = req.src_topic # dst_topic = req.dst_topic src_topic = "/haptix_deka_controller/command" dst_topic = "/haptix_deka_controller/command" src_bag = req.src_bag dst_bag = req.dst_bag dyn_tuner = dynamic_tuner.dynamic_tuner(params, values, src_bag, dst_bag, src_topic, dst_topic, objective=obj_func, start_signal=start, end_signal=end) # # TODO: ROSLOG # print "starting..." # dyn.tune_params() return True
def create_selected(self): # Create the appropriate OnClick class for the selected protocol type clickType = ItemSelectOnClick(self.selectedCreateType) if self.selectedOption != "": clickType = ItemSelectOnClick(self.selectedOption) if self.selectedOption == "Integer": clickType = ItemValueSelectOnClick(self.selectedOption) # Create the appropriate protocol for the type selected if self.selectedCreateType == "Variable": if self.selectedOption == "": return self.protocolItems.append( protocolitem.ProtocolItem( variable.Variable(self.selectedOption), clickType)) if self.selectedCreateType == "Function" and self.selectedOption != "": if self.selectedOption == "": return arguments = soldier.Soldier.commands[self.selectedOption] self.protocolItems.append( protocolitem.ProtocolItem( function.Function(arguments[0], self.selectedOption, arguments[1], arguments[2]), clickType)) if self.selectedCreateType == "If": self.protocolItems.append( protocolitem.ProtocolItem(function.IfStatement(), clickType)) if self.selectedCreateType == "While": self.protocolItems.append( protocolitem.ProtocolItem(function.WhileLoop(), clickType)) if self.selectedCreateType == "Foreach": self.protocolItems.append( protocolitem.ProtocolItem(function.ForeachLoop(), clickType)) # Update part of the master list self.buttons = self.regularButtons + self.createButtons + self.protocolItems self.selectedItemIndex = len(self.buttons) - 1 self.selectedItemType = self.selectedOption # Simulate clicking a value for the protocol item, so that it has some initial value if self.selectedCreateType in self.valueButtons.keys(): self.valueButtons[self.selectedCreateType][0].set_visible(True) self.valueButtons[self.selectedCreateType][0].clicked(self) self.valueButtons[self.selectedCreateType][0].unclicked(self) self.valueButtons[self.selectedCreateType][0].set_visible(False) if self.selectedOption in self.valueButtons.keys(): self.valueButtons[self.selectedOption][0].set_visible(True) self.valueButtons[self.selectedOption][0].clicked(self) self.valueButtons[self.selectedOption][0].unclicked(self) self.valueButtons[self.selectedOption][0].set_visible(False) self.select_create_type("", 0) self.select_item("")
def expression_functiondef(p): if (isinstance(function.getfunction(p[1].getstr()), function.PycFunction) != True): if (isinstance(p[3], typed.ListCodeBlock)): if (isinstance(p[2], typed.ListCodeBlock)): function.createfunction( function.Function(p[1].getstr(), p[2].evalv(), p[3].evalv())) else: function.createfunction( function.Function(p[1].getstr(), [p[2].evalv()], p[3].evalv())) else: if (isinstance(p[2], typed.ListCodeBlock)): function.createfunction( function.Function(p[1].getstr(), p[2].evalv(), [p[3].evalv()])) else: function.createfunction( function.Function(p[1].getstr(), [p[2].evalv()], [p[3].evalv()])) else: raise ValueError("Can't overwrite default function")
def _recognize_medium_level_il(self, ctxt, data, func, il): try: file_metadata = filemetadata.FileMetadata( handle=core.BNGetFileForView(data)) view = binaryview.BinaryView(file_metadata=file_metadata, handle=core.BNNewViewReference(data)) func = function.Function(view, handle=core.BNNewFunctionReference(func)) il = mediumlevelil.MediumLevelILFunction( func.arch, handle=core.BNNewMediumLevelILFunctionReference(il)) return self.recognize_medium_level_il(view, func, il) except: log.log_error(traceback.format_exc()) return False
def _low_level_il_instruction_action(cls, view, func, instr, action): try: file_metadata = filemetadata.FileMetadata( handle=core.BNGetFileForView(view)) view_obj = binaryview.BinaryView( file_metadata=file_metadata, handle=core.BNNewViewReference(view)) owner = function.Function(view_obj, core.BNGetLowLevelILOwnerFunction(func)) func_obj = lowlevelil.LowLevelILFunction( owner.arch, core.BNNewLowLevelILFunctionReference(func), owner) action(view_obj, func_obj[instr]) except: log.log_error(traceback.format_exc())
def _function_is_valid(cls, view, func, is_valid): try: if is_valid is None: return True file_metadata = filemetadata.FileMetadata( handle=core.BNGetFileForView(view)) view_obj = binaryview.BinaryView( file_metadata=file_metadata, handle=core.BNNewViewReference(view)) func_obj = function.Function(view_obj, core.BNNewFunctionReference(func)) return is_valid(view_obj, func_obj) except: log.log_error(traceback.format_exc()) return False
class Test(unittest.TestCase): jive = function.Function(webdriver) robot = function.Function(webdriver) def test1_VerifyGUI(self): Test.jive.open(data.driver, data.jivePath) version = Test.jive.get(data.jiveVersion) self.assertEqual(data.jiveVersion, version) def test2_FindNoneRobot(self): Test.jive.click(data.findRobot) ip = Test.jive.get(data.ipRobotBox) self.assertEqual(data.ipNone, ip) def test3_FindRobot(self): Test.robot.open(data.driver, data.robotPath) Test.jive.click(data.findRobot) ip = Test.jive.get(data.ipRobotBox) self.assertEqual(data.ipRobot, ip) def test4_ConnectRobot(self): Test.jive.click(data.connect) status = Test.jive.get(data.connected) self.assertEqual(data.connected, status) def test5_ConnectRobotLifeTime(self): Test.robot.close() status = Test.jive.get(data.disconnected) result = [status] Test.robot.open(data.driver, data.robotPath) _status = Test.jive.get(data.connected) result.append(_status) self.assertEqual([data.disconnected, data.connected], result) # End testing Test.jive.close() Test.robot.close()
def _medium_level_il_function_action(cls, view, func, action): try: file_metadata = filemetadata.FileMetadata( handle=core.BNGetFileForView(view)) view_obj = binaryview.BinaryView( file_metadata=file_metadata, handle=core.BNNewViewReference(view)) owner = function.Function( view_obj, core.BNGetMediumLevelILOwnerFunction(func)) func_obj = mediumlevelil.MediumLevelILFunction( owner.arch, core.BNNewMediumLevelILFunctionReference(func), owner) action(view_obj, func_obj) except: log.log_error(traceback.format_exc())
def _low_level_il_function_is_valid(cls, view, func, is_valid): try: if is_valid is None: return True file_metadata = filemetadata.FileMetadata( handle=core.BNGetFileForView(view)) view_obj = binaryview.BinaryView( file_metadata=file_metadata, handle=core.BNNewViewReference(view)) owner = function.Function(view_obj, core.BNGetLowLevelILOwnerFunction(func)) func_obj = lowlevelil.LowLevelILFunction( owner.arch, core.BNNewLowLevelILFunctionReference(func), owner) return is_valid(view_obj, func_obj) except: log.log_error(traceback.format_exc()) return False
def _get_parameter_var_for_incoming_var(self, ctxt, in_var, func, result): try: if func is None: func_obj = None else: func_obj = function.Function( binaryview.BinaryView(handle=core.BNGetFunctionData(func)), core.BNNewFunctionReference(func)) in_var_obj = function.Variable(func_obj, in_var[0].type, in_var[0].index, in_var[0].storage) out_var = self.perform_get_parameter_var_for_incoming_var( in_var_obj, func_obj) result[0].type = out_var.source_type result[0].index = out_var.index result[0].storage = out_var.storage except: log.log_error(traceback.format_exc()) result[0].type = in_var[0].type result[0].index = in_var[0].index result[0].storage = in_var[0].storage
try: for i in xrange(tests): inp = start + i * interval sum_diff += abs(int(inp) - int(f(inp))**2) #compares the input to the square of the estimated function if sum_diff > 1000000: return 1000000 return sum_diff / tests #average difference except: return 99999 #initate function set funcs = [] #set of functions to be evolved for i in xrange(N): funcs.append(function.Function(NUM_VARS)) for j in xrange(10): #start with 7 mutations funcs[i].mutateFunction() funcs[i].update_func() print "initial functions created" GENERATIONS = 50 tester = function.FitnessTester(math.sqrt, 1, 10000, 30) for g in xrange(GENERATIONS): print "generation " + str(g) + " begin" #fitness assignment print "beginning fitness assignment" t0 = time.time() for i in xrange(N): #funcs[i].setFitness(meanDiffSqrt(funcs[i].func, 0, 1000, 50))
def execute_lines(line_mgr, env, executing_constructor=False, supplied_hooks=None): """ Executes lines of code in a given environment. """ prgm_counter = 0 cond_flag = False env.line_mgr = line_mgr line_mgr.classify_statements() while prgm_counter < len(line_mgr): env.prgm_counter = prgm_counter line = line_mgr[prgm_counter] directive = execute_statement(line_mgr.get_line_data(prgm_counter), executing_constructor, env) if directive is not None: if directive[0] == ':cond': cond_flag = eval_parentheses(directive[1], env) prgm_counter += 1 elif directive[0] == ':j': prgm_counter = int(directive[1]) elif (cond_flag and directive[0] == ':jt') or \ ((not cond_flag) and directive[0] == ':jf'): prgm_counter = int(directive[1]) elif directive[0] == ':inc': var_name = directive[1] env.update_numeric(var_name, 1) prgm_counter += 1 elif directive[0] == ':dec': var_name = directive[1] env.update_numeric(var_name, -1) prgm_counter += 1 elif directive[0] == ':skiptoelse': # Nothing was thrown in this try clause _, j = find_next_end_else(line_mgr, prgm_counter + 1, False) prgm_counter = j + 1 elif directive[0] == ':hook': env.activate_hook(directive[1], supplied_hooks) prgm_counter += 1 elif directive[0] == 'return': if directive[1] == 'this': # Create a user-defined object. # Do not use env.pop() because the function # will automatically remove the stack frame # upon completion. obj = env.last() # Allow the object to refer to itself # TODO : assign the 'this' variable at the start # of scope creation, not when it is returned. obj['this'] = obj for key, method in obj.items(): if key == '$eq' and method.__class__ is function.Function and env.has_hook( method): # Methods must remember the environment of the object # where they were defined. obj[key] = method.supply(env) return obj else: value = eval_parentheses(directive[1], env) # Functions that were defined in the current scope # using a 'sub' statement or 'func' statement # must be supplied with the current environment, # so that closures work correctly. # This does not apply to functions that were passed # in as an argument, or functions produced by other means. # In other words, existing function objects should not # be re-supplied with the current environment, only newly # created ones. if function.is_function(value) and env.has_hook(value): # TODO : instead of supplying an environment when a defined # function is returned, supply it when the function is first # defined. This will allow for more complex return values, # such as a list of functions, to operate correctly as closures. value = value.supply(env) return value elif directive[0] == 'try': env.exception_push(prgm_counter) prgm_counter += 1 elif directive[0] == 'throw': mirage = 'mirage' if len(directive) == 2 and (directive[1].startswith(mirage + ' ') or \ directive[1].startswith(mirage + '\t')): constructor_call = directive[1][len(mirage) + 1:].lstrip() directive[1] = constructor_call i = constructor_call.find('(') if i == -1: throw_exception_with_line( 'InvalidSyntax', 'mirage keyword must be followed by a constructor call', line_mgr, prgm_counter) class_name = constructor_call[:i] new_class = None class_body = [ '$type="{0}"'.format(class_name), 'return this' ] if i + 1 < len(constructor_call) and constructor_call[ i + 1] == ')': # No arguments new_class = function.Function(class_name, [], class_body) else: # 1 argument new_class = function.Function(class_name, ['message'], class_body) env.frames[0][class_name] = new_class # TODO : this class should inherit from Exception, not Object. env.new_type(['Object'], class_name) last_prgm_counter = prgm_counter prgm_counter = env.exception_pop() if prgm_counter is None: throw_exception_with_line( 'UncaughtException', 'Thrown value ' + str(directive[1:]), line_mgr, last_prgm_counter) else: original_counter = prgm_counter # Look for a matching catch statement prgm_counter = find_catch(directive, line_mgr, prgm_counter, env) if prgm_counter is None: # We can't find a catch statement. # Let the exception bubble up from its current scope. env.exception_push(original_counter) return Trigger(directive[1]) elif directive[0] in ['catch', 'else']: kind, j = find_next_end_else(line_mgr, prgm_counter + 1, True) # Travel to the next end prgm_counter = j + 1 elif directive[0] == 'end': # This is an isolated end statement. Ignore it. prgm_counter += 1 else: prgm_counter += 1 else: prgm_counter += 1 # All functions should return something, # which is null for 'void' functions. return None
def img_aug(image_dir, xml_dir, image_txt, save_img_dir, save_xml_dir): fun = FUN.Function() #method_all = ["inverse", "elastic", "blur", "noise", "flip", "rotate", "translate", "zoom", "invertColor", "enhance", "contrastAndBright"] method = [ "inverse", "enhance", "contrastAndBright", "invertColor", "elastic", "blur", "noise", "flip", "rotate", "translate", "zoom" ] print image_txt #print 'thread %s starts.' % threading.current_thread().name num = 0 #处理txt对应的图片 with codecs.open(image_txt, 'rb', "utf-8") as ann_file: lines = ann_file.readlines() print(len(lines)) for l in lines: lst = l.strip().split() print lst[0] img_name11 = lst[0] + '.jpg' img_path = image_dir + img_name11 if not os.path.exists(img_path): continue #print img_path img = cv2.imread(img_path) lst1 = img_name11.strip().split("/") img_name = lst1[0] #index = img_name[:len(img_name)-4] xml_path = xml_dir + lst[0] + '.xml' #index + ".xml" #print xml_path if not os.path.exists(xml_path): continue if img is None: continue num = num + 1 #print num xmin, ymin, xmax, ymax = read_xml(xml_path) '''img2 = img.copy() for i in range(len(xmin)): cv2.rectangle (img2,(xmin[i],ymin[i]),(xmax[i],ymax[i]),(0,0,255),1) cv2.imwrite("test.jpg", img2)''' w = img.shape[1] h = img.shape[0] result = None #随机选择增强方式进行增强 if 0: #表示处理, 反色 result = fun.inverse(img) if result is None: continue image_height = result.shape[0] image_width = result.shape[1] img_name1 = img_name[:len(img_name) - 4] + "_inverse.jpg" save_path = save_dir + "inverse/img/" + img_name1 save_path_xml = save_dir + "inverse/xml/" + img_name[:len( img_name) - 4] + "_inverse.xml" cv2.imwrite(save_path, result) write_xml(xml_path, save_path_xml, xmin, ymin, xmax, ymax, image_width, image_height) a = np.random.rand(1) b = 0 if a > 0.8: b = 1 else: b = 0 if g1 and b: #增强 f = get_index() if f and (result is not None): img = result.copy() if 0: #0 == random.randint(0, 2): result = fun.enhance(img, 0) #type=0(直方图均衡化) type=3(gamma变换) else: result = fun.enhance(img, 3) if result is None: continue image_height = result.shape[0] image_width = result.shape[1] img_name1 = img_name[:len(img_name) - 4] + "_enhance" + str(loopNum) + ".jpg" save_path = save_img_dir + img_name1 #print "\n" #print save_path save_path_xml = save_xml_dir + img_name[:len(img_name) - 4] + "_enhance" + str( loopNum) + ".xml" cv2.imwrite(save_path, result) write_xml(xml_path, save_path_xml, xmin, ymin, xmax, ymax, image_width, image_height) a = np.random.rand(1) b = 0 if a > 0.8: b = 1 else: b = 0 if g2 and b: #b: #对比度、亮度 mean = getImgMean(img) c = random.randint(0, 2) if c == 0 and 1: alpha = (1.8 - 0.0) * (1 - mean / 255) beta = random.randint(-10, -1) elif c == 1 and 0: alpha = (3.0 - 0.0) * (1 - mean / 255) beta = random.randint(-20, 20) elif c == 2 and 0: alpha = (3.0 - 0.5) * (1 - mean / 255) beta = random.randint(-10, 10) f = get_index() if f and (result is not None): img = result.copy() result = fun.contrastAndBright(img, alpha, beta) if result is None: continue image_height = result.shape[0] image_width = result.shape[1] img_name1 = img_name[:len(img_name) - 4] + "_contrastAndBright" + str( loopNum) + ".jpg" save_path = save_img_dir + img_name1 save_path_xml = save_xml_dir + img_name[:len( img_name) - 4] + "_contrastAndBright" + str( loopNum) + ".xml" cv2.imwrite(save_path, result) write_xml(xml_path, save_path_xml, xmin, ymin, xmax, ymax, image_width, image_height) if g3 and get_index(): #颜色变换,针对彩色,只改变hsv的h分量 f = get_index() if f and (result is not None): img = result.copy() min_h = -5 max_h = 5 result = fun.invertColor(img, min_h, max_h) if result is None: continue image_height = result.shape[0] image_width = result.shape[1] img_name1 = img_name[:len(img_name) - 4] + "_invertColor" + str( loopNum) + ".jpg" save_path = save_img_dir + img_name1 save_path_xml = save_xml_dir + img_name[:len( img_name) - 4] + "_invertColor" + str(loopNum) + ".xml" cv2.imwrite(save_path, result) write_xml(xml_path, save_path_xml, xmin, ymin, xmax, ymax, image_width, image_height) if 0: #弹性变形,主要针对手写字体 result = fun.elastic(img, kernel_dim=31, sigma=6, alpha=47) if result is None: continue image_height = result.shape[0] image_width = result.shape[1] img_name1 = img_name[:len(img_name) - 4] + "_elastic.jpg" save_path = save_dir + "elastic/img/" + img_name1 save_path_xml = save_dir + "elastic/xml/" + img_name[:len( img_name) - 4] + "_elastic.xml" cv2.imwrite(save_path, result) write_xml(xml_path, save_path_xml, xmin, ymin, xmax, ymax, image_width, image_height) a = np.random.rand(1) b = 0 if a > 0.8: b = 1 else: b = 0 if g4 and b: #模糊 f = get_index() if f and (result is not None): img = result.copy() type = random.randint(0, 2) result = fun.blur( img, kernel_dim=5, sigma=1.5, type=type) #type:(0=blur,1= GaussianBlur, 2= medianBlur) if result is None: continue image_height = result.shape[0] image_width = result.shape[1] img_name1 = img_name[:len(img_name) - 4] + "_blur" + str(loopNum) + ".jpg" save_path = save_img_dir + img_name1 save_path_xml = save_xml_dir + img_name[:len(img_name) - 4] + "_blur" + str( loopNum) + ".xml" cv2.imwrite(save_path, result) write_xml(xml_path, save_path_xml, xmin, ymin, xmax, ymax, image_width, image_height) a = np.random.rand(1) b = 0 if a > 0.8: b = 1 else: b = 0 if g5 and b: #噪音 f = get_index() if f and (result is not None): img = result.copy() type = 0 #random.randint(0, 2) result = fun.noise(img, param=0.03, type=type) #type:(0=高斯,1=椒盐噪声) if result is None: continue image_height = result.shape[0] image_width = result.shape[1] img_name1 = img_name[:len(img_name) - 4] + "_noise" + str(loopNum) + ".jpg" save_path = save_img_dir + img_name1 save_path_xml = save_xml_dir + img_name[:len(img_name) - 4] + "_noise" + str( loopNum) + ".xml" cv2.imwrite(save_path, result) write_xml(xml_path, save_path_xml, xmin, ymin, xmax, ymax, image_width, image_height) if 0: #镜像 flipCode = 1 result = fun.flip( img, flipCode=flipCode ) #flipCode:(0=沿x轴翻转(上下),>0 沿y轴翻转(左右),<0 180旋转) if result is None: continue image_height = result.shape[0] image_width = result.shape[1] img_name1 = img_name[:len(img_name) - 4] + "_flip.jpg" save_path = save_dir + "flip/img/" + img_name1 save_path_xml = save_dir + "flip/xml/" + img_name[:len( img_name) - 4] + "_flip.xml" cv2.imwrite(save_path, result) xmin1, ymin1, xmax1, ymax1 = get_flip(img, flipCode, xmin, ymin, xmax, ymax) write_xml(xml_path, save_path_xml, xmin1, ymin1, xmax1, ymax1, image_width, image_height) a = np.random.rand(1) b = 0 if a > 0.8: b = 1 else: b = 0 if g6 and b: #旋转,改 f = get_index() if f and (result is not None): img = result.copy() angle = random.randint( -2, 2) #random.randint(-5,5)#random.random()*30 #0~5, 角度 type = 1 #改 scale = 1.0 result = fun.rotate(img, angle, scale=scale, type=type) #type:(0=旋转不带放大,1=旋转带放大) if result is None: continue image_height = result.shape[0] image_width = result.shape[1] img_name1 = img_name[:len(img_name) - 4] + "_rotate" + str(loopNum) + ".jpg" save_path = save_img_dir + img_name1 save_path_xml = save_xml_dir + img_name[:len(img_name) - 4] + "_rotate" + str( loopNum) + ".xml" cv2.imwrite(save_path, result) #为了查看旋转后的效果,将XML的结果画在图中验证 '''result1=img.copy() for i in xrange(len(xmin)): cv2.rectangle(result1, (xmin[i],ymin[i]), (xmax[i],ymax[i]), (255, 255, 255), 2) cv2.imwrite('org.jpg', result1)''' xmin1, ymin1, xmax1, ymax1 = get_rotate_change( img, result, angle, xmin, ymin, xmax, ymax, scale) write_xml(xml_path, save_path_xml, xmin1, ymin1, xmax1, ymax1, image_width, image_height) #为了查看旋转后的效果,将XML的结果画在图中验证 '''result2=result.copy() for i in xrange(len(xmin)): cv2.rectangle(result2, (xmin1[i],ymin1[i]), (xmax1[i],ymax1[i]), (255, 255, 255), 2) cv2.imwrite('rota1.jpg', result2)''' if g7 and get_index(): #平移 f = get_index() if f and (result is not None): img = result.copy() x_trans = random.randint(0, 10) y_trans = random.randint(0, 10) #print x_trans,y_trans type = 0 result = fun.translate( img, x_trans, y_trans, type=type) #type:(0=平移后大小不变,1=平移后大小改变,从图的下方、右边裁剪) if result is None: continue image_height = result.shape[0] image_width = result.shape[1] img_name1 = img_name[:len(img_name) - 4] + "_translate" + str(loopNum) + ".jpg" save_path = save_img_dir + img_name1 save_path_xml = save_xml_dir + img_name[:len( img_name) - 4] + "_translate" + str(loopNum) + ".xml" cv2.imwrite(save_path, result) #print (x_trans,y_trans) xmin1, ymin1, xmax1, ymax1 = get_translate( img, x_trans, y_trans, xmin, ymin, xmax, ymax, type) if 0 == type: write_xml(xml_path, save_path_xml, xmin1, ymin1, xmax1, ymax1, image_width, image_height) if 1 == type: write_xml(xml_path, save_path_xml, xmin1, ymin1, xmax1, ymax1, image_width, image_height) if 0: #变焦/拉伸 p1x = random.randint(0, 10) p1y = random.randint(0, 10) #print p1x,p1y p2x = img.shape[1] - 1 - p1x p2y = img.shape[0] - 1 - p1y result = fun.zoom(img, p1x, p1y, p2x, p2y) #按照区域(p1x, p1y, p2x, p2y)crop,并放大到原图 if result is None: continue image_height = result.shape[0] image_width = result.shape[1] img_name1 = img_name[:len(img_name) - 4] + "_zoom.jpg" save_path = save_dir + "zoom/img/" + img_name1 save_path_xml = save_dir + "zoom/xml/" + img_name[:len( img_name) - 4] + "_zoom.xml" cv2.imwrite(save_path, result) xmin1, ymin1, xmax1, ymax1 = get_zoom(img, result, p1x, p1y, p2x, p2y, xmin, ymin, xmax, ymax) write_xml(xml_path, save_path_xml, xmin1, ymin1, xmax1, ymax1, image_width, image_height)
def parsing(self, string): 'Main function called to read the content of the input' if string.count('=') != 1: errors.usage() return string_list = string.split('=') var_name = string_list[0].replace(" ", "") var_value = string_list[1].replace(" ", "") if var_value == '?': var_name, var_value = var_value, var_name if var_name.isalpha() or var_name == '?': if var_name == 'i': errors.var_name(var_name) return if '[' in var_value: m = self.check_expr_matrix(var_value) if m: m = self.replace_var(m, "") if not m: return m1 = self.matrix_calculation(m) if m1: if var_name != '?': self.matrixs[var_name.lower()] = m1 print(m1.str_matrix("\n")) else: value = self.check_expr(var_value, "") if value and value[-1] == '-': errors.operator('-') if not value or value[-1] == '-': return a = False for el in value: if isinstance(el, list): a = True break if not a: if '**' in value: errors.operator('**') return rp = rpn.shunting(value) if not rp: return var = rpn_eval.eval_postfix(rp[-1][2].split()) if not var: return if var_name != '?': self.variables[var_name.lower()] = var print(var.str_comp()) else: m1 = self.matrix_calculation(value) if m1: if var_name != '?': self.matrixs[var_name.lower()] = m1 print(m1.str_matrix("\n")) elif '(' in var_name: if var_name.count(')') != 1 or var_name.count(')') != 1: errors.function_name(var_name) return func_name, unknown = check.check_function_name(var_name) char = '' while var_value[-1] == ' ': var_value = var_value[:-1] if var_value[-1] == '?': char = '?' var_value = var_value[:-1] if len(var_value) > 0: func_expr = func_tools.format(var_value) else: func_expr = [] if not func_expr: return if len(func_expr) > 0: func_expr = self.replace_var(func_expr, unknown) if not func_expr: return if len(func_expr) > 0: func_expr = self.check_function_expr(func_expr, unknown, var_name) if not func_expr: return if '**' in func_expr: errors.operator('**') return if unknown not in func_expr: rp = rpn.shunting(func_expr) if not rp: return func_expr_2 = rpn_eval.eval_postfix(rp[-1][2].split()) if not func_expr_2: return func_expr = func_expr_2.str_comp() if char == '?' and len(func_expr) > 0: if func_name.lower() not in self.functions: errors.function(func_name) return self.functions[func_name.lower()].resolve(func_expr) elif char == '?' and len(func_expr) == 0: if func_name.lower() not in self.functions: errors.function(func_name) return self.functions[func_name.lower()].print_function() else: f = func.Function(func_name, unknown, func_expr) self.functions[func_name.lower()] = f f.print_function() else: errors.var_name(var_name)
def add_native_functions(self, context: execution_context.ExecutionContext): for name, native_fn in native_functions.FUNCTIONS.items(): fn = function.Function(name, [], [], native_fn) context.declare(name, 'native_function') context.assign(name, fn)
def function(self): """Basic block function (read-only)""" func = core.BNGetBasicBlockFunction(self.handle) if func is None: return None return function.Function(self.view, func)
# ---------- logger.py Loggerクラスのメソッド ---------- # ・loggging(pop, total_evals) # [total_evals, pop.x, pop.f] を配列で格納 # ・outlog(pop, total_evals) # 格納した情報をcsv出力し,最終的な解の評価値を標準出力 # ----------------------------------------------------- # 実行処理 def run(opt, cnf, fnc, log): opt.initializeSolutions() # 初期化 log.logging(opt.pop, fnc.total_evals) # 初期個体群ログ while fnc.total_evals < cnf.max_evals: # 評価回数上限まで実行 opt.getNextPopulation() # 次世代個体群生成 log.logging(opt.pop, fnc.total_evals) # 次世代個体群ログ log.outLog(opt.pop, fnc.total_evals) # ログ出力(trial'n'.csv) """ main (最初に実行) """ if __name__ == '__main__': cnf = cf.Configuration() # configurationインスタンス生成 for i in range(len(cnf.prob_name)): # 関数の個数だけ探索 log = lg.Logger(cnf, cnf.prob_name[i]) # loggerインスタンス生成 fnc = fc.Function(cnf.prob_name[i], cnf.prob_dim) # 探索する関数のインスタンス生成 for j in range(cnf.max_trial): # 試行回数まで実行 fnc.resetTotalEvals() # 総評価回数(functionクラス内変数)リセット cnf.setRandomSeed(seed=j+1) # ランダムシード値設定 opt = op.GeneticAlgorithm(cnf, fnc) # optimizerインスタンス生成 run(opt, cnf, fnc, log) # 探索実行 sts = lg.Statistics(cnf, fnc, log.path_out, log.path_trial) # 関数ごとの統計を作成 sts.outStatistics() # 統計出力(all_trials.csv, statistics.csv)
def execute(self, context: execution_context.ExecutionContext): fn = function.Function(self.name, self.parameters, self.defaults, self.body) context.declare(self.name, 'function') context.assign(self.name, fn)
class Main: f = function.Function() e = 0.001 def __init__(self, epsilon=0.001): e = epsilon def newtons_method(self, start_approximation, func): while start_approximation > 0\ and ((self.f.function(0)) * (self.f.function(start_approximation)) >= 0): start_approximation -= 0.005 n = 0 while abs(func.function(start_approximation)) > self.e: n += 1 start_approximation -= (func.function(start_approximation) ) / func.derivative(start_approximation) return start_approximation, n def fixed_point_iteration_method(self, start_approximation, func): while start_approximation > 0\ and ((self.f.function(0)) * (self.f.function(start_approximation)) >= 0): start_approximation -= 0.005 n = 0 while abs(func.function(start_approximation)) > self.e: n += 1 start_approximation += func.function(start_approximation) return start_approximation, n def fixed_point_iteration_method_reverse(self, start_approximation, func): while start_approximation > 0\ and ((self.f.function(0)) * (self.f.function(start_approximation)) >= 0): start_approximation -= 0.005 n = 0 while abs(func.function(start_approximation)) > self.e: n += 1 start_approximation -= func.function(start_approximation) return start_approximation, n def find_fwhm(self, method_left, method_right): max_x, max_value = function.Function.get_max() x_l, n_l = method_left(max_x - 0.4, self.f) print(f"Left roote is {x_l}") x_r, n_r = method_right(max_x + 0.2, self.f) print(f"Right roote is {x_r}") return x_r - x_l, n_l, n_r def show_results(self): print("Solving with Newton's method") r_n, n_l_n, n_r_n = self.find_fwhm(self.newtons_method, self.newtons_method) print("\nSolving with fixed-point iteration method") r_fpi, n_l_fpi, n_r_fpi = self.find_fwhm( self.fixed_point_iteration_method_reverse, self.fixed_point_iteration_method) print() print( f"For the fixed-point iteration method results are:\nx_r - x_l = {r_fpi}" ) print(f"Number of iterations for finding the left limit: {n_l_fpi}") print(f"Number of iterations for finding the right limit: {n_r_fpi}\n") print(f"For the Newton's method results are:\nx_r - x_l = {r_n}") print(f"Number of iterations for finding the left limit: {n_l_n}") print(f"Number of iterations for finding the right limit: {n_r_n}")
#coding=utf-8 import codecs import random import cv2, os import sys #sys是Python内建标准库 sys.path.append(os.getcwd()) from function import * import function as FUN import math import numpy as np fun = FUN.Function() #step1:设置路径、使用算法 image_dir = "/opt/yushan/data/oil_vehicle_person_10cls/VOCdevkit2007/VOC2007/JPEGImages_30_40/" #原始图片路径 xml_dir = "/opt/yushan/data/oil_vehicle_person_10cls/VOCdevkit2007/VOC2007/Annotations_30_40/" #图片对应xml的路径 image_txt = "/opt/yushan/data/oil_vehicle_person_10cls/VOCdevkit2007/VOC2007/ImageSets_30_40/Main/train.txt" #原始图片 标签对应的txt,比如文字识别:1.jpg 总结会, #备注:如果处理的是文件夹下面的图片,没有这个txt文件,代码会自动生成的 save_dir = "/opt/yushan/data/oil_vehicle_person_10cls/VOCdevkit2007/VOC2007/JPEGImages_train_30_40_aug/" #处理后保存的路径,每个增强方法会保存在对应文件夹下 #method_all = ["inverse", "elastic", "blur", "noise", "flip", "rotate", "translate", "zoom", "invertColor", "enhance", "contrastAndBright"] method = [ "inverse", "enhance", "contrastAndBright", "invertColor", "elastic", "blur", "noise", "flip", "rotate", "translate", "zoom" ] #step2:创建文件夹 for i in range(len(method)): s = save_dir + method[i] ss = s + "/img" if not os.path.exists(ss):
def test_function_construction(self): func = function.Function() assert func.body == [] assert func.mapping == {} assert func.end_block is None assert func.start_block is None