From 0761984bcd10a65d44e432895a5afa414366925f Mon Sep 17 00:00:00 2001 From: dgtlmoon Date: Sat, 12 Feb 2022 23:37:02 +0100 Subject: [PATCH] tweaks to image diff highlighter --- changedetectionio/__init__.py | 20 ++++++++-- changedetectionio/image_diff.py | 41 +++++++++++++++++++++ changedetectionio/templates/diff-image.html | 19 +++++++++- 3 files changed, 76 insertions(+), 4 deletions(-) create mode 100644 changedetectionio/image_diff.py diff --git a/changedetectionio/__init__.py b/changedetectionio/__init__.py index a3ad7a3f..2654e0bf 100644 --- a/changedetectionio/__init__.py +++ b/changedetectionio/__init__.py @@ -808,10 +808,24 @@ def changedetection_app(config=None, datastore_o=None): return output # render an image which contains the diff of two images - @app.route("/diff/image///") - def render_diff_image(uuid, first_date, second_date): + # We always compare the newest against whatever compare_date we are given + @app.route("/diff/image//") + def render_diff_image(uuid, compare_date): + from changedetectionio import image_diff + from flask import make_response - resp = make_response("xxxxx") + watch = datastore.data['watching'][uuid] + newest = list(watch['history'].keys())[-1] + + # @todo this is weird + if compare_date == 'None' or compare_date is None: + second_date = list(watch['history'].keys())[0] + + new_img = watch['history'][newest] + prev_img = watch['history'][second_date] + img = image_diff.render_diff(new_img, prev_img) + + resp = make_response(img) resp.headers['Content-Type'] = 'image/jpeg' return resp diff --git a/changedetectionio/image_diff.py b/changedetectionio/image_diff.py new file mode 100644 index 00000000..fe4e5fd4 --- /dev/null +++ b/changedetectionio/image_diff.py @@ -0,0 +1,41 @@ +# import the necessary packages +from skimage.metrics import structural_similarity as compare_ssim +import argparse +import imutils +import cv2 + +# From https://www.pyimagesearch.com/2017/06/19/image-difference-with-opencv-and-python/ +def render_diff(fpath_imageA, fpath_imageB): + + imageA = cv2.imread(fpath_imageA) + imageB = cv2.imread(fpath_imageB) + + # convert the images to grayscale + grayA = cv2.cvtColor(imageA, cv2.COLOR_BGR2GRAY) + grayB = cv2.cvtColor(imageB, cv2.COLOR_BGR2GRAY) + + # compute the Structural Similarity Index (SSIM) between the two + # images, ensuring that the difference image is returned + (score, diff) = compare_ssim(grayA, grayB, full=True) + diff = (diff * 255).astype("uint8") + print("SSIM: {}".format(score)) + + # threshold the difference image, followed by finding contours to + # obtain the regions of the two input images that differ + thresh = cv2.threshold(diff, 0, 255, + cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU)[1] + cnts = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL, + cv2.CHAIN_APPROX_SIMPLE) + cnts = imutils.grab_contours(cnts) + + # loop over the contours + for c in cnts: + # compute the bounding box of the contour and then draw the + # bounding box on both input images to represent where the two + # images differ + (x, y, w, h) = cv2.boundingRect(c) + cv2.rectangle(imageA, (x, y), (x + w, y + h), (0, 0, 255), 2) + cv2.rectangle(imageB, (x, y), (x + w, y + h), (0, 0, 255), 2) + + #return cv2.imencode('.jpg', imageB)[1].tobytes() + return cv2.imencode('.jpg', imageB)[1].tobytes() \ No newline at end of file diff --git a/changedetectionio/templates/diff-image.html b/changedetectionio/templates/diff-image.html index 9e3b81e9..19b16569 100644 --- a/changedetectionio/templates/diff-image.html +++ b/changedetectionio/templates/diff-image.html @@ -23,11 +23,28 @@
- +
+ {% endblock %} \ No newline at end of file