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 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 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(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(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 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
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 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 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 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 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))
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