def isWearingMask(file_path): prediction_client = automl.PredictionServiceClient() # Get the full path of the model. model_full_id = automl.AutoMlClient.model_path(project_id, "us-central1", model_id) # Read the file. with open(file_path, "rb") as content_file: content = content_file.read() image = automl.Image(image_bytes=content) payload = automl.ExamplePayload(image=image) # params is additional domain-specific parameters. # score_threshold is used to filter the result # https://cloud.google.com/automl/docs/reference/rpc/google.cloud.automl.v1#predictrequest params = {"score_threshold": "0.1"} request = automl.PredictRequest(name=model_full_id, payload=payload, params=params) response = prediction_client.predict(request=request) #print("Prediction results:") print(len(response.payload)) for result in response.payload: print("Predicted class name: {}".format(result.display_name)) print("Predicted class score: {}".format(result.classification.score)) return True if (result.display_name == "with_mask" and result.classification.score > 0.9999) else False return False
def get_results_from_automl(filepath): project_id = "numeric-polygon-283403" model_id = "ICN2007288218877165568" prediction_client = automl.PredictionServiceClient() # Get the full path of the model. model_full_id = automl.AutoMlClient.model_path(project_id, "us-central1", model_id) # Read the file. with open(filepath, "rb") as content_file: content = content_file.read() image = automl.Image(image_bytes=content) payload = automl.ExamplePayload(image=image) # params is additional domain-specific parameters, score_threshold is used to filter the result # https://cloud.google.com/automl/docs/reference/rpc/google.cloud.automl.v1#predictrequest params = {"score_threshold": "0.5"} request = automl.PredictRequest(name=model_full_id, payload=payload, params=params) response = prediction_client.predict(request=request) results = [] for result in response.payload: results.append([result.display_name, result.classification.score]) return results
def predict(project_id, model_id, content): """Predict.""" # [START automl_language_entity_extraction_predict] from google.cloud import automl # TODO(developer): Uncomment and set the following variables # project_id = "YOUR_PROJECT_ID" # model_id = "YOUR_MODEL_ID" # content = "text to predict" prediction_client = automl.PredictionServiceClient() # Get the full path of the model. model_full_id = automl.AutoMlClient.model_path(project_id, "us-central1", model_id) # Supported mime_types: 'text/plain', 'text/html' # https://cloud.google.com/automl/docs/reference/rpc/google.cloud.automl.v1#textsnippet text_snippet = automl.TextSnippet(content=content, mime_type="text/plain") payload = automl.ExamplePayload(text_snippet=text_snippet) response = prediction_client.predict(name=model_full_id, payload=payload) for annotation_payload in response.payload: print("Text Extract Entity Types: {}".format( annotation_payload.display_name)) print("Text Score: {}".format( annotation_payload.text_extraction.score)) text_segment = annotation_payload.text_extraction.text_segment print("Text Extract Entity Content: {}".format(text_segment.content)) print("Text Start Offset: {}".format(text_segment.start_offset)) print("Text End Offset: {}".format(text_segment.end_offset))
def predict(content): """ Predict labels """ project_id = c.PROJECT_ID model_id = c.MODEL_ID location = c.LOCATION predictor = automl.PredictionServiceClient.from_service_account_json( "service_account.json") model_full_id = automl.AutoMlClient.model_path(project_id, location, model_id) image = automl.Image(image_bytes=content) payload = automl.ExamplePayload(image=image) request = automl.PredictRequest(name=model_full_id, payload=payload) response = predictor.predict(request=request) pred_sum = 0 prediction = {} for result in response.payload: prediction[result.display_name] = result.classification.score pred_sum += result.classification.score if pred_sum != 0: for pred in prediction: prediction[pred] /= pred_sum return prediction
def predict(filePath): project_id = "762814515048" model_id = "ICN244084984296505344" file_path = filePath prediction_client = automl.PredictionServiceClient() # Get the full path of the model. model_full_id = automl.AutoMlClient.model_path(project_id, "us-central1", model_id) # Read the file. with open(file_path, "rb") as content_file: content = content_file.read() image = automl.Image(image_bytes=content) payload = automl.ExamplePayload(image=image) # params is additional domain-specific parameters. # score_threshold is used to filter the result # https://cloud.google.com/automl/docs/reference/rpc/google.cloud.automl.v1#predictrequest params = {"score_threshold": "0.7"} request = automl.PredictRequest(name=model_full_id, payload=payload, params=params) response = prediction_client.predict(request=request) print("Prediction results:") results = [] for result in response.payload: results.append(result.display_name) results.append(result.classification.score) print("Predicted class name: {}".format(result.display_name)) print("Predicted class score: {}".format(result.classification.score)) return (results)
def predict(project_id, model_id, file_path): """Predict.""" # [START automl_vision_classification_predict] from google.cloud import automl # TODO(developer): Uncomment and set the following variables # project_id = "YOUR_PROJECT_ID" # model_id = "YOUR_MODEL_ID" # file_path = "path_to_local_file.jpg" prediction_client = automl.PredictionServiceClient() # Get the full path of the model. model_full_id = automl.AutoMlClient.model_path(project_id, "us-central1", model_id) # Read the file. with open(file_path, "rb") as content_file: content = content_file.read() image = automl.Image(image_bytes=content) payload = automl.ExamplePayload(image=image) # params is additional domain-specific parameters. # score_threshold is used to filter the result # https://cloud.google.com/automl/docs/reference/rpc/google.cloud.automl.v1#predictrequest params = {"score_threshold": "0.8"} request = automl.PredictRequest(name=model_full_id, payload=payload, params=params) response = prediction_client.predict(request=request) print("Prediction results:") for result in response.payload: print("Predicted class name: {}".format(result.display_name)) print("Predicted class score: {}".format(result.classification.score))
def predict(project_id, model_id, file_path): """Predict.""" # [START automl_translate_predict] from google.cloud import automl # TODO(developer): Uncomment and set the following variables # project_id = "YOUR_PROJECT_ID" # model_id = "YOUR_MODEL_ID" # file_path = "path_to_local_file.txt" prediction_client = automl.PredictionServiceClient() # Get the full path of the model. model_full_id = automl.AutoMlClient.model_path(project_id, "us-central1", model_id) # Read the file content for translation. with open(file_path, "rb") as content_file: content = content_file.read() content.decode("utf-8") text_snippet = automl.TextSnippet(content=content) payload = automl.ExamplePayload(text_snippet=text_snippet) response = prediction_client.predict(name=model_full_id, payload=payload) translated_content = response.payload[0].translation.translated_content print(u"Translated content: {}".format(translated_content.content))
def input_symptoms(): if request.method == 'POST': try: travel = int(request.form['travel']) except: travel = 0 try: tiredcough = int(request.form['commonsym']) except: tiredcough = 0 try: breath = int(request.form['majorsym']) except: breath = 0 try: exposure = int(request.form['exposure']) except: exposure = 0 try: image = request.files['image'] image.save('beach1.bmp') except: return render_template('index.html', results="No File Found") file_path = "beach1.bmp" with open(file_path, "rb") as content_file: content = content_file.read() image = automl.Image(image_bytes=content) payload = automl.ExamplePayload(image=image) # params is additional domain-specific parameters. # score_threshold is used to filter the result # https://cloud.google.com/automl/docs/reference/rpc/google.cloud.automl.v1#predictrequest params = {"score_threshold": "0.8"} request2 = automl.PredictRequest(name=model_full_id, payload=payload, params=params) # 'content' is base-64-encoded image data. response = prediction_client.predict(request=request2) for result in response.payload: var1 = result.display_name var2 = result.classification result2 = 'Disease: ', var1, ' Probability of disease: ', var2 result = travel + tiredcough + breath + exposure if result >= 3: result = 'You have high chances of being covid positive' else: result = 'Please take care of yourself(wear a mask :D)' return render_template('index.html', results=result, result2=result2) return None
async def process(req: models.Request): """ Analyze a given piece of text to find the bias. Any given piece of text will be checked against the database to check if it has already been processed. If it has not yet been processed, it will be sent for inference on a GCP NLP trained model. """ # Either fetch the content from the site or use the provided text if req.url != "": try: article = Article(req.url) article.download() article.parse() if article.text != "" and article.text is not None: text = article.text else: text = req.text except ArticleException: text = req.text else: text = req.text # Calculate hash of id and text job_hash = compute_job_hash(req.id, text) # Ensure job has not been computed or is being computed job_ref = db.collection("text").document(job_hash) job = job_ref.get() if job.exists: job_data = job.to_dict() return { "success": True, "bias": job_data.get("bias"), "extent": job_data.get("extent") } # Process the data snippet = automl.TextSnippet(content=text, mime_type="text/plain") payload = automl.ExamplePayload(text_snippet=snippet) response = predictor.predict(name=MODEL_PATH, payload=payload) bias, extent = extract_from_categories(response.payload) # Set the processed data to the database job_ref.set({ "hash": job_hash, "bias": bias, "extent": extent }) return { "success": True, "bias": bias, "extent": extent }
def predictionCall(file_path_image): #returns faces detected in image faces = detectFaces(file_path_image) count = 0 for face in faces: count = count + 1 im = Image.open(file_path_image) xTopLeft = face[0] yTopLeft = face[1] xBotRight = face[2] yBotRight = face[3] im1 = im.crop((xTopLeft, yTopLeft, xBotRight, yBotRight)) img_path = "Predict" + str(count) + ".jpg" im1 = im1.save(img_path) # Read the file. with open(img_path, "rb") as content_file: content = content_file.read() image = automl.Image(image_bytes=content) payload = automl.ExamplePayload(image=image) params = {"score_threshold": "0.8"} request = automl.PredictRequest( name=model_full_id, payload=payload, params=params ) response = prediction_client.predict(request=request) mask = False unmask = False both = False print("Prediction results:") for result in response.payload: if "UnmaskedPeople" == result.display_name: unmask = True if "MaskedPeople" == result.display_name: mask = True #print("Predicted class name: {}".format(result.display_name)) #print("Predicted class score: {}".format(result.classification.score)) if mask == True and unmask == True: both = True if both == True: print("Person " + str(count) + ": Error. Both Mask and Unmask detected") elif mask == True: print("Person " + str(count) + " is wearing mask :)") else: print("Person " + str(count) + " is NOT wearing a mask :(")
def predict(): if request.method == 'POST': comment = request.form['comment'] #data = [comment] text_snippet = automl.TextSnippet(content=comment, mime_type="text/plain") payload = automl.ExamplePayload(text_snippet=text_snippet) my_prediction = prediction_client.predict(name=model_full_id, payload=payload) return render_template('results.html', prediction=my_prediction, comment=comment)
def get_prediction(img): with open(img, "rb") as content_file: content = content_file.read() image = automl.Image(image_bytes=content) payload = automl.ExamplePayload(image=image) request = automl.PredictRequest(name=model_full_id, payload=payload, params={}) response = prediction_client.predict(request=request) t = response.payload[0] if t.display_name == 'negative': return 1-t.classification.score else: return t.classification.score
def prediction(): if request.method == 'POST': commentInput = request.form for key, item in commentInput.items(): text_snippet = automl.TextSnippet(content=item, mime_type="text/plain") payload = automl.ExamplePayload(text_snippet=text_snippet) response = prediction_client.predict(name=model_full_id, payload=payload) for annotation_payload in response.payload: sentPred = annotation_payload.display_name break return render_template('results.html', sentPred=sentPred, content=content)
def predict(filepath): threshold = "0.1" with open(filepath, "rb") as content_file: content = content_file.read() image = automl.Image(image_bytes=content) payload = automl.ExamplePayload(image=image) params = {"score_threshold": threshold} request = automl.PredictRequest( name=model_full_id, payload=payload, params=params ) response = prediction_client.predict(request=request) return {**{result.display_name.capitalize(): result.classification.score for result in response.payload}, **{'Name': os.path.basename(filepath)}}
def predict(content): # Supported mime_types: 'text/plain', 'text/html' # https://cloud.google.com/automl/docs/reference/rpc/google.cloud.automl.v1#textsnippet text_snippet = automl.TextSnippet(content=content, mime_type="text/plain") payload = automl.ExamplePayload(text_snippet=text_snippet) response = prediction_client.predict(name=model_full_id, payload=payload) for annotation_payload in response.payload: # print( # u"Predicted class name: {}".format(annotation_payload.display_name) # ) # print( # u"Predicted class score: {}".format( # annotation_payload.classification.score # ) # ) return annotation_payload.display_name break
def predict(content): prediction_client = automl.PredictionServiceClient() # Get the full path of the model. model_full_id = automl.AutoMlClient.model_path(project_id, "us-central1", model_id) # Supported mime_types: 'text/plain', 'text/html' # https://cloud.google.com/automl/docs/reference/rpc/google.cloud.automl.v1#textsnippet text_snippet = automl.TextSnippet(content=content, mime_type="text/plain") payload = automl.ExamplePayload(text_snippet=text_snippet) response = prediction_client.predict(name=model_full_id, payload=payload) for annotation_payload in response.payload: print("Predicted class name: {}".format( annotation_payload.display_name)) print("Predicted sentiment score: {}".format( annotation_payload.text_sentiment.sentiment)) return annotation_payload.text_sentiment.sentiment
def predict(content): from google.cloud import automl # You must first create a dataset, using the `eu` endpoint, before you can # call other operations such as: list, get, import, delete, etc. client_options = {'api_endpoint': 'eu-automl.googleapis.com:443'} project_id = '685330484131' # (model AI Crowd) model_id = 'TCN3300918624537018368' model_id = 'TCN4629621252099670016' prediction_client = automl.PredictionServiceClient( client_options=client_options) # Get the full path of the model. model_full_id = automl.AutoMlClient.model_path(project_id, "eu", model_id) text_snippet = automl.TextSnippet(content=content, mime_type="text/plain") payload = automl.ExamplePayload(text_snippet=text_snippet) response = prediction_client.predict(name=model_full_id, payload=payload) return response
def get_prediction(file_path): project_id = "msds-434-final" model_id = "ICN7344581542892535808" # file_path = "uploads/city.png" ## for local image testing credentials = service_account.Credentials.from_service_account_file("/home/jesse_lybianto/msds-434-final/msds-434-final-f32a0ccc78d5.json") prediction_client = automl.PredictionServiceClient(credentials=credentials) # Get the full path of the model. model_full_id = automl.AutoMlClient.model_path( project_id, "us-central1", model_id ) # Read the file. with open(file_path, "rb") as content_file: content = content_file.read() image = automl.Image(image_bytes=content) payload = automl.ExamplePayload(image=image) # params is additional domain-specific parameters. # score_threshold is used to filter the result # https://cloud.google.com/automl/docs/reference/rpc/google.cloud.automl.v1#predictrequest params = {"score_threshold": "0.0"} request = automl.PredictRequest( name=model_full_id, payload=payload, params=params ) response = prediction_client.predict(request=request) print(response.payload) return response.payload # For non-modular console output # print("Prediction results:") # for result in response.payload: # print("Predicted class name: {}".format(result.display_name)) # print("Predicted class score: {}".format(result.classification.score))
def home_page(): # from flask import request if request.method == 'GET': context = {"done": False} return render_template('site.html', **context) else: # get image content from POST request inputs file = request.files["myFile"] image_content = b'' for data in file.stream: # read image as a stream image_content += data # google api variables project_id = "alpine-infinity-290102" prediction_client = automl.PredictionServiceClient() # Get the full path of the model. model_full_id = automl.AutoMlClient.model_path(project_id, "us-central1", model_id) image = automl.Image(image_bytes=image_content) payload = automl.ExamplePayload(image=image) req = automl.PredictRequest( name=model_full_id, payload=payload, ) response = prediction_client.predict(request=req) class_ = response.payload[0].display_name measure = response.payload[0].classification.score context = { "done": True, "guess_mal": class_ == "Malignant", "class_": class_, "measure": '{:.2%}'.format(measure) } return render_template('site.html', **context)
def nlp_predict(self, model_id: (str, 'the id of the deployed nlp model'), content: (str, 'the text to submit to the prediction model')): response_payload = None try: model_full_id = self.automl_client.model_path( self.project_id, self.region, model_id) text_snippet = automl.TextSnippet(content=content, mime_type="text/plain") payload = automl.ExamplePayload(text_snippet=text_snippet) #payload = {'text_snippet': {'content': content, 'mime_type': 'text/plain' }} response = self.prediction_client.predict(name=model_full_id, payload=payload) response_payload = response.payload except Exception: logger.exception("") return response_payload
def produce_automl_results(dir_img_path: str = "", dir_destination_path: str = ""): from google.cloud import automl project_id = "hidden" model_id = "hidden" imgs = os.listdir(dir_img_path) relevant_images = [ os.path.join(dir_img_path, i) for i in imgs if "png" in i ] prediction_client = automl.PredictionServiceClient() project_id = "hidden" model_id = "hidden" # Get the full path of the model. model_full_id = automl.AutoMlClient.model_path(project_id, "us-central1", model_id) file_path = "../../data/005.png" with open(file_path, "rb") as content_file: content = content_file.read() image = automl.Image(image_bytes=content) payload = automl.ExamplePayload(image=image) params = {} request = automl.PredictRequest(name=model_full_id, payload=payload, params=params) response = prediction_client.predict(request=request) print("Prediction results:") for result in response.payload: print("Predicted class name: {}".format(result.display_name)) print("Predicted class score: {}".format(result.classification.score))
import os from google.cloud import automl from dotenv import load_dotenv load_dotenv() project_id = os.getenv("PROJECT_ID") model_id = os.getenv("MODEL_ID") content = "How does the len() function work?" prediction_client = automl.PredictionServiceClient() # Get the full path of the model. model_full_id = automl.AutoMlClient.model_path(project_id, "us-central1", model_id) text_snippet = automl.TextSnippet(content=content, mime_type="text/plain") payload = automl.ExamplePayload(text_snippet=text_snippet) response = prediction_client.predict(name=model_full_id, payload=payload) for annotation_payload in response.payload: print(u"Predicted question tag: {}".format( annotation_payload.display_name)) print(u"Predicted score: {}".format( annotation_payload.classification.score))
def automl_vision(file_path): """Predict.""" # [START automl_vision_classification_predict] #print(file_path) # autoML JSon 키 설정 #credential_path = r"키.json" os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = credential_path project_id = "수정" model_id = "수정" # 원본 이미지 img = cv2.imread(file_path) # 원본 이미지 크기 img_height = img.shape[0] img_width = img.shape[1] # RGB 색상으로 변환 orig = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) prediction_client = automl.PredictionServiceClient() # Get the full path of the model. model_full_id = automl.AutoMlClient.model_path(project_id, "us-central1", model_id) # 파일 읽어들이기 with open(file_path, "rb") as content_file: content = content_file.read() image = automl.Image(image_bytes=content) payload = automl.ExamplePayload(image=image) # params is additional domain-specific parameters. # score_threshold is used to filter the result # https://cloud.google.com/automl/docs/reference/rpc/google.cloud.automl.v1#predictrequest params = {"score_threshold": "0.8"} request = automl.PredictRequest(name=model_full_id, payload=payload, params=params) response = prediction_client.predict(request=request) tags = [] for result in response.payload: # 태그로 잡힌 박스 몇 개인지 bounding_box = result.image_object_detection.bounding_box # normalized vertices로 되어있는 좌표를 실사이즈로 변환 및 직사각형의 네 꼭짓점 좌표를 구함 lu = bounding_box.normalized_vertices[0] # left/up x, y 좌표 rd = bounding_box.normalized_vertices[1] # right/down x, y 좌표 w = rd.x - lu.x h = rd.y - lu.y # cropping cropped_img = orig[int(lu.y * img_height):int((lu.y + h) * img_height), int(lu.x * img_width):int((lu.x + w) * img_width)] texts = my_detect_text_mat( cropped_img) #잘린 이미지들 속 글자 추출하기 위해 my_detect_text_mat() 호출 tags.append(texts[0].description) return tags
prediction_client = automl.PredictionServiceClient() # Get the full path of the model. model_full_id = automl.AutoMlClient.model_path( project_id, "us-central1", model_id ) # Read the file. with open(file_path, "rb") as content_file: content = content_file.read() image = automl.Image(image_bytes=content) payload = automl.ExamplePayload(image=image) request = automl.PredictRequest( name=model_full_id, payload=payload ) response = prediction_client.predict(request=request) print("Prediction results:") for result in response.payload: print("Predicted class name: {}".format(result.display_name)) print("Predicted class score: {}".format(result.classification.score))
def request_prediction(file): # model path project_id = "241661365506" model_id = "IOD6459342741137522688" name = "projects/{}/locations/us-central1/models/{}".format( project_id, model_id) prediction_client = automl.PredictionServiceClient.from_service_account_json( "GCPkeys.json") # open image file and convert to base64 image = automl.Image(image_bytes=file.read()) payload = automl.ExamplePayload(image=image) params = {} request = automl.PredictRequest(name=name, payload=payload, params=params) # request a prediction try: response = prediction_client.predict(request=request) except: card = "The Eye is shut. Please try again later." score = 0 # now translate the prediction to nicer looking text try: score = round(response.payload[0].image_object_detection.score, 4) if response.payload[0].display_name == "chariot": card = "The Chariot" elif response.payload[0].display_name == "death": card = "Death" elif response.payload[0].display_name == "devil": card = "The Devil" elif response.payload[0].display_name == "emperor": card = "The Emperor" elif response.payload[0].display_name == "empress": card = "The Empress" elif response.payload[0].display_name == "fool": card = "The Fool" elif response.payload[0].display_name == "Fortune": card = "Fortune" elif response.payload[0].display_name == "hanged": card = "The Hanged Man" elif response.payload[0].display_name == "hermit": card = "The Hermit" elif response.payload[0].display_name == "hierophant": card = "The Hierophant" elif response.payload[0].display_name == "judgment": card = "Judgment" elif response.payload[0].display_name == "justice": card = "Justice" elif response.payload[0].display_name == "lovers": card = "The Lovers" elif response.payload[0].display_name == "magician": card = "The Magician" elif response.payload[0].display_name == "moon": card = "The Moon" elif response.payload[0].display_name == "priestess": card = "The Priestess" elif response.payload[0].display_name == "star": card = "The Star" elif response.payload[0].display_name == "strength": card = "Strength" elif response.payload[0].display_name == "sun": card = "The Sun" elif response.payload[0].display_name == "temperance": card = "Temperance" elif response.payload[0].display_name == "tower": card = "The Tower" elif response.payload[0].display_name == "world": card = "The World" else: card = "Your future is murky. Try another card." score = 666 except: card = "Your future is murky. Try another card." score = 666 return card, score