Example #1
0
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)
Example #2
0
class ImageClassification:
    def __init__(self):
        self.threshold = 0.7
        # Add your Computer Vision subscription key to your environment variables.
        self.subscription_key = "dfb4354998be4e10970026e96d23c916"
        # Add your Computer Vision endpoint to your environment variables.
        self.endpoint = "https://shadow.cognitiveservices.azure.com/"
        self.computervision_client = ComputerVisionClient(
            self.endpoint, CognitiveServicesCredentials(self.subscription_key))

    def get_classification_result(self, image):
        return self.computervision_client.detect_objects_in_stream(image)

    def get_classified_name_of_image(self, image, intent):
        name = []
        result = self.get_classification_result(image)
        if result != None:
            for obj in result.objects:
                print(obj)
                if obj.confidence > self.threshold:
                    name.append(obj.object_property)
        return name

    def get_classified_color_of_image(self, image):
        pass
Example #3
0
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
        image1 = request.files['image']
        image2 = image1
        client = ComputerVisionClient(
            COGSVCS_CLIENTURL, CognitiveServicesCredentials(COGSVCS_KEY))
        # try:
        #     result = client.describe_image_in_stream(image1)
        #     message = result.captions[0].text
        # except ComputerVisionErrorException as e:
        #     message = str(e.response.text)

        try:
            result = client.detect_objects_in_stream(image2)
            message = ''
        except ComputerVisionErrorException as e:
            message += str(e.response.text)

        img = Image.open(image2)
        dctx = ImageDraw.Draw(img)  # create drawing context
        for detection in result.objects:
            w, h = detection.rectangle.w, detection.rectangle.h
            bbox = [(detection.rectangle.x, detection.rectangle.y),
                    (w + detection.rectangle.x, h + detection.rectangle.y)]
            dctx.rectangle(bbox, outline="red")
            dctx.text((detection.rectangle.x, detection.rectangle.y),
                      detection.object_property, (255, 0, 0))
        del dctx  # destroy drawing context

        output = BytesIO()
        img.save(output, 'jpeg', quality=100)
        output.seek(0)
        img = base64.b64encode(output.getvalue())

        return render_template('result.html',
                               message=message,
                               img=img.decode('ascii'))
Example #4
0
# Create client.

client = ComputerVisionClient(endpoint, credentials)

# Check the URL supplied or path exists and is an image.

# Send provided image (url or path) to azure to extract text.

# ----------------------------------------------------------------------
# URL or path
# ----------------------------------------------------------------------

path = args.path

# ----------------------------------------------------------------------
# Objects
# ----------------------------------------------------------------------

if is_url(path):
    analysis = client.detect_objects(path)
else:
    path = os.path.join(get_cmd_cwd(), path)
    with open(path, 'rb') as fstream:
        analysis = client.detect_objects_in_stream(fstream)

for object in analysis.objects:
    print(f"{object.rectangle.x} {object.rectangle.y} " +
          f"{object.rectangle.x + object.rectangle.w} " +
          f"{object.rectangle.y + object.rectangle.h}")
else:
    print("Image is good clip art.")

if remote_image_analysis.image_type.line_drawing_type == 0:
    print("Image is not a line drawing.")
else:
    print("Image is a line drawing")
#   END - Detect image types (clip art/line drawing) of a remote image

#   Detect objects in a local image by:
#   1. Opening the binary file for reading.
#   2. Calling the Computer Vision service's detect_objects_in_stream with the:
#      - image
#   3. Displaying the location of the objects.
local_image = open(local_image_path, "rb")
local_image_objects = computervision_client.detect_objects_in_stream(
    local_image)

print("\nDetecting objects in local image:")
if len(local_image_objects.objects) == 0:
    print("No objects detected.")
else:
    for object in local_image_objects.objects:
        print("object at location {}, {}, {}, {}".format( \
        object.rectangle.x, object.rectangle.x + object.rectangle.w, \
        object.rectangle.y, object.rectangle.y + object.rectangle.h))
#   END - Detect objects in a local image

#   Detect objects in a remote image by:
#   1. Opening the binary file for reading.
#   2. Calling the Computer Vision service's detect_objects with the:
#      - image
Example #6
0
# </snippet_type>
print()
'''
END - Detect Image Types - remote
'''

'''
Detect Objects - local
This example detects different kinds of objects with bounding boxes in a local image.
'''
print("===== Detect Objects - local =====")
# Get local image with different objects in it
local_image_path_objects = "resources\\objects.jpg"
local_image_objects = open(local_image_path_objects, "rb")
# Call API with local image
detect_objects_results_local = computervision_client.detect_objects_in_stream(local_image_objects)

# Print results of detection with bounding boxes
print("Detecting objects in local image:")
if len(detect_objects_results_local.objects) == 0:
    print("No objects detected.")
else:
    for object in detect_objects_results_local.objects:
        print("object at location {}, {}, {}, {}".format( \
        object.rectangle.x, object.rectangle.x + object.rectangle.w, \
        object.rectangle.y, object.rectangle.y + object.rectangle.h))
print()
'''
END - Detect Objects - 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()
'''
Detect Objects - remote
This example detects different kinds of objects with bounding boxes in a remote image.
'''
print("===== Detect Objects - remote =====")
# Call API with URL
detect_objects_results_remote = computervision_client.detect_objects_in_stream(
    open(photoFilename, 'rb'))

# Print detected objects results with bounding boxes
print("Detecting objects in remote image:")
if len(detect_objects_results_remote.objects) == 0:
    print("No objects detected.")
else:

    img = Image.open(photoFilename)
    # For each face returned use the object rectangle and draw a red box.
    print('Drawing rectangle around objects... see popup for results.')
    draw = ImageDraw.Draw(img)

    for object in detect_objects_results_remote.objects:
        print("object of type ", object.object_property, " with a confidence of ", object.confidence, "at location {}, {}, {}, {}".format( \
        object.rectangle.x, object.rectangle.x + object.rectangle.w, \
Example #8
0
class AzureObjectDetectionModule(abstract.AbstractModule):
    """An object detection module using Microsoft Azure.

    Attributes:
        key (str): key to Microsoft Cognitive Services for Object Detection API
    """
    @staticmethod
    def name():
        return "Microsoft Azure / Cognitive Services Object Detection"

    @staticmethod
    def description():
        return "An object detection module using Microsoft Azure."

    @staticmethod
    def input_ius():
        return [ImageIU]

    @staticmethod
    def output_iu():
        return DetectedObjectsIU

    def authenticate(self):
        # The resource key for the SlimComputerVision resource on Azure
        COMPUTER_VISION_SUBSCRIPTION_KEY = self.key

        # The endpoint associated with the Azure resource
        COMPUTER_VISION_ENDPOINT = self.endpoint

        # Authenticating the client.
        self.client = ComputerVisionClient(
            COMPUTER_VISION_ENDPOINT,
            CognitiveServicesCredentials(COMPUTER_VISION_SUBSCRIPTION_KEY))

    def __init__(self, key, endpoint, width=200, height=200, **kwargs):
        """Initializes the object detector module, authenticates with Azure/MS Cognitive Services.

        Args:
            model_dir (str): The path to the directory of the NLU model
                generated by rasa_nlu.train.
        """
        super().__init__(**kwargs)
        self.client = None
        self.key = key
        self.endpoint = endpoint
        self.width = width
        self.height = height
        self.f = 'tmp{}.png'.format(random())
        self.queue = deque()

    def process_iu(self, input_iu):
        self.queue.clear()  # drop frames, if still waiting
        self.queue.append(input_iu)

        return None

    def run_detector(self):

        while True:
            if len(self.queue) == 0:
                time.sleep(0.3)
                continue
            input_iu = self.queue.popleft()
            print(input_iu)
            image = input_iu.payload
            cv2.imwrite(self.f, cv2.cvtColor(image, cv2.COLOR_RGB2BGR))
            try:
                with open(self.f, "rb") as image_fd:
                    detected_objects = self.client.detect_objects_in_stream(
                        image_fd)
                    print(detected_objects)
                    if len(detected_objects.objects) == 0:
                        return None
                    else:
                        returning_dictionary = {}
                        count = 0
                        for object in detected_objects.objects:
                            inner_dict = {}
                            inner_dict['xmin'] = object.rectangle.x
                            inner_dict[
                                'xmax'] = object.rectangle.x + object.rectangle.w
                            inner_dict['ymin'] = object.rectangle.y
                            inner_dict[
                                'ymax'] = object.rectangle.y + object.rectangle.h
                            inner_dict['label'] = object.object_property
                            returning_dictionary["object" +
                                                 str(count)] = inner_dict
                            count += 1
                        returning_dictionary['num_objs'] = len(
                            returning_dictionary)
                        output_iu = self.create_iu(input_iu)
                        output_iu.set_detected_objects(image,
                                                       returning_dictionary)
                        self.append(output_iu)
            except FileNotFoundError:
                print("The file, {}, was not found.".format(image_file_path))
                exit(1)

    def setup(self):
        self.authenticate()
        t = threading.Thread(target=self.run_detector)
        t.start()
Example #9
0
## Costum Vision Configuration ##

# Endpoint and Key
endpoint_c = "https://testdeletes123.cognitiveservices.azure.com/"
key_c = "de6ba6ab6d3246e48cdba750fbd0f17e"

# Authenticate the client object
computervision_client = ComputerVisionClient(
    endpoint_c, CognitiveServicesCredentials(key_c))

# Open the test image
im = Image.open(image_path)
img = open(image_path, "rb")

# Detect the photo on the image :)
detected_object = computervision_client.detect_objects_in_stream(img)

# This example detects different kinds of objects with bounding boxes in a remote image.
X = ""
Xw = ""
Y = ""
Yh = ""

if len(detected_object.objects) == 0:
    print("No objects detected.")

else:
    for object in detected_object.objects:
        X = object.rectangle.x
        Xw = object.rectangle.x + object.rectangle.w
        Y = object.rectangle.y
Example #10
0
def main():
    """ Text and Image Recognizer """

    st.markdown("bla bla")

    uploaded_image = st.file_uploader("Selectione une image")
    if uploaded_image is not None:
        image = Image.open(uploaded_image)
        st.image(image, use_column_width=True)
        image.save('test_images/test11111.png', 'PNG')

        # Dictionary to save the informations
        dic = {}

        ## Form Recognizer Configuration ##

        # Endpoint and Key
        endpoint_f = "https://recettesfromrecognizer.cognitiveservices.azure.com/"
        key_f = "dee1ba127bbf442489c58a86932ae162"

        # Authenticate the client object
        form_recognizer_client = FormRecognizerClient(
            endpoint_f, AzureKeyCredential(key_f))

        # Model ID (The Model that we have trined on Azure Labeling Tool)
        model_id = "491ce87f-878c-4eaf-8bc4-a336a4a209a5"

        # image path
        image_path = os.path.join("test_images", "test1.png")

        # Open and test the image

        with open(image_path, "rb") as f:
            poller = form_recognizer_client.begin_recognize_custom_forms(
                model_id=model_id, form=f)

        # Result of the test for new image
        forms = poller.result()

        # To get cles and valeurs
        for recognized_form in forms:
            for name, field in recognized_form.fields.items():
                print(" '{}' : ({}) Accuracy  \n '{}' \n".format(
                    name,
                    field.confidence,
                    field.value,
                ))
                dic[name] = field.value

        ## Costum Vision Configuration ##

        # Endpoint and Key
        endpoint_c = "https://testdeletes123.cognitiveservices.azure.com/"
        key_c = "de6ba6ab6d3246e48cdba750fbd0f17e"

        # Authenticate the client object
        computervision_client = ComputerVisionClient(
            endpoint_c, CognitiveServicesCredentials(key_c))
        # Open the test image

        im = Image.open(image_path)
        img = open(image_path, "rb")

        # Detect the photo on the image :)
        detected_object = computervision_client.detect_objects_in_stream(img)

        # This example detects different kinds of objects with bounding boxes in a remote image.
        X = ""
        Xw = ""
        Y = ""
        Yh = ""

        if len(detected_object.objects) == 0:
            print("No objects detected.")

        else:
            for object in detected_object.objects:
                X = object.rectangle.x
                Xw = object.rectangle.x + object.rectangle.w
                Y = object.rectangle.y
                Yh = object.rectangle.y + object.rectangle.h

        # Create Box
        box = (X, Y, Xw, Yh)

        # Crop Image
        area = im.crop(box)
        #image = Image.open(uploaded_image)
        st.image(area, use_column_width=True)
        #area.show()
        # Convert the image to an array
        image_array = asarray(area)
        dic["image"] = image_array
Example #11
0
end_frame = len(list_local_imgs)
for i in range(ini_frame, end_frame, 1):  #local_img in list_local_imgs: #

    # # print frame
    local_img = list_local_imgs[i]
    print(list_local_imgs[i])

    # img
    #local_image = open(os.path.join(frames_folder,local_img),"rb")
    pth = os.path.join(frames_folder, local_img)
    img = Image.open(pth)

    # Classify only every 30 frames
    if i % step_frames == 0:
        # Call API with local image
        detect_objects_results_remote = computervision_client.detect_objects_in_stream(
            open(pth, 'rb'))
        # pause
        #time.sleep(5.0)

        # # Get image as array
        # img = Image.open(local_image)

        # Print detected objects and bounding box coords
        if len(detect_objects_results_remote.objects) == 0:
            print("No objects detected.")

        else:
            for obj in detect_objects_results_remote.objects:
                # if detected object is tiger: feed crop to action classifier
                if obj.object_property in ["tiger", "animal", "mammal"]:
                    # Crop bbox with margin