示例#1
0
文件: image.py 项目: Kingjmk/mlfaati
def classify(uploaded_file: 'SimpleUploadedFile', **options):
    """
    Function to classify images based on selected algorithm in settings,
    only works in *unix systems*
    """
    try:
        from imageai.Classification import ImageClassification
    except ImportError as e:
        logger.warning(
            '"ImageClassification" Import not found, please import to use classify functions'
        )
        raise e

    model_options = settings.MODEL_OPTIONS[settings.IMAGE_CLASSIFY_MODEL]
    prediction = ImageClassification()
    prediction.setModelTypeAsInceptionV3()
    prediction.setModelPath(model_options['path'])
    prediction.loadModel()

    result = {}
    new_uploaded_file = compress(uploaded_file, quality=100)
    # Write file to tmp to get path
    temp = tempfile.NamedTemporaryFile(suffix='.png')
    temp.write(new_uploaded_file.read())
    temp.seek(0)

    predictions, probabilities = prediction.classifyImage(temp.name,
                                                          result_count=5)
    for eachPrediction, eachProbability in zip(predictions, probabilities):
        result[eachPrediction] = eachProbability

    return {'classification': result}
示例#2
0
def test_recognition_model_densenet():

    predictor = ImageClassification()
    predictor.setModelTypeAsDenseNet121()
    predictor.setModelPath(os.path.join(main_folder, "data-models", "DenseNet-BC-121-32.h5"))
    predictor.loadModel()
    predictions, probabilities = predictor.classifyImage(image_input=os.path.join(main_folder, main_folder, "data-images", "1.jpg"))

    assert isinstance(predictions, list)
    assert isinstance(probabilities, list)
    assert isinstance(predictions[0], str)
    assert isinstance(probabilities[0], float)
示例#3
0
def test_recognition_model_inceptionv3():

    predictor = ImageClassification()
    predictor.setModelTypeAsInceptionV3()
    predictor.setModelPath(os.path.join(main_folder, "data-models", "inception_v3_weights_tf_dim_ordering_tf_kernels.h5"))
    predictor.loadModel()
    predictions, probabilities = predictor.classifyImage(image_input=os.path.join(main_folder, main_folder, "data-images", "1.jpg"))

    assert isinstance(predictions, list)
    assert isinstance(probabilities, list)
    assert isinstance(predictions[0], str)
    assert isinstance(probabilities[0], float)
示例#4
0
def test_recognition_model_resnet():

    predictor = ImageClassification()
    predictor.setModelTypeAsResNet50()
    predictor.setModelPath(os.path.join(main_folder, "data-models", "resnet50_imagenet_tf.2.0.h5"))
    predictor.loadModel()
    predictions, probabilities = predictor.classifyImage(image_input=os.path.join(main_folder, main_folder, "data-images", "1.jpg"))

    assert isinstance(predictions, list)
    assert isinstance(probabilities, list)
    assert isinstance(predictions[0], str)
    assert isinstance(probabilities[0], float)
示例#5
0
class AI:
    def __init__(self):
        self.execution_path = os.getcwd()

    def init_object_detection(self):
        #import the model
        self.detector = ObjectDetection()
        self.detector.setModelTypeAsRetinaNet()
        self.detector.setModelPath(
            os.path.join(self.execution_path, "resnet50_coco_best_v2.1.0.h5"))
        self.detector.loadModel()

    def grab_object_tags(self, frame, timestamp):
        detections = self.detector.detectObjectsFromImage(
            input_image=frame,
            output_image_path=frame,
            minimum_percentage_probability=30)

        tags = []
        for eachObject in detections:
            t = {
                "tag": eachObject["name"],
                "probability": eachObject["percentage_probability"],
                "location": eachObject["box_points"]
            }
            tags.append(t)

        return {"timestamp": timestamp, "tags": tags}

    def init_image_classification(self):
        # import the model
        self.prediction = ImageClassification()
        self.prediction.setModelTypeAsResNet50()
        self.prediction.setModelPath(
            os.path.join(self.execution_path, "resnet50_imagenet_tf.2.0.h5"))
        self.prediction.loadModel()

    def grab_image_tags(self, frame, timestamp):
        predictions, probabilities = self.prediction.classifyImage(
            frame, result_count=10)
        for eachPrediction, eachProbability in zip(predictions, probabilities):
            print(eachPrediction, " : ", eachProbability)
示例#6
0
def detectobjectinimage():
    path = os.getcwd()
    image_prediction = ImageClassification()
    image_prediction.setModelTypeAsResNet50()
    image_prediction.setModelPath(
        os.path.join(path, "resnet50_imagenet_tf.2.0.h5"))
    image_prediction.loadModel()

    predictions, percentage_probabilities = image_prediction.classifyImage(
        os.path.join(path, "/home/akwa/PycharmProjects/remindme/img.jpg"),
        result_count=10)

    for eachPrediction, eachProbability in zip(predictions,
                                               percentage_probabilities):
        print(eachPrediction, " : ", eachProbability)
        result = "Cup wasn't detected, hence, user hasn't drank water today"
        vampire = 'Raid come to our aid, we are dying!!!'
        if eachPrediction == "mosquito_net":
            detection_window(vampire)
        break
示例#7
0
import os
from imageai.Classification import ImageClassification

# get current directory
execution_path = os.getcwd()

prediction = ImageClassification()
prediction.setModelTypeAsMobileNetV2()
prediction.setModelPath(os.path.join(execution_path, "mobilenet_v2.h5"))
prediction.loadModel()

predictions, probabilities = prediction.classifyImage(os.path.join(
    execution_path, "giraffe.jpg"),
                                                      result_count=5)
for eachPrediction, eachProbability in zip(predictions, probabilities):
    print(eachPrediction, " : ", eachProbability)
示例#8
0
from imageai.Classification import ImageClassification
import os
executionpath = os.getcwd()
print(executionpath)
detection = ImageClassification()
detection.setModelTypeAsResNet50()
detection.setModelPath(executionpath + "/resnet50_imagenet_tf.2.0.h5")
detection.loadModel()
detections, percentageprobabilites = detection.classifyImage("Testimage.jpg",
                                                             result_count=5)
for Index in range(len(detections)):
    print(detections[Index], ":", percentageprobabilites[Index])