예제 #1
0
class RemoteSubscriber(object):
    def __init__(self, uuid, commandID, ipaddress="", port=32400, protocol="http", name=""):
        self.poller         = False
        self.uuid           = uuid
        self.commandID      = commandID
        self.url            = ""
        self.name           = name
        self.lastUpdated    = Timer()

        if ipaddress and protocol:
            self.url = "%s://%s:%s" % (protocol, ipaddress, port)

    def refresh(self, sub):
        log.debug("RemoteSubscriber::refresh %s (cid=%s)" % (self.uuid, sub.commandID))

        if sub.url != self.url:
            log.debug("RemoteSubscriber::refresh new url %s", sub.url)
            self.url = sub.url

        if sub.commandID != self.commandID:
            log.debug("RemoteSubscriber::refresh new commandID %s", sub.commandID)
            self.commandID = sub.commandID

        self.lastUpdated.restart()

    def shouldRemove(self):
        if self.lastUpdated.elapsed() > SUBSCRIBER_REMOVE_INTERVAL:
            log.debug("RemoteSubscriber::shouldRemove removing %s because elapsed: %lld" % (self.uuid, self.lastUpdated.elapsed()))
            return True

        log.debug("RemoteSubscriber::shouldRemove will not remove %s because elapsed: %lld" % (self.uuid, self.lastUpdated.elapsed()))
        return False
예제 #2
0
파일: detector.py 프로젝트: yyg192/MTCNN
 def detect(self, img, ths, min_size, factor, debug=False):
     '''detect face, return bboxes, [bbox score offset landmark]
 if debug is on, return bboxes of every stage and time consumption
 '''
     timer = Timer()
     ts = [0, 0, 0, 0]
     bb = [[], [], [], []]
     # stage-1
     timer.tic()
     base = 12. / min_size
     height, width = img.shape[:-1]
     l = min(width, height)
     l *= base
     scales = []
     while l > 12:
         scales.append(base)
         base *= factor
         l *= factor
     if not self.pnet_single_forward or len(scales) <= 1:
         bboxes = np.zeros((0, 4 + 1 + 4 + 10), dtype=np.float32)
         for scale in scales:
             w, h = int(math.ceil(scale * width)), int(
                 math.ceil(scale * height))
             data = cv2.resize(img, (w, h))
             data = data.transpose((2, 0, 1)).astype(np.float32)
             data = (data - 128) / 128
             data = data.reshape((1, 3, h, w))
             prob, bbox_pred, landmark_pred = self._forward(
                 self.pnet, data, ['prob', 'bbox_pred', 'landmark_pred'])
             _bboxes = self._gen_bbox(prob[0][1], bbox_pred[0],
                                      landmark_pred[0], scale, ths[0])
             keep = nms(_bboxes, 0.5)
             _bboxes = _bboxes[keep]
             bboxes = np.vstack([bboxes, _bboxes])
     else:
         # convert to a single image
         data, pyramid_info = convert_image_pyramid(img, scales, interval=2)
         # forward pnet
         data = data.astype(np.float32)
         data = (data.transpose((2, 0, 1)) - 128) / 128
         data = data[np.newaxis, :, :, :]
         prob, bbox_pred, landmark_pred = self._forward(
             self.pnet, data, ['prob', 'bbox_pred', 'landmark_pred'])
         bboxes = self._gen_bbox(prob[0][1], bbox_pred[0], landmark_pred[0],
                                 1, ths[0])
         # nms over every pyramid
         keep = nms(bboxes, 0.5)
         bboxes = bboxes[keep]
         # map to original image
         bboxes = get_original_bboxes(bboxes, pyramid_info)
     keep = nms(bboxes, 0.7)
     bboxes = bboxes[keep]
     bboxes = self._bbox_reg(bboxes)
     bboxes = self._make_square(bboxes)
     timer.toc()
     ts[0] = timer.elapsed()
     bb[0] = bboxes.copy()
     self._clear_network_buffer(self.pnet)
     # stage-2
     if self.rnet is None or len(bboxes) == 0:
         if debug is True:
             return bb, ts
         else:
             return bboxes
     timer.tic()
     n = len(bboxes)
     data = np.zeros((n, 3, 24, 24), dtype=np.float32)
     for i, bbox in enumerate(bboxes):
         face = crop_face(img, bbox[:4])
         data[i] = cv2.resize(face, (24, 24)).transpose((2, 0, 1))
     data = (data - 128) / 128
     prob, bbox_pred, landmark_pred = self._forward(
         self.rnet, data, ['prob', 'bbox_pred', 'landmark_pred'])
     prob = prob.reshape(n, 2)
     bbox_pred = bbox_pred.reshape(n, 4)
     landmark_pred = landmark_pred.reshape(n, 10)
     keep = prob[:, 1] > ths[1]
     bboxes = bboxes[keep]
     bboxes[:, 4] = prob[keep, 1]
     bboxes[:, 5:9] = bbox_pred[keep]
     bboxes[:, 9:] = landmark_pred[keep]
     keep = nms(bboxes, 0.7)
     bboxes = bboxes[keep]
     bboxes = self._bbox_reg(bboxes)
     bboxes = self._make_square(bboxes)
     timer.toc()
     ts[1] = timer.elapsed()
     bb[1] = bboxes.copy()
     self._clear_network_buffer(self.rnet)
     # stage-3
     if self.onet is None or len(bboxes) == 0:
         if debug is True:
             return bb, ts
         else:
             return bboxes
     timer.tic()
     n = len(bboxes)
     data = np.zeros((n, 3, 48, 48), dtype=np.float32)
     for i, bbox in enumerate(bboxes):
         face = crop_face(img, bbox[:4])
         data[i] = cv2.resize(face, (48, 48)).transpose((2, 0, 1))
     data = (data - 128) / 128
     prob, bbox_pred, landmark_pred = self._forward(
         self.onet, data, ['prob', 'bbox_pred', 'landmark_pred'])
     prob = prob.reshape(n, 2)
     bbox_pred = bbox_pred.reshape(n, 4)
     landmark_pred = landmark_pred.reshape(n, 10)
     keep = prob[:, 1] > ths[2]
     bboxes = bboxes[keep]
     bboxes[:, 4] = prob[keep, 1]
     bboxes[:, 5:9] = bbox_pred[keep]
     bboxes[:, 9:] = landmark_pred[keep]
     bboxes = self._locate_landmark(bboxes)
     bboxes = self._bbox_reg(bboxes)
     keep = nms(bboxes, 0.7, 'Min')
     bboxes = bboxes[keep]
     timer.toc()
     ts[2] = timer.elapsed()
     bb[2] = bboxes.copy()
     self._clear_network_buffer(self.onet)
     # stage-4
     if self.lnet is None or len(bboxes) == 0:
         if debug is True:
             return bb, ts
         else:
             return bboxes
     timer.tic()
     n = len(bboxes)
     data = np.zeros((n, 15, 24, 24), dtype=np.float32)
     w, h = bboxes[:, 2] - bboxes[:, 0], bboxes[:, 3] - bboxes[:, 1]
     l = np.maximum(w, h) * 0.25
     for i in range(len(bboxes)):
         x1, y1, x2, y2 = bboxes[i, :4]
         landmark = bboxes[i, 9:].reshape((5, 2))
         for j in range(5):
             x, y = landmark[j]
             patch_bbox = [
                 x - l[i] / 2, y - l[i] / 2, x + l[i] / 2, y + l[i] / 2
             ]
             patch = crop_face(img, patch_bbox)
             patch = cv2.resize(patch, (24, 24))
             patch = patch.transpose((2, 0, 1))
             data[i, (3 * j):(3 * j + 3)] = patch
     data = (data - 128) / 128
     offset = self._forward(self.lnet, data, ['landmark_offset'])[0]
     offset = offset.reshape(n, 10)
     offset *= l.reshape((-1, 1))
     bboxes[:, 9:] += offset
     timer.toc()
     ts[3] = timer.elapsed()
     bb[3] = bboxes.copy()
     self._clear_network_buffer(self.lnet)
     if debug is True:
         return bb, ts
     else:
         return bboxes
예제 #3
0
파일: timeline.py 프로젝트: noonat/omplex
class TimelineManager(threading.Thread):
    def __init__(self):
        self.currentItems   = {}
        self.currentStates  = {}
        self.idleTimer      = Timer()
        self.subTimer       = Timer()
        self.serverTimer    = Timer()
        self.stopped        = False
        self.halt           = False

        threading.Thread.__init__(self)

    def stop(self):
        self.halt = True
        self.join()

    def run(self):
        while not self.halt:
            if playerManager._player and playerManager._video:
                if not playerManager.is_paused():
                    self.SendTimelineToSubscribers()
                playerManager.update()
                self.idleTimer.restart()
            else:
                if settings.display_sleep > 0 and self.idleTimer.elapsed() >= settings.display_sleep:
                    if display.is_on:
                        log.debug("TimelineManager::run putting display to sleep")
                        display.power_off()

            time.sleep(1)

    def SendTimelineToSubscribers(self):
        log.debug("TimelineManager::SendTimelineToSubscribers updating all subscribers")
        for sub in remoteSubscriberManager.subscribers.values():
            self.SendTimelineToSubscriber(sub)

    def SendTimelineToSubscriber(self, subscriber):
        timelineXML = self.GetCurrentTimeLinesXML(subscriber)
        url = "%s/:/timeline" % subscriber.url

        log.debug("TimelineManager::SendTimelineToSubscriber sending timeline to %s" % url)

        tree = et.ElementTree(timelineXML)
        tmp  = StringIO()
        tree.write(tmp, encoding="utf-8", xml_declaration=True)

        tmp.seek(0)
        xmlData = tmp.read()

        # TODO: Abstract this into a utility function and add other X-Plex-XXX fields
        requests.post(url, data=xmlData, headers={
            "Content-Type":             "application/x-www-form-urlencoded",
            "Connection":               "keep-alive",
            "Content-Range":            "bytes 0-/-1",
            "X-Plex-Client-Identifier": settings.client_uuid
        })

    def WaitForTimeline(self, subscriber):
        log.info("TimelineManager::WaitForTimeline not implemented...")

    def GetCurrentTimeLinesXML(self, subscriber):
        tlines = self.GetCurrentTimeline()

        #
        # Only "video" is supported right now
        #
        mediaContainer = et.Element("MediaContainer")
        if subscriber.commandID is not None:
            mediaContainer.set("commandID", str(subscriber.commandID))
        mediaContainer.set("location", tlines["location"])

        lineEl = et.Element("Timeline")
        for key, value in tlines.items():
            lineEl.set(key, str(value))
        mediaContainer.append(lineEl)

        return mediaContainer

    def GetCurrentTimeline(self):
        # https://github.com/plexinc/plex-home-theater-public/blob/pht-frodo/plex/Client/PlexTimelineManager.cpp#L142
        options = {
            "location": "navigation",
            "state":    playerManager.get_state(),
            "type":     "video"
        }
        controllable = []

        video  = playerManager._video
        player = playerManager._player

        if video and player:
            media = playerManager._video.parent

            options["location"]          = "fullScreenVideo"

            options["time"]              = player.position * 1e3
            
            options["ratingKey"]         = video.get_video_attr("ratingKey")
            options["key"]               = video.get_video_attr("key")
            options["containerKey"]      = video.get_video_attr("key")
            options["guid"]              = video.get_video_attr("guid")
            options["duration"]          = video.get_video_attr("duration", "0")
            options["address"]           = media.path.hostname
            options["protocol"]          = media.path.scheme
            options["port"]              = media.path.port
            options["machineIdentifier"] = media.get_machine_identifier()
            options["seekRange"]         = "0-%s" % options["duration"]

            controllable.append("playPause")
            controllable.append("stop")
            controllable.append("stepBack")
            controllable.append("stepForward")
            controllable.append("subtitleStream")
            controllable.append("audioStream")
            controllable.append("seekTo")

            # If the duration is unknown, disable seeking
            if options["duration"] == "0":
                options.pop("duration")
                options.pop("seekRange")
                controllable.remove("seekTo")

            # Volume control is enabled only if output isn't HDMI,
            # although technically I'm pretty sure we can still control
            # the volume even if the output is hdmi...
            if settings.audio_output != "hdmi":
                controllable.append("volume")
                options["volume"] = str(playerManager.get_volume(percent=True)*100 or 0)

            options["controllable"] = ",".join(controllable)
        else:
            options["time"] = 0

        return options
예제 #4
0
def main(argv):
    # rma, drug, stress = load_data()
    rma, label = data.load_mdd_data()

    alpha_range = np.logspace(-2, 7, 10)
    l1_ratio_range = np.arange(0., 1., 0.1)
    en_param_grid = dict(alpha=alpha_range, l1_ratio=l1_ratio_range)

    c_range = np.logspace(-2, 7, 10)
    gamma_range = np.logspace(-6, 3, 10)
    svm_param_grid = dict(gamma=gamma_range, C=c_range)

    logit_param_grid = dict(C=c_range)

    test_size = float(argv[1])
    n_iter = int(argv[2])
    n_folds = int(argv[3])
    target = argv[4]
    classifier = argv[5]
    pca_components = int(argv[7])
    log = {
        'target': target,
        'std_select': {
            'n_feat': pca_components
        },
        'split': {
            'type': 'StratifiedShuffleSplit',
            'n_iter': n_iter,
            'test_size': test_size
        },
        'cross_val': {
            'n_folds': n_folds
        },
        'classifier': classifier
    }
    # if target == 'drug':
    #     target = drug
    # else:
    #     target = stress

    # pca = PCA(n_components=pca_components)
    pca = SelectStd(n_features=pca_components)

    if classifier == 'svm':
        clf = SVC()
        param_grid = svm_param_grid
        grid_search = True
    elif classifier == 'en':
        clf = SGDClassifier(loss='log', penalty='elasticnet', n_jobs=1)
        param_grid = en_param_grid
        grid_search = True
    elif classifier == 'logit':
        clf = LogisticRegression()
        param_grid = logit_param_grid
        grid_search = True

    timer = Timer()
    print('\nStarting...' + ' '.join(argv))
    pprint(log)
    split = StratifiedShuffleSplit(label[target],
                                   n_iter=n_iter,
                                   test_size=test_size)

    if grid_search:
        clf = GridSearchCV(clf, param_grid=param_grid, cv=n_folds, n_jobs=1)

    pipeline = Pipeline([('pca', pca), ('clf', clf)])

    accuracy = cross_val_score(pipeline,
                               rma,
                               y=label[target],
                               scoring='accuracy',
                               cv=split,
                               n_jobs=n_iter,
                               verbose=1)
    print('\n{}: Accuracy: {:.2%} +/- {:.2%}'.format(timer.elapsed(),
                                                     np.nanmean(accuracy),
                                                     np.nanstd(accuracy)))

    log['results'] = {
        'accuracy': {
            'scores': accuracy.tolist(),
            'mean': accuracy.mean(),
            'std': accuracy.std()
        }
    }
    log['time'] = timer.elapsed()

    # results = [dict(log, accuracy=acc) for acc in accuracy]
    # log_results(results)
    # save_results(results, folder=argv[6], filename='results_new.json')
    save_experiment(log, folder=argv[6])
예제 #5
0
파일: epi_ad.py 프로젝트: hmourit/MScThesis
    if args.clf == 'logit':
        pass
    elif args.clf == 'en':
        alpha_range = np.logspace(-2, 7, 10)
        l1_ratio_range = np.arange(0., 1., 10)
        param_grid = dict(alpha=alpha_range, l1_ratio=l1_ratio_range)
        clf = SGDClassifier(loss='log', penalty='elasticnet')
        grid = GridSearchCV(clf, param_grid=param_grid, cv=args.n_folds, n_jobs=-1)
        pipeline.append(('en', grid))
    else:
        result['error'] = '{} is not a valid classifier.'.format(args.clf)
        save_experiment(result, folder=args.results_path, error=True,
                        verbose=args.verbose)

    pipeline = Pipeline(pipeline)

    timer = Timer()
    accuracy = cross_val_score(pipeline, betas, y=target, scoring='accuracy', cv=split, n_jobs=1)
    result['time'] = timer.elapsed()

    result['results'] = {
        'accuracy': {
            'scores': accuracy.tolist(),
            'mean': accuracy.mean(),
            'std': accuracy.std()
        }
    }

    save_experiment(result, folder=args.results_path, verbose=args.verbose)
예제 #6
0
def main(argv):
    # rma, drug, stress = load_data()
    rma, label = data.load_mdd_data()

    alpha_range = np.logspace(-2, 7, 10)
    l1_ratio_range = np.arange(0.0, 1.0, 0.1)
    en_param_grid = dict(alpha=alpha_range, l1_ratio=l1_ratio_range)

    c_range = np.logspace(-2, 7, 10)
    gamma_range = np.logspace(-6, 3, 10)
    svm_param_grid = dict(gamma=gamma_range, C=c_range)

    logit_param_grid = dict(C=c_range)

    test_size = float(argv[1])
    n_iter = int(argv[2])
    n_folds = int(argv[3])
    target = argv[4]
    classifier = argv[5]
    pca_components = int(argv[7])
    log = {
        "target": target,
        "pca": {"n_components": pca_components},
        "split": {"type": "StratifiedShuffleSplit", "n_iter": n_iter, "test_size": test_size},
        "cross_val": {"n_folds": n_folds},
        "classifier": classifier,
    }
    # if target == 'drug':
    #     target = drug
    # else:
    #     target = stress

    pca = PCA(n_components=pca_components)

    if classifier == "svm":
        clf = SVC()
        param_grid = svm_param_grid
        grid_search = True
    elif classifier == "en":
        clf = SGDClassifier(loss="log", penalty="elasticnet", n_jobs=1)
        param_grid = en_param_grid
        grid_search = True
    elif classifier == "logit":
        clf = LogisticRegression()
        param_grid = logit_param_grid
        grid_search = True

    timer = Timer()
    print("\nStarting..." + " ".join(argv))
    pprint(log)
    split = StratifiedShuffleSplit(label[target], n_iter=n_iter, test_size=test_size)

    if grid_search:
        clf = GridSearchCV(clf, param_grid=param_grid, cv=n_folds, n_jobs=1)

    pipeline = Pipeline([("pca", pca), ("clf", clf)])

    accuracy = cross_val_score(pipeline, rma, y=label[target], scoring="accuracy", cv=split, n_jobs=n_iter, verbose=1)
    print("\n{}: Accuracy: {:.2%} +/- {:.2%}".format(timer.elapsed(), np.nanmean(accuracy), np.nanstd(accuracy)))

    log["results"] = {"accuracy": {"scores": accuracy.tolist(), "mean": accuracy.mean(), "std": accuracy.std()}}
    log["time"] = timer.elapsed()

    # results = [dict(log, accuracy=acc) for acc in accuracy]
    # log_results(results)
    # save_results(results, folder=argv[6], filename='results_new.json')
    save_experiment(log, folder=argv[6])
예제 #7
0
                            cv=args.n_folds,
                            n_jobs=-1)
        pipeline.append(('en', grid))
    else:
        result['error'] = '{} is not a valid classifier.'.format(args.clf)
        save_experiment(result,
                        folder=args.results_path,
                        error=True,
                        verbose=args.verbose)

    pipeline = Pipeline(pipeline)

    timer = Timer()
    accuracy = cross_val_score(pipeline,
                               betas,
                               y=target,
                               scoring='accuracy',
                               cv=split,
                               n_jobs=1)
    result['time'] = timer.elapsed()

    result['results'] = {
        'accuracy': {
            'scores': accuracy.tolist(),
            'mean': accuracy.mean(),
            'std': accuracy.std()
        }
    }

    save_experiment(result, folder=args.results_path, verbose=args.verbose)
예제 #8
0
파일: player.py 프로젝트: noonat/omplex
class PlayerManager(object):
    """
    Manages the relationship between a ``Player`` instance and a ``Media``
    item.  This is designed to be used as a singleton via the ``playerManager``
    instance in this module.  All communication between a caller and either the
    current ``player`` or ``media`` instance should be done through this class
    for thread safety reasons as all methods that access the ``player`` or
    ``media`` are thread safe.
    """
    def __init__(self):
        self._player      = None
        self._video       = None
        self._lock        = RLock()
        self.last_update = Timer()

        self.__part      = 1

    @synchronous('_lock')
    def update(self):
        if self._video and self._player:
            # Check to see if we need to turn the display on
            if not display.is_on:
                log.debug("PlayerManager::update display is off, turning on")
                self._player.pause()
                display.power_on()

            if self.last_update.elapsed() > SCROBBLE_INTERVAL and not self.is_paused():
                if not self._video.played:
                    position = self._player.position * 1e3   # In ms
                    duration = self._video.get_duration()
                    if float(position)/float(duration)  >= COMPLETE_PERCENT:
                        log.info("PlayerManager::update setting media as watched")
                        self._video.set_played()
                    else:
                        log.info("PlayerManager::update updating media position")
                        self._video.update_position(position)
                self.last_update.restart()

    @synchronous('_lock')
    def play(self, video, offset=0):
        self.stop()

        args = []
        if offset > 0:
            args.extend(("-l", str(offset)))

        audio_idx = video.get_audio_idx()
        if audio_idx is not None:
            log.debug("PlayerManager::play selecting audio stream index=%s" % audio_idx)
            args.extend(["-n", audio_idx])

        sub_idx = video.get_subtitle_idx()
        if sub_idx is not None:
            log.debug("PlayerManager::play selecting subtitle index=%s" % sub_idx)
            args.extend(["-t", sub_idx])
        else:
            # No subtitles -- this is pretty hacky
            log.debug("PlayerManager::play disabling subtitles")
            args.extend(["--subtitles", "/dev/null"])

        # TODO: Check settings for transcode settings...
        url = video.get_playback_url()
        if not url:
            log.error("PlayerManager::play no URL found")
            return
            
        self._player = Player(mediafile=url, args=args, start_playback=True, finished_callback=self.finished_callback)
        self._video  = video

    @synchronous('_lock')
    def stop(self):
        if not self._video or not self._player:
            return

        log.debug("PlayerManager::stop stopping playback of %s" % self._video)

        osd.hide()

        self._player.stop()

        self._player = None
        self._video  = None

    @synchronous('_lock')
    def get_volume(self, percent=False):
        if self._player:
            if not percent:
                return self._player._volume
            return self._player._VOLUME_STEPS.index(self._player._volume)/float(len(self._player._VOLUME_STEPS))

    @synchronous('_lock')
    def toggle_pause(self):
        if self._player:
            self._player.toggle_pause()
            if self.is_paused() and self._video:
                log.debug("PlayerManager::toggle_pause showing OSD")
                try:
                    duration = int(int(self._video.get_duration())*1e-3)
                except:
                    duration = 0
                osd.show(int(self._player.position), duration, self._video.get_proper_title())
            else:
                log.debug("PlayerManager::toggle_pause hiding OSD")
                osd.hide()

    @synchronous('_lock')
    def seek(self, offset):
        """
        Seek to ``offset`` seconds
        """
        if self._player:
            osd.hide()
            self._player.seek(offset)

    @synchronous('_lock')
    def set_volume(self, pct):
        if self._player:
            self._player.set_volume(pct)

    @synchronous('_lock')
    def get_state(self):
        if not self._player:
            return "stopped"

        if self._player._paused:
            return "paused"

        return "playing"
    
    @synchronous('_lock')
    def is_paused(self):
        if self._player:
            return self._player._paused
        return False

    @synchronous('_lock')
    def finished_callback(self):
        if not self._video:
            return
        
        if self._video.is_multipart():
            log.debug("PlayerManager::finished_callback media is multi-part, checking for next part")
            # Try to select the next part
            next_part = self.__part+1
            if self._video.select_part(next_part):
                self.__part = next_part
                log.debug("PlayerManager::finished_callback starting next part")
                self.play(self._video)

            log.debug("PlayerManager::finished_callback no more parts found")

    @synchronous('_lock')
    def get_video_attr(self, attr, default=None):
        if self._video:
            return self._video.get_video_attr(attr, default)
        return default