class ImageProcessor(object): def __init__(self): self.computervision_client = ComputerVisionClient( endpoint, CognitiveServicesCredentials(subscription_key)) def get_img_captions(self, image_path): local_image = open(image_path, "rb") local_image_description = self.computervision_client.describe_image_in_stream( local_image) captions = [] if (len(local_image_description.captions) == 0): print("No captions detected.") else: for caption in local_image_description.captions: captions.append(caption.text) return captions def get_img_objects(self, image_path): local_image = open(image_path, "rb") local_image_objects = self.computervision_client.detect_objects_in_stream( local_image) objects = [] if len(local_image_objects.objects) == 0: print("No objects detected.") else: for obj in local_image_objects.objects: objects.append((obj.object_property, obj.rectangle.x, obj.rectangle.x + obj.rectangle.w, \ obj.rectangle.y, obj.rectangle.y + obj.rectangle.h)) dims = (local_image_objects.metadata.width, local_image_objects.metadata.height) return (objects, dims)
def caption(update: 'telegram.Update', context: 'telegram.ext.CallbackContext') -> None: """Uses NLP to generate a caption for an image.""" try: if update.message and update.message.reply_to_message: message: 'telegram.Message' = update.message.reply_to_message else: return file: telegram.File = context.bot.getFile(message.photo[-1].file_id) buffer = BytesIO(file.download_as_bytearray()) buffer.seek(0) api_key = config["AZURE_KEY"] endpoint = 'https://tgbot.cognitiveservices.azure.com/' computervision_client = ComputerVisionClient( endpoint, CognitiveServicesCredentials(api_key)) description_results = computervision_client.describe_image_in_stream( buffer) caption = description_results.captions[0].text confidence = description_results.captions[0].confidence text = f"{caption}.\n*Confidence:* {round(confidence * 100, 2)}%" text = "%s%s" % (text[0].upper(), text[1:]) message.reply_text(text=text) except AttributeError: update.message.reply_text(text="*Usage:* `/caption`\n" "Type /caption in response to an image.\n")
def index(): if request.method == 'GET': # User is requesting the form return render_template('form.html') elif request.method == 'POST': # User has sent us data image = request.files['image'] client = ComputerVisionClient( COGSVCS_CLIENTURL, CognitiveServicesCredentials(COGSVCS_KEY)) result = client.describe_image_in_stream(image) message = 'No dog found. How sad. :-(' if 'dog' in result.tags: message = 'There is a dog! Wonderful!!' return render_template('result.html', message=message)
def describe(image_path): endpoint = os.getenv("COMPUTER_VISION_ENDPOINT") subscription_key = os.getenv("COMPUTER_VISION_SUBSCRIPTION_KEY") computervision_client = ComputerVisionClient( endpoint, CognitiveServicesCredentials(subscription_key)) if (not os.path.isfile(image_path)): description_results = computervision_client.describe_image(image_path) else: with open(image_path, "rb") as image: description_results = computervision_client.describe_image_in_stream( image) if (len(description_results.captions) == 0): return ("", 0) else: return (description_results.captions[0].text, description_results.captions[0].confidence * 100)
class CognitiveModel: def __init__(self, image_file: str): self.subscription_key = "a3747513ff724c229b41274483acb06f" self.endpoint = "https://cvision-crushyna.cognitiveservices.azure.com/" self.computervision_client = ComputerVisionClient( self.endpoint, CognitiveServicesCredentials(self.subscription_key)) self.image_file = image_file self.description = None def get_image_desc(self) -> str: """ Describe an Image - remote This example describes the contents of an image with the confidence score. """ # print("===== Describe an image - remote =====") # Call API logging.info("Opening image file as binary file") local_image = open(self.image_file, 'rb') logging.info("Streaming data to Azure Server") description_results = self.computervision_client.describe_image_in_stream( local_image) # Get the captions (descriptions) from the response, with confidence level if len(description_results.captions) == 0: no_desc = "No description detected." logging.warning(no_desc) self.description = no_desc return no_desc else: for caption in description_results.captions: logging.info(f"Description found: {caption.text}") desc_found = "'{}' with confidence {:.2f}%".format( caption.text, caption.confidence * 100) self.description = desc_found return desc_found
print("No description detected.") else: for caption in description_results.captions: print("'{}' with confidence {:.2f}%".format(caption.text, caption.confidence * 100)) print() ''' Describe an Image - local This example describes the contents of an image with the confidence score. ''' print("===== Describe an Image - local =====") # Open local image file local_image_path = "Images/Landmark.jpg" local_image = open(local_image_path, "rb") # Call API description_result = computervision_client.describe_image_in_stream( local_image) # Get the captions (descriptions) from the response, with confidence level print("Description of local image: ") if len(description_result.captions) == 0: print("No description detected.") else: for caption in description_result.captions: print("'{}' with confidence {:.2f}%".format(caption.text, caption.confidence * 100)) ''' END - Describe an Image - local '''
CognitiveServicesCredentials(KEY)) rootFilePath = os.path.dirname( os.path.realpath('__file__')) + '/' # Get the Root File Path camera = PiCamera() # Create an instance of the PiCamera # Take a photo photoFilename = takePicture() # Get an image to get a description #remote_image_url = "https://petecodes.co.uk/images/skateboard.png" #Alternate #remote_image_url = "https://raw.githubusercontent.com/Azure-Samples/cognitive-services-sample-data-files/master/ComputerVision/Images/landmark.jpg" ''' Describe an Image - local This example describes the contents of an image with the confidence score. ''' print("===== Describe an image - local =====") # Call API description_results = computervision_client.describe_image_in_stream( open(photoFilename, 'rb')) # Get the captions (descriptions) from the response, with confidence level print("Description of image: ") if (len(description_results.captions) == 0): print("No description detected.") else: for caption in description_results.captions: print("'{}' with confidence {:.2f}%".format(caption.text, caption.confidence * 100))
analysis = client.analyze_image_by_domain_in_stream(domain, image, language) for landmark in analysis.result["landmarks"]: print(landmark["name"]) print(landmark["confidence"]) #-----------------TEXT DESCRIPTION OF IMAGE-------------# print("#-----------------TEXT DESCRIPTION OF IMAGE-------------#") image = open(path, 'rb') domain = "landmarks" # url = "http://www.public-domain-photos.com/free-stock-photos-4/travel/san-francisco/golden-gate-bridge-in-san-francisco.jpg" language = "en" max_descriptions = 3 analysis = client.describe_image_in_stream(image, max_descriptions, language) for caption in analysis.captions: print(caption.text) print(caption.confidence) #-----------------TEXT FROM IMAGE-------------# print("#-----------------TEXT FROM IMAGE-------------#") # import models from azure.cognitiveservices.vision.computervision.models import TextOperationStatusCodes import time image = open(path, 'rb') # url = "https://smhttp-ssl-39255.nexcesscdn.net/wp-content/uploads/2016/01/Handwritten-note-on-Presidential-stationery-900x616.jpg" raw = True custom_headers = None
f.write('\n'+namePara+'$'+str(1)+'$') f.close() while True: #ultSonic.calculateDistance() if(ultSonic.calculateDistance() <= 10): camera.start_preview() sleep(5) camera.capture('/home/pi/Desktop/image.jpg') camera.stop_preview() buffer = io.BytesIO() buffer.write(open(imagePath, 'rb').read()) buffer.seek(0) print("===== Detect Objects - remote =====") print("===== Describe an image - remote =====") # Call API description_results = computervision_client.describe_image_in_stream(image=buffer) print("Description of remote image: ") if (len(description_results.captions) == 0): print("No description detected.") else: for caption in description_results.captions: print("'{}' with confidence {:.2f}%".format(caption.text, caption.confidence * 100)) print(api.detect_food_in_text(caption.text).json()) name = api.detect_food_in_text(caption.text).json()['annotations'] if len(name)!=0: name2 = name[0]['annotation'] if name2!="": print(name2) putInFridge(name2) print("New entry added to the fridge") sleep(2)