Esempio n. 1
0
    X=np.array([[0.697,0.460],[0.774,0.376],[0.634,0.264],[0.608,0.318],[0.556,0.215],
                [0.403,0.237],[0.481,0.149],[0.437,0.211],[0.666,0.091],[0.243,0.267],
                [0.245,0.057],[0.343,0.099],[0.639,0.161],[0.657,0.198],[0.360,0.370],
                [0.593,0.042],[0.719,0.103],[0.359,0.188],[0.339,0.241],[0.282,0.257],
                [0.748,0.232],[0.714,0.346],[0.483,0.312],[0.478,0.437],[0.525,0.369],
                [0.751,0.489],[0.532,0.472],[0.473,0.376],[0.725,0.445],[0.446,0.459]])

    X_test=X
    agnes=AGNES()
    agnes.fit(X)
    print('C:', agnes.C)
    print(agnes.labels_)
    plt.figure(12)
    plt.subplot(121)
    plt.scatter(X[:, 0], X[:, 1], c=agnes.labels_)
    plt.title('tinyml')

    from sklearn.cluster.hierarchical import AgglomerativeClustering
    sklearn_agnes=AgglomerativeClustering(n_clusters=7,affinity='l2',linkage='average')
    sklearn_agnes.fit(X)
    print(sklearn_agnes.labels_)
    plt.subplot(122)
    plt.scatter(X[:,0],X[:,1],c=sklearn_agnes.labels_)
    plt.title('sklearn')
    plt.show()





Esempio n. 2
0
def cluster(diss_matrix, n_clusters):

    agglo_clusterer = AgglomerativeClustering(affinity='precomputed',
                                              linkage='average',
                                              n_clusters=n_clusters)
    return agglo_clusterer.fit(diss_matrix).labels_
    def run_concurrent(self, args, sign_progress):
        idx = 0
        movie_path = args[0]
        resolution = args[1]
        in_hists = args[3]
        fps = args[2]
        indices = args[4]
        frame_resolution = args[5]
        n_cluster_range = args[6]
        alt_resolution = args[7]
        cluster_sizes = range(n_cluster_range[0], n_cluster_range[1], 1)
        histograms = []
        frames = []

        cap = cv2.VideoCapture(movie_path)
        length = cap.get(cv2.CAP_PROP_FRAME_COUNT)

        resize_f = 192.0 / cap.get(cv2.CAP_PROP_FRAME_WIDTH)

        data_idx = 0
        read_img = -1  # We only want to read every second image
        if indices is None:
            indices = []
            resolution = alt_resolution
        for i in range(int(length)):
            if self.aborted:
                return None
            if i % resolution == 0:
                read_img += 1
                if in_hists is not None and data_idx >= len(in_hists):
                    break
                if read_img % frame_resolution == 0:
                    cap.set(cv2.CAP_PROP_POS_FRAMES, i)
                    ret, frame = cap.read()
                    frames.append(
                        cv2.resize(frame, None, None, resize_f, resize_f,
                                   cv2.INTER_CUBIC))
                    read_img = 0

                sign_progress(i / length)
                if in_hists is not None:
                    histograms.append(
                        np.resize(in_hists[data_idx], new_shape=16**3))
                else:
                    cap.set(cv2.CAP_PROP_POS_FRAMES, i)
                    ret, frame = cap.read()
                    if frame is None:
                        break

                    frame = cv2.cvtColor(floatify_img(frame),
                                         cv2.COLOR_BGR2LAB)
                    data = np.resize(frame,
                                     (frame.shape[0] * frame.shape[1], 3))
                    hist = cv2.calcHist([data[:, 0], data[:, 1], data[:, 2]],
                                        [0, 1, 2], None, [16, 16, 16],
                                        [0, 100, -128, 128, -128, 128])
                    indices.append(i)
                    histograms.append(np.resize(hist, new_shape=16**3))
                data_idx += 1

        connectivity = np.zeros(shape=(len(histograms), len(histograms)),
                                dtype=np.uint8)
        for i in range(1, len(histograms) - 1, 1):
            connectivity[i][i - 1] = 1
            connectivity[i][i] = 1
            connectivity[i][i + 1] = 1

        clusterings = []
        for i, n_cluster in enumerate(cluster_sizes):
            sign_progress(i / len(cluster_sizes))

            if len(histograms) > n_cluster:
                model = AgglomerativeClustering(linkage="ward",
                                                connectivity=connectivity,
                                                n_clusters=n_cluster,
                                                compute_full_tree=True)
                model.fit(histograms)
                clusterings.append(model.labels_)

        return [
            clusterings, frames, indices, fps, frame_resolution,
            n_cluster_range
        ]
    def process(self, args, sign_progress):
        """
        This is the actual analysis, which takes place in a WorkerThread.
        Do NOT and NEVER modify the project within this function.

        We want to read though the movie and get the Average Colors from each Segment.

        Once done, we create an Analysis Object from it.
        """
        args, sign_progress = super(ShotSegmentationAnalysis,
                                    self).process(args, sign_progress)
        # Signal the Progress
        sign_progress(0.0)

        video_capture = cv2.VideoCapture(args['movie_path'])

        duration = video_capture.get(cv2.CAP_PROP_FRAME_COUNT)
        resize_f = 192.0 / video_capture.get(cv2.CAP_PROP_FRAME_WIDTH)
        resize_clamp = self.frame_width_clamp / video_capture.get(
            cv2.CAP_PROP_FRAME_WIDTH)
        width = video_capture.get(cv2.CAP_PROP_FRAME_WIDTH)
        height = video_capture.get(cv2.CAP_PROP_FRAME_HEIGHT)

        n = int(np.floor(duration / self.resolution))
        X = np.zeros(shape=(n, 16**3), dtype=np.float16)
        frame_pos = np.zeros(shape=n, dtype=np.int32)

        frames = []
        for i in range(int(n)):
            if self.aborted:
                return None

            idx = int(i * self.resolution)
            video_capture.set(cv2.CAP_PROP_POS_FRAMES, idx)

            ret, frame = video_capture.read()
            if frame is None:
                continue

            if self.return_frames and i % self.frame_resolution == 0:
                frames.append(
                    cv2.resize(frame, None, None, resize_f, resize_f,
                               cv2.INTER_CUBIC))

            if resize_clamp < 1.0:
                frame = cv2.resize(frame, None, None, resize_clamp,
                                   resize_clamp, cv2.INTER_CUBIC)
            frame = cv2.cvtColor(floatify_img(frame), cv2.COLOR_BGR2LAB)
            X[i] = np.resize(calculate_histogram(frame, lab_mode=True),
                             new_shape=16**3) / (width * height)
            frame_pos[i] = idx
            sign_progress(round(i / n, 4))

        connectivity = np.zeros(shape=(n, n), dtype=np.uint8)
        for i in range(1, n - 1, 1):
            connectivity[i][i - 1] = 1
            connectivity[i][i] = 1
            connectivity[i][i + 1] = 1
        clusterings = []

        cluster_sizes = range(self.cluster_range[0], self.cluster_range[1], 1)
        for i, n_cluster in enumerate(cluster_sizes):
            sign_progress(i / len(cluster_sizes))
            if X.shape[0] > n_cluster:
                model = AgglomerativeClustering(linkage="ward",
                                                connectivity=connectivity,
                                                n_clusters=n_cluster,
                                                compute_full_tree=True)
                model.fit(X)
                timestamps = self._generate_segments(
                    model.labels_, frame_pos,
                    video_capture.get(cv2.CAP_PROP_FPS))
                clusterings.append(timestamps)

        if self.return_hdf5_compatible:
            result = np.zeros(shape=self.dataset_shape,
                              dtype=self.dataset_dtype)
            for i in range(len(clusterings)):
                result[i][0:len(clusterings[i])] = [[
                    c['label'], c['f_start'], c['f_stop']
                ] for c in clusterings[i]]
            # Creating an IAnalysisJobAnalysis Object that will be handed back to the Main-Thread
            analysis = IAnalysisJobAnalysis(
                name="My Analysis",
                results=result,
                analysis_job_class=self.__class__,
                parameters=dict(resolution=self.resolution),
                container=args['movie_descriptor'])
        else:
            analysis = AnalysisContainer(name=self.name, data=clusterings)
        sign_progress(1.0)
        return analysis