Esempio n. 1
0
 def visitH5(self, url, deviceName="7555", browserName="Browser"):
     config.get_config('../lib/conf/conf.properties')
     self.caps = {}
     self.caps["deviceName"] = config.config['deviceName' + deviceName]
     self.caps["platformName"] = config.config['platformName' + deviceName]
     self.caps["platformVersion"] = config.config['platformVersion' +
                                                  deviceName]
     self.caps["browserName"] = browserName
     self.caps["noReset"] = True
     self.driver = webdriver.Remote(
         "http://127.0.0.1:" + self.port + "/wd/hub", self.caps)
     self.driver.implicitly_wait(30)
     self.driver.get(url)
     if browserName == 'chrome':
         try:
             WebDriverWait(self.driver, 10,
                           1).until(lambda x: x.find_element_by_xpath(
                               '//*[@text="否"]')).click()
             logger.info('翻译提示已出现,且关闭')
         except:
             logger.info('翻译提示未出现')
     else:
         pass
     logger.info('获取到的当前句柄:' + str(self.driver.contexts))
     logger.info('使用浏览器:' + browserName + '打开网站:' + url + '成功')
Esempio n. 2
0
def main():
    images_transformed_path = get_config('images_transformed_path')
    with open(images_transformed_path, 'w') as output_file:
        writer = csv.writer(output_file, delimiter=',')

        training_images_labels_path = get_config('training_images_labels_path')
        with open(training_images_labels_path, 'r') as file:
            lines = file.readlines()

        for line in lines:
            print line
            print("\n\n" + line.strip())
            image_path, image_label = line.split()
            print image_path, image_label
            print image_path
            frame = cv2.imread(image_path)
            print frame
    
            try:
               
                write_frame_to_file(frame, image_label, writer)
            except Exception:
                exception_traceback = traceback.format_exc()
                print("Error while applying image transformation on image path '{}' with the following exception trace:\n{}".format(
                    image_path, exception_traceback))
                os.remove(image_path)
                continue
            
    cv2.destroyAllWindows()
    print "The program completed successfully !!"
Esempio n. 3
0
def main():
    images_transformed_path = get_config('images_transformed_path')
    with open(images_transformed_path, 'w') as output_file:
        writer = csv.writer(output_file, delimiter=',')

        training_images_labels_path = get_config('training_images_labels_path')
        with open(training_images_labels_path, 'r') as file:
            lines = file.readlines()

        for line in lines:
            print("\n\n" + line.strip())
            image_path, image_label = line.split()

            # Read the input image.
            frame = cv2.imread(image_path)
            # `frame` is a HxW numpy ndarray of triplets (pixels), where H and W are
            # the dimensions of the input image.
            # cv2.imshow("Original", frame)
            try:
                frame = apply_image_transformation(frame)
                write_frame_to_file(frame, image_label, writer)
            except Exception:
                exception_traceback = traceback.format_exc()
                print(
                    "Error while applying image transformation on image path '{}' with the following exception trace:\n{}"
                    .format(image_path, exception_traceback))
                continue
            # cv2.waitKey(1000)
    cv2.destroyAllWindows()
    print "The program completed successfully !!"
Esempio n. 4
0
    def __init__(self, config, msg_callback):
        self._log = logging.getLogger(logger_name)
        self._config = config
        self.client_name = get_config(config, "queue.client_name")

        self._client = mqtt.Client(client_id = self.client_name)
        self._client.on_connect = self._on_connect
        self._client.on_message = self._on_message

        tls_enabled = False
        if (get_config_default(config, "queue.tls", False)):
            tls_enabled = get_config_default(config, "queue.tls.enabled", False)
            tls_capath = get_config_default(config, "queue.tls.capath", None)
            tls_cert = get_config(config, "queue.tls.cert")
            tls_key = get_config(config, "queue.tls.key")
            tls_insecure = get_config_default(config, "queue.tls.disableHostnameCheck", False)
        else:
            self._log.warning("TLS not configured, not using encryption")
        
        if tls_enabled:
            self._client.tls_set(ca_certs = tls_capath, certfile = tls_cert, keyfile = tls_key)
            if tls_insecure:
                self._client.tls_insecure_set(True)
        
        self._log.info("Initializing MQTT client")
        self._client.connect_async(get_config(self._config, "queue.host"), get_config(self._config, "queue.port"))

        self._msg_callback = msg_callback
        self._client.loop_start()
Esempio n. 5
0
 def openPackage(self,
                 deviceName="7555",
                 appPackage="com.android.browser",
                 appActivity=".BrowserActivity"):
     config.get_config('../lib/conf/conf.properties')
     self.caps = {}
     self.caps["deviceName"] = config.config['deviceName' + deviceName]
     self.caps["platformName"] = config.config['platformName' + deviceName]
     self.caps["platformVersion"] = config.config['platformVersion' +
                                                  deviceName]
     self.caps["appPackage"] = appPackage
     self.caps["appActivity"] = appActivity
     self.caps["noReset"] = True
     # 下面chromeOptions的添加很重要,涉及小程序的能否定位
     if appPackage == 'com.tencent.mm':
         self.caps['chromeOptions'] = {
             'androidProcess': 'com.tencent.mm:appbrand0'
         }
     elif appPackage == 'com.tencent.mobileqq':
         self.caps['chromeOptions'] = {
             'androidProcess': 'com.tencent.mobileqq:mini'
         }
     else:
         pass
     self.driver = webdriver.Remote(
         "http://127.0.0.1:" + self.port + "/wd/hub", self.caps)
     self.driver.implicitly_wait(30)
     logger.info('使用设备:' + config.config['deviceName' + deviceName] +
                 '打开应用:' + appPackage + '成功')
Esempio n. 6
0
def main():
    #sys.argv is a list in Python, which contains the command-line arguments passed to the script.
    #With the len(sys.argv) function you can count the number of arguments.
    #If you are gonna work with command line arguments, you probably want to use sys.argv.
    model_name = sys.argv[1]
    if model_name not in ['svm', 'logistic', 'knn']:
        print("Invalid model-name '{}'!".format(model_name))
        return

    #get model file path
    model_output_dir_path = get_config(
        'model_{}_output_dir_path'.format(model_name))
    model_stats_file_path = os.path.join(model_output_dir_path,
                                         "stats-{}.txt".format(model_name))
    print("Model stats will be written to the file at path '{}'.".format(
        model_stats_file_path))

    with open(model_stats_file_path, "w") as model_stats_file:
        images_transformed_path = get_config('images_transformed_path')
        images, labels = read_images_transformed(images_transformed_path)
        classifier_model = generate_classifier(model_name)  #naming classifier

        model_stats_file.write("Model used = '{}'".format(model_name))
        model_stats_file.write("Classifier model details:\n{}\n\n".format(
            classifier_model))  #write details into main file
        training_images, testing_images, training_labels, testing_labels = divide_data_train_test(
            images, labels, 0.2)  #creating the training dataset

        print("\nTraining the model...")
        classifier_model = classifier_model.fit(training_images,
                                                training_labels)
        #it looks like scikit-learn fits the data for almost all the algorithms by name.fit
        print("Done!\n")

        #not sure what dumping the model means....storing? saving new version?
        model_serialized_path = get_config(
            'model_{}_serialized_path'.format(model_name))
        print("\nDumping the trained model to disk at path '{}'...".format(
            model_serialized_path))
        joblib.dump(classifier_model,
                    model_serialized_path)  #used instead of pickle
        print("Dumped\n")

        #score(X, y[, sample_weight])	Returns the mean accuracy on the given test data and labels.
        #unsure of the output....is it percentage? magnitude?
        print("\nWriting model stats to file...")
        score = classifier_model.score(testing_images, testing_labels)
        model_stats_file.write("Model score:\n{}\n\n".format(
            print_with_precision(score)))

        #predict(X)	Perform classification on samples in X.
        predicted = classifier_model.predict(testing_images)
        #metrics.classification_report(y_true, y_pred)	Build a text report showing the main classification metrics
        #Returns:	report : string -->Text summary of the precision, recall, F1 score for each class.
        report = metrics.classification_report(testing_labels, predicted)
        model_stats_file.write("Classification report:\n{}\n\n".format(report))
        print("Done!\n")

        print("\nFinished!\n")
Esempio n. 7
0
    def __init__(self, config, event_callback):
        self._log = logging.getLogger(logger_name)
        self._config = config
        self._event_callback = event_callback
        self._serial_lock = Lock()
        
        self._serial_lock.acquire()

        # Open serial port
        try:
            serial_port = get_config(self._config, "prt3.port")
            serial_speed = int(get_config(self._config, "prt3.speed"))
        except:
            self._log.error("Invalid port configuration: %s" % (get_config(self._config, "prt3")))
            sys.exit(1)

        self._log.info("Opening serial port %s" % (serial_port))
        self._ser = serial.Serial(port=serial_port, baudrate=serial_speed, parity=serial.PARITY_NONE, stopbits=serial.STOPBITS_ONE, 
                    bytesize=serial.EIGHTBITS, timeout=0.1, xonxoff=False, rtscts=False, dsrdtr=False)

        # Load panel configuration
        cfg_panel = get_config(self._config, "panel")
    
        try:
            # Set basic config variables
            self._panel_type = cfg_panel["type"]

            # Load monitored area ranges
            for i in cfg_panel["areas"]:
                for i2 in range(i[0], i[1]+1):
                    self.areas[i2-1] = PRT_Area(i2)
                    self._sum_areas += 1

            # Load monitored zone ranges
            for i in cfg_panel["zones"]:
                for i2 in range(i[0], i[1]+1):
                    self.zones[i2-1] = PRT_Zone(i2)
                    self._sum_zones += 1

            # Load monitored user ranges
            self.users[0] = PRT_User(0)
            for i in cfg_panel["users"]:
                for i2 in range(i[0], i[1]+1):
                    self.users[i2] = PRT_User(i2)
                    self._sum_users += 1

        except:
            self._log.error("Unable to parse panel config")
            sys.exit(1)

        # Init PGMs
        for i in range(1,30):
            self.pgm[i-1] = PRT_PGM(i)
        
        self._log.debug("Panel object successfully initialized")
        self._log.debug("* Areas = %d; Zones = %d; Users = %d" % (self._sum_areas, self._sum_zones, self._sum_users))
        
        self._serial_lock.release()
Esempio n. 8
0
def publish_test(testcase):
    broker = get_config(RABBITMQ_KEY, "RABBIT_MQ_IP")
    user = get_config(RABBITMQ_KEY, "RABBIT_MQ_USERNAME")
    password = get_config(RABBITMQ_KEY, "RABBIT_MQ_PASSWORD")
    credentials = pika.PlainCredentials(user, password)
    connection = pika.BlockingConnection(
        pika.ConnectionParameters(host=broker, credentials=credentials))
    channel = connection.channel()

    channel.queue_declare(queue=QUEUE_NAME)

    channel.basic_publish(exchange='', routing_key=ROUTING_KEY, body=testcase)
    connection.close()
Esempio n. 9
0
    def _on_connect(self, client, userdata, flags, rc):
        self._log.info("Connected to queue with result code %d" % (rc))

        req_topic_events = get_config(self._config, "queue.queues.events") + "/#"
        req_topic_broadcasts = get_config(self._config, "queue.queues.broadcasts") + "/#"
        req_topic_responses = get_config(self._config, "queue.queues.responses") + "/" + self.client_name+ "/#"

        self._log.info("Subscribing to topic: %s" % (req_topic_events))
        self._log.info("Subscribing to topic: %s" % (req_topic_broadcasts))
        self._log.info("Subscribing to topic: %s" % (req_topic_responses))
        client.subscribe(req_topic_events)
        client.subscribe(req_topic_broadcasts)
        client.subscribe(req_topic_responses)
Esempio n. 10
0
def main():
    model_name = "svm"
    if model_name not in ['svm']:
        print("Invalid model-name '{}'!".format(model_name))
        return

    model_output_dir_path = get_config(
        'model_{}_output_dir_path'.format(model_name))
    model_stats_file_path = os.path.join(model_output_dir_path,
                                         "stats-{}.txt".format(model_name))
    print("Model stats will be written to the file at path '{}'.".format(
        model_stats_file_path))

    with open(model_stats_file_path, "w") as model_stats_file:
        images_transformed_path = get_config('images_transformed_path')
        images, labels = read_images_transformed(images_transformed_path)
        classifier_model = generate_classifier(model_name)

        model_stats_file.write("Model used = '{}'".format(model_name))
        model_stats_file.write(
            "Classifier model details:\n{}\n\n".format(classifier_model))
        training_images, testing_images, training_labels, testing_labels = divide_data_train_test(
            images, labels, 0.25)

        print("\nTraining the model...")
        print training_images
        print training_labels
        classifier_model = classifier_model.fit(training_images,
                                                training_labels)
        print("Done!\n")

        model_serialized_path = get_config(
            'model_{}_serialized_path'.format(model_name))
        print("\nDumping the trained model to disk at path '{}'...".format(
            model_serialized_path))
        joblib.dump(classifier_model, model_serialized_path)
        print("Dumped\n")

        print("\nWriting model stats to file...")
        score = classifier_model.score(testing_images, testing_labels)
        model_stats_file.write("Model score:\n{}\n\n".format(
            print_with_precision(score)))

        predicted = classifier_model.predict(testing_images)
        report = metrics.classification_report(testing_labels, predicted)
        model_stats_file.write("Classification report:\n{}\n\n".format(report))
        print("Done!\n")

        print("\nFinished!\n")
def main():
    #Get the classifier name
    model_name = sys.argv[1]
    if model_name not in ['svm', 'logistic', 'knn']:
        logger.error("Invalid model-name '{}'!".format(model_name))
        return
    model_output_dir_path = get_config(
        'model_{}_output_dir_path'.format(model_name))
    model_stats_file_path = os.path.join(model_output_dir_path,
                                         "stats-{}.txt".format(model_name))
    logger.info("Model stats will be written to the file at path '{}'.".format(
        model_stats_file_path))

    os.makedirs(os.path.dirname(model_stats_file_path), exist_ok=True)
    print(model_stats_file_path)
    with open(model_stats_file_path, "w") as model_stats_file:
        images_transformed_path = get_config('images_transformed_path')
        images, labels = read_images_transformed(images_transformed_path)
        classifier_model = generate_classifier(model_name)

        model_stats_file.write("Model used = '{}'".format(model_name))
        model_stats_file.write(
            "Classifier model details:\n{}\n\n".format(classifier_model))
        training_images, testing_images, training_labels, testing_labels = \
            divide_data_train_test(images, labels, 0.2)

        logger.info("Training the model...")
        classifier_model = classifier_model.fit(training_images,
                                                training_labels)
        logger.info("Done!\n")

        model_serialized_path = get_config(
            'model_{}_serialized_path'.format(model_name))
        logger.info("Dumping the trained model to disk at path '{}'...".format(
            model_serialized_path))
        joblib.dump(classifier_model, model_serialized_path)
        logger.info("Dumped\n")

        logger.info("Writing model stats to file...")
        score = classifier_model.score(testing_images, testing_labels)
        model_stats_file.write("Model score:\n{}\n\n".format(
            print_with_precision(score)))

        predicted = classifier_model.predict(testing_images)
        report = metrics.classification_report(testing_labels, predicted)
        model_stats_file.write("Classification report:\n{}\n\n".format(report))
        logger.info("Done!\n")

        logger.info("Finished!\n")
def get_classes():
    id_token = request.args.get('id_token')
    access_token = request.args.get('access_token')

    gc = GoogleCredentials()
    if access_token and id_token:
        creds, info = gc.get_credential_from_token(id_token, access_token)
    else:
        creds = gc.get_credential()  # RM organization courses
        info = {
            'name': get_config("application_org"),
            'email': get_config("application_email")
        }

    courses = list_courses(creds)
    return jsonify({'user': info, 'courses': courses})
Esempio n. 13
0
    def fetch_area(self, id):
        self._log.debug("Fetching area [%d]" % (id))
        name_update = get_config(self._config, "prt3.refresh.names") * 60

        # Request area label
        if (self.areas[id-1].last_name_update == None) or ((time.time() - self.areas[id-1].last_name_update) >= name_update):
            ret = self.prt3_command("AL%03d" % (id), "^(AL)([0-9]{3})(.{16})$")
            for area in ret:
                if area[1] == "AL":
                    self.areas[id-1].name = area[3].strip()
                    self.areas[id-1].last_name_update = time.time()

        # Request area status
        ret = self.prt3_command("RA%03d" % (id), "^(RA)([0-9]{3})([DAFSI])([MO])([TO])([NO])([PO])([AO])([SO])$")
        for area in ret:
            if area[1] == "RA":
                self.areas[id-1].arm_disarmed = (area[3] == 'D')
                self.areas[id-1].arm_armed = (area[3] == 'A')
                self.areas[id-1].arm_force = (area[3] == 'F')
                self.areas[id-1].arm_stay = (area[3] == 'S')
                self.areas[id-1].arm_instant = (area[3] == 'I')
                self.areas[id-1].zone_in_memory = (area[4] == 'M')
                self.areas[id-1].trouble = (area[5] == 'T')
                self.areas[id-1].not_ready = (area[6] == 'N')
                self.areas[id-1].in_programming = (area[7] == 'P')
                self.areas[id-1].in_alarm = (area[8] == 'A')
                self.areas[id-1].strobe = (area[9] == 'S')
                self.areas[id-1].last_state_update = time.time()
def main():
    model_name = sys.argv[1]
    if model_name not in ['svm', 'logistic', 'knn']:
        logger.error("Invalid model-name '{}'!".format(model_name))
        return

    logger.info("Using model '{}'...".format(model_name))

    model_serialized_path = get_config(
        'model_{}_serialized_path'.format(model_name))
    logger.info(
        "Model deserialized from path '{}'".format(model_serialized_path))

    for root, dir, filex in os.walk("C:/xampp/htdocs/file_upload_api/files/",
                                    topdown=True):
        image_path = "C:/xampp/htdocs/file_upload_api/files/" + filex[0]
        break
    frame = cv2.imread(image_path)
    try:
        frame = apply_image_transformation(frame)
        frame_flattened = frame.flatten()
        classifier_model = joblib.load(model_serialized_path)
        predicted_labels = classifier_model.predict(
            frame_flattened.reshape(1, -1))
        predicted_label = predicted_labels[0]
        print("Predicted labelx={}".format(predicted_label))
    except Exception:
        exception_traceback = traceback.format_exc()
        print(
            "Error while applying image transformation on image path '{}' with the following exception trace:\n{}"
            .format(image_path, exception_traceback))
    os.remove(image_path)
    cv2.destroyAllWindows()
    logger.info("The program completed successfully !!")
Esempio n. 15
0
 def connect_config_device():
     from common import config
     config = config.Config()
     device_name = config.get_config(config.TITLE_DEVICE,
                                     config.VALUE_DEVICE_NAME)
     L.i('connect_config_device %s ' % device_name, tag=TAG)
     ADB(device_name).connect()
Esempio n. 16
0
 def connect(self):
     db_config = get_config("db_provider")
     self.my_db = mysql.connector.connect(
         host=db_config.get("db_host"),
         user=db_config.get("db_user"),
         passwd=db_config.get("db_password"),
         database=db_config.get("db_name"))
Esempio n. 17
0
def run():
    config = get_config()

    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    sock.bind((config['server']['ip'], config['server']['port']))
    
    sock.listen(1)   #监听新的连接
    print('server listening on address %s port %s' %(config['server']['ip'], config['server']['port']))

    bytes_buffer = {}   #每一个会话接收包,但可能不是一次性接收到完整的包
    received = {}    #目前这个包收到了多少字节
    to_receive = {}   #是每一个会话接收到前4个字节length of message之后,接下来要接收直到整个包完成的大小
    while(1):
        rlist, wlist, xlist = select.select(list(map(lambda x: x.socket, sessions))+[sock], [],[])
        

        for s in rlist:
            if s == sock : # new connection
                new_sess = server_new_session(s)
                socket_to_sessions[new_sess.socket] = new_sess #从套接字快速找到会话通道; 自定义的通道类直接保存了socket
                sessions.append(new_sess)
                bytes_buffer[new_sess] = bytes()
                received[new_sess]=0
                to_receive[new_sess]=0
                continue
               
            sess = socket_to_sessions[s]    #旧的会话
            if received[sess] == 0 and to_receive[sess] == 0: #接收一个新的包
                try:
                    length_bytes = sess.socket.recv(4, socket.MSG_WAITALL)
                except ConnectionError:
                    sess.socket.close()
                    remove_session(sess)
                    print("one client leave")
                    continue
                if length_bytes == '' or len(length_bytes)!=4:
                    print("client broke down.")
                    sess.socket.close()
                    remove_session(sess)
                    print(len(length_bytes))
                    print(length_bytes)
                    continue
                
                if len(length_bytes) == 4:
                    bytes_buffer[sess] = bytes()   # struct.unpack的返回结果为元组,即使只包含一个条目
                    # msg length + padding 1+ nonce 12 + tag 16 + msg
                    to_receive[sess] = struct.unpack('!L', length_bytes)[0] + 1 + 12 + 16
                         
            bytes_buffer[sess] += sess.socket.recv(to_receive[sess] - received[sess])
            #print("length of buffer %d "  % len(bytes_buffer[sess]))
            received[sess] = len(bytes_buffer[sess])

            if received[sess] == to_receive[sess] and len(bytes_buffer[sess])!=0:
                received[sess] = 0
                to_receive[sess] = 0
                data = sess.get_message(bytes_buffer[sess])
                handle_event(sess, data['msg_type'], data['msg_body'])
                bytes_buffer[sess] = bytes()
Esempio n. 18
0
    def panel_sync(self):
        self._log.debug("Panel sync poll")
        area_update = get_config(self._config, "prt3.refresh.area") * 60
        zone_update = get_config(self._config, "prt3.refresh.zone") * 60
        user_update = get_config(self._config, "prt3.refresh.user") * 60

        for area in self.areas:
            if (area != None) and ( (area.last_state_update == None) or (time.time() - area.last_state_update >= area_update) ):
                self.fetch_area(area.id)

        for zone in self.zones:
            if (zone != None) and ( (zone.last_state_update == None) or (time.time() - zone.last_state_update >= zone_update) ):
                self.fetch_zone(zone.id)

        for user in self.users:
            if (user != None) and (user.id != 0) and ( (user.last_name_update == None) or (time.time() - user.last_name_update >= user_update) ):
                self.fetch_user(user.id)
Esempio n. 19
0
 def __init__(self, window=None):
     """ constructor
     :param window: window handler
     """
     self._API_CONFIG = get_config('1q_api')
     self._api = HanaAPI(self._API_CONFIG)
     self._transaction_processor = TransactionProcessor()
     self._window = window
def main():
    images_source = sys.argv[1]
    if images_source not in ['train', 'test']:
        logger.error("Invalid image-source '{}'!".format(images_source))
        return
    images_dir_path = get_config('{}ing_images_dir_path'.format(images_source))
    images_labels_path = get_config(
        '{}ing_images_labels_path'.format(images_source))

    logger.info(
        "Gathering info about images at path '{}'...".format(images_dir_path))
    images_labels_list = get_images_labels_list(images_dir_path)
    logger.info("Done!")

    logger.info("Writing images labels info to file at path '{}'...".format(
        images_labels_path))
    write_images_labels_to_file(images_labels_list, images_labels_path)
    logger.info("Done!")
Esempio n. 21
0
def setup_logging(app: Flask) -> None:
    """
    Set up logging for app
    :param app: the `Flask` app
    """
    config = get_config()
    app.config['PROPAGATE_EXCEPTIONS'] = config.getboolean('WEB',
                                                           'logexception',
                                                           fallback=False)
    logger.init_app(app)
Esempio n. 22
0
 def chooseBrowserOpenUrl(self, url, browsertype = 'chrome'):
     if browsertype == 'chrome':
         op = Options()
         # 去掉浏览器中的提示信息
         op.add_argument("--disable-infobars")
         # 使用用户缓存文件:默认自动获取目录,错误则使用指定目录
         # try:
         #     userdir = os.environ['USERPROFILE']
         # except Exception as e:
         #     userdir = '--user-data-dir=C:/Users/leez/AppData/Local/Google/Chrome/User Data/'
         config.get_config('./lib/conf/conf.properties')
         userdir = config.config['chrome_user']
         op.add_argument(r'--user-data-dir=' + userdir)
         # 最大化浏览器
         op.add_argument('--start-maximized')
         self.driver = webdriver.Chrome(executable_path='../lib/driver/chromedriver', options=op)
     elif browsertype == 'firefox':
         self.driver = webdriver.Firefox(executable_path='../lib/driver/geckodriver')
         self.driver.maximize_window()
     elif browsertype == 'ie':
         self.driver = webdriver.Ie(executable_path='../lib/driver/IEDriverServer')
         self.driver.maximize_window()
     else:
         op = Options()
         # 去掉浏览器中的提示信息
         op.add_argument("--disable-infobars")
         # 使用用户缓存文件:默认自动获取目录,错误则使用指定目录
         # try:
         #     userdir = os.environ['USERPROFILE']
         # except Exception as e:
         #     userdir = '--user-data-dir=C:\\Users\\leez\\AppData\\Local\\Google\\Chrome\\User Data'
         config.get_config('./lib/conf/conf.properties')
         userdir = config.config['chrome_user']
         op.add_argument(r'--user-data-dir=' + userdir)
         # 最大化浏览器
         op.add_argument('--start-maximized')
         self.driver = webdriver.Chrome(executable_path='../lib/driver/chromedriver', options=op)
         self.driver.get(url)
     self.driver.implicitly_wait(30)
     self.driver.get(url)
     # self.driver.execute_script(js)
     logger.info("使用浏览器:" + browsertype + "打开指定url:" + url + "成功")
     return self.driver
Esempio n. 23
0
def get_quiz_form_db(json_data):
    user_id = json_data.get('user_id')
    user_profile = json_data.get('user_profile')
    limit = json_data.get('limit', 500)

    user_email = user_profile.get('email')
    user_id = user_profile.get('googleId')

    # get all items by user
    sql = queries[12].format(user_email, limit)
    # get all items for admin
    if user_email in get_config("admin_users") and \
            user_id in get_config("admin_user_ids"):
        sql = queries[16].format(limit)

    items = connect_and_query(sql)
    for item in items:
        item['choices'] = json.loads(item.get('choices', {}))
        item['metadata'] = json.loads(item.get('metadata', {}))
        try:
            item['topic'] = json.loads(item['topic'])
            item['sub_topics'] = json.loads(item['sub_topics'])
        except:
            pass

    # get all quizzes by user
    sql = queries[13].format(user_email, limit)
    # for admin, get all quizzes
    if user_email in get_config("admin_users") and \
            user_id in get_config("admin_user_ids"):
        sql = queries[17].format(limit)

    exams = connect_and_query(sql)
    for item in exams:
        item['metadata'] = json.loads(item.get('metadata') or '{}')
        item['user_profile'] = json.loads(item.get('user_profile') or '{}')
        #print(item.get('analysis'))
        item['analysis'] = json.loads(item.get('analysis') or '{}')

    return {"items": items,
            "exams": exams,
            "items_count": len(items),
            "exams_count": len(exams)}
Esempio n. 24
0
    def connect(self):
        db_config = get_config("db_provider_cloud")
        self.my_db = psycopg2.connect(
            host=db_config.get("db_host"),
            user=db_config.get("db_user"),
            passwd=db_config.get("db_password"),
            database=db_config.get("db_name")
        )

        return self.my_db
Esempio n. 25
0
def setup_db(app: Flask) -> None:
    """
    Set up database functionality for the app.
    :param app: the `Flask` app
    """
    config = get_config()
    app.config['SQLALCHEMY_DATABASE_URI'] = 'postgresql://%(username)s:%(password)s@%(host)s:%(port)s/%(database)s' % \
                                            dict(config.items('DB'))
    app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
    db.init_app(app)
Esempio n. 26
0
def main():
    model_name = sys.argv[1]
    if model_name not in ['svm', 'logistic', 'knn']:
        print("Invalid model-name '{}'!".format(model_name))
        return

    print("Using model '{}'...".format(model_name))

    model_serialized_path = get_config(
        "model_{}_serialized_path".format(model_name))
    print("Model deserialized from path '{}'".format(model_serialized_path))

    camera = cv2.VideoCapture(0)

    while True:

        ret, frame = camera.read()
        cv2.imshow("Recording", frame)
        key = cv2.waitKey(1)
        if key == ord('q'):
            break
        elif key == ord('c'):
            if not ret:
                print("Failed to capture image!")
                continue
            frame = resize_image(frame, 400)
            r = cv2.selectROI(frame)
            imCrop = frame[int(r[1]):int(r[1] + r[3]),
                           int(r[0]):int(r[0] + r[2])]
            cv2.imshow("Image", imCrop)
            #cv2.imshow("Webcam recording", frame)
            frame = imCrop
            try:
                frame = apply_image_transformation(frame)
                frame_flattened = frame.flatten()
                classifier_model = joblib.load(model_serialized_path)
                predicted_labels = classifier_model.predict(frame_flattened)
                predicted_label = predicted_labels[0]
                print("Predicted label = {}".format(predicted_label))
                predicted_image = get_image_from_label(predicted_label)
                predicted_image = resize_image(predicted_image, 200)
                cv2.imshow("Prediction = '{}'".format(predicted_label),
                           predicted_image)
                engine = pyttsx.init()
                engine.say("The predicted text is " + str(predicted_label))
                engine.runAndWait()
                engine.stop()
            except Exception:
                exception_traceback = traceback.format_exc()
                print(
                    "Error while applying image transformation with the following exception trace:\n{}"
                    .format(exception_traceback))

    cv2.destroyAllWindows()
    print "The program completed successfully !!"
Esempio n. 27
0
def main():
    model_name = sys.argv[1]
    if model_name not in ['svm', 'logistic', 'knn']:
        logger.error("Invalid model-name '{}'!".format(model_name))
        return

    logger.info("Using model '{}'...".format(model_name))

    model_serialized_path = get_config(
        'model_{}_serialized_path'.format(model_name))
    logger.info(
        "Model deserialized from path '{}'".format(model_serialized_path))

    testing_images_labels_path = get_config('testing_images_labels_path')
    with open(testing_images_labels_path, 'r') as file:
        lines = file.readlines()
        for line in lines:
            if not line:
                continue
            image_path, image_label = line.split()
            frame = cv2.imread(image_path)
            try:
                frame = apply_image_transformation(frame)
                frame_flattened = frame.flatten()
                classifier_model = joblib.load(model_serialized_path)
                predicted_labels = classifier_model.predict(
                    frame_flattened.reshape(1, -1))
                predicted_label = predicted_labels[0]
                logger.info('"{}" {} ---> {}'.format(image_path, image_label,
                                                     predicted_label))
                if image_label != predicted_label:
                    log_msg = "Incorrect prediction '{}' instead of '{}'\n)"
                    logger.error(log_msg.format(predicted_label, image_label))
                    cv2.waitKey(5000)
            except Exception:
                exception_traceback = traceback.format_exc()
                logger.error("Error applying image transformation to image "
                             "'{}'".format(image_path))
                logger.debug(exception_traceback)
                continue
    cv2.destroyAllWindows()
    logger.info("The program completed successfully !!")
Esempio n. 28
0
def test_user_role_update():
    log('user_manage', '角色更新测试开始')
    phone = config.get_config('app', 'phone')
    db_cur = db.cursor()
    db_cur.execute('select id from clb_user where telephone = %s',[phone])
    user_data = db_cur.fetchone()
    data = {'role': 4, 'user_id': user_data[0]}
    response = global_params.post('ucenter/role/update', data)
    assert response['status'] == 0
    log('user_manage', '角色更新测试结束')
    pass
Esempio n. 29
0
def main():
    images_source = "train"
    if images_source not in ['train', 'test']:
        print("Invalid image-source '{}'!".format(images_source))
        return

    images_dir_path = get_config('{}ing_images_dir_path'.format(images_source))
    images_labels_path = get_config(
        '{}ing_images_labels_path'.format(images_source))

    print(
        "Gathering info about images at path '{}'...".format(images_dir_path))
    images_labels_list = get_images_labels_list(images_dir_path)
    print("Done!")

    print("Writing images labels info to file at path '{}'...".format(
        images_labels_path))
    print images_labels_list, images_labels_path
    write_images_labels_to_file(images_labels_list, images_labels_path)
    print("Done!")
Esempio n. 30
0
def run_producer():
    logger.info('Starting producer service')
    config = get_config()
    try:
        db_model = DbModel(config)
        with db_model:
            sites = db_model.get_sites_list(config['group_name'])
        logger.info('Start checking sites: {}'.format([x[1] for x in sites]))
        check_sites_availability(config, sites)
    except Exception as ex:
        logger.exception(ex)
Esempio n. 31
0
def main():
    model_name = sys.argv[1]
    if model_name not in ['svm', 'logistic', 'knn']:
        print("Invalid model-name '{}'!".format(model_name))
        return

    print("Using model '{}'...".format(model_name))

    model_serialized_path = get_config(
        'model_{}_serialized_path'.format(model_name))
    print("Model deserialized from path '{}'".format(model_serialized_path))

    testing_images_labels_path = get_config('testing_images_labels_path')
    with open(testing_images_labels_path, 'r') as file:
        lines = file.readlines()
        for line in lines:
            print("\n\n" + line.strip())
            image_path, image_label = line.split()
            frame = cv2.imread(image_path)
            try:
                frame = apply_image_transformation(frame)
                frame_flattened = frame.flatten()
                classifier_model = joblib.load(model_serialized_path)
                predicted_labels = classifier_model.predict(frame_flattened)
                predicted_label = predicted_labels[0]
                print("Predicted label = {}".format(predicted_label))
                engine = pyttsx.init()
                engine.say("The predicted text is " + str(predicted_label))
                engine.runAndWait()
                engine.stop()
                if image_label != predicted_label:
                    print("Incorrect prediction!!")
                    cv2.waitKey(5000)
            except Exception:
                exception_traceback = traceback.format_exc()
                print("Error while applying image transformation on image path '{}' with the following exception trace:\n{}".format(
                    image_path, exception_traceback))
                continue
    cv2.waitKey()
    cv2.destroyAllWindows()
    print "The program completed successfully !!"
Esempio n. 32
0
def test_user_update():
    log('user_manage', '用户信息更新测试开始')
    phone = config.get_config('app', 'phone')
    db_cur = db.cursor()
    db_cur.execute('select id,fullname,telephone,email,superior,cost_center_id,level_id from clb_user where telephone = %s',[phone])
    user_data = db_cur.fetchone()
    data = {'fullname': '坤123', 'telephone': user_data[2], 'email': user_data[3], 'superior': user_data[4], 'cost_center_id': user_data[5], 'level_id': user_data[6]}

    response = global_params.post('ucenter/user/update', data)

    assert response['status'] == 0,response['message']
    assert response['data']['fullname'] == '坤123',response['message']
    log('user_manage', '用户信息更新测试结束')
    pass
Esempio n. 33
0
def test_subject_add_single_type():
    """
    添加费用类型到费用科目
    :return:
    """
    log('subject', '费用科目添加费用类型测试开始')
    host = config.get_config('app', 'host')
    db_cur = db.cursor()
    db_cur.execute('select id from clb_cost_subject order by id desc limit 1')
    cost_subject_data = db_cur.fetchone()
    subject_id = cost_subject_data[0]
    data = {'title': '测试费用类型', 'surl': '123456', 'icon': 'ad', 'iconPath': host+'/static/assets/cost/ad_2x.png', 'subject_id': subject_id}
    response = global_params.post('cost_type/create', data)
    assert response['status'] == 0, response['message']
    log('subject', '费用科目添加费用类型测试结束')
    pass