-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
136 lines (108 loc) · 4.36 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import cv2
import numpy as np
from argparse import ArgumentParser
def imageMode(args, threshold):
# Get templates for landmarks
landmarkFile = open('./pcb/'+args.pcb+'.txt', 'r')
landmarks = []
for line in landmarkFile:
# (x,y) \ ./landmarkLib/<type>/<name>.jpg
line = line.rstrip('\n')
pos, templatePath = line.split(' | ')
nameArr = templatePath.split('/')
name = nameArr[-1]
nameArr = name.split('.')
name = nameArr[0]
template = cv2.imread(templatePath, 0)
landmarks.append({'pos':pos,'template':template,'name':name})
# Open PCB image file
im = cv2.imread('./pcb/'+args.pcb+'.jpg')
img = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)
# All the 6 methods for comparison in a list
methods = ['cv2.TM_CCOEFF', 'cv2.TM_CCOEFF_NORMED', 'cv2.TM_CCORR',
'cv2.TM_CCORR_NORMED', 'cv2.TM_SQDIFF', 'cv2.TM_SQDIFF_NORMED']
detectedLandmarks = []
for landmark in landmarks:
# print(landmark)
template = landmark['template']
w, h = template.shape[::-1]
# Apply template Matching
res = cv2.matchTemplate(img,template,cv2.TM_CCOEFF_NORMED)
loc = np.where(res >= threshold)
possiblePt = []
for pt in zip(*loc[::-1]):
possiblePt.append(pt)
cv2.rectangle(im, pt, (pt[0] + w, pt[1] + h), (0,255,255), 2)
detectedLandmarks.append({'landmark':landmark['name'],
'expectedPos':landmark['pos'],
'actualPosArr':possiblePt})
print('Wrapping up detection...')
# cv2.startWindowThread()
cv2.namedWindow("Detected")
imSized = cv2.resize(im, (1040,480))
cv2.imshow('Detected',imSized)
# cv2.resizeWindow('Detected', 600,600)
cv2.waitKey(0)
def videoMode(args, threshold):
# Get templates for landmarks
landmarkFile = open('./pcb/'+args.pcb+'.txt', 'r')
landmarks = []
for line in landmarkFile:
# (x,y) \ ./landmarkLib/<type>/<name>.jpg
line = line.rstrip('\n')
pos, templatePath = line.split(' | ')
nameArr = templatePath.split('/')
name = nameArr[-1]
nameArr = name.split('.')
name = nameArr[0]
template = cv2.imread(templatePath, 0)
landmarks.append({'pos':pos,'template':template,'name':name})
# Open PCB video
cap = cv2.VideoCapture('./pcb/'+args.pcb+'.m4v')
while(True):
ret, frame = cap.read()
img = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
detectedLandmarks = []
for landmark in landmarks:
# print(landmark)
template = landmark['template']
w, h = template.shape[::-1]
# Apply template Matching
res = cv2.matchTemplate(img,template,cv2.TM_CCOEFF_NORMED)
loc = np.where(res >= threshold)
possiblePt = []
for pt in zip(*loc[::-1]):
possiblePt.append(pt)
cv2.rectangle(frame, pt, (pt[0] + w, pt[1] + h), (0,255,255), 2)
detectedLandmarks.append({'landmark':landmark['name'],
'expectedPos':landmark['pos'],
'actualPosArr':possiblePt})
print('Wrapping up detection...')
# cv2.startWindowThread()
cv2.namedWindow("Detected")
imSized = cv2.resize(frame, (700,1000))
cv2.imshow('Detected',imSized)
# cv2.resizeWindow('Detected', 600,600)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
if __name__ == "__main__":
print('Initializing...')
# Parse Arguments
parser = ArgumentParser()
parser.add_argument("-p", "--pcb", dest="pcb",
help="The PCB image to detect landmarks of",
metavar="QUEUE")
parser.add_argument("-m", "--mode", dest="mode",
help="'image' or 'video'",
metavar="LENGTH")
parser.add_argument("-t", "--threshold", dest="threshold",
help="File name containing names/pos of landmarks to template match. x,y | relative to project root",
metavar="LENGTH")
args = parser.parse_args()
threshold = float(args.threshold)
if args.mode == 'image':
imageMode(args, threshold)
if args.mode == 'video':
videoMode(args, threshold)