コード例 #1
0
def get_prediction(content):
    prediction_client = automl_v1beta1.PredictionServiceClient()

    name = 'projects/<>/locations/us-central1/models/<>'
    payload = {'image': {'image_bytes': content}}
    params = {}
    request = prediction_client.predict(name, payload, params)

    result_json = ast.literal_eval(
        pf.json_format.MessageToJson(request,
                                     including_default_value_fields=False))
    try:
        prediction = result_json["payload"][0]["displayName"]
    except Exception as e:
        prediction = "nothing"

    return prediction
コード例 #2
0
    def predictClass(self, jpeg):
        # Get the full path of the model.
        model_full_id = automl_client.model_path(project_id, compute_region,
                                                 model_id)
        # Create client for prediction service.
        prediction_client = automl.PredictionServiceClient()
        payload = {"image": {"image_bytes": jpeg}}

        params = {}
        if score_threshold:
            params = {"score_threshold": score_threshold}
        response = prediction_client.predict(model_full_id, payload, params)
        res = {}
        for result in response.payload:
            res[result.display_name] = result.classification.score
            if result.display_name == "fall":
                self.fall += 1
            elif result.display_name == "stand":
                self.stand += 1

        # Uploading to storage
        name = str(datetime.now()).replace(" ", "") + '.jpg'
        blob = self.bucket.blob(name)
        blob.upload_from_string(jpeg)
        blob = self.bucket.blob(name.replace(".jpg", ".json"))
        blob.upload_from_string(json.dumps(res))
        if self.stand < 4 and self.stand > 0:
            self.get_images(name,
                            "static/images/stand-0" + str(self.stand) + ".jpg")
            print(name, res)
        elif self.fall < 4 and self.fall > 0:
            self.get_images(name,
                            "static/images/fall-0" + str(self.fall) + ".jpg")
            print(name, res)
        if self.fall > 4 and self.done:
            self.done = False
            account_sid = 'ACb7fcbdfdd696b6395691d777af1539b2'
            auth_token = 'eb69fd893fb3d3f664692efcc16cbaa9'
            client = Client(account_sid, auth_token)
            message = client.messages \
                           .create(
                                body="EMERGENCY ALERT! We have detected that your contact has suffered a fall at "+str(datetime.now()),
                                from_='+14092456334',
                                to='+14803346945'
                            )
            print(message.sid)
コード例 #3
0
def get_prediction(content, project_id, model_id):
    prediction_client = automl_v1beta1.PredictionServiceClient()

    name = 'projects/{}/locations/us-central1/models/{}'.format(
        project_id, model_id)
    payload = {'image': {'image_bytes': content}}
    params = {}
    #The response has the image classification and the confidence score for that image
    response = prediction_client.predict(name, payload, params)
    labels = response.payload  # Get labels from response
    #Get the classification of that image from labels
    image_class = labels[0].display_name
    #score_result has  has the confidence score for that category
    score_result = labels[0].classification
    confidence = score_result.score
    #print(labels)
    return image_class, confidence  # waits till request is returned
def predict(
    project_id,
    compute_region,
    model_id,
    file_path,
    translation_allow_fallback=False,
):
    """Translate the content."""
    # [START automl_translation_predict]
    # project_id = 'PROJECT_ID_HERE'
    # compute_region = 'COMPUTE_REGION_HERE'
    # model_id = 'MODEL_ID_HERE'
    # file_path = '/local/path/to/file'
    # translation_allow_fallback = True allows fallback to Google Translate

    from google.cloud import automl_v1beta1 as automl

    automl_client = automl.AutoMlClient()

    # Create client for prediction service.
    prediction_client = automl.PredictionServiceClient()

    # Get the full path of the model.
    model_full_id = automl_client.model_path(
        project_id, compute_region, model_id
    )

    # Read the file content for translation.
    with open(file_path, "rb") as content_file:
        content = content_file.read()
    content.decode("utf-8")

    # Set the payload by giving the content of the file.
    payload = {"text_snippet": {"content": content}}

    # params is additional domain-specific parameters.
    # translation_allow_fallback allows to use Google translation model.
    params = {}
    if translation_allow_fallback:
        params = {"translation_allow_fallback": "True"}

    response = prediction_client.predict(model_full_id, payload, params)
    translated_content = response.payload[0].translation.translated_content

    print(u"Translated content: {}".format(translated_content.content))
コード例 #5
0
ファイル: MyProject.py プロジェクト: tirzak/Willow
def get_prediction(content):
  prediction_client = automl_v1beta1.PredictionServiceClient()
  project_id='144025793143'
  model_id='ICN8431156316856123392' 

  name = 'projects/{}/locations/us-central1/models/{}'.format(project_id, model_id)
  payload = {'image': {'image_bytes': content }}
  params = {}
  request = prediction_client.predict(name, payload, params)
 
  
  xs=str(request)
  if "Happy" in xs:
    speakText("Your pet is happy. You are doing a great job!")
  elif "Sad" in xs:
      speakText("Your pet is not emotionally well. Please take care")
  elif "Anxious" in xs:
      speakText("Your pet is stressed. Please give it some space, or let it go outside.")   
コード例 #6
0
def get_prediction_folder(folder_path, project_id, model_id):
    """Use model from GCP AutoML Vision to do inference and get results"""
    time_a = time.time()
    prediction_client = automl_v1beta1.PredictionServiceClient()
    name = 'projects/{}/locations/us-central1/models/{}'.format(
        project_id, model_id)
    params = {}
    output = {}
    f = []
    time_b = time.time()
    print("init_time: ", time_b - time_a, "(s)")
    with open("confusion_matrix.txt", 'w+') as cf:
        cf.write("Results: ")
        cf.write("\n")

    for (dirpath, dirnames, filenames) in os.walk(folder_path):
        f.extend(filenames)
        for dirname in dirnames:
            label = dirname
            for file in os.listdir(os.path.join(dirpath, dirname)):
                time_c = time.time()
                file_path = os.path.join(dirpath, dirname, file)
                with open(file_path, 'rb') as ff:
                    content = ff.read()
                payload = {'image': {'image_bytes': content}}
                response = prediction_client.predict(name, payload, params)
                for result in response.payload:
                    score = result.classification.score
                    predict_result = result.display_name
                    output['file_path'] = file_path
                    output['predict_result'] = predict_result
                    output['score'] = score
                    output['true_label'] = label
                    time_d = time.time()
                    output['inference_time'] = time_d - time_c
                    output_json = json.dumps(output)
                    outputs.append(output_json)
                    print(output_json)
                    with open("confusion_matrix.txt", 'a+') as cf:
                        cf.write(output_json)
                        cf.write("\n")
        #break
    return outputs
コード例 #7
0
    def processObjectDetection(self, base64_image):
        self.content = base64_image
        self.project_id = '125372421991'
        self.model_id = 'IOD6167596126800183296'

        prediction_client = automl_v1beta1.PredictionServiceClient()

        name = 'projects/{}/locations/us-central1/models/{}'.format(
            self.project_id, self.model_id)

        payload = {'image': {'image_bytes': self.content}}
        params = {}
        request = prediction_client.predict(name, payload, params).payload

        if (len(request) > 0):
            if (request[0].display_name == 'date'):
                c1, c2 = request[
                    0].image_object_detection.bounding_box.normalized_vertices

                nparr = np.fromstring(self.content, np.uint8)
                img = cv.imdecode(nparr, cv.IMREAD_COLOR)

                #img = cv.imread('renamed/'+f)
                #img = cv.cvtColor(img,cv.COLOR_BGR2RGB)

                width = img.shape[0]
                height = img.shape[1]

                x1 = int(c1.x * height)
                y1 = int(c1.y * width)
                x2 = int(c2.x * height)
                y2 = int(c2.y * width)

                retval, buffer = cv.imencode('.jpeg', img[y1:y2, x1:x2])
                jpg_as_text = base64.b64encode(buffer)

                return jpg_as_text

                #imsave('crop.jpeg'+f,img[y1:y2,x1:x2])
            else:
                return None
        else:
            return None
コード例 #8
0
ファイル: application.py プロジェクト: sasogeek/qualiscan
def predict(image):
    project_id = 'qualiscan-216706'
    compute_region = 'us-central1'
    model_id = 'ICN2956512205565128229'
    score_threshold = '0.5'

    automl_client = automl.AutoMlClient()

    # Get the full path of the model.
    model_full_id = automl_client.model_path(
        project_id, compute_region, model_id
    )

    # Create client for prediction service.
    prediction_client = automl.PredictionServiceClient()
    img_data = b64decode(image)
    filename = 'some_image.jpg'  # I assume you have a way of picking unique filenames
    with open(filename, 'wb') as f:
        f.write(img_data)

    # Read the image and assign to payload.
    with open(filename, "rb") as image_file:
        content = image_file.read()
    payload = {"image": {"image_bytes": content}}

    # params is additional domain-specific parameters.
    # score_threshold is used to filter the result
    # Initialize params
    params = {}
    if score_threshold:
        params = {"score_threshold": score_threshold}

    response = prediction_client.predict(model_full_id, payload, params)
    print("Prediction results:")
    results = []
    for result in response.payload:
        print(result)
        results.append({"display_name": result.display_name,
                        "classification_score": result.classification.score})
        # print("Predicted class name: {}".format(result.display_name))
        # print("Predicted class score: {}".format(result.classification.score))
    # print(response)
    return results
コード例 #9
0
def predict(project_id,
            compute_region,
            model_id,
            file_path,
            score_threshold=""):
    """Make a prediction for an image."""
    # [START automl_vision_predict]
    # TODO(developer): Uncomment and set the following variables
    # project_id = 'PROJECT_ID_HERE'
    # compute_region = 'COMPUTE_REGION_HERE'
    # model_id = 'MODEL_ID_HERE'
    # file_path = '/local/path/to/file'
    # score_threshold = 'value from 0.0 to 0.5'

    from google.cloud import automl_v1beta1 as automl

    automl_client = automl.AutoMlClient()

    # Get the full path of the model.
    model_full_id = automl_client.model_path(project_id, compute_region,
                                             model_id)

    # Create client for prediction service.
    prediction_client = automl.PredictionServiceClient()

    # Read the image and assign to payload.
    with open(file_path, "rb") as image_file:
        content = image_file.read()
    payload = {"image": {"image_bytes": content}}

    # params is additional domain-specific parameters.
    # score_threshold is used to filter the result
    # Initialize params
    params = {}
    if score_threshold:
        params = {"score_threshold": score_threshold}

    response = prediction_client.predict(model_full_id, payload, params)
    print("Prediction results:")
    for result in response.payload:
        print("Predicted class name: {}".format(result.display_name))
        print("Predicted class score: {}".format(result.classification.score))
コード例 #10
0
def predict():
    # TODO(developer): Uncomment and set the following variables
    project_id = 'nutriscan'
    compute_region = 'us-central1'
    model_id = 'ICN6689485917993828352'
    file_path = '/home/abdullahz/Desktop/food41/caesar_salad/70283.jpg'
    score_threshold = '0.5'

    from google.cloud import automl_v1beta1 as automl
    import os
    os.environ[
        "GOOGLE_APPLICATION_CREDENTIALS"] = "./NutriScan-363118dbe915.json"

    automl_client = automl.AutoMlClient()

    # Get the full path of the model.
    model_full_id = automl_client.model_path(project_id, compute_region,
                                             model_id)

    # Create client for prediction service.
    prediction_client = automl.PredictionServiceClient()

    # Read the image and assign to payload.
    with open(file_path, "rb") as image_file:
        content = image_file.read()
    payload = {"image": {"image_bytes": content}}

    # params is additional domain-specific parameters.
    # score_threshold is used to filter the result
    # Initialize params
    params = {}
    if score_threshold:
        params = {"score_threshold": score_threshold}

    response = prediction_client.predict(model_full_id, payload, params)
    print("Prediction results:")
    for result in response.payload:
        print("Predicted class name: {}".format(result.display_name))
        print("Predicted class score: {}".format(result.classification.score))
    return result.display_name
コード例 #11
0
ファイル: plswork.py プロジェクト: taywong00/siteseer
def analyze():
    repeat_frequently()
    global response_display_name
    os.environ["GOOGLE_APPLICATION_CREDENTIALS"]="/home/pi/siteseer-030a672b14ba.json"
    project = 'siteseer'
    storage_client = storage.Client(project=project)
    bucket = storage_client.get_bucket('siteseer')

    automl_client = automl.AutoMlClient()
    model_full_id = automl_client.model_path(
    project_id, compute_region, model_id) # Get the full path of the model.
    prediction_client = automl.PredictionServiceClient()

    # repeat_frequently()

    with open(file_path, "rb") as image_file:
        content = image_file.read()
        # repeat_frequently()
        payload = {"image": {"image_bytes": content}}

    params = { }

    if score_threshold:
        params = {"score_threshold": score_threshold}

    # repeat_frequently()

    response = prediction_client.predict(model_full_id, payload, params)
    for result in response.payload:
        touch_sensor()
        print("Date: {} Prediction: {} {}".format(str(datetime.datetime.now()), result.display_name, result.classification.score))
        if not result.display_name == response_display_name:
            response_display_name = result.display_name
            firebase.put('sight', 'speech/1', result.display_name)

    # repeat_frequently()

    image = bucket.blob('Sidewalk')
    image.upload_from_filename('image.jpg')
    def test_predict(self):
        # Setup Expected Response
        expected_response = {}
        expected_response = prediction_service_pb2.PredictResponse(
            **expected_response)

        # Mock the API response
        channel = ChannelStub(responses=[expected_response])
        client = automl_v1beta1.PredictionServiceClient(channel=channel)

        # Setup Request
        name = client.model_path('[PROJECT]', '[LOCATION]', '[MODEL]')
        payload = {}

        response = client.predict(name, payload)
        assert expected_response == response

        assert len(channel.requests) == 1
        expected_request = prediction_service_pb2.PredictRequest(
            name=name, payload=payload)
        actual_request = channel.requests[0][1]
        assert expected_request == actual_request
コード例 #13
0
    def test_batch_predict_exception(self):
        # Setup Response
        error = status_pb2.Status()
        operation = operations_pb2.Operation(
            name="operations/test_batch_predict_exception", done=True)
        operation.error.CopyFrom(error)

        # Mock the API response
        channel = ChannelStub(responses=[operation])
        patch = mock.patch("google.api_core.grpc_helpers.create_channel")
        with patch as create_channel:
            create_channel.return_value = channel
            client = automl_v1beta1.PredictionServiceClient()

        # Setup Request
        name = client.model_path("[PROJECT]", "[LOCATION]", "[MODEL]")
        input_config = {}
        output_config = {}

        response = client.batch_predict(name, input_config, output_config)
        exception = response.exception()
        assert exception.errors[0] == error
コード例 #14
0
def AutoMlFunc(file_path):

    os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = "..\**************"

    project_id = '*************'
    compute_region = 'us-central1'
    model_id = 'ICN1060821442219325010'
    score_threshold = '0'

    from google.cloud import automl_v1beta1 as automl

    automl_client = automl.AutoMlClient()

    # Get the full path of the model.
    model_full_id = automl_client.model_path(project_id, compute_region,
                                             model_id)

    # Create client for prediction service.
    prediction_client = automl.PredictionServiceClient()

    # Read the image and assign to payload.
    with open(file_path, "rb") as image_file:
        content = image_file.read()
    payload = {"image": {"image_bytes": content}}

    # params is additional domain-specific parameters.
    # score_threshold is used to filter the result
    # Initialize params
    params = {}
    if score_threshold:
        params = {"score_threshold": score_threshold}

    response = prediction_client.predict(model_full_id, payload, params)
    #    print("Prediction results:")
    #    for result in response.payload:
    #        print("Predicted class name: {}".format(result.display_name))
    #        print("Predicted class score: {}".format(result.classification.score))

    return response.payload
コード例 #15
0
def get_prediction(request):
    request_json = request.get_json()
    
    if request_json and 'content' in request_json:
        content = request_json['content']
        
        prediction_client = automl_v1beta1.PredictionServiceClient()
	
        project_id = os.environ.get('PROJECT_ID', 'No Project ID is supplied')
        model_id = os.environ.get('MODEL_ID', 'No Model ID is supplied')

        name = 'projects/{}/locations/us-central1/models/{}'.format(
            project_id, model_id)
        payload = {'image': {'image_bytes': content}}
        params = {}
        request = prediction_client.predict(name, payload, params)

        print(request)

        return request  # waits till request is returned
    else:
        return f'Pastikan data sudah benar'
コード例 #16
0
def predict(project_id, compute_region, model_id):
    project_id = "cdmx-safe-map"
    compute_region = "us-central1"
    model_id = "TCN3023487612629800943"
    automl_client = automl.AutoMlClient()

    # Create client for prediction service.
    prediction_client = automl.PredictionServiceClient()

    # Get the full path of the model.
    model_full_id = automl_client.model_path(project_id, compute_region,
                                             model_id)

    text = request.form['comment']
    document = types.Document(content=text,
                              type=enums.Document.Type.PLAIN_TEXT)

    # Set the payload by giving the content and type of the file.
    payload = {
        "text_snippet": {
            "content": document,
            "mime_type": "text/plain"
        }
    }

    # params is additional domain-specific parameters.
    # currently there is no additional parameters supported.
    params = {}
    response = prediction_client.predict(model_full_id, payload, params)
    labels = response.payload
    #for result in response.payload:
    #if(result.classification.score >0.6):
    #print("{}".format(result.display_name))
    #print("Predicted class score: {}".format(result.classification.score))

    # [END automl_language_predict]

    return render_template('homepage.html', text=text, labels=labels)
    def test_batch_predict(self):
        # Setup Expected Response
        expected_response = {}
        expected_response = prediction_service_pb2.BatchPredictResult(
            **expected_response
        )
        operation = operations_pb2.Operation(
            name="operations/test_batch_predict", done=True
        )
        operation.response.Pack(expected_response)

        # Mock the API response
        channel = ChannelStub(responses=[operation])
        patch = mock.patch("google.api_core.grpc_helpers.create_channel")
        with patch as create_channel:
            create_channel.return_value = channel
            client = automl_v1beta1.PredictionServiceClient()

        # Setup Request
        name = client.model_path("[PROJECT]", "[LOCATION]", "[MODEL]")
        input_config = {}
        output_config = {}
        params = {}

        response = client.batch_predict(name, input_config, output_config, params)
        result = response.result()
        assert expected_response == result

        assert len(channel.requests) == 1
        expected_request = prediction_service_pb2.BatchPredictRequest(
            name=name,
            input_config=input_config,
            output_config=output_config,
            params=params,
        )
        actual_request = channel.requests[0][1]
        assert expected_request == actual_request
コード例 #18
0
def predict(project_id,
            compute_region,
            model_id,
            file_path,
            score_threshold="0.5"):
    model_full_id = automl_client.model_path(project_id, compute_region,
                                             model_id)

    prediction_client = automl.PredictionServiceClient()

    with open(file_path, "rb") as image_file:
        content = image_file.read()
    payload = {"image": {"image_bytes": content}}

    params = {}
    if score_threshold:
        params = {"score_threshold": score_threshold}

    response = prediction_client.predict(model_full_id, payload, params)
    '''
    # full result
    print("Prediction results:")
    for result in response.payload:
        print("Predicted class name: {}".format(result.display_name))
        print("Predicted class score: {}".format(result.classification.score))
    '''

    # csv result
    output = []
    for result in response.payload:
        output.append((result.classification.score, result.display_name))

    output.sort(reverse=True)
    print(file_path, end='')
    for result in output:
        print(",", result[1], end='')
    print()
コード例 #19
0
ファイル: objectLabeler.py プロジェクト: Exploding/FoodBuddy
    def __init__(self, threadID, name, q, qL):
        threading.Thread.__init__(self, group=None)
        self.threadID = threadID
        self.name = name
        self.weight = 0
        self.q = q
        self.qL = qL

        # filter warnings, load the configuration
        warnings.filterwarnings("ignore")
        self.conf = json.load(open('conf.json'))

        # initialize the AutoML API
        credentials = service_account.Credentials.from_service_account_file(r"/home/pi/creds.json")
        self.prediction_client = automl_v1beta1.PredictionServiceClient(credentials=credentials)
        self.modelName = 'projects/{}/locations/us-central1/models/{}'.format(self.conf['project_id'], self.conf['model_id'])

        # initialize the camera and grab a reference to the raw camera capture
        self.camera = PiCamera()
        self.camera.resolution = tuple(self.conf[ "resolution"])
        self.camera.framerate = self.conf[ "fps"]
        self.rawCapture = PiRGBArray(self.camera, size=tuple(self.conf[ "resolution"]))

        # allow the camera to warmup, then initialize the average frame, last
        # uploaded timestamp, and frame motion counter
        print self.name + "| warming up camera..."
        time.sleep(self.conf[ "camera_warmup_time"])
        self.avg = None
        self.lastUploaded = datetime.datetime.now()
        self.motionCounter = 0

        # stop event to terminate the thread 
        self.stopEvent = threading.Event()

        # init GPIO
        GPIO.setmode(GPIO.BCM)
        GPIO.setup(self.conf['motion_led_pin'], GPIO.OUT)
def predict(project_id, compute_region, model_id, file_path):
    """Classify the content."""
    # [START automl_language_predict]
    # TODO(developer): Uncomment and set the following variables
    # project_id = 'PROJECT_ID_HERE'
    # compute_region = 'COMPUTE_REGION_HERE'
    # model_id = 'MODEL_ID_HERE'
    # file_path = '/local/path/to/file'

    from google.cloud import automl_v1beta1 as automl

    automl_client = automl.AutoMlClient()

    # Create client for prediction service.
    prediction_client = automl.PredictionServiceClient()

    # Get the full path of the model.
    model_full_id = automl_client.model_path(
        project_id, compute_region, model_id
    )

    # Read the file content for prediction.
    with open(file_path, "rb") as content_file:
        snippet = content_file.read()

    # Set the payload by giving the content and type of the file.
    payload = {"text_snippet": {"content": snippet, "mime_type": "text/plain"}}

    # params is additional domain-specific parameters.
    # currently there is no additional parameters supported.
    params = {}
    response = prediction_client.predict(model_full_id, payload, params)
    print("Prediction results:")
    for result in response.payload:
        if(result.classification.score >6.0)
                print("Predicted class name: {}".format(result.display_name))
                print("Predicted class score: {}".format(result.classification.score))
コード例 #21
0
def run_automl_single(ocr_path, list_fields, model_id, main_project_id,
                      compute_region):
    """Runs AutoML NER on a single document and returns the dictionary of results."""

    # Set up client for AutoML NER model
    automl_client = automl.AutoMlClient()
    model_full_id = automl_client.model_path(main_project_id, compute_region,
                                             model_id)
    prediction_client = automl.PredictionServiceClient()

    # Load text
    text = utils.download_string(ocr_path).read().decode('utf-8')

    # Call AutoML
    payload = {"text_snippet": {"content": text, "mime_type": "text/plain"}}
    params = {}
    response = prediction_client.predict(model_full_id, payload, params)

    # Parse results
    results = {'file': os.path.basename(ocr_path).replace('.txt', '.pdf')}
    for field in list_fields:
        value_field = extract_field_from_payload(text, response.payload, field)
        results[field] = value_field
    return results
コード例 #22
0
    def get_predictions(self,
                        image_bytes,
                        model_id='',
                        score_threshold=u'0.10'):
        if model_id == '':
            model_id = self.model_id

        model_full_id = self.client.model_path(self.project_id,
                                               self.compute_region, model_id)

        prediction_client = automl.PredictionServiceClient()

        payload = {"image": {"image_bytes": image_bytes}}
        params = {}
        if score_threshold:
            params = {"score_threshold": score_threshold}

        result = []
        predictions = prediction_client.predict(model_full_id, payload, params)

        for p in predictions.payload:
            result.append((p.display_name, p.classification.score))

        return sorted(result, key=lambda _: _[1], reverse=True)
コード例 #23
0
def prediction(data, context):
    """Background Cloud Function to be triggered by Cloud Storage.
       This generic function logs relevant data when a file is changed.

    Args:
        data (dict): The Cloud Functions event payload.
        context (google.cloud.functions.Context): Metadata of triggering event.
    Returns:
        None; the output is written to Stackdriver Logging
    """

    print('Event ID: {}'.format(context.event_id))
    print('Event type: {}'.format(context.event_type))
    print('Bucket: {}'.format(data['bucket']))
    print('File: {}'.format(data['name']))
    print('Metageneration: {}'.format(data['metageneration']))
    print('Created: {}'.format(data['timeCreated']))
    print('Updated: {}'.format(data['updated']))

    prediction_client = automl_v1beta1.PredictionServiceClient()
    name = 'projects/{}/locations/us-central1/models/{}'.format('377104581238', 'IOD2191836847552856064')
    imagepath = data['bucket'] + "/" + data['name']
    storage_client = storage.Client()
    bucket = storage_client.get_bucket(data['bucket'])
    blob = bucket.blob(data['name'])
    content = blob.download_as_string()

    payload = {'image': {'image_bytes': content}}
    # print(content)
    params = {}
    response = prediction_client.predict(name, payload, params)
    # print(response)
    for result in response.payload:
        print("Display Name: {}".format(result.display_name))
        print("Bounding Box: {}".format(result.image_object_detection.bounding_box))
        print("Score: {}".format(result.image_object_detection.score))
コード例 #24
0
ファイル: predict.py プロジェクト: ajfantine/hoyaHacks2019
def get_prediction(content, project_id, model_id):

    prediction_client = automl_v1beta1.PredictionServiceClient()

    name = 'projects/{}/locations/us-central1/models/{}'.format(project_id, model_id)
    payload = {'text_snippet': {'content': content, 'mime_type': 'text/plain' }}
    params = {}
    request = prediction_client.predict(name, payload, params)

    str_request = str(request)
    old_list = str_request.split("\n")
    bad_chars = ['','{', '}', ' ']
    list_request = []
    for item in old_list:
      #print(item)
      item = item.split(" ")

      item = [element for element in item if element not in bad_chars]
      #list_request.append(item)
      #item = ' '.join([token for token in item])
      #print(item)
      if(item != ""):
        for part in item:
            list_request.append(part)

    pos_ind = list_request.index('"positive"')
    neg_ind = list_request.index('"negative"')
    if(pos_ind < neg_ind):
      ret_str = "positive"
    else:
      ret_str = "negative"

    #score = text.find('score')
    # str_request.split("")'''
    print("your mood is: "+ret_str)
    return request  # waits till request is returned
コード例 #25
0
def get_prediction(content, project_id, model_id,number_of_users):
  prediction_client = automl_v1beta1.PredictionServiceClient()
  name = 'projects/{}/locations/us-central1/models/{}'.format(project_id, model_id)
  payload = {'image': {'image_bytes': content }}

  # Return the output of activities having confidence score is greater than "0.0"
  params = { "score_threshold":"0.0" }  

  request = prediction_client.predict(name, payload, params)
	
  # Returns a dictionary of all labels that have confidence score greater than 0.0
  labels = request.payload

  #Array to store confience scores of activities
  conf = [0.0, 0.0, 0.0, 0.0] 
  i = 0
  while i<len(labels):
  	image_label = labels[i].display_name # print this
  	score_result = labels[i].classification
  	confidence = score_result.score # print this
  	conf[i] = confidence
    print(image_label)
  	print(confidence)
  	i = i+1
コード例 #26
0
os.system('export GOOGLE_APPLICATION_CREDENTIALS="service.json"')
project_id = 'pennapps-252204'
compute_region = 'us-central1'
model_id = 'untitled_1567868172858'
file_path = 'test.png'
score_threshold = u'0.4'

from google.cloud import automl_v1beta1 as automl

automl_client = automl.AutoMlClient()

# Get the full path of the model.
model_full_id = automl_client.model_path(project_id, compute_region, model_id)

# Create client for prediction service.
prediction_client = automl.PredictionServiceClient()

# Read the image and assign to payload.
with open(file_path, "rb") as image_file:
    content = image_file.read()
payload = {"image": {"image_bytes": content}}

params = {}
if score_threshold:
    params = {"score_threshold": score_threshold}

print(model_full_id)
print(params)
response = prediction_client.predict(model_full_id, payload, params)
print("Prediction results:")
for result in response.payload:
コード例 #27
0
ファイル: dog2.py プロジェクト: notlcry/doudouAlert
# -*- coding: UTF-8 -*-

from google.cloud import automl_v1beta1

prediction_client = automl_v1beta1.PredictionServiceClient()


def get_prediction(content):
    project_id = '305237550374'
    model_id = 'ICN5607979892634288128'
    name = 'projects/{}/locations/us-central1/models/{}'.format(
        project_id, model_id)
    payload = {'image': {'image_bytes': content}}
    params = {}
    request = prediction_client.predict(name, payload, params)
    return request.payload  # waits till request is returned


def is_dog(img):
    try:
        result = get_prediction(img)
        if len(result) > 0 and result[0].display_name == 'doudou':
            return True
        elif len(result) > 2:
            pass

    except Exception as e:
        print 'detect error' + str(e)
    return False
コード例 #28
0
ファイル: predict.py プロジェクト: Yash-Sardhara/PixelFood
key = 'pixelfood-e2b93b66a82f.json'

if __name__ == '__main__':
    file_path = sys.argv[1]

from google.cloud import automl_v1beta1 as automl

automl_client = automl.AutoMlClient()

# Get the full path of the model.
model_full_id = automl_client.model_path(project_id, compute_region, model_id)

print(model_full_id)

# Create client for prediction service.
prediction_client = automl.PredictionServiceClient().from_service_account_file(
    key)

# Read the image and assign to payload.
with open(file_path, "rb") as image_file:
    content = image_file.read()
payload = {"image": {"image_bytes": content}}

# params is additional domain-specific parameters.
# score_threshold is used to filter the result
# Initialize params
params = {}
if score_threshold:
    params = {"score_threshold": score_threshold}

response = prediction_client.predict(model_full_id, payload, params)
コード例 #29
0
def detect_cartoon(frame):
    model = automl_v1beta1.PredictionServiceClient()
    path = "projects/" + project_id + "/locations/us-central1/models/" + model_id
    result = model.predict(path, {'image': {'image_bytes': frame}})
    return result
コード例 #30
0
ファイル: app.py プロジェクト: brandenkang/Nutriscan
def predict():
    global calorieScores, foodName
    # TODO(developer): Uncomment and set the following variables
    automl_client = automl.AutoMlClient()

    # Get the full path of the model.
    model_full_id = automl_client.model_path(project_id, compute_region,
                                             model_id)

    r = request
    # # # convert string of image data to uint8
    # nparr = np.fromstring(r.data, np.uint8)
    # # # decode image
    # img = cv2.imdecode(nparr, cv2.IMREAD_COLOR)

    # print("THE TYPE OF IMAGE IS ")
    # print(img)
    # Create client for prediction service.
    prediction_client = automl.PredictionServiceClient()

    # Read the image and assign to payload.
    # with open(r.data, "rb") as image_file:
    #     content = image_file.read()
    payload = {"image": {"image_bytes": r.data}}

    # params is additional domain-specific parameters.
    # score_threshold is used to filter the result
    # Initialize params
    params = {}
    if score_threshold:
        params = {"score_threshold": score_threshold}
    calorieScores = 0
    response = prediction_client.predict(model_full_id, payload, params)
    for result in response.payload:
        foodName = result.display_name
        classScore = result.classification.score
    if foodName == "caesar_salad":
        foodName = "Caesar Salad"
        calorieScores = 189
    elif foodName == "chicken_wings":
        foodName = "Chicken Wings"
        calorieScores = 283
    elif foodName == "donuts":
        foodName = "Donut"
        calorieScores = 324
    elif foodName == "fried_rice":
        foodName = "Ice Cream"
        calorieScores = 170
    elif foodName == "pizza":
        foodName = "Pizza"
        calorieScores = 277
    elif foodName == "dumplings":
        foodName = "Dumplings"
        calorieScores = 145
    elif foodName == "french_fries":
        foodName = "French Fries"
        calorieScores = 155
    elif foodName == "hamburger":
        foodName = "Hamburger"
        calorieScores = 186

    calories = CALORIES[foodName]
    print(calories)

    payload = {"foodName": foodName, "calories": calories}

    return (json.dumps(payload))