|
| 1 | +from sklearn.cluster import DBSCAN |
| 2 | +import numpy as np |
| 3 | +import cv2 |
| 4 | + |
| 5 | + |
| 6 | +class Detect(object): |
| 7 | + def __init__(self, input): |
| 8 | + self.image = cv2.imread(input) |
| 9 | + |
| 10 | + def siftDetector(self): |
| 11 | + sift = cv2.SIFT_create() |
| 12 | + # sift = cv2.xfeatures2d.SIFT_create() |
| 13 | + gray = cv2.cvtColor(self.image, cv2.COLOR_BGR2GRAY) |
| 14 | + self.key_points, self.descriptors = sift.detectAndCompute(gray, None) |
| 15 | + return self.key_points, self.descriptors |
| 16 | + |
| 17 | + def showSiftFeatures(self): |
| 18 | + gray_image = cv2.cvtColor(self.image, cv2.COLOR_BGR2GRAY) |
| 19 | + sift_image = cv2.drawKeypoints( |
| 20 | + self.image, self.key_points, self.image.copy()) |
| 21 | + return sift_image |
| 22 | + |
| 23 | + def locateForgery(self, eps=40, min_sample=2): |
| 24 | + clusters = DBSCAN(eps=eps, min_samples=min_sample).fit( |
| 25 | + self.descriptors) |
| 26 | + size = np.unique(clusters.labels_).shape[0]-1 |
| 27 | + forgery = self.image.copy() |
| 28 | + if (size == 0) and (np.unique(clusters.labels_)[0] == -1): |
| 29 | + print('No Forgery Found!!') |
| 30 | + return None |
| 31 | + if size == 0: |
| 32 | + size = 1 |
| 33 | + cluster_list = [[] for i in range(size)] |
| 34 | + for idx in range(len(self.key_points)): |
| 35 | + if clusters.labels_[idx] != -1: |
| 36 | + cluster_list[clusters.labels_[idx]].append( |
| 37 | + (int(self.key_points[idx].pt[0]), int(self.key_points[idx].pt[1]))) |
| 38 | + for points in cluster_list: |
| 39 | + if len(points) > 1: |
| 40 | + for idx1 in range(1, len(points)): |
| 41 | + # Green color in BGR |
| 42 | + cv2.line(forgery, points[0], points[idx1], (0, 255, 0), 5) |
| 43 | + # cv2.line(forgery, points[0], points[idx1], (255, 0, 0), 5) |
| 44 | + return forgery |
0 commit comments