8
8
import matplotlib .pyplot as plt
9
9
import os
10
10
import numpy as np
11
+ import json
12
+ from tqdm import tqdm
11
13
12
14
base_dir = os .getcwd ()
13
15
data_folder = os .path .join (base_dir , "Dataset" )
16
18
crops_folder = os .path .join (data_folder , "Crops" )
17
19
sample_testset = os .path .join (data_folder , "sample_testset" )
18
20
21
+ sample_json_result = os .path .join (sample_testset , "sample_result.json" )
19
22
sample_images = os .path .join (sample_testset , "images" )
20
23
sample_crops = os .path .join (sample_testset , "crops" )
21
24
@@ -32,9 +35,23 @@ def ModifiedFLANN(img1, img2):
32
35
kp2 , des2 = sift .detectAndCompute (img2 , None )
33
36
34
37
orgBorder = None
38
+ flannMatch = True
35
39
36
40
if (des1 is None ) or (des2 is None ):
37
- return orgBorder
41
+ flannMatch = False
42
+
43
+ if (img2 .shape [0 ] > img1 .shape [0 ]) and (img2 [1 ].shape [1 ] > img1 .shape [1 ]):
44
+ res = cv2 .matchTemplate (img2 ,img1 , cv2 .TM_CCOEFF )
45
+ min_val , max_val , min_loc , max_loc = cv2 .minMaxLoc (res )
46
+ h , w , c = img1 .shape
47
+
48
+ pts = [int (min_loc [0 ]), int (min_loc [0 ]) + w ,
49
+ int (max_loc [0 ]), int (max_loc [1 ])]
50
+
51
+ return flannMatch , pts
52
+
53
+ return flannMatch , orgBorder
54
+
38
55
flann = cv2 .FlannBasedMatcher (index_params , search_params )
39
56
matches = flann .knnMatch (des1 , des2 , k = 2 )
40
57
@@ -58,37 +75,33 @@ def ModifiedFLANN(img1, img2):
58
75
59
76
H , status = cv2 .findHomography (cropImg , orgImg , cv2 .RANSAC , 3.0 )
60
77
78
+ if H is None :
79
+ return flannMatch , orgBorder
80
+
61
81
h , w , c = img1 .shape
62
82
63
83
cropBorder = np .float32 ([[[0 ,0 ], [0 ,h - 1 ], [w - 1 ,h - 1 ], [w - 1 ,0 ]]])
84
+
64
85
orgBorder = cv2 .perspectiveTransform (cropBorder , H )
65
86
66
87
cv2 .polylines (img2 , [np .int32 (orgBorder )], True , (0 , 255 , 0 ), 5 )
67
-
68
- else :
69
- print ("No Good Matches Found" )
70
-
71
- matched = cv2 .cvtColor (img2 , cv2 .COLOR_BGR2RGB )
72
- plt .imshow (matched )
73
- plt .show ()
74
-
75
- return orgBorder
88
+
89
+ return flannMatch , orgBorder
76
90
77
91
def findMinMax (border ):
78
92
x , y = np .transpose (border )[0 ], np .transpose (border )[1 ]
79
93
80
- x1 , x2 = x .min (), x .max ()
94
+ x1 , x2 = int ( x .min ()), int ( x .max () )
81
95
82
- y1 , y2 = y .min (), x .max ()
96
+ y1 , y2 = int ( y .min ()), int ( x .max () )
83
97
84
- return ( x1 , y1 , x2 , y2 )
98
+ return [ x1 , y1 , x2 , y2 ]
85
99
86
100
completeTracker = {}
87
101
88
- allImages = os .listdir (sample_images )
89
- allcropImages = os .listdir (sample_crops )
102
+ noAssociationCropImages = os .listdir (sample_crops )
90
103
91
- for imagefile in os .listdir (sample_images ):
104
+ for imagefile in tqdm ( os .listdir (sample_images ) ):
92
105
img = cv2 .imread (os .path .join (sample_images ,
93
106
imagefile ))
94
107
imageTracker = []
@@ -97,20 +110,31 @@ def findMinMax(border):
97
110
crop_img = cv2 .imread (os .path .join (sample_crops ,
98
111
cropfile ))
99
112
100
- crop_border = ModifiedFLANN (crop_img , img )
101
-
102
- if crop_border is None :
103
- print ("Images are Not associated" )
113
+ flannMatch , crop_border = ModifiedFLANN (crop_img , img )
104
114
115
+ if flannMatch :
116
+ if crop_border is not None :
117
+ pts = findMinMax (crop_border [0 ])
118
+ imageTracker .append ((cropfile .replace (".jpg" , "" ), pts ))
119
+ if cropfile in noAssociationCropImages :
120
+ noAssociationCropImages .remove (cropfile )
105
121
else :
106
- print ("images are Associated" )
107
- pts = findMinMax (crop_border [0 ])
108
- imageTracker .append ((cropfile .replace (".jpg" , "" ), pts ))
122
+ if crop_border is not None :
123
+ imageTracker .append ((cropfile .replace (".jpg" , "" ), crop_border ))
124
+ if cropfile in noAssociationCropImages :
125
+ noAssociationCropImages .remove (cropfile )
109
126
110
127
completeTracker [imagefile .replace (".jpg" , "" )] = imageTracker
111
- # =============================================================================
112
- # plt.imshow(cv2.cvtColor(crop_img, cv2.COLOR_BGR2RGB))
113
- # plt.show()
114
- #
115
- # =============================================================================
116
128
129
+ NA_Crops = []
130
+
131
+ for crop in noAssociationCropImages :
132
+ NA_Crops .append ([crop .replace (".jpg" , "" ), []])
133
+
134
+ completeTracker ["NA" ] = NA_Crops
135
+
136
+ with open (sample_json_result , "w" ) as f :
137
+ json .dump (completeTracker , f )
138
+
139
+
140
+ print ("Output Json File is generated" )
0 commit comments