def imageSplit(self): import MainApp.conf as conf self.splitPoints = eval(str(conf.get('splitPoints'))) if len(self.splitPoints) != 4: print "Board not detected!" rows, cols, ch = self.img.shape pts1 = np.float32(self.splitPoints) pts2 = np.float32([[0, 0], [600, 0], [0, 600], [600, 600]]) M = cv2.getPerspectiveTransform(pts1, pts2) self.trimmed = cv2.warpPerspective(self.img, M, (600, 600)) #~~~~~~~~~~~~~~IMAGE ROTATION~~~~~~~~~~~~ rows, cols, depth = self.trimmed.shape matrix = cv2.getRotationMatrix2D((cols / 2, rows / 2), 45 * int(conf.get('rotate')), 1) self.trimmed = cv2.warpAffine(self.trimmed, matrix, (cols, rows)) #~~~~~~~~~~~~~~~generating edges image~~~~~~~~~~~~ self.edgesImage = cv2.cvtColor(self.trimmed, cv2.COLOR_BGR2GRAY) self.edgesImage = cv2.medianBlur(self.edgesImage, 5) self.edgesImage = cv2.GaussianBlur(self.edgesImage, (3, 3), 0) #self.edgesImage = copy.deepcopy(image) self.edgesImage = cv2.Canny(self.edgesImage, float(conf.get('param1')), float(conf.get('param2'))) #90, 200 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ self.normalImage = copy.deepcopy(self.trimmed) #~~~~~ROTATING FOR ALGORITHM:~~~~~~~~~~~~~~~~~~~~~~ rows, cols, depth = self.trimmed.shape matrix = cv2.getRotationMatrix2D((cols / 2, rows / 2), -180, 1) self.trimmed = cv2.warpAffine(self.trimmed, matrix, (cols, rows)) points = [] for i in range(1, 602, 75): for j in range(1, 602, 75): points.append([j, i]) dic = {} for i in range(0, 9): for j in range(0, 9): dic[i, j] = points[9 * i + j] for i in xrange(8): for j in xrange(8): if (i + j) % 2 == 1: x1 = dic[i, j][0] y1 = dic[i, j][1] x2 = dic[i + 1, j + 1][0] y2 = dic[i + 1, j + 1][1] self.FieldTable[i][j] = self.trimmed[y1:y2, x1:x2] for i in xrange(8): for j in xrange(8): x1 = dic[i, j][0] y1 = dic[i, j][1] x2 = dic[i + 1, j + 1][0] y2 = dic[i + 1, j + 1][1] cv2.circle(self.trimmed, (x1, y1), 2, (0, 0, 255)) cv2.circle(self.trimmed, (x2, y2), 2, (0, 0, 255))
def configButton_click(self): import MainApp.conf as Conf log("opening configuration window") ui = configWindow.configWindow(self) ui.WorkingThread = self.thread ui.lineEdit.setText(config.get("IP")) ui.colorThresholdVal.setValue(int(config.get("threshold"))) ui.p1.setValue(int(Conf.get("param1"))) ui.p2.setValue(int(Conf.get("param2"))) if ui.exec_(): config.set("IP", ui.lineEdit.text()) config.set("threshold", ui.colorThresholdVal.value())
def take_photo(): import MainApp.conf as Conf import urllib global success log("Connecting... ",line=False) try: urllib.urlretrieve("http://" + str(Conf.get('IP')) + ":8080/shot.jpg", "shot.jpg") except Exception as ex: success = False log("Cannot connect!" + str(ex), error=True,time=False) return False else: log("OK",False) success = True return True
def take_photo(): import MainApp.conf as Conf import urllib global success log("Connecting... ", line=False) try: urllib.urlretrieve("http://" + str(Conf.get('IP')) + ":8080/shot.jpg", "shot.jpg") except Exception as ex: success = False log("Cannot connect!" + str(ex), error=True, time=False) return False else: log("OK", False) success = True return True
#!/usr/bin/env python import socket import MainApp.conf as conf try: s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.connect(('127.0.0.1', int(conf.get('TCP_PORT')))) except: print "TCP connection failed!" def close(): s.close() def send_move(x1, y1, x2, y2): b = 7 - x1 a = 7 - y1 d = 7 - x2 c = 7 - y2 if c == -1: #beats c = 8 d = 0 if x2 == 8: #camera position a = 0 b = 0 c = 8
#!/usr/bin/env python import socket import MainApp.conf as conf try: s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.connect(('127.0.0.1', int(conf.get('TCP_PORT')))) except: print "TCP connection failed!" def close(): s.close() def send_move(x1, y1, x2, y2): b = 7 - x1 a = 7 - y1 d = 7 - x2 c = 7 - y2 if c == -1: #beats c = 8 d = 0 if x2 == 8: #camera position a = 0 b = 0
def searchForPawn(self, img, pos, returnColorValue=False): import MainApp.conf as conf """function returns: 0 - no figure 1 - white -1 - black""" img2 = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) img2 = cv2.medianBlur(img2, 5) edges = cv2.GaussianBlur(img2, (3, 3), 0) edges = cv2.Canny(edges, float(conf.get('param1')), float(conf.get('param1'))) #show_image(edges) x, dst = cv2.threshold(img2, 50, 255, cv2.THRESH_BINARY) #show_image(dst) circles = cv2.HoughCircles(edges, cv2.cv.CV_HOUGH_GRADIENT, 1, 20, param1=70, param2=20, minRadius=10, maxRadius=0) #print circles if circles is None: #or len(circles[0]) > 1: #print "NO_PAWN" return 0 circles = np.uint16(np.around(circles)) i = circles[0][0] d = int(i[2] / 1.6) x = i[0] y = i[1] #cv2.rectangle(img, (x - d, y - d), (x + d, y + d), (0, 255, 0)) sums = [0.0, 0.0, 0.0] divi = 0 for row in dst[x - d:x + d, y - d:y + d]: for px in row: sums += px divi += 1 #if divi != 0: sum = [i/divi for i in sum] try: sums /= divi except TypeError: pass #print circles #print "|!!!| ", sums, divi suma = sums[0] + sums[1] + sums[2] #print "KOLOR: ",suma if returnColorValue: return suma thr = int(conf.get('threshold')) #print thr, suma if suma > thr: #print "WHITE PAWN" cv2.circle(img, (i[0], i[1]), i[2], (0, 255, 0), 2) cv2.circle(self.edgesImage, (i[0], i[1]), i[2], (0, 255, 0), 2) else: #print "BLACK PAWN" cv2.circle(img, (i[0], i[1]), i[2], (0, 0, 255), 2) cv2.circle(self.edgesImage, (i[0], i[1]), i[2], (0, 0, 255), 2) #cv2.imshow('detected circles', img) #cv2.waitKey(0) #cv2.destroyAllWindows() if suma > thr: ##self.Threshold[pos[0]][pos[1]]: return 1 else: return -1
def searchForPawn(self, img, pos, returnColorValue=False): import MainApp.conf as conf """function returns: 0 - no figure 1 - white -1 - black""" img2 = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) img2 = cv2.medianBlur(img2, 5) edges = cv2.GaussianBlur(img2, (3, 3), 0) edges = cv2.Canny(edges, float(conf.get('param1')), float(conf.get('param1'))) #show_image(edges) x, dst = cv2.threshold(img2, 50, 255, cv2.THRESH_BINARY) #show_image(dst) circles = cv2.HoughCircles(edges, cv2.cv.CV_HOUGH_GRADIENT, 1, 20, param1=70, param2=20, minRadius=10, maxRadius=0) #print circles if circles is None: #or len(circles[0]) > 1: #print "NO_PAWN" return 0 circles = np.uint16(np.around(circles)) i = circles[0][0] d = int(i[2] / 1.6) x = i[0] y = i[1] #cv2.rectangle(img, (x - d, y - d), (x + d, y + d), (0, 255, 0)) sums = [0.0, 0.0, 0.0] divi = 0 for row in dst[x - d:x + d, y - d:y + d]: for px in row: sums += px divi += 1 #if divi != 0: sum = [i/divi for i in sum] try: sums /= divi except TypeError: pass #print circles #print "|!!!| ", sums, divi suma = sums[0] + sums[1] + sums[2] #print "KOLOR: ",suma if returnColorValue: return suma thr = int(conf.get('threshold')) #print thr, suma if suma > thr: #print "WHITE PAWN" cv2.circle(img, (i[0], i[1]), i[2], (0, 255, 0), 2) cv2.circle(self.edgesImage, (i[0], i[1]), i[2], (0, 255, 0), 2) else: #print "BLACK PAWN" cv2.circle(img, (i[0], i[1]), i[2], (0, 0, 255), 2) cv2.circle(self.edgesImage, (i[0], i[1]), i[2], (0, 0, 255), 2) #cv2.imshow('detected circles', img) #cv2.waitKey(0) #cv2.destroyAllWindows() if suma > thr:##self.Threshold[pos[0]][pos[1]]: return 1 else: return -1
def on_rightButton_clicked(self): x = int(conf.get('rotate')) conf.set('rotate', x - 1)
def on_leftButton_clicked(self): x = int(conf.get('rotate')) conf.set('rotate', x + 1)