コード例 #1
0
    def identify(self, request, *args, **kwargs):
        """Identify a photo."""
        logging.debug("going to identify photo")
        photo = self.get_object()
        logging.debug("retrieved object")

        # If the ROI is set, use that. If no ROI is set, then use the existing
        # ROI if any. If the ROI is set, but evaluates to False, then set the
        # ROI to None.
        roi = request.data.get('roi', photo.roi)
        if not roi:
            roi = None
            logging.debug("no ROI")

        if photo.roi != roi:
            photo.roi = roi
            photo.save()
            logging.debug("stored ROI")

        # Set the ROI for the classifier.
        if roi:
            try:
                roi = roi.split(',')
                roi = [int(x) for x in roi]
                assert len(roi) == 4
            except:
                return Response(
                    {'roi': "Must be of the format `x,y,width,height`"},
                    status=status.HTTP_400_BAD_REQUEST)

        # Delete all photo identities, if any.
        Identity.objects.filter(photo=photo).delete()
        logging.debug("deleted photo identities")

        # Classify the photo.
        config = open_config(CONFIG_FILE)
        logging.debug("read config file")
        classifier = ImageClassifier(config)
        logging.debug("instantiated ImageClassifier")
        classifier.set_roi(roi)
        logging.debug("applied ROI to ImageClassifier")
        classes = classify_image(classifier, photo.image.path, ANN_DIR)
        logging.debug("received classification")

        # Identify this photo.
        for c in classes:
            if not c.get('genus'):
                continue

            id_ = Identity(photo=photo,
                           genus=c.get('genus'),
                           section=c.get('section'),
                           species=c.get('species'),
                           error=c.get('error'))

            # Save the identity into the database.
            id_.save()

        return self.retrieve(request, *args, **kwargs)
コード例 #2
0
ファイル: views.py プロジェクト: naturalis/nbclassify
    def identify(self, request, *args, **kwargs):
        """Identify a photo."""
        photo = self.get_object()

        # If the ROI is set, use that. If no ROI is set, then use the existing
        # ROI if any. If the ROI is set, but evaluates to False, then set the
        # ROI to None.
        roi = request.data.get('roi', photo.roi)
        if not roi:
            roi = None

        if photo.roi != roi:
            photo.roi = roi
            photo.save()

        # Set the ROI for the classifier.
        if roi:
            try:
                roi = roi.split(',')
                roi = [int(x) for x in roi]
                assert len(roi) == 4
            except:
                return Response({'roi': "Must be of the format `x,y,width,height`"},
                    status=status.HTTP_400_BAD_REQUEST)

        # Delete all photo identities, if any.
        Identity.objects.filter(photo=photo).delete()

        # Classify the photo.
        config = open_config(CONFIG_FILE)
        classifier = ImageClassifier(config)
        classifier.set_roi(roi)
        classes = classify_image(classifier, photo.image.path, ANN_DIR)

        # Identify this photo.
        for c in classes:
            if not c.get('genus'):
                continue

            id_ = Identity(
                photo=photo,
                genus=c.get('genus'),
                section=c.get('section'),
                species=c.get('species'),
                error=c.get('error')
            )

            # Save the identity into the database.
            id_.save()

        return self.retrieve(request, *args, **kwargs)
コード例 #3
0
    def test_trainer_ad(self):
        """Test the `classify` subcommands."""
        filter_ = self.config.classification.filter.as_dict()
        image = os.path.join(IMAGE_DIR,
            "Cypripedium/Arietinum/plectrochilum/14990382409.jpg")

        with db.session_scope(META_FILE) as (session, metadata):
            classes = db.get_classes_from_filter(session, metadata, filter_)
            if not classes:
                raise ValueError("No classes found for filter `%s`" % filter_)
            codewords = get_codewords(classes)

            classifier = ImageClassifier(self.config)
            codeword = classifier.classify_image(image, self.ann_file,
                self.config)
            classification = get_classification(codewords, codeword, 0.001)

        class_ = [class_ for mse,class_ in classification]
        print "Image is classified as {0}".format(", ".join(class_))