Esempio n. 1
0
	def __init__(self, cam, **kwargs):
		super(Screen, self).__init__(**kwargs)
		self._updateTime = 0
		self._fps = "FPS:00.00"

		self._cam = cam
		self._looper = None
		self._classifier = RankClassifier()
		self._disp_mode = 'rectangle'
Esempio n. 2
0
    def __init__(self, cam, sws, **kwargs):
        super(Screen, self).__init__(**kwargs)
        self._updateTime = 0
        self._fps = "FPS:00.00"

        self._cam = cam
        self._label1 = False
        self._looper = None
        self._classifier = RankClassifier()
        self._sws = sws
        self._wm = WorkloadMonitor()
Esempio n. 3
0
class PredictionScreen(Screen):
	_fps = StringProperty()

	def __init__(self, cam, **kwargs):
		super(Screen, self).__init__(**kwargs)
		self._updateTime = 0
		self._fps = "FPS:00.00"

		self._cam = cam
		self._looper = None
		self._classifier = RankClassifier()
		self._disp_mode = 'rectangle'

	def on_enter(self):
		self._looper = Clock.schedule_interval(self.on_loop, 0.1)

	def on_leave(self):
		self._looper.cancel()
		
	def on_loop(self, dt):
		self.capture()

	def capture(self):
		result = self._cam.capture()
		if not result.moving and len(result.images) > 0:
			h_ratio = 0 * MAX_RESIZE_RATIO #TODO: show conpane
			w_ratio = 0 * MAX_RESIZE_RATIO
			predictions = self._classifier.predict(result.images, result.areas, h_ratio, w_ratio)

			if self._disp_mode == 'rectangle':
				self.draw_box_and_label(result.images, result.rects, result.centers, predictions)
			else:
				self.draw_shadow(result.shadow, result.centers)
			self.draw_label(result.images, result.rects, result.centers, predictions)
		else:
			if self._disp_mode == 'rectangle':
				self.draw_box(result.rects)
			else:
				self.draw_shadow(result.shadow, result.centers)
		self.draw_fps()
			
	def draw_box_and_label(self, images, rects, centers, predictions):
		canvas = self.ids.monitor.canvas
		canvas.clear()
		for img, rect, c, p, acc in zip(images, rects, centers, predictions.get_all_labels(), predictions.get_all_accuraces()):
			#Color
			if p[0] < 5:
				color = (0.75, 0.40, 0.62, 1)
			elif p[0] >= 5 and p[0] < 9:
				color = (0.40, 0.75, 0.62, 1)
			elif p[0] >= 8:
				color = (0.7, 0.7, 0.7, 1)
			r,g,b,_ = color

			label_text = LABEL[p[0]]
			label = CoreLabel(text="Rank  :%s"%(label_text), font_size=30, color=color, italic=True)
			label.refresh()
			texture = label.texture
			texture_size = list(texture.size)

			h,w,_ = img.shape
			cm = h * PX2CM_FACTOR
			length = CoreLabel(text="Length:%.1fcm"%(cm), font_size=30, color=color, italic=True)
			length.refresh()
			texture2 = length.texture
			texture2_size = list(texture2.size)

			accuracy = CoreLabel(text="Accuracy:%d"%(int(acc[0]*100))+"%", font_size=30, color=color, italic=True)
			accuracy.refresh()
			texture3 = accuracy.texture
			texture3_size = list(texture3.size)
			
			label2_text = LABEL[p[1]]
			label2 = CoreLabel(text="(%s)"%(label2_text), font_size=20,italic=True)
			label2.refresh()
			texture4 = label2.texture
			texture4_size = list(texture4.size)
			with canvas:
				Color(r, g, b)
				Line(points=rect, width=3, close=True)
				Line(points=[(c[0]-5, 110), (c[0]-35, 5), (c[0] + texture2_size[0], 5)], width=1, color=color)
				Rectangle(texture=texture, pos=(c[0], texture3_size[1]*2+5), size=texture_size)
				Rectangle(texture=texture3, pos=(c[0]-7 , texture2_size[1]+5), size=texture3_size) 
				Rectangle(texture=texture2, pos=(c[0]-14, 5), size=texture2_size)
				Color(0.7,0.7,0.7)
				Rectangle(texture=texture4, pos=(c[0] + texture_size[0] + 10, texture3_size[1]*2+10), size=texture4_size)


	def draw_box(self, rects):
		canvas = self.ids.monitor.canvas
		canvas.clear()
		for rect in rects:
			with canvas:
				Color(0.5, 0.76, 0.86)
				Line(points=rect, width=2, close=True)


	def draw_shadow(self, shadow, centers):
		canvas = self.ids.monitor.canvas
		canvas.clear()
		with canvas:
			texture = Texture.create(size=(shadow.shape[1], shadow.shape[0]), colorfmt='rgb')
			texture.blit_buffer(shadow.tostring(), bufferfmt='ubyte', colorfmt='rgb')
			texture_size = list(texture.size)
			texture.flip_vertical()
			Rectangle(texture=texture, pos=(0, 0), size=self.size)
		
	
	def draw_label(self,images, rects, centers, predictions):
		canvas = self.ids.monitor.canvas
		for img, rect, c, p, acc in zip(images, rects, centers, predictions.get_all_labels(), predictions.get_all_accuraces()):
			#Color
			if p[0] < 5:
				color = (0.75, 0.40, 0.62, 1)
			elif p[0] >= 5 and p[0] < 9:
				color = (0.40, 0.75, 0.62, 1)
			elif p[0] >= 8:
				color = (0.7, 0.7, 0.7, 1)
			r,g,b,_ = color

			label_text = LABEL[p[0]]
			label = CoreLabel(text="Rank  :%s"%(label_text), font_size=30, color=color, italic=True)
			label.refresh()
			texture = label.texture
			texture_size = list(texture.size)

			h,w,_ = img.shape
			cm = h * PX2CM_FACTOR
			length = CoreLabel(text="Length:%.1fcm"%(cm), font_size=30, color=color, italic=True)
			length.refresh()
			texture2 = length.texture
			texture2_size = list(texture2.size)

			accuracy = CoreLabel(text="Accuracy:%d"%(int(acc[0]*100))+"%", font_size=30, color=color, italic=True)
			accuracy.refresh()
			texture3 = accuracy.texture
			texture3_size = list(texture3.size)
			
			label2_text = LABEL[p[1]]
			label2 = CoreLabel(text="(%s)"%(label2_text), font_size=20,italic=True)
			label2.refresh()
			texture4 = label2.texture
			texture4_size = list(texture4.size)
			with canvas:
				Color(r, g, b)
				Line(points=[(c[0]-5, 110), (c[0]-35, 5), (c[0] + texture2_size[0], 5)], width=1, color=color)
				Rectangle(texture=texture, pos=(c[0], texture3_size[1]*2+5), size=texture_size)
				Rectangle(texture=texture3, pos=(c[0]-7 , texture2_size[1]+5), size=texture3_size) 
				Rectangle(texture=texture2, pos=(c[0]-14, 5), size=texture2_size)
				Color(0.7,0.7,0.7)
				Rectangle(texture=texture4, pos=(c[0] + texture_size[0] + 10, texture3_size[1]*2+10), size=texture4_size)


	def draw_fps(self):
		elapsed = time.time() - self._updateTime
		fps = 1.0 / elapsed
		self._fps = "FPS:%02.2f"%(fps)
		self._updateTime = time.time()


	def changed_disp_mode(self, sw):
		if sw == 0:
			self._disp_mode = 'rectangle'
		else:
			self._disp_mode = 'shadow'
Esempio n. 4
0
def main():

    start_time = time.time()

    # Read documents, divide according to the topics and separate train and test data-set.

    t_path = os.getcwd() + "/bbc/"

    all_docs = defaultdict(lambda: list())

    topic_list = list()

    print("Reading all the documents...\n")

    for topic in os.listdir(t_path):
        d_path = t_path + topic + '/'
        topic_list.append(topic)
        temp_docs = list()

        for f in os.listdir(d_path):
            f_path = d_path + f
            temp_docs.append(Document(f_path, topic))

        all_docs[topic] = temp_docs[:]

    fold_count = 10

    train_docs, test_docs = list(), list()

    for key, value in all_docs.items():
        random.shuffle(value)
        test_len = int(len(value) / fold_count)
        train_docs += value[:-test_len]
        test_docs += value[-test_len:]

    # Create tfidf and tfidfie index of training docs, and store into the docs.
    index = Index(train_docs)

    print("Train Document Count: " + str(len(train_docs)))
    print("Test  Document Count: " + str(len(test_docs)))

    test_topics = [d.topic for d in test_docs]

    for doc in train_docs:
        doc.vector = doc.tfidfie

    for doc in test_docs:
        doc.vector = doc.tf

    # create classifier instances.
    nb = NaiveBayes()
    rc = RankClassifier()
    kmeans = KMeans(topic_list)

    classifier_list = [rc, nb, kmeans]

    for i in range(len(classifier_list)):

        print("\nClassifier #" + str(i + 1) + "\n")

        classifier = classifier_list[i]

        classifier.confusion_matrix, c_dict = init_confusion_matrix(topic_list)

        print("Training...\n")

        classifier.train(train_docs)

        print("Testing... Classifying the test docs...\n")

        predictions = classifier.classify(test_docs)

        # Update the confusion matrix and statistics with updated values.
        classifier.confusion_matrix = update_confusion_matrix(
            test_topics, predictions, classifier.confusion_matrix, c_dict)

        classifier.stats = cal_stats(classifier.confusion_matrix)

        print("Confusion Matrix\n")
        for item in classifier.confusion_matrix:
            print(item)

        print("\nStatistics\n")
        print_table(get_stats_table(classifier.stats))

    print("Run time...{} secs \n".format(round(time.time() - start_time, 4)))

    # call recommendation system once classifiers are ready.
    recommendation(all_docs, test_docs, classifier_list)
Esempio n. 5
0
def main():

    start_time = time.time()

    t_path = "../data_set/bbc/"

    all_docs = defaultdict(lambda: list())

    topic_list = list()

    print("Reading all the documents...\n")
    print(os.listdir(t_path))
    for topic in os.listdir(t_path):
        d_path = t_path + topic + '/'
        topic_list.append(topic)
        temp_docs = list()

        for f in os.listdir(d_path):
            f_path = d_path + f
            temp_docs.append(Document(f_path, topic))

        all_docs[topic] = temp_docs[:]

    fold_count = 10

    train_docs, test_docs = list(), list()

    for key, value in all_docs.items():
        random.shuffle(value)
        test_len = int(len(value) / fold_count)
        train_docs += value[:-test_len]
        test_docs += value[-test_len:]

    index = Index(train_docs)

    print("Train Document Count: " + str(len(train_docs)))
    print("Test  Document Count: " + str(len(test_docs)))

    test_topics = [d.topic for d in test_docs]

    for doc in train_docs:
        doc.vector = doc.tfidfie

    for doc in test_docs:
        doc.vector = doc.tf

    nb = NaiveBayes()
    rc = RankClassifier()
    kmeans = KMeans(topic_list)

    classifier_list = [rc, nb, kmeans]

    for i in range(len(classifier_list)):

        print("\nClassifier #" + str(i + 1) + "\n")

        classifier = classifier_list[i]

        classifier.confusion_matrix, c_dict = init_confusion_matrix(topic_list)

        print("Training...\n")

        classifier.train(train_docs)

        print("Testing... Classifying the test docs...\n")

        predictions = classifier.classify(test_docs)

        classifier.confusion_matrix = update_confusion_matrix(
            test_topics, predictions, classifier.confusion_matrix, c_dict)

        classifier.stats = cal_stats(classifier.confusion_matrix)

        print("Confusion Matrix\n")
        for item in classifier.confusion_matrix:
            print(item)

        print("\nStatistics\n")
        print_table(get_stats_table(classifier.stats))

    print("Run time...{} secs \n".format(round(time.time() - start_time, 4)))

    recommendation(all_docs, test_docs, classifier_list)
def main():

    start_time = time.time()

    # Read documents, divide according to the topics and separate train and test data-set.

    t_path = "../bbc/"

    all_docs = defaultdict(lambda: list())

    topic_list = list()
    for topic in os.listdir(t_path):
        d_path = t_path + topic + '/'
        topic_list.append(topic)
        temp_docs = list()

        for f in os.listdir(d_path):
            f_path = d_path + f
            temp_docs.append(Document(f_path, topic))

        all_docs[topic] = temp_docs[:]
    fold_count = 10

    train_docs, test_docs = list(), list()

    for key, value in all_docs.items():
        random.shuffle(value)
        test_len = int(len(value) / fold_count)
        train_docs += value[:-test_len]
        # explanation
        #   lis = [1,2,3,4,5]
        # print(lis[:-4])
        # print(lis[-4:])
        test_docs += value[-test_len:]

    # Create tfidf and tfidfie index of training docs, and store into the docs.
    index = Index(train_docs)

    test_topics = [d.topic for d in test_docs]

    for doc in train_docs:
        doc.vector = doc.tfidfie

    for doc in test_docs:
        doc.vector = doc.tf

    # create classifier instances.
    nb = NaiveBayes()
    rc = RankClassifier()
    kmeans = KMeans(topic_list)

    classifier_list = [nb, rc, kmeans]

    for i in range(len(classifier_list)):

        classifier = classifier_list[i]

        classifier.confusion_matrix, c_dict = init_confusion_matrix(topic_list)

        classifier.train(train_docs)
        predictions = classifier.classify(test_docs)

        # Update the confusion matrix and statistics with updated values.
        classifier.confusion_matrix = update_confusion_matrix(
            test_topics, predictions, classifier.confusion_matrix, c_dict)

        classifier.stats = cal_stats(classifier.confusion_matrix)

    global lst
    lst = []
    lst.append(all_docs)
    lst.append(test_docs)
    lst.append(classifier_list)
    return redirect('http://localhost:5000/recommend')
Esempio n. 7
0
class PredictionScreen(Screen):
    _fps = StringProperty()

    def __init__(self, cam, sws, **kwargs):
        super(Screen, self).__init__(**kwargs)
        self._updateTime = 0
        self._fps = "FPS:00.00"

        self._cam = cam
        self._label1 = False
        self._looper = None
        self._classifier = RankClassifier()
        self._sws = sws
        self._wm = WorkloadMonitor()

    def on_enter(self):
        self._looper = Clock.schedule_interval(self.on_loop, 0.3)

    def on_leave(self):
        self._looper.cancel()

    def capture(self):
        result = self._cam.capture()
        if not result.moving and len(result.images) > 0:
            h_ratio = self._sws.length_meter.get_balance() * MAX_RESIZE_RATIO
            w_ratio = self._sws.width_meter.get_balance() * MAX_RESIZE_RATIO
            predictions = self._classifier.predict(result.images, result.areas,
                                                   h_ratio, w_ratio)
            self.draw_box_and_label(result.images, result.rects,
                                    result.centers, predictions)
            self._wm.count(predictions.get_top_labels())
        else:
            self.draw_box(result.rects)
            if len(result.images) == 0:
                self._wm.clear()

    def on_loop(self, dt):
        select = self._sws.select_meter.get_balance()
        if select < 0:
            self._label1 = False
        else:
            self._label1 = True

        mode = self._sws.get_mode_str()
        if mode == 'prediction':
            self.capture()
        else:
            self.manager.current = mode

    def draw_box_and_label(self, images, rects, centers, predictions):
        elapsed = time.time() - self._updateTime
        fps = 1.0 / elapsed

        canvas = self.ids.monitor.canvas
        canvas.clear()
        for img, rect, c, p, acc in zip(images, rects, centers,
                                        predictions.get_all_labels(),
                                        predictions.get_all_accuraces()):
            #Color
            if p[0] < 5:
                color = (0.75, 0.40, 0.62, 1)
            elif p[0] >= 5 and p[0] < 9:
                color = (0.40, 0.75, 0.62, 1)
            elif p[0] >= 8:
                color = (0.7, 0.7, 0.7, 1)
            r, g, b, _ = color

            label_text = LABEL1[p[0]] if self._label1 else LABEL2[p[0]]
            label = CoreLabel(text="Rank  :%s" % (label_text),
                              font_size=30,
                              color=color,
                              italic=True)
            label.refresh()
            texture = label.texture
            texture_size = list(texture.size)

            h, w, _ = img.shape
            cm = h * PX2CM_FACTOR
            length = CoreLabel(text="Length:%.1fcm" % (cm),
                               font_size=30,
                               color=color,
                               italic=True)
            length.refresh()
            texture2 = length.texture
            texture2_size = list(texture2.size)

            accuracy = CoreLabel(text="Accuracy:%d" % (int(acc[0] * 100)) +
                                 "%",
                                 font_size=30,
                                 color=color,
                                 italic=True)
            accuracy.refresh()
            texture3 = accuracy.texture
            texture3_size = list(texture3.size)

            label2_text = LABEL1[p[1]] if self._label1 else LABEL2[p[1]]
            label2 = CoreLabel(text="(%s)" % (label2_text),
                               font_size=20,
                               italic=True)
            label2.refresh()
            texture4 = label2.texture
            texture4_size = list(texture4.size)
            with canvas:
                Color(r, g, b)
                Line(points=rect, width=3, close=True)
                Line(points=[(c[0] - 5, 110), (c[0] - 35, 5),
                             (c[0] + texture2_size[0], 5)],
                     width=1,
                     color=color)
                Rectangle(texture=texture,
                          pos=(c[0], texture3_size[1] * 2 + 5),
                          size=texture_size)
                Rectangle(texture=texture3,
                          pos=(c[0] - 7, texture2_size[1] + 5),
                          size=texture3_size)
                Rectangle(texture=texture2,
                          pos=(c[0] - 14, 5),
                          size=texture2_size)
                Color(0.7, 0.7, 0.7)
                Rectangle(texture=texture4,
                          pos=(c[0] + texture_size[0] + 10,
                               texture3_size[1] * 2 + 10),
                          size=texture4_size)

        self._fps = "FPS:%02.2f" % (fps)
        self._updateTime = time.time()

    def draw_box(self, rects):
        elapsed = time.time() - self._updateTime
        fps = 1.0 / elapsed

        canvas = self.ids.monitor.canvas
        canvas.clear()
        for rect in rects:
            with canvas:
                Color(0.5, 0.76, 0.86)
                Line(points=rect, width=2, close=True)

        self._fps = "FPS:%02.2f" % (fps)
        self._updateTime = time.time()