Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP:avoid unnecessary segmentation #783

Open
wants to merge 10 commits into
base: main
Choose a base branch
from

Conversation

Animesh404
Copy link
Contributor

@Animesh404 Animesh404 commented Jun 19, 2024

What kind of change does this PR introduce?

attempt for issue #614 to extract the portion of active window that is different and combine with similar image

Summary

Trying to extract the portion of the active window that is different (i.e. greater than some tolerance) from the window retrieved, this can be used later to segment + describe this portion rather than the full thing.

  • reference image segments
    Screenshot 2024-06-29 165925

  • new image segments
    Screenshot 2024-06-29 165952

  • non-matching image section
    Screenshot 2024-06-29 170036

  • matching image section
    Screenshot 2024-06-29 210741

  • non-matching image section segments
    Screenshot 2024-06-29 170048

  • matching image section segments
    Screenshot 2024-06-29 210803

  • combined matching image section segments with non-matching image section segments

    Screenshot 2024-06-29 210830

  • Final result

    Screenshot 2024-06-29 165938

Checklist

  • My code follows the style guidelines of OpenAdapt
  • I have performed a self-review of my code
  • If applicable, I have added tests to prove my fix is functional/effective
  • I have linted my code locally prior to submission
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation (e.g. README.md, requirements.txt)
  • New and existing unit tests pass locally with my changes

How can your code be run and tested?

try to run experiments/visualizing_segments.py

@abrichr
Copy link
Member

abrichr commented Jun 19, 2024

That's a great start!

The next steps are:

  • identify reference segments that overlap with the difference areas
  • extract the portion of the new image corresponding to the reference segments from the previous step
  • segment the extracted portion from the previous step
  • combine a) the segments extracted in the previous step with b) the reference segments that do not overlap with the difference areas

Copy link
Member

@abrichr abrichr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is looking really good @Animesh404 ! Almost there.

# difference_image = extract_difference_image(new_image, old_image, tolerance=0.05)
# difference_image.show()

new_image = Image.open('./winCalNew.png')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please move to a main function and remove commented out code.

import cv2
from skimage.metrics import structural_similarity as ssim

def extract_difference_image(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add two newlines before/after a standalone function, as per Google PyDocStyle.

@@ -0,0 +1,45 @@
from PIL import Image
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add brief module documentation and example usage.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe I should remove this file? already implemented same function in vision.py

@@ -47,9 +47,11 @@
from dataclasses import dataclass
from pprint import pformat
import time
from xml.etree.ElementPath import find
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove if unused


from loguru import logger
from PIL import Image, ImageDraw
from typing import List
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use list instead of List

combined_centroids = previous_centroids + new_centroids

return Segmentation(
image=combined_image,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe this should be the new image, not the previous image.


combined_image = previous_segmentation.image
combined_masked_images = previous_segmentation.masked_images + new_masked_images
combined_descriptions = previous_segmentation.descriptions + new_descriptions
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe we need to filter out the masked_images and the descriptions that are not relevant for the new image, i.e. those that overlap with difference areas between the old and the new image.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

alrighty!

new_bounding_boxes, new_centroids = vision.calculate_bounding_boxes(new_masks)

combined_bounding_boxes = previous_bounding_boxes + new_bounding_boxes
combined_centroids = previous_centroids + new_centroids
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As above, we need to filter out the irrelevant boxes/centroids.

@@ -382,6 +412,26 @@ def get_window_segmentation(
# TODO XXX: create copy of similar_segmentation, but overwrite with segments of
# regions of new image where segments of similar_segmentation overlap non-zero
# regions of similar_segmentation_diff
new_image = vision.extract_difference_image(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you think about naming this difference_image rather than new_image?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

makes sense, I'll change this


# Create a mask of the differences
mask = np.zeros_like(new_image_np)
cv2.drawContours(mask, contours, -1, (255), thickness=cv2.FILLED)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe contours should not be necessary. A more direct approach involves getting the indexes of the pixels whose difference is above a threshold, and setting only those pixels directly.

@Animesh404 Animesh404 changed the title feat:extracting difference between two images WIP avoid unnecessary segmentation Jun 27, 2024
@Animesh404 Animesh404 changed the title WIP avoid unnecessary segmentation WIP:avoid unnecessary segmentation Jun 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants