Exemplo n.º 1
0
def conv(books):
    data = baseline(books)
    plt.figure(figsize=(10, 2))
    #plt.axis('off')
    plt.plot(data.Time, data.Record)
    plt.savefig('1234.png')
    prediction_key = "45183f01c6a5411aa812727e157ff00f"
    project_id = "40428416-afc2-4201-819e-27ce80646b0f"
    iteration_id = "21d2b2bc-bc4b-4e7f-903e-4d961df831b7"
    print(time.strftime("%H:%M:%S"))
    predictor = prediction_endpoint.PredictionEndpoint(prediction_key)
    with open("1234.png", mode="rb") as test_data:
        results = predictor.predict_image(project_id, test_data, iteration_id)


#print(pd.DataFrame(results.predictions))
    appended_data = []
    for prediction in results.predictions:
        appended_data.append(prediction.tag_name +
                             ": {0:.2f}%".format(prediction.probability * 100))
        #print ("\t" + prediction.tag_name + ": {0:.2f}%".format(prediction.probability * 100))
    k1 = pd.DataFrame(appended_data).to_json(orient='records')
    #print(k1)
    #print (time.strftime("%H:%M:%S"))
    return k1
Exemplo n.º 2
0
def tag():
    # try:
    if (request.method == 'POST'):
        from azure.cognitiveservices.vision.customvision.prediction import prediction_endpoint
        from azure.cognitiveservices.vision.customvision.prediction.prediction_endpoint import models

        # Now there is a trained endpoint, it can be used to make a prediction

        prediction_key = os.environ['NEW_CUSTOM_VISION_PREDICTION_KEY']

        predictor = prediction_endpoint.PredictionEndpoint(prediction_key)

        # test_img_url = "http://efoodrecipe.com/wp-content/uploads/2017/01/pempek-recipes.jpg"
        # results = predictor.predict_image_url("713bf156-aae0-4994-91aa-cc565185421d", "ae152805-1e2c-46fe-a6db-a57a032d2177", url=test_img_url)

        # Alternatively, if the images were on disk in a folder called Images along side the sample.py then
        # they could be added by the following.
        #
        # Open the sample image and get back the prediction results.
        # test_data = request.form['image']
        # print('sampe')
        try:
            test_data = request.files['image']
        except:
            return jsonify(result='false', msg='test_data is None')
        print('sampe 2')
        print(test_data)
        results = predictor.predict_image(
            os.environ['NEW_CUSTOM_VISION_PROJECT_ID'], test_data.read(),
            os.environ['NEW_CUSTOM_VISION_ITERATION_ID'])
        # with open("image/nasi_goreng.jpg", mode="rb") as test_data:
        # results = predictor.predict_image(os.environ['NEW_CUSTOM_VISION_PROJECT_ID'], test_data.read(), os.environ['NEW_CUSTOM_VISION_ITERATION_ID'])

        prediction_array = []
        prediction_json = {}
        max_probability = 0
        max_tag = 'None'

        # Display the results.
        for prediction in results.predictions:
            # print ("\t" + prediction.tag + ": {0:.2f}%".format(prediction.probability * 100))
            # prediction_json['tag'] = prediction.tag
            # prediction_json['probability'] = round(prediction.probability * 100, 2)
            # prediction_array.append(prediction_json)

            if (prediction.probability > max_probability):
                max_probability = prediction.probability
                max_tag = prediction.tag

        calory_amount = db.get_calory_from_food_name(max_tag)
        if (calory_amount == 0):
            # Return only one tag
            return jsonify(result='false', msg='food name not found in db')
        else:
            # Return only one tag
            return jsonify(result='true',
                           prediction_tag=max_tag,
                           prediction_probability=round(
                               max_probability * 100, 2),
                           calories=calory_amount)
Exemplo n.º 3
0
def main():
    trainer = training_api.TrainingApi(CUSTOMVISION_TRAINING_KEY)
    predictor = prediction_endpoint.PredictionEndpoint(
        CUSTOMVISION_PREDICTION_KEY)

    # find project
    print('Get project {} info...'.format(PROJECT_NAME))
    project_id = None
    for p in trainer.get_projects():
        if p.name == PROJECT_NAME:
            project_id = p.id
            break

    if project_id is None:
        print('[ERROR] Project {} not found!'.format(PROJECT_NAME))
        return

    print('Predicting...')
    with open('test_image.jpg', 'rb') as image_data:
        results = predictor.predict_image(project_id, image_data.read())

    print('Result:')
    for prediction in results.predictions:
        print("{0}: {1:.2f}%".format(prediction.tag_name,
                                     prediction.probability * 100))
Exemplo n.º 4
0
def get_msvision_classif(img, api_credentials, model_setup):
    """
    Retrieve the classification result for the MS Vision API

    Args:
        img (np.ndarray): image (or cropped image) to classify
        api_credentials (dict): all credentials required to access the API 

    Returns:
        pred_conf (dict): predicted confidence for each class
    """

    try:
        predictor = prediction_endpoint.PredictionEndpoint(
            api_credentials['prediction_key'])
        results = predictor.predict_image(model_setup['project_id'], img,
                                          model_setup['iter_id'])
        pred_conf = {}
        for prediction in results.predictions:
            pred_conf[prediction.tag_name] = prediction.probability

    except Exception:
        raise ValueError('Call to MS Vision API failed.')

    return pred_conf
Exemplo n.º 5
0
def predict(prediction_key, test_img_url):
    # Now there is a trained endpoint that can be used to make a prediction
    predictor = prediction_endpoint.PredictionEndpoint(prediction_key)
    results = predictor.predict_image_url(project.id,
                                          iteration.id,
                                          url=test_img_url)
    for prediction in results.predictions:
        print("\t" + prediction.tag_name +
              ": {0:.2f}%".format(prediction.probability * 100))
def predict_project(subscription_key):
    predictor = prediction_endpoint.PredictionEndpoint(subscription_key)

    # Find or train a new project to use for prediction.
    project = find_or_train_project()

    with open(os.path.join(IMAGES_FOLDER, "Test", "test_image.jpg"),
              mode="rb") as test_data:
        results = predictor.predict_image(project.id, test_data.read())

    # Display the results.
    for prediction in results.predictions:
        print("\t" + prediction.tag_name +
              ": {0:.2f}%".format(prediction.probability * 100))
def predict_project(prediction_key, project, iteration):
    predictor = prediction_endpoint.PredictionEndpoint(prediction_key)

    # Open the sample image and get back the prediction results.
    with open(os.path.join(IMAGES_FOLDER, "Test", "test_od_image.jpg"),
              mode="rb") as test_data:
        results = predictor.predict_image(project.id, test_data, iteration.id)

    # Display the results.
    for prediction in results.predictions:
        print(
            "\t" + prediction.tag_name +
            ": {0:.2f}%".format(prediction.probability * 100),
            prediction.bounding_box.left, prediction.bounding_box.top,
            prediction.bounding_box.width, prediction.bounding_box.height)
Exemplo n.º 8
0
def get_msvision_objdetection(img, api_credentials, model_setup):
    """
    Retrieve the classification result for the MS Vision API

    Args:
        img (np.ndarray): image (or cropped image) to classify
        api_credentials (dict): all credentials required to access the API 

    Returns:
        pred_conf (dict): predicted confidence for each class
    """

    try:
        predictor = prediction_endpoint.PredictionEndpoint(
            api_credentials['prediction_key'])
        results = predictor.predict_image(model_setup['project_id'], img,
                                          model_setup['iter_id'])
        predictions = []
        pred_boxes, pred_labs, pred_conf = [], [], []
        for prediction in results.predictions:
            pred_boxes.append([
                prediction.bounding_box.left, prediction.bounding_box.top,
                prediction.bounding_box.width, prediction.bounding_box.height
            ])
            pred_labs.append(prediction.tag_name)
            pred_conf.append(prediction.probability)
            predictions.append({
                'tag':
                prediction.tag_name,
                'conf':
                prediction.probability,
                'box': [
                    prediction.bounding_box.left, prediction.bounding_box.top,
                    prediction.bounding_box.width,
                    prediction.bounding_box.height
                ]
            })

        pred_boxes, pred_labs, pred_conf = np.asarray(pred_boxes), np.asarray(
            pred_labs), np.asarray(pred_conf)
        sorted_idx = np.where(pred_conf >= 0.2)
        pred_boxes, pred_labs, pred_conf = pred_boxes[sorted_idx], pred_labs[
            sorted_idx], pred_conf[sorted_idx]
    except Exception:
        raise ValueError('Call to MS Vision API failed.')

    return pred_boxes, pred_labs, pred_conf
def evaluate_person(image_url='camImage.png', wish='casual', gender=None):
    from gender_recognizer import get_gender
    # from azure.cognitiveservices.vision.customvision.training import training_api

    training_key = "1dcf529eee10461c951c4e1b324dcdc8"
    prediction_key = "f5bbd790c81e4b379e0250cab326b1ab"
    pred_key_test = "81f0135a10924fa8baabe39c5adb8e4a"

    #PREDICTION
    from azure.cognitiveservices.vision.customvision.prediction import prediction_endpoint
    from azure.cognitiveservices.vision.customvision.prediction.prediction_endpoint import models

    # Now there is a trained endpoint that can be used to make a prediction
    from take_image import take_photo

    if gender == None:
        gender = get_gender(image_url)

    predictor = prediction_endpoint.PredictionEndpoint(pred_key_test)
    print(predictor)

    # Open the sample image and get back the prediction results.
    with open(image_url, mode="rb") as test_data:
        results = predictor.predict_image(
            '109b6711-e20a-47cb-ad0e-c90e6103d689', test_data.read())

    # Display the results. and save
    predictions = {}
    for prediction in results.predictions:
        #print ("\t" + prediction.tag + ": {0:.2f}%".format(prediction.probability * 100))
        predictions[prediction.probability] = prediction.tag

    keys = predictions.keys()
    max_key = max(keys)
    best_prediction = predictions[max_key]

    intern_gender = 'man' if gender == 'male' else 'woman'

    print(best_prediction)
    print(wish + '_' + intern_gender)
    if best_prediction == wish + '_' + intern_gender:
        print('MATCH')
        return True, best_prediction

    else:
        return False, best_prediction
Exemplo n.º 10
0
def identify(image):

    predictor = prediction_endpoint.PredictionEndpoint(
        "15c7f6bd782b4fab887295c83a608f42")
    with open(image, mode="rb") as test_data:
        results = predictor.predict_image(
            "ac4d0722-29ce-4116-b9d2-225b453a3df3", test_data.read())
    answer = None
    percent = 0
    for prediction in results.predictions:
        if prediction.probability > .5:
            if (answer == None):
                answer = prediction.tag
            else:
                if prediction.probability > percent:
                    answer = prediction.tag
                    percent = prediction.probability
    return answer
def azure_classification(filename):
    """Get an azure classification from the Azure Custom Vision API for an image.
    
    """

    # Azure API Call
    project_id = "<PROJECT ID>"
    prediction_key = "<PREDICTION KEY>"
    iteration_id = "<MODEL ITERATION ID>"

    predictor = prediction_endpoint.PredictionEndpoint(prediction_key)
    with open(filename, mode="rb") as image_data:
        results = predictor.predict_image(project_id, image_data, iteration_id)
        tags = list()
        for prediction in results.predictions:
            if prediction.probability > 0.1:
                tags.append(prediction.tag_name)
        return tags
Exemplo n.º 12
0
def predict_image(**kwargs):
    json_body = kwargs.get('json_body', None)
    if (not json_body):
        log.log_error("Body is missing")
        return -1

    if (not "img_url" in json_body):
        log.log_error("img_url is required as input.")
        return -1

    try:
        # Extract the img_url input from the json body
        test_img_url = json_body["img_url"]

        # Here's how to debug
        print(test_img_url)  # You can check this in Postman
        log.log_debug(
            test_img_url)  # You can check this in Application Insights

        # Call the Custom Vision service endpoint
        predictor = prediction_endpoint.PredictionEndpoint(prediction_key)
        results = predictor.predict_image_url(project_id,
                                              iteration_id,
                                              url=test_img_url)

        # Construct the predictions for output
        ret = []
        for prediction in results.predictions:
            pred = {}
            pred['prediction'] = prediction.tag_name
            pred['confidence'] = "{0:.2f}%".format(prediction.probability *
                                                   100)
            ret.append(pred.copy())

        # Format and return output
        return json.dumps(ret)
    except:
        print(sys.exc_info()[0])
        log.log_exception(sys.exc_info()[0])
Exemplo n.º 13
0
def azure_objectdetection(filename):
    """
    Get an azure object detection result from the Azure Custom Vision API for an image.
    
    """
    # Change these to your custom vision keys
    project_id = "PROJECT ID"
    prediction_key = "PREDICTION KEY"
    iteration_id = "MODEL ITERATION ID"

    predictor = prediction_endpoint.PredictionEndpoint(prediction_key)

    # Open the sample image and get back the prediction results.
    with open(filename, mode="rb") as image_data:
        results = predictor.predict_image(project_id, image_data, iteration_id)
        tags = list()
        for prediction in results.predictions:
            if prediction.probability > 0.2:
                tags.append(prediction.tag_name)
        if len(tags) == 0:
            tags.append("nothing found")

        return tags
Exemplo n.º 14
0
def findsigns(image_data, sign_recognizer):

    predictor = prediction_endpoint.PredictionEndpoint(
        settings.customvision_prediction_key)

    results = predictor.predict_image(settings.customvision_projectid,
                                      image_data,
                                      settings.customvision_iterationid)

    image = Image.open(io.BytesIO(image_data))
    np_image = np.array(image)

    signs = []
    arrows = []

    arrowadjustment = 20

    # Display the results.
    for prediction in results.predictions:
        #print ("\t" + prediction.tag_name + ": {0:.2f}%".format(prediction.probability * 100), prediction.bounding_box.left, prediction.bounding_box.top, prediction.bounding_box.width, prediction.bounding_box.height)

        if ((prediction.tag_name == 'ParkingSign'
             and prediction.probability > 0.3)
                or ('Arrow' in prediction.tag_name
                    and prediction.probability >= 0.05)):  #

            h = np_image.shape[0]
            w = np_image.shape[1]
            x = int(prediction.bounding_box.left * w)
            y = int((prediction.bounding_box.top) * h)
            width = int(prediction.bounding_box.width * w)
            height = int(prediction.bounding_box.height * h)
            if (prediction.tag_name == 'ParkingSign'):
                signs.append({
                    'probability': prediction.probability,
                    'sign_coordinates': [x, y, x + width, y + height],
                    'sign_size': [width, height]
                })
            if ('Arrow' in prediction.tag_name):
                arrows.append({
                    'probability':
                    prediction.probability,
                    'arrow':
                    prediction.tag_name.replace('Arrow', ''),
                    'arrow_coordinates': [x, y, x + width, y + height],
                    'arrow_size': [width, height]
                })

    texts = []

    if (sign_recognizer == 'recognizeText'):
        analysis = recognizeText(image_data, 'Printed')

        if ("recognitionResult" in analysis):
            # Extract the recognized text, with bounding boxes.
            polygons = [(line["boundingBox"], line["text"])
                        for line in analysis["recognitionResult"]["lines"]]

            #overlay the image with the extracted text
            for polygon in polygons:
                vertices = [(polygon[0][i], polygon[0][i + 1])
                            for i in range(0, len(polygon[0]), 2)]
                text = {
                    "x": vertices[0][0],
                    "y": vertices[0][1],
                    "text": polygon[1],
                    "boundingtype": "polygon",
                    "coordinates": vertices
                }
                texts.append(text)

    if (sign_recognizer == 'ocr'):
        analysis = ocr(image_data)
        if ("regions" in analysis):
            # Extract the word bounding boxes and text.
            line_infos = [region["lines"] for region in analysis["regions"]]
            word_infos = []
            for line in line_infos:
                for word_metadata in line:
                    for word_info in word_metadata["words"]:
                        word_infos.append(word_info)

            for word in word_infos:
                bbox = [int(num) for num in word["boundingBox"].split(",")]
                text = word["text"]
                texts.append({
                    "x":
                    bbox[0],
                    "y":
                    bbox[1],
                    "text":
                    word["text"],
                    "boundingtype":
                    "rectangle",
                    "coordinates": [bbox[0], bbox[1], bbox[2], bbox[3]]
                })

    for sign in signs:

        sign_x1 = sign['sign_coordinates'][0]
        sign_y1 = sign['sign_coordinates'][1]
        sign_x2 = sign['sign_coordinates'][2]
        sign_y2 = sign['sign_coordinates'][3]
        sign_w = sign['sign_size'][0]
        sign_h = sign['sign_size'][1]

        sign["texts"] = []
        for text in texts:

            if (text["x"] >= sign_x1 and text["x"] < sign_x2
                    and text["y"] >= sign_y1 and text["y"] < sign_y2):
                sign["texts"].append(text)

        for arrow in arrows:

            arrow_x1 = arrow['arrow_coordinates'][0]
            arrow_y1 = arrow['arrow_coordinates'][1]
            arrow_x2 = arrow['arrow_coordinates'][2]
            arrow_y2 = arrow['arrow_coordinates'][3]
            arrow_w = arrow['arrow_size'][0]
            arrow_h = arrow['arrow_size'][1]

            if ((arrow_x1 >= sign_x1
                 or abs(arrow_x1 - sign_x1) <= arrowadjustment)
                    and (arrow_x2 <= sign_x2
                         or abs(arrow_x2 - sign_x2) <= arrowadjustment)
                    and (arrow_y1 >= sign_y1
                         or abs(arrow_y1 - sign_y1) <= arrowadjustment)
                    and (arrow_y2 <= sign_y2
                         or abs(arrow_y2 - sign_y2) <= arrowadjustment)):

                if ("arrow" in sign):

                    if (sign["arrow"]["probability"] < arrow["probability"]):
                        sign["arrow"] = arrow
                else:
                    sign["arrow"] = arrow

    return signs
Exemplo n.º 15
0
def evalPic(test_img_url):

    # Now there is a trained endpoint that can be used to make a prediction
    training_key = "9f84608619f943e1abd429dce654f187"
    prediction_key = "30e812af3b4e4122a837e5aac8b61218"

    predictor = prediction_endpoint.PredictionEndpoint(prediction_key)

    #test_img_url = str(input("Enter an image url: "))
    results = predictor.predict_image_url(
        "76e20514-28a8-4028-85fe-184acfafdb3c", url=test_img_url)

    # Alternatively, if the images were on disk in a folder called Images alongside the sample.py, then
    # they can be added by using the following.
    #
    # Open the sample image and get back the prediction results.
    # with open("Images\\test\\test_image.jpg", mode="rb") as test_data:
    #     results = predictor.predict_image(project.id, test_data.read(), iteration.id)

    res = {
        "Good Quality": "",
        "Bad Quality": "",
        "Centered": "",
        "Not Centered": "",
        "Face Visible": "",
        "Face Obscured": "",
        "Good Lighting": "",
        "Bad Lighting": ""
    }

    # Display the results.
    for prediction in results.predictions:
        # print ("\t" + prediction.tag + ": {0:.2f}%".format(prediction.probability * 100))
        if prediction.tag == "goodquality":
            res["Good Quality"] = prediction.probability * 100
        elif prediction.tag == "badquality":
            res["Bad Quality"] = prediction.probability * 100
        elif prediction.tag == "notcentered":
            res["Not Centered"] = prediction.probability * 100
        elif prediction.tag == "centered":
            res["Centered"] = prediction.probability * 100
        elif prediction.tag == "facevisible":
            res["Face Visible"] = prediction.probability * 100
        elif prediction.tag == "faceobscured":
            res["Face Obscured"] = prediction.probability * 100
        elif prediction.tag == "goodlighting":
            res["Good Lighting"] = prediction.probability * 100
        elif prediction.tag == "badlighting":
            res["Bad Lighting"] = prediction.probability * 100

    #print(res)

    print("Picture evaluation:")
    s1 = "Picture Evaluation: "
    score = 0
    print()

    if res["Good Quality"] > res["Bad Quality"]:
        val = res["Good Quality"]
        if val > 85:
            print("Great Resolution")
            score += 25
            s2 = "Great Resolution"
        elif val > 15:
            print("Good Resolution")
            score += 20
            s2 = "Good Resolution"
        else:
            print("Decent Resolution")
            score += 15
            s2 = "Decent Resolution"
    else:
        print("Poor Image Quality")
        s2 = "Poor Image Quality"

    if res["Good Lighting"] > res["Bad Lighting"]:

        val = res["Good Lighting"]
        if val > 85:
            print("Great Lighting")
            score += 25
            s3 = "Great Lighting"
        elif val > 15:
            print("Good Lighting")
            score += 20
            s3 = "Good Lighting"
        else:
            print("Decent Lighting")
            score += 15
            s3 = "Decent Lighting"
    else:
        print("Poor Lighting")
        s3 = "Poor Lighting"

    if res["Face Visible"] > res["Face Obscured"]:
        score += 25
        print("Face is Visible")
        s4 = "Face is Visible"
    else:
        print("Face is obscured")
        s4 = "Face is Obscured"

    if res["Centered"] > res["Not Centered"]:
        score += 25
        print("Good subject position in frame")
        s5 = "Good subject position in frame"
    else:
        print("Poor subject position in frame")
        s5 = "Poor subject position in frame"

    print()
    print()
    print("Total selfie score: ", score, "/100")

    return (s1, s2, s3, s4, s5, str(score))
def Google_Microsoft_Fun(image_path, file_name_created):

    # Google: Instantiates a google client
    client = vision.ImageAnnotatorClient()

    # Microsoft: a trained endpoint that can be used to make a prediction
    prediction_key = "82399d964ace4ef8a3b8a8b5a2540540"
    predictor = prediction_endpoint.PredictionEndpoint(prediction_key)
    # iteration = trainer.train_project(3c2e78ba-4367-4941-8cf8-b668deb28726)

    # Activity_Recognition_New
    project_id = "d620a7d4-2aec-42a1-8ea3-810077cdcbc2"
    """create a list of file paths of images"""
    path = image_path
    dirs = os.listdir(path)
    image_list = []
    activity_highest = []
    # print(dirs)

    for f in dirs:
        if f.endswith('jpg'):
            image_list.append(os.path.join(path, f))

    # sort the images with the number of image (P.S the convention of image name has to be whatever_1.jpg, whatever_2.jpg)
    image_list = sorted(image_list,
                        key=lambda x: int(x.split('.')[-2].split('_')[-1]))
    """write the returned labels  """
    python_name = file_name_created

    file = open(python_name, "w")
    for f in image_list:
        with open(python_name, "a") as file:
            file.write(f)
            file.write("\n")
        with io.open(f, 'rb') as image_file:
            # google_labels
            content = image_file.read()
            image = types.Image(content=content)
            response = client.label_detection(image=image)
            labels = response.label_annotations
            for i in labels:
                with open(python_name, "a") as file:
                    file.write("#")
                    file.write(i.description.encode("utf-8"))
                    file.write("\n")

        # microsoft
        with io.open(f, 'rb') as image_file:
            results = predictor.predict_image(project_id, image_file)
            count = 0
            with open(python_name, "a") as file:
                for prediction in results.predictions:
                    if count == 0:

                        str = prediction.tag_name.encode("utf-8")
                        # print("highest score is "+str)

                        file.write("!")
                        file.write(str)
                        file.write("\n")
                    # print ("\t" + prediction.tag_name + ": {0:.2f}%".format(prediction.probability * 100))
                    file.write("---" + prediction.tag_name +
                               ": {0:.2f}%".format(prediction.probability *
                                                   100))
                    count = count + 1
                file.write("\n")

    file.close()
Exemplo n.º 17
0
from azure.cognitiveservices.vision.customvision.prediction import prediction_endpoint
from azure.cognitiveservices.vision.customvision.prediction.prediction_endpoint import models
import os
from werkzeug.utils import secure_filename

#app = Flask(__name__,static_url_path="/static")
# Initialize the app
app = Flask(__name__)


# Now there is a trained endpoint that can be used to make a prediction
training_key = "3df4462657d8403090db9cb591a7e285"
prediction_key = "3cb74b07eb924c75b11888bc64c776e4"
projectid="7fa336ce-fb4d-49b0-a44a-46fce8c90483"

predictor = prediction_endpoint.PredictionEndpoint(prediction_key)
APP_ROOT = os.path.dirname(os.path.abspath(__file__))

@app.route("/")

def main():
    return render_template("index.html")

@app.route('/upload', methods=['POST'])

def upload_file():
	isfile=False
	results = []
	if request.method == 'POST':
		f = request.files['photo']
		results = predictor.predict_image(projectid, f)
Exemplo n.º 18
0
print("Training...")
iteration = trainer.train_project(project.id)
while (iteration.status != "Completed"):
    iteration = trainer.get_iteration(project.id, iteration.id)
    print("Training status: " + iteration.status)
    time.sleep(1)

# The iteration is now trained. Make it the default project endpoint
trainer.update_iteration(project.id, iteration.id, is_default=True)
print("Done!")

# Now there is a trained endpoint that can be used to make a prediction
from azure.cognitiveservices.vision.customvision.prediction import prediction_endpoint
from azure.cognitiveservices.vision.customvision.prediction.prediction_endpoint import models

predictor = prediction_endpoint.PredictionEndpoint(PREDICTION_KEY)

test_img_url = base_image_url + "Images/Test/test_image.jpg"
results = predictor.predict_image_url(project.id,
                                      iteration.id,
                                      url=test_img_url)

# Alternatively, if the images were on disk in a folder called Images alongside the sample.py, then
# they can be added by using the following.
#
# Open the sample image and get back the prediction results.
# with open("Images\\test\\test_image.jpg", mode="rb") as test_data:
#     results = predictor.predict_image(project.id, test_data, iteration.id)

print("Doing prediction:")
# Display the results.
Exemplo n.º 19
0
def main():
    trainer = training_api.TrainingApi(TRAINING_KEY)

    # Create a new project
    print("Creating project...")
    project = None
    projects = trainer.get_projects()
    existing_project = list(filter(lambda t: t.name == PROJECT_NAME, projects))
    if existing_project:
        project = existing_project[0]
    if project is None:
        project = trainer.create_project(PROJECT_NAME)
    print(project.id)

    # Make tags in the new project
    print("Creating tags...")
    tag_list = trainer.get_tags(project.id)
    if len(tag_list.tags) == 0:
        for i in range(10):
            trainer.create_tag(project.id, i)
            tag_list = trainer.get_tags(project.id)

    # Import labels
    labels = pd.read_csv('mnist/train-labels.csv', header=None)
    labels.columns = ['filename', 'label']

    # Upload tagged images
    print("Uploading tagged images...")
    tagged_images = trainer.get_tagged_images(project.id)
    if len(tagged_images) == 0:
        for index, row in labels.iterrows():
            # comment the next two rows if you want to upload the whole MNIST dataset
            if index > 1000:
                break
            with open("mnist/" + os.fsdecode(row.filename),
                      mode="rb") as img_data:
                # Lookup tag from label
                tag = list(
                    filter(lambda t: t.name == str(row.label),
                           tag_list.tags))[0]
                buffer = img_data.read()
                response = trainer.create_images_from_data(
                    project.id, buffer, [tag.id])
                print(response)

    # Train
    print("Training...")
    iteration_id = project.current_iteration_id
    try:
        iteration = trainer.train_project(project.id)
        while iteration.status == "Training":
            iteration = trainer.get_iteration(project.id, iteration.id)
            print("Training status: " + iteration.status)
            time.sleep(1)
        iteration_id = iteration.id
    except HttpOperationError as error:
        print(error.response.text)

    # The iteration is now trained. Make it the default project endpoint
    trainer.update_iteration(project.id, iteration_id, is_default=True)
    print("Done!")

    # Make a predictions
    predictor = prediction_endpoint.PredictionEndpoint(PREDICTION_KEY)
    with open("mnist/test-images/128.jpg", mode="rb") as test_data:
        results = predictor.predict_image(project.id, test_data.read(),
                                          iteration.id)

    # Display the results.
    for prediction in results.predictions:
        print("\t" + prediction.tag +
              ": {0:.2f}%".format(prediction.probability * 100))
Exemplo n.º 20
0
        for blob in block_blob_service.list_blobs(label_container_name)
        if re.match(r'tagged_(.*).csv', blob.name)
    ]
    if file_date:
        block_blob_service.get_blob_to_path(
            label_container_name,
            max(file_date, key=lambda x: x[1])[0],
            config_file["tagged_output"])
    else:
        raise ValueError(
            "No tagged data exists. Cannot train model without any tagged data."
        )

    from map_validation import detectortest
    trainer = training_api.TrainingApi(config_file["training_key"])
    predictor = prediction_endpoint.PredictionEndpoint(
        config_file["prediction_key"])
    file_date = [
        (blob.name, blob.properties.last_modified)
        for blob in block_blob_service.list_blobs(label_container_name)
        if re.match(r'test_(.*).csv', blob.name)
    ]
    if file_date:
        block_blob_service.get_blob_to_path(
            label_container_name,
            max(file_date, key=lambda x: x[1])[0], config_file["test_output"])
        train_cv_model(config_file["tagged_output"],
                       trainer,
                       config_file["project_id"],
                       config_file["image_dir"],
                       config_file["user_folders"] == "True",
                       tag_names=config_file["classes"].split(","),
Exemplo n.º 21
0
def prediction(testing_car_df, iteration):

    # Now there is a trained endpoint that can be used to make a prediction
    predictor = prediction_endpoint.PredictionEndpoint(prediction_key)

    img_name_list = list()

    # instantiate a dictionary and fill up keys
    for row_index, row in testing_car_df.iterrows():
        image_name = row['ImageFilename']
        # img_dict[image_name] = list()
        img_name_list.append(image_name)

    column_list = [
        'ImageFilename', 'GlassDamage', 'WaterDamage', 'BumperDamage',
        'RolledOver', 'SideDoorDamage', 'SlidOffRoad', 'UrbanLocation',
        'SuburbanLocation', 'RuralLocation', 'DayTime', 'NightTime',
        'ClearWeather', 'RainyWeather', 'SnowyWeather', 'DryRoadCondition',
        'WetRoadCondition', 'SnowyRoadCondition', 'Bumps&Dings',
        'EngineDamage', 'ExtremeDamageSeverity', 'HighDamageSeverity',
        'ModerateDamageSeverity', 'LowDamageSeverity', 'NoDamageSeverity'
    ]

    result_df = pd.DataFrame(columns=column_list)
    df_list = list()

    for img_name in img_name_list:
        test_img_url = base_image_url + img_name
        results = predictor.predict_image_url(project_id,
                                              iteration.id,
                                              url=test_img_url)

        # Alternatively, if the images were on disk in a folder called Images alongside the sample.py, then
        # they can be added by using the following.
        #
        # Open the sample image and get back the prediction results.
        # with open("Images\\test\\test_image.jpg", mode="rb") as test_data:
        #     results = predictor.predict_image(project.id, test_data, iteration.id)

        # create a dictionary to store all the tag information related to one image
        tag_dict = dict(zip(column_list, column_list))

        # insert tag name into the dictionary value
        tag_dict['ImageFilename'] = img_name

        # Display the results.
        print('\n' + 'Uploading Prediction Image ' + img_name + ':')
        for prediction in results.predictions:
            print("\t" + prediction.tag_name +
                  ": {0:.2f}%".format(prediction.probability * 100))
            tag_dict[prediction.tag_name] = prediction.probability * 100

        # convert all the dict values aka tag values into a list
        tag_result_list = list(tag_dict.values())

        # Convert the list of tag info to one row of dataFrame
        one_row_df = pd.DataFrame(data=[tag_result_list], columns=column_list)

        # Append the one row dataFrame to result_df, which combines all results
        result_df = result_df.append(one_row_df, ignore_index=True)
        # print(result_df)

    csv_output(result_df, 'prediction_result.csv')
Exemplo n.º 22
0
print ("Done!")

# COMMAND ----------

# MAGIC %md
# MAGIC Try this image for prediction:<br> "https://tse2.mm.bing.net/th?id=OIP.QpxuXic1_vcHKsSO7cwkQwHaHa&pid=Api"<br>
# MAGIC <img src="https://tse2.mm.bing.net/th?id=OIP.QpxuXic1_vcHKsSO7cwkQwHaHa&pid"></img>

# COMMAND ----------

from azure.cognitiveservices.vision.customvision.prediction import prediction_endpoint
from azure.cognitiveservices.vision.customvision.prediction.prediction_endpoint import models

# Now there is a trained endpoint that can be used to make a prediction

predictor = prediction_endpoint.PredictionEndpoint(CUSTOM_VISION_PREDICTION_KEY)

test_img_url = "https://tse2.mm.bing.net/th?id=OIP.QpxuXic1_vcHKsSO7cwkQwHaHa&pid=Api"
results = predictor.predict_image_url(project.id, iteration.id, url=test_img_url)

# Display the results.
for prediction in results.predictions:
    print ("\t" + prediction.tag_name + ": {0:.2f}%".format(prediction.probability * 100))

# COMMAND ----------

# MAGIC %md
# MAGIC Export the model to run on edge/mobile: <br>
# MAGIC https://github.com/MicrosoftDocs/azure-docs/blob/master/articles/cognitive-services/Custom-Vision-Service/export-model-python.md

# COMMAND ----------
Exemplo n.º 23
0
def create_predictor(prediction_key):
    return prediction_endpoint.PredictionEndpoint(prediction_key)