Exemple #1
0
def loadModel():
    prediction = CustomImagePrediction()
    prediction.setModelTypeAsResNet()
    prediction.setModelPath("model200.h5")
    prediction.setJsonPath("class1.json")
    prediction.loadModel(num_objects=12)
    return prediction
def fileUpload():
    target = os.path.join(UPLOAD_FOLDER, 'test_docs')
    if not os.path.isdir(target):
        os.mkdir(target)
    logger.info("welcome to upload`")
    file = request.files['file']
    filename = secure_filename(file.filename)
    destination = "/".join([target, filename])
    file.save(destination)
    session['uploadFilePath'] = destination
    execution_path = os.getcwd()
    dir = os.path.dirname(os.path.realpath(__file__))
    dir.replace("\\", "\\\\")
    prediction = CustomImagePrediction()
    prediction.setModelTypeAsResNet()
    prediction.setJsonPath(dir + r"\model_class.json")
    prediction.setModelPath(dir + r"\model_ex-085_acc-0.966261.h5")
    prediction.loadModel(num_objects=2)

    try:
        predictions, probabilities = prediction.predictImage(
            dir + r"\\test_docs\\" + filename, result_count=2)
    except:
        tf.keras.backend.clear_session()
        os.remove('test_docs\\' + filename)
        return "error"

    tf.keras.backend.clear_session()

    p1, p2 = zip(predictions, probabilities)

    result = p1[0]
    os.remove('test_docs\\' + filename)
    return result
Exemple #3
0
 def detect_move_from_webcam(
     self,
     model_file_name=None,
     config_file_name="model_class.json",
     webcam_index=0,
     sensibility=80,
 ):
     # Instantiate the CustomImagePrediction object that will predict our moves
     predictor = CustomImagePrediction()
     # Set the model type of the neural network (it must be the same of the training)
     self._set_proper_model_type(self.model_type, predictor)
     if not model_file_name:
         # If model file name is not set, ask user to choose which model must be used
         model_file_name = self._get_model_file_name(self.dataset_name)
     # Set path to the trained model file
     predictor.setModelPath(
         os.path.join(self.base_path, self.dataset_name, "models",
                      model_file_name))
     # Set path to the json file that contains our classes and their labels
     predictor.setJsonPath(
         os.path.join(self.base_path, self.dataset_name, "json",
                      config_file_name))
     # Load the trained model and set it to use "class_number" classes
     predictor.loadModel(num_objects=self.class_number)
     # Context manager that help us to activate/deactivate our webcam
     with opencv_video_capture(webcam_index) as cap:
         # Run until user press "q" key
         while True:
             # Acquire a frame from webcam
             _, frame = cap.read()
             # Do a prediction on that frame
             predictions, probabilities = predictor.predictImage(
                 frame, result_count=3, input_type="array")
             # Get a tuple (class_predicted, probability) that contains the best
             # prediction
             best_prediction = max(zip(predictions, probabilities),
                                   key=lambda x: x[1])
             # If probability of the best prediction is >= of the min sensibility
             # required, it writes a label with predicted class name and probability
             # over the frame
             if best_prediction[1] >= sensibility:
                 cv2.putText(
                     frame,
                     f"{best_prediction[0]} ({round(best_prediction[1], 2)})",
                     (10, 30),
                     cv2.FONT_HERSHEY_SIMPLEX,
                     1,
                     (255, 255, 255),
                     2,
                     cv2.LINE_AA,
                 )
             # Display the acquired frame on a dedicated window
             cv2.imshow("Move predictor", frame)
             # Break cycle if user press "q" key
             if cv2.waitKey(1) & 0xFF == ord("q"):
                 break
Exemple #4
0
def ImageML():
    prediction = CustomImagePrediction()
    prediction.setModelTypeAsResNet()
    prediction.setModelPath("idenprof/models/idenprof_061-0.7933.h5")
    prediction.setJsonPath("idenprof/json/model_class.json")
    prediction.loadModel(num_objects=10)
    predictions, probabilities = prediction.predictImage("image.jpg",
                                                         result_count=3)
    for eachPrediction, eachProbability in zip(predictions, probabilities):
        print(eachPrediction, " : ", eachProbability)
Exemple #5
0
def getModel():
    global prediction
    if prediction is not None:
        return prediction
    prediction = CustomImagePrediction()
    prediction.setModelTypeAsResNet50()
    prediction.setModelPath("model.h5")
    prediction.setJsonPath("styles.json")
    prediction.loadModel(num_objects=25)
    return prediction
class RockPaperScissorsPredictor:
    """
    This class contains the required code for model training and move prediction using a
    webcam
    """

    MODEL_TYPE_SET_LOOKUP = {
        ModelTypeEnum.RESNET: lambda x: x.setModelTypeAsResNet(),
        ModelTypeEnum.SQEEZENET: lambda x: x.setModelTypeAsSqueezeNet(),
        ModelTypeEnum.INCEPTIONV3: lambda x: x.setModelTypeAsInceptionV3(),
        ModelTypeEnum.DENSENET: lambda x: x.setModelTypeAsDenseNet(),
    }

    MOVES_LOOKUP = {
        "rock": MovesEnum.ROCK,
        "paper": MovesEnum.PAPER,
        "scissors": MovesEnum.SCISSORS,
    }

    def __init__(
            self,
            model_type=ModelTypeEnum.RESNET,
            class_number=3,  # We have 3 different objects: "rock", "paper", "scissors"
    ):
        self.model_type = model_type
        self.class_number = class_number
        self.base_path = os.getcwd()
        # Instantiate the CustomImagePrediction object that will predict our moves
        self.predictor = CustomImagePrediction()
        # Set the model type of the neural network (it must be the same of the training)
        self._set_proper_model_type(self.model_type)
        # Set path to the trained model file
        self.predictor.setModelPath(
            os.path.join(self.base_path, "data", "move_detector", "model.h5"))
        # Set path to the json file that contains our classes and their labels
        self.predictor.setJsonPath(
            os.path.join(self.base_path, "data", "move_detector",
                         "model_class.json"))
        # Load the trained model and set it to use "class_number" classes
        self.predictor.loadModel(num_objects=self.class_number)

    def _set_proper_model_type(self, model_type):
        self.MODEL_TYPE_SET_LOOKUP[model_type](self.predictor)

    def detect_move_from_picture(self, picture, sensibility=90):
        predictions, probabilities = self.predictor.predictImage(
            picture, result_count=3, input_type="array")
        # Get a tuple (class_predicted, probability) that contains the best
        # prediction
        best_prediction = max(zip(predictions, probabilities),
                              key=lambda x: x[1])
        if best_prediction[1] < sensibility:
            return

        return self.MOVES_LOOKUP[best_prediction[0]]
Exemple #7
0
def classify_trash(images):
    prediction = CustomImagePrediction()
    prediction.setModelTypeAsResNet()
    prediction.setModelPath(os.path.join(execution_path, "model.h5"))
    prediction.setJsonPath(os.path.join(execution_path, "model_class.json"))
    prediction.loadModel(num_objects=6)
    trashTypes = set()
    for i in images:
        pre, prob = prediction.predictImage(i, input_type="array")
        trashTypes.add(pre[0])
    return trashTypes
Exemple #8
0
def getOccupation(filename):
    prediction = CustomImagePrediction()
    prediction.setModelTypeAsResNet()
    prediction.setModelPath("preTrainedModels/idenprof.h5")
    prediction.setJsonPath("preTrainedModels/idenprof.json")
    prediction.loadModel(num_objects=10)

    predictions, probabilities = prediction.predictImage(filename,
                                                         result_count=1)

    return predictions[0]
Exemple #9
0
def ImageClassifier(Image, ModelFile, JsonFile, Classes):
    execution_path = os.getcwd()  #get model path
    prediction = CustomImagePrediction(
    )  #create instance for the prediction model
    prediction.setModelTypeAsResNet()  #set classification model to ResNet
    prediction.setModelPath(ModelFile)  #loads best model trained
    prediction.setJsonPath(JsonFile)  #loads best model trained
    prediction.loadModel(num_objects=Classes)  #Loads best model trained
    predictions, probabilities = prediction.predictImage(
        Image, result_count=3)  #classify the image
    return predictions, probabilities
Exemple #10
0
def main(path_model, path_classes, path_image):
    prediction = CustomImagePrediction()
    prediction.setModelTypeAsResNet()
    prediction.setModelPath(path_model)
    prediction.setJsonPath(path_classes)
    prediction.loadModel(num_objects=10)

    predictions, probabilities = prediction.predictImage(path_image,
                                                         result_count=5)

    for eachPrediction, eachProbability in zip(predictions, probabilities):
        print(eachPrediction, " : ", eachProbability)
Exemple #11
0
    def ObjectDetect(self):
        execution_path = "C:\Tensorflow\models\Research\object_detection\Engine\customPrediction"
        print(execution_path)
        detector = ObjectDetection()
        detector.setModelTypeAsRetinaNet()
        detector.setModelPath(
            os.path.join(execution_path, "resnet50_coco_best_v2.0.1.h5"))
        detector.loadModel()
        detections1 = detector.detectCustomObjectsFromImage(
            input_image=os.path.join(execution_path, "trail1.jpg"),
            output_image_path=os.path.join(execution_path, "example3.jpg"))

        detector2 = ObjectDetection()
        detector2.setModelTypeAsYOLOv3()
        detector2.setModelPath(os.path.join(execution_path, "yolo.h5"))
        detector2.loadModel()
        detections2 = detector2.detectCustomObjectsFromImage(
            input_image=os.path.join(execution_path, "trail1.jpg"),
            output_image_path=os.path.join(execution_path, "example4.jpg"))

        detector3 = ObjectDetection()
        detector3.setModelTypeAsTinyYOLOv3()
        detector3.setModelPath(os.path.join(execution_path, "yolo-tiny.h5"))
        detector3.loadModel()
        detections3 = detector3.detectCustomObjectsFromImage(
            input_image=os.path.join(execution_path, "trail1.jpg"),
            output_image_path=os.path.join(execution_path, "example5.jpg"))

        prediction = CustomImagePrediction()
        prediction.setModelTypeAsResNet()
        prediction.setModelPath(
            os.path.join(execution_path, "model_ex-027_acc-0.843750.h5"))
        prediction.setJsonPath(os.path.join(execution_path,
                                            "model_class.json"))
        prediction.loadModel(num_objects=2)
        predictions, probabilities = prediction.predictImage(os.path.join(
            execution_path, "trail1.jpg"),
                                                             result_count=5)

        detections = detections1 + detections2 + detections3
        List = []
        for i in detections:
            List.append(i["name"])

        for eachPrediction, eachProbability in zip(predictions, probabilities):
            if eachProbability > 50:
                List.append(eachPrediction)
        """for eachObject in detections:
            print(eachObject["name"], " : ", eachObject["percentage_probability"])

        for eachPrediction, eachProbability in zip(predictions, probabilities):
            print(eachPrediction, " : ", eachProbability)"""
        return List
def foo():
    a=[]
    prediction= CustomImagePrediction()
    prediction.setModelTypeAsResNet()
    prediction.setModelPath("model_ex-030_acc-0.996974.h5")
    prediction.setJsonPath("model_class2.json")
    prediction.loadModel(num_objects=13)
    predictions, probabilities = prediction.predictImage("images/image.jpg", result_count=3)
    for eachPrediction, eachProbability in zip(predictions, probabilities):
        #print(eachPrediction , " : " , eachProbability)
        a.append(eachPrediction)
    return jsonify({"Your image result is here and solution has been sent via a mail": a[0]})
def detect():
	prediction = CustomImagePrediction()
	prediction.setModelTypeAsResNet()
	prediction.setModelPath("model200.h5")
	prediction.setJsonPath("class1.json")
	prediction.loadModel(num_objects=12)
	predictions, probabilities = prediction.predictImage("C:/xampp/htdocs/tcs/temp.jpg", result_count=1)

	for eachPrediction, eachProbability in zip(predictions, probabilities):
		item = eachPrediction
	
	return item
Exemple #14
0
def match_tracks(detections, video_name, thresh=10, scale=4):
    matched = set()
    scores = dict()
    frame_num = 0
    cap = cv2.VideoCapture(video_name)

    prediction = CustomImagePrediction()
    prediction.setModelTypeAsResNet()
    prediction.setModelPath("models/resnet_model.h5")
    prediction.setJsonPath("models/resnet_model_class.json")
    prediction.loadModel(num_objects=2)
    a = 0
    while True:
        ret, frame = cap.read()
        if not ret:
            break
        if frame_num in detections:
            for ID in detections[frame_num]:
                if ID in matched:
                    continue
                try:
                    box = scaleROI(detections[frame_num][ID], scale)
                    img = frame[box[1]:box[3], box[0]:box[2]]

                    cv2.imwrite("temp.1.jpg", img)
                    predictions, probabilities = prediction.predictImage(
                        "temp.1.jpg", result_count=2)
                    boat_score = probabilities[0] if predictions[
                        0] == 'boat' else probabilities[1]
                    if boat_score >= 65:
                        a += 1
                        print(predictions, probabilities, a)
                        cv2.imshow("", img)
                        cv2.waitKey(1)
                        scores[ID] = scores[ID] + 1 if ID in scores else 1
                        if scores[ID] >= thresh:
                            matched.add(ID)
                except Exception as e:
                    pass
        frame_num += 1
    tracks = dict({-1: dict()})
    for frame_num in detections:
        for ID in detections[frame_num]:
            if ID not in tracks:
                tracks[ID] = dict()
            if ID in matched:
                tracks[-1][frame_num] = detections[frame_num][ID]
            else:
                if len(tracks[ID]) >= 3 * 20:
                    tracks[ID][frame_num] = detections[frame_num][ID]

    return tracks
def test_custom_recognition_model_densenet():

    predictor = CustomImagePrediction()
    predictor.setModelTypeAsDenseNet()
    predictor.setModelPath(os.path.join(main_folder, "data-models", "idenprof_densenet-0.763500.h5"))
    predictor.setJsonPath(model_json=os.path.join(main_folder, "data-json", "idenprof.json"))
    predictor.loadModel(num_objects=10)
    predictions, probabilities = predictor.predictImage(image_input=os.path.join(main_folder, main_folder, "data-images", "9.jpg"))

    assert isinstance(predictions, list)
    assert isinstance(probabilities, list)
    assert isinstance(predictions[0], str)
    assert isinstance(probabilities[0], str)
Exemple #16
0
def detect():
    prediction = CustomImagePrediction()
    prediction.setModelTypeAsResNet()
    prediction.setModelPath(os.path.join("model200.h5"))
    prediction.setJsonPath("class1.json")
    prediction.loadModel(num_objects=14)
    predictions, probabilities = prediction.predictImage(
        "UPLOAD_FOLDER/photo.jpg", result_count=1)

    # for eachPrediction, eachProbability in zip(predictions, probabilities):
    #     print(eachPrediction)
    #     item = eachPrediction
    item = database(predictions[0])
    return item
Exemple #17
0
async def main(file_type: str, files: str):
    model = CustomImagePrediction()
    model.setModelTypeAsResNet()

    model.setModelPath(
        os.path.join(EXECUTION_PATH, 'data/images/models/kermit_finder.h5'))
    model.setJsonPath(
        os.path.join(EXECUTION_PATH, 'data/images/json/model_class.json'))
    model.loadModel(num_objects=2)  # number of objects on your trained model

    if file_type == 'image':
        for image in files.split(','):
            print(await predict_image(image_name=image, model=model))
    else:
        await predict_video(video_path=files, model=model)
Exemple #18
0
def init():
    #Prepare and load model
    global execution_path, prediction, graph, session
    execution_path = os.getcwd()
    session = tf.Session()
    graph = tf.get_default_graph()
    tf.keras.backend.set_session(session)
    prediction = CustomImagePrediction()
    prediction.setModelTypeAsResNet()
    prediction.setModelPath(
        execution_path +
        "/Train&Test/chest_xray/models/model_ex-003_acc-0.773026.h5")
    prediction.setJsonPath(execution_path +
                           "/Train&Test/chest_xray/json/model_class.json")
    prediction.loadModel(num_objects=2)
Exemple #19
0
def agricheck():
    prediction = CustomImagePrediction()
    prediction.setModelTypeAsResNet()
    prediction.setModelPath(r"C:\Users\Kunal Jindal\PycharmProjects\agricure.tech\Files\model_ex-022_acc-0.966484.h5")
    prediction.setJsonPath(r"C:\Users\Kunal Jindal\PycharmProjects\agricure.tech\Files\model_class.json")
    prediction.loadModel(num_objects=18)
    for a, b, c in os.walk(r"C:\Users\Kunal Jindal\PycharmProjects\agricure.tech\static\upload"):
        imglist = a + r"\\" + c[0]
    predictions, probabilities = prediction.predictImage(imglist, result_count=1)
    os.remove(imglist)
    db = TinyDB(r"C:\Users\Kunal Jindal\PycharmProjects\agricure.tech\Files\db.json")
    User = Query()
    val = (db.search(User.code == predictions[0]))[0]
    outlist = {'code': predictions[0], 'plant': val["plant"], 'treatment': val["treatment"], 'disease': val["disease"]}
    return outlist
Exemple #20
0
def obj_prediction(img):
    execution_path = os.getcwd()

    prediction = CustomImagePrediction()
    prediction.setModelTypeAsResNet()
    prediction.setModelPath(os.path.join(execution_path, "rat_model.h5"))
    prediction.setJsonPath(os.path.join(execution_path, "model_class.json"))
    prediction.loadModel(num_objects=2, prediction_speed="fastest")

    predictions, probabilities = prediction.predictImage(os.path.join(
        execution_path, img + ".png"),
                                                         result_count=2)

    max_prediction = ""
    max_probability = -1.00
    print("\n=================================")
    for eachPrediction, eachProbability in zip(predictions, probabilities):
        if (max_probability < float(eachProbability)):
            max_prediction = eachPrediction
            max_probability = float(eachProbability)

        print(eachPrediction + " : " + eachProbability)
    print("=================================\n")

    # create Image object with the input image
    image = Image.open(img + ".png")

    # initialise the drawing context with the image object as background
    draw = ImageDraw.Draw(image)

    # create font object with the font file and specify desired size
    font = ImageFont.truetype("Roboto-Black.ttf", size=size, encoding="unic")
    colour = "rgb(255, 255, 255)"

    # draw the message on the background
    draw.rectangle(((0, 0), (1500, 160)),
                   outline="rgb(0, 0, 0)",
                   fill="rgb(0, 0, 0)")
    draw.text((x, y),
              max_prediction + ": " + str(max_probability),
              fill=colour,
              font=font)

    # save the edited image
    image.save(img + "_pro.png")
    print("Generated " + img_name + "_pro.png")
Exemple #21
0
def predict_oil_quality(request):
    try:

        filename = 'model_ex-003_acc-0.677249.h5'
        request_image = request.FILES['photo']
        #request_image = request.data['photo']

        path = request_image.file.name
        print("path: " + path)
        
       # test_image = image.load_img(request_image, target_size=(64, 64, 3))
        #test_image = image.img_to_array(test_image)
        #test_image = np.expand_dims(test_image, axis = 0)
        
        execution_path = os.getcwd()

        prediction = CustomImagePrediction()
        prediction.setModelTypeAsResNet()
        prediction.setModelPath(filename)
        prediction.setJsonPath("model_class.json")
        prediction.loadModel(num_objects=3)
        
       # predictions, probabilities = prediction.predictImage(os.path.join(execution_path, request_image), result_count=3)
        predictions, probabilities = prediction.predictImage(path, result_count=3)

        result = 0
        result_text = ''

        for eachPrediction, eachProbability in zip(predictions, probabilities):
            if(eachProbability > result):
                result = eachProbability
                result_text = eachPrediction

        if(result_text == 'good_oil'):
            result_text = 'GOOD'
        elif(result_text == 'bad_oil'):
            result_text = 'BAD'
        else:
            result_text = 'NO OIL'


        return Response(data=result_text, status=200)
    except Exception as e:
        print(e)
        return Response(status=400)
Exemple #22
0
def test_custom_recognition_model_resnet():

    try:
        keras.backend.clear_session()
    except:
        None

    predictor = CustomImagePrediction()
    predictor.setModelTypeAsResNet()
    predictor.setModelPath(os.path.join(main_folder, "data-models", "idenprof_resnet.h5"))
    predictor.setJsonPath(model_json=os.path.join(main_folder, "data-json", "idenprof.json"))
    predictor.loadModel(num_objects=10)
    predictions, probabilities = predictor.predictImage(image_input=os.path.join(main_folder, main_folder, "data-images", "9.jpg"))

    assert isinstance(predictions, list)
    assert isinstance(probabilities, list)
    assert isinstance(predictions[0], str)
    assert isinstance(probabilities[0], str)
Exemple #23
0
    def modelPredict(model_path='data/models/model_ex-001_acc-0.500000.h5',
                     class_path='data/json/model_class.json',
                     pic_path='a.jpg',
                     classNum=2,
                     resNum=5):
        '''

        模型预测部分
        prediction_speed[模型加载的速度]:fast faster fastest
        '''
        prediction = CustomImagePrediction()
        prediction.setModelTypeAsResNet()
        prediction.setModelPath(model_path)
        prediction.setJsonPath(class_path)
        prediction.loadModel(num_objects=classNum, prediction_speed='fastest')
        prediction, probabilities = prediction.predictImage(
            pic_path, result_count=resNum)
        for eachPrediction, eachProbability in zip(predictions, probabilities):
            print(eachPrediction + " : " + str(eachProbability))
Exemple #24
0
def image_pred(image):
    execution_path = os.getcwd()

    prediction = CustomImagePrediction()
    prediction.setModelTypeAsSqueezeNet()
    prediction.setModelPath(
        os.path.join(execution_path, "model_ex-020_acc-0.992188.h5"))
    prediction.setJsonPath(os.path.join(execution_path, "model_class.json"))
    prediction.loadModel(num_objects=2)

    f = io.BytesIO(image)
    predictions, probabilities = prediction.predictImage(f, result_count=2)

    out = ("", 0)
    for eachPrediction, eachProbability in zip(predictions, probabilities):
        print(eachPrediction, " : ", eachProbability)
    if probabilities[0] > 90:
        return predictions[0]
    return predictions[1]
def analisisFunction(file_path):
    n = 0
    prediction = CustomImagePrediction()
    prediction.setModelTypeAsResNet()
    prediction.setModelPath("assets/models/model_061-0.7933.h5")
    prediction.setJsonPath("assets/json/model_class.json")
    prediction.loadModel(num_objects=10)

    predictions, probabilities = prediction.predictImage(file_path,
                                                         result_count=3)
    encabezado = "RESULTADOS:", file_path
    Lb1.insert(n, encabezado)
    for eachPrediction, eachProbability in zip(predictions, probabilities):
        n += 1
        pro = round(float(eachProbability), 2)
        line = eachPrediction, pro, " %"
        print(line)
        Lb1.insert(n, line)

    Lb1.insert(n + 1, " ")
Exemple #26
0
def test_custom_recognition_model_resnet_multi():
    predictor = CustomImagePrediction()
    predictor.setModelTypeAsResNet()
    predictor.setModelPath(
        os.path.join(main_folder, "data-models", "idenprof_resnet.h5"))
    predictor.setJsonPath(
        model_json=os.path.join(main_folder, "data-json", "idenprof.json"))
    predictor.loadModel(num_objects=10)
    images_to_image_array()
    result_array = predictor.predictMultipleImages(
        sent_images_array=all_images_array)

    assert isinstance(result_array, list)
    for result in result_array:
        assert "predictions" in result
        assert "percentage_probabilities" in result
        assert isinstance(result["predictions"], list)
        assert isinstance(result["percentage_probabilities"], list)
        assert isinstance(result["predictions"][0], str)
        assert isinstance(result["percentage_probabilities"][0], str)
Exemple #27
0
def run_Image_AI(filename="empty"):
    execution_path = os.getcwd()

    prediction = CustomImagePrediction()
    prediction.setModelTypeAsResNet()
    prediction.setModelPath("model_ex-092_acc-0.959971.h5")
    prediction.setJsonPath("model_class.json")
    prediction.loadModel(num_objects=7)

    predictions, probabilities = prediction.predictImage(filename,
                                                         result_count=7)
    count = 0
    finPred = ""
    finProb = 0.0
    for eachPrediction, eachProbability in zip(predictions, probabilities):
        finPred = eachPrediction
        finProb = eachProbability
        break

    return finPred
def prediction_custom(image):
    pred = ""
    execution_path = os.getcwd()

    prediction = CustomImagePrediction()
    prediction.setModelTypeAsInceptionV3()
    prediction.setModelPath(
        os.path.join(execution_path, "model_ex-006_acc-0.836483.h5"))
    prediction.setJsonPath(os.path.join(execution_path, "model_class.json"))
    prediction.loadModel(num_objects=12, prediction_speed="fast")

    predictions, probabilities = prediction.predictImage(os.path.join(
        execution_path, image),
                                                         result_count=1)
    # print(str(prediction + " " + probabilities))

    for eachPrediction, eachProbability in zip(predictions, probabilities):
        class1 = eachPrediction
        pred = eachProbability

    return class1, pred
Exemple #29
0
class Model(object):
    def __init__(self):
        self.prediction = CustomImagePrediction()
        self.prediction.setModelTypeAsResNet()
        self.prediction.setModelPath(
            "datasets/models/model_ex-062_acc-0.916385.h5")
        self.prediction.setJsonPath("datasets/json/model_class.json")
        self.prediction.loadModel(num_objects=2)
        self.webcam = Webcam()

    def predict(self, frame):
        pred, prob = self.prediction.predictImage(detect(frame)[1],
                                                  input_type="array",
                                                  result_count=2)

        if prob[0] < 80:
            cv2.rectangle(frame, (0, 0), (50, 50), (0, 255, 0), -1)
        else:
            cv2.rectangle(frame, (0, 0), (50, 50), (0, 0, 255), -1)

        return frame
    def loadPrediction(self, prediction_speed='normal', num_objects=10):
        if self.__modelloaded == False:
            if self.__modelType == "":
                raise ValueError(
                    "You must set a valid model type before loading the model."
                )
                if self.__jsonPath == "":
                    raise ValueError(
                        "You must set a valid json path before loading the model."
                    )
            elif self.__modelType == "resnet":
                prediction = CustomImagePrediction()
                prediction.setModelTypeAsResNet()

            elif self.__modelType == "squeezenet":
                prediction = CustomImagePrediction()
                prediction.setModelTypeAsSqueezeNet()

            elif self.__modelType == "densenet":
                prediction = CustomImagePrediction()
                prediction.setModelTypeAsDenseNet()

            elif self.__modelType == "inceptionv3":
                prediction = CustomImagePrediction()
                prediction.setModelTypeAsInceptionV3()

            elif self.__modelType == "vgg":
                prediction = CustomImagePrediction()
                prediction.setModelTypeAsVgg()

            prediction.setModelPath(self.modelPath)
            prediction.setJsonPath(self.__jsonPath)
            prediction.loadModel(prediction_speed, num_objects)
            self.__prediction_collection.append(prediction)
            self.__modelloaded = True
        else:
            raise ValueError(
                "You must set a valid model type before loading the model.")
from imageai.Prediction.Custom import CustomImagePrediction
import os

execution_path = os.getcwd()

prediction = CustomImagePrediction()
prediction.setModelTypeAsResNet()
prediction.setModelPath(os.path.join(execution_path, "resnet_model_ex-020_acc-0.651714.h5"))
prediction.setJsonPath(os.path.join(execution_path, "model_class.json"))
prediction.loadModel(num_objects=10)

predictions, probabilities = prediction.predictImage(os.path.join(execution_path, "4.jpg"), result_count=5)

for eachPrediction, eachProbability in zip(predictions, probabilities):
    print(eachPrediction + " : " + eachProbability)