Exemplo n.º 1
0
    def loadimg(self, args=()):
        i = args
        self.selected = i
        self.img = cv2.imread(self.imagelistFiles[i])
        self.match.reset()
        #load the labels if the xml file exists
        xmlfilename = self.imagelistFiles[self.selected][:-3] + "xml"
        if os.path.exists(xmlfilename):
            root = ET.parse(xmlfilename).getroot()
            for l in root.findall('object'):
                name = l.find('name')
                #print(name.text)

                bndbox = l.find('bndbox')
                xmin = bndbox.find("xmin")
                ymin = bndbox.find("ymin")
                xmax = bndbox.find("xmax")
                ymax = bndbox.find("ymax")

                x = float(xmin.text)
                y = float(ymin.text)
                w = float(xmax.text) - float(xmin.text)
                h = float(ymax.text) - float(ymin.text)
                try:
                    i = self.actionbar.classes.index(name.text)
                    c = hsv_to_rgb(self.actionbar.colors[i].slideValue / 360,
                                   1, 1)
                except:
                    c = (0, 0, 0)

                loadedlabel = label(self.world, self.match, x, y, w, h,
                                    name.text, c)
                self.match.labels.append(loadedlabel)
            print("loaded xml")
Exemplo n.º 2
0
	def match(self, img, template=None, templatename=""):
		if not template is None:
			self.template=template
		else:
			if self.template is None:
				return
			
		#cv2.imwrite("test.png",template)
		#self.tempautolabels=[]
		templabel=[]
		
		self.templateheight,self.templatewidth,_=self.template.shape
		
		#run template matching, get minimum val
		gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
		gray_template = cv2.cvtColor(self.template,cv2.COLOR_BGR2GRAY)
		
		res = cv2.matchTemplate(gray, gray_template, cv2.TM_CCOEFF_NORMED)#TM_SQDIFF_NORMED
		min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res)
		
		# create threshold from min val, find where sqdiff is less than thresh
		min_thresh = self.accuracyslider.slideValue#.9# (min_val + 1e-6) * 1.5
		match_locations = np.where(res >= min_thresh)
		
		match_locations = (np.array(match_locations).T).tolist()
		match_locations = group_points(match_locations,10)
		
		#get the defaults from the actionbar
		try:
			labeltext = self.controller.actionbar.classes[self.controller.actionbar.defaultmatch]
			labelcolor=hsv_to_rgb(self.controller.actionbar.colors[self.controller.actionbar.defaultmatch].slideValue/360,1,1)
		except:
			labeltext = "NONE"
			labelcolor=(0,0,0)
			
		for m in match_locations:
			l=label(self.world,self.controller,m[1],m[0],self.templatewidth,self.templateheight,labeltext,(0,255,0),accuracy=res[m[0]][m[1]]*100,template=templatename)
			#self.labels.append(l)
			#self.tempautolabels.append(l)
			templabel.append(l)
			
		#print(self.match_locations)
		self.predict_missing_boxes(labeltext,templabel,templatename=templatename)
Exemplo n.º 3
0
    def update(self):
        try:
            self.height, self.width, channels = self.img.shape
        except:
            return

        #update the labels selection
        tempselect = False
        for l in self.labels:
            l.update()
            #deselecting
            if self.world.mouse_left_down:
                if (self.x < self.world.mouse_x < self.x + self.width and
                        self.y < self.world.mouse_y < self.y + self.height):
                    t = l.checkselect()
                    if t:
                        tempselect = True
        if self.world.mouse_left_down:
            if (self.x < self.world.mouse_x < self.x + self.width
                    and self.y < self.world.mouse_y < self.y + self.height):
                if not tempselect:
                    self.updatelabelselect(None)

        #start single select
        if self.world.mouse_left_down:
            if self.selectsinglebox:
                if (self.x < self.world.mouse_x < self.x + self.width and
                        self.y < self.world.mouse_y < self.y + self.height):
                    self.selectbox[0] = self.world.mouse_x
                    self.selectbox[1] = self.world.mouse_y
                    self.selecting = True
                    self.selected = False

        #start multi select
        if self.world.mouse_left_down:
            if self.selectmultibox:
                if (self.x < self.world.mouse_x < self.x + self.width and
                        self.y < self.world.mouse_y < self.y + self.height):
                    self.selectbox[0] = self.world.mouse_x
                    self.selectbox[1] = self.world.mouse_y
                    self.selecting = True
                    self.selected = False
                    self.lines = []
                    self.intersections = []
                    #self.labels=[]
                    self.tempautolabels = []

        if self.selecting:
            self.selectbox[2] = clamp(self.world.mouse_x - self.selectbox[0],
                                      self.x - self.selectbox[0],
                                      self.x + self.width - self.selectbox[0])
            self.selectbox[3] = clamp(self.world.mouse_y - self.selectbox[1],
                                      self.y - self.selectbox[1],
                                      self.y + self.height - self.selectbox[1])

            #single
            if self.selectsinglebox:
                if self.world.mouse_left_up:
                    self.selecting = False
                    self.selectmultibox = False
                    if abs(self.selectbox[2]) > 0 and abs(
                            self.selectbox[3]) > 0:
                        self.selected = True
                        #create the label
                        #if not self.controller.actionbar.classes==None:
                        if not self.controller.actionbar.selected == None:
                            labeltext = self.controller.actionbar.classes[
                                self.controller.actionbar.selected]
                            labelcolor = hsv_to_rgb(
                                self.controller.actionbar.colors[
                                    self.controller.actionbar.selected].
                                slideValue / 360, 1, 1)
                        else:
                            labeltext = "None"
                            labelcolor = (0, 0, 0)

                        l = label(self.world, self, self.selectbox[0] - self.x,
                                  self.selectbox[1] - self.y,
                                  self.selectbox[2], self.selectbox[3],
                                  labeltext, labelcolor)
                        self.labels.append(l)
                        self.selectsinglebox = False
                        #select the label that was just created
                        self.updatelabelselect(l)
                        l.selected = True
                    else:
                        self.selected = False
                        return
            #multi
            if self.selectmultibox:
                if self.world.mouse_left_up:
                    self.selecting = False
                    self.selectmultibox = False
                    if abs(self.selectbox[2]) > 0 and abs(
                            self.selectbox[3]) > 0:
                        self.selected = True
                    else:
                        self.selected = False
                        return
                    #create the template
                    if self.selectbox[2] > 0:
                        x1 = self.selectbox[0] - self.x
                        x2 = self.selectbox[0] + self.selectbox[2] - self.x
                    else:
                        x1 = self.selectbox[0] + self.selectbox[2] - self.x
                        x2 = self.selectbox[0] - self.x
                    if self.selectbox[3] > 0:
                        y1 = self.selectbox[1] - self.y
                        y2 = self.selectbox[1] + self.selectbox[3] - self.y
                    else:
                        y1 = self.selectbox[1] + self.selectbox[3] - self.y
                        y2 = self.selectbox[1] - self.y
                    self.template = self.img[y1:y2, x1:x2]

                    #run template matching, get minimum val
                    gray = cv2.cvtColor(self.img, cv2.COLOR_BGR2GRAY)
                    template = cv2.cvtColor(self.template, cv2.COLOR_BGR2GRAY)
                    res = cv2.matchTemplate(
                        gray, template,
                        cv2.TM_CCOEFF_NORMED)  #TM_SQDIFF_NORMED
                    min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res)

                    # create threshold from min val, find where sqdiff is less than thresh
                    min_thresh = .9  # (min_val + 1e-6) * 1.5
                    match_locations = np.where(res >= min_thresh)

                    match_locations = (np.array(match_locations).T).tolist()
                    match_locations = group_points(match_locations, 10)

                    #get the defaults from the actionbar
                    try:
                        labeltext = self.controller.actionbar.classes[
                            self.controller.actionbar.defaultmatch]
                        labelcolor = hsv_to_rgb(
                            self.controller.actionbar.colors[
                                self.controller.actionbar.defaultmatch].
                            slideValue / 360, 1, 1)
                    except:
                        labeltext = "NONE"
                        labelcolor = (0, 0, 0)

                    for m in match_locations:
                        l = label(self.world, self, m[1], m[0],
                                  self.selectbox[2], self.selectbox[3],
                                  labeltext, (0, 255, 0))
                        #self.labels.append(l)
                        self.tempautolabels.append(l)

                    #print(self.match_locations)

                    self.predict_missing_boxes(labeltext)
Exemplo n.º 4
0
    def predict_missing_boxes(self, goodlabeltext):
        #get the defaults from the actionbar
        try:
            labeltext = self.controller.actionbar.classes[
                self.controller.actionbar.defaultmiss]
            labelcolor = hsv_to_rgb(
                self.controller.actionbar.colors[
                    self.controller.actionbar.defaultmiss].slideValue / 360, 1,
                1)
        except:
            labeltext = "NONE"
            labelcolor = (0, 0, 0)

        for p in self.tempautolabels:
            x = p.x
            y = p.y
            #print(str(x)+","+str(y))
            hline = line(self.world, self.x, self.y, [x, x + 10], [y, y])
            vline = line(self.world, self.x, self.y, [x, x], [y, y + 10])
            self.lines.append(hline)
            self.lines.append(vline)

        #combine the overlapping lines
        slopethresh = .005
        interceptthresh = 15
        for i in range(len(self.lines)):
            for j in range(len(self.lines)):
                if not i == j:
                    try:
                        #for vertical lines
                        if (isNaN(self.lines[i].slope)
                                and isNaN(self.lines[j].slope)):
                            if np.mean(
                                    self.lines[i].xlist
                            ) - interceptthresh < np.mean(
                                    self.lines[j].xlist) < np.mean(
                                        self.lines[i].xlist) + interceptthresh:
                                self.lines[i].xlist[0] = (
                                    self.lines[i].xlist[0] +
                                    self.lines[i].xlist[1]) / 2
                                del self.lines[j]
                    except Exception as e:
                        #print(e)
                        pass
                    try:
                        if not (isNaN(self.lines[i].slope)
                                and isNaN(self.lines[j].slope)):
                            if self.lines[i].slope - slopethresh < self.lines[
                                    j].slope < self.lines[
                                        i].slope + slopethresh:
                                if self.lines[
                                        i].intercept - interceptthresh < self.lines[
                                            j].intercept < self.lines[
                                                i].intercept + interceptthresh:
                                    self.lines[i].slope = (
                                        self.lines[i].slope +
                                        self.lines[j].slope) / 2
                                    self.lines[i].intercept = (
                                        self.lines[i].intercept +
                                        self.lines[j].intercept) / 2
                                    del self.lines[j]
                    except Exception as e:
                        #print(e)
                        pass
        print("total lines: " + str(len(self.lines)))

        #find the intersect of all the lines
        '''
		1. go through the lines
		2. check if all the other lines dont intersect, that is if they have a different slope
		'''
        colnum = 0
        for l1 in self.lines:
            for l2 in self.lines:
                if l1 != l2:
                    #get the intersection
                    x = intersection(l1, l2)
                    p = []
                    if not x == None:
                        if not isNaN(l1.slope):
                            p = [x, l1.returny(x)]
                        else:
                            p = [x, l2.returny(x)]
                    if p:
                        if not p in self.intersections:
                            self.intersections.append(p)
        print("total intersections: " + str(len(self.intersections)))

        #find the missing points (intersections with no prediction box) with tolerance
        tolerance = 5
        for p in self.intersections:
            #point = [p[1],p[0]]
            inMatch = False
            #for a in self.match_locations:
            for a in self.tempautolabels:
                if a.label == goodlabeltext:  #"match":
                    x = a.x
                    y = a.y
                    if (p[0] - tolerance < x < p[0] + tolerance
                            and p[1] - tolerance < y < p[1] + tolerance):
                        #if not point in self.match_locations:
                        inMatch = True
            if not inMatch:
                l = label(self.world, self, p[0], p[1], self.selectbox[2],
                          self.selectbox[3], labeltext, labelcolor)
                self.tempautolabels.append(l)
                #self.miss_locations.append(point)
        print("total labels: " + str(len(self.tempautolabels)))
        #add the temp labels and the labels, delete the temp labels
        self.labels = self.labels + self.tempautolabels
        return self.tempautolabels
Exemplo n.º 5
0
    def find(self, labels, img):
        self.img = img
        #loop through the labels and figure out what labels are in the rows
        rows = []
        for l1 in self.controller.labels:
            if not self.inrow(rows, l1):
                temp = [l1]
                for l2 in self.controller.labels:
                    if not l1 == l2:
                        if not self.inrow(rows, l2):
                            if ((l1.y <= l2.y <= l1.y + l1.h - 10)
                                    or (l2.y <= l1.y <= l2.y + l2.h - 10)):
                                temp.append(l2)
                rows.append(temp)
        #sort the rows
        rows = [sorted(r, key=lambda l: l.x) for r in rows]

        #filter out all the rows with only one template label in them
        temp = []
        for r in rows:
            template1 = False
            template2 = False
            for l in r:
                if l.template == "square.png":
                    template1 = True
                if l.template == "test.png":
                    template2 = True
            if (template1 and template2):
                temp.append(r)
        rows = temp

        #make the labels for the rows
        for r in rows:
            #print(["("+str(l.y)+","+str(l.h)+")" for l in r])
            for i in range(len(r) - 1):

                #seperate
                if r[i].template != "square.png":
                    #underneath the squiggly part
                    xx = r[i].x
                    ww = r[i].w
                    yy = r[i].y + r[i].h
                    hh = r[i].h * .55

                    l = label(self.world, self.controller, xx, yy, ww, hh, "1",
                              (241, 255, 0))
                    self.tempautolabels.append(l)

                    #middle section, 3 parts
                    xx = r[i].x + r[i].w
                    ww = r[i + 1].x - xx

                    d = .48
                    d1 = .3

                    miny = min(r[i].y, r[i + 1].y) + (
                        max(r[i].y + r[i].h, r[i + 1].y + r[i + 1].h) -
                        min(r[i].y, r[i + 1].y)) * .15
                    totalh = (max(r[i].y + r[i].h, r[i + 1].y + r[i + 1].h) -
                              miny)

                    yy1 = miny
                    hh1 = totalh * d

                    yy2 = yy1 + hh1
                    hh2 = hh1 * d1  #hh1*(1-d)

                    yy3 = yy2 + hh2
                    hh3 = (totalh * 1.07) - hh2 - hh1

                    l = label(self.world, self.controller, xx, yy1, ww, hh1,
                              "2", (241, 51, 255))
                    self.tempautolabels.append(l)

                    l = label(self.world, self.controller, xx, yy2, ww, hh2,
                              "3", (64, 255, 249))
                    self.tempautolabels.append(l)

                    l = label(self.world, self.controller, xx, yy3, ww, hh3,
                              "4", (163, 32, 171))
                    self.tempautolabels.append(l)

                else:
                    xx = r[i].x + r[i].w + (r[i + 1].x - r[i].x +
                                            r[i].w) * .025
                    yy = min(r[i].y, r[i + 1].y) + (
                        max(r[i].y + r[i].h, r[i + 1].y + r[i + 1].h) -
                        min(r[i].y, r[i + 1].y)) * .15

                    ww = r[i + 1].x - xx
                    hh = (max(r[i].y + r[i].h, r[i + 1].y + r[i + 1].h) - yy)

                    #yy1=yy-hh*.05
                    #hh1=hh*95

                    l = label(self.world, self.controller, xx, yy, ww, hh, "5",
                              (0, 0, 255))
                    self.tempautolabels.append(l)

        #check the labels for defects
        for l in self.tempautolabels:
            self.defectedAreaCheck(l)