From 2f4bed072b9ecff2503c499cd75026915c0a0175 Mon Sep 17 00:00:00 2001 From: Joao Date: Mon, 17 Feb 2020 16:20:14 -0300 Subject: [PATCH 1/3] added sift --- README.md | 17 +++++++- SIFT.py | 121 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 137 insertions(+), 1 deletion(-) create mode 100644 SIFT.py diff --git a/README.md b/README.md index 37c676d..efc6a73 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,18 @@ +## SIFT.py +- This code will calc the difference between the images, after the code read all the images matrix one by one the function findSimilarities will apply the sift method saving all images that dont repeat it self and discard the rest. + +### Requirements +- Python 2.7 +- pip install opencv-python==3.4.2.16 +- pip install opencv-contrib-python==3.4.2.16 +- pip install termcolor + +### Optionals +- Change the variable **frame_start** to start the video in a specific frame. +- Change the variable **IMAGE_DIR** to point your video path. +- Change the variable **folderTosave** to point to folder that save the frames. +- Change the variable **thresholdDifference** to change the minimum threshold at compare two frames. + ## differenceBetweenImages.py - Calc the difference between two frames, and save the next frame that difference is less than a threshold @@ -47,4 +62,4 @@ - pip3.5 install --user Pillow==5.2.1 ### Optionals - Change the variable **mypath** to point folder that contains the originals masks -- Change the variable **destPath** to point folder that will saved the changed mask. +- Change the variable **destPath** to point folder that will saved the changed mask. \ No newline at end of file diff --git a/SIFT.py b/SIFT.py new file mode 100644 index 0000000..f3517a9 --- /dev/null +++ b/SIFT.py @@ -0,0 +1,121 @@ +#!/usr/bin/env python +# coding: utf-8 + +# In[1]: + + +#pip install opencv-python==3.4.2.16 +#pip install opencv-contrib-python==3.4.2.16 +#pip install termcolor + + +# In[1]: + + +from PIL import Image +import glob +import cv2 +from termcolor import colored +from os.path import join + + +# In[3]: + + +IMAGE_DIR = join('zip10') +images = glob.glob(f'{IMAGE_DIR}/*.jpg') +folderTosave = "Tosave" +print(f'Found {len(images)} images') +thresholdDifference = 5 +frame_start = -1 + + +# In[5]: + + +def main(): + print("Image Path: %s" % IMAGE_DIR) + for image in images: + img = Image.open(f'{image}') + width, height = img.size[:2] + width, height = int(width), int(height) + + count = -1 + origin = 0 + frameOriginal = 0 + ############################ + #Frame Size + yframeFrom = 0 + yframeTo = height + + xFrameFrom = 0 + xFrameTo = width + + for img in glob.glob(f'{IMAGE_DIR}/*.jpg'): + count+=1 + cap = cv2.imread(img) + ret = cap + print("Frame: "+str(count)) + if count == frame_start+1: + frameOriginal = cap[yframeFrom:yframeTo, xFrameFrom:xFrameTo] + pathToSave = join(folderTosave, "%s.jpg" % (count)) + cv2.imwrite(pathToSave, frameOriginal) + else: + if not ret.any(): + break + frameCompare = cap[yframeFrom:yframeTo,xFrameFrom:xFrameTo] + #if u wanna see what frames r being compared + #uncommente the lines below + #cv2.imshow("Original", frameOriginal) + #cv2.imshow("Comparacao",frameCompare) + #cv2.waitKey(0) + #cv2.destroyAllWindows() + try: + difference = findSimilarities(frameOriginal,frameCompare) + if(difference > thresholdDifference): + print(colored("Accepted",'green')) + pathToSave = join(folderTosave, "%s.jpg" % (count)) + cv2.imwrite(pathToSave, frameCompare) + frameOriginal = frameCompare.copy() + else: + print("Diff expected, less than %s" % str(thresholdDifference)) + print(colored("Denied",'red')) + if cv2.waitKey(1) & 0xFF == ord('q'): + break + print("\n") + except: + continue + +def findSimilarities(original, compare): + sift = cv2.xfeatures2d.SIFT_create() + kp_1, desc_1 = sift.detectAndCompute(original, None) + kp_2, desc_2 = sift.detectAndCompute(compare, None) + + index_params = dict(algorithm=0, trees=5) + search_params = dict() + flann = cv2.FlannBasedMatcher(index_params, search_params) + + matches = flann.knnMatch(desc_1, desc_2, k=2) + + good_points = [] + ratio = 0.6 + for m, n in matches: + if m.distance < ratio*n.distance: + good_points.append(m) + lGoodPoints = len(good_points) + + #TO SEE THE FRAME COMPARED UNCOMMENT THE LINES BELOW + #result = cv2.drawMatches(original, kp_1, compare, kp_2, good_points, None) + #cv2.imshow("result", result) + #cv2.waitKey(0) + #cv2.destroyAllWindows() + return lGoodPoints +if __name__ == '__main__': + main() + + +# In[ ]: + + + + From d44d8f6bdd29d1e3888ea618852df0bb04ed598d Mon Sep 17 00:00:00 2001 From: JoaoPedroRMarcelino <61026979+JoaoPedroRMarcelino@users.noreply.github.com> Date: Mon, 17 Feb 2020 16:23:10 -0300 Subject: [PATCH 2/3] cleanup --- SIFT.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/SIFT.py b/SIFT.py index f3517a9..f74f2ab 100644 --- a/SIFT.py +++ b/SIFT.py @@ -64,8 +64,6 @@ def main(): if not ret.any(): break frameCompare = cap[yframeFrom:yframeTo,xFrameFrom:xFrameTo] - #if u wanna see what frames r being compared - #uncommente the lines below #cv2.imshow("Original", frameOriginal) #cv2.imshow("Comparacao",frameCompare) #cv2.waitKey(0) From 7d29c899b95574c1ab61c24079df6a600d09a5b1 Mon Sep 17 00:00:00 2001 From: Joao <61026979+JoaoPedroRMarcelino@users.noreply.github.com> Date: Tue, 18 Feb 2020 00:06:28 -0300 Subject: [PATCH 3/3] Update README.md --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index efc6a73..be703d2 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,5 @@ ## SIFT.py -- This code will calc the difference between the images, after the code read all the images matrix one by one the function findSimilarities will apply the sift method saving all images that dont repeat it self and discard the rest. +- This code will calc the difference between the images and read all the images matrix one by one, the function findSimilarities will apply the sift method saving all images that dont repeat it self and discard the rest. ### Requirements - Python 2.7 @@ -62,4 +62,4 @@ - pip3.5 install --user Pillow==5.2.1 ### Optionals - Change the variable **mypath** to point folder that contains the originals masks -- Change the variable **destPath** to point folder that will saved the changed mask. \ No newline at end of file +- Change the variable **destPath** to point folder that will saved the changed mask.