def detect_glyph(image, hBox, vBox, game): augmented = Augmented() board = Board(image) board.start(size = 4) if board.hasTui(): tuis = board.getTuis(getClassifier()) board.causalBoard, causalTuis = feedCausal(board, tuis, hBox, vBox) causalOutput(board, causalTuis) game.setArea(board.causalBoard) display(board) cv2.namedWindow('score', cv2.WINDOW_NORMAL) cv2.imshow('score', game.getScoreBoard()) if cv2.waitKey(1) & 0xFF == ord(' '): # end subround game.endSubRound(causalTuis[:]) return augmented.start(board, causalTuis) else: display(board) if cv2.waitKey(1) & 0xFF == ord(' '): # end subround self.game.endSubRound(causalTuis[:]) return []
def main(): #camera setup cap = cv2.VideoCapture(0) print 'Video resolution: '+' x '.join(set_res(cap,1280,720)) tuis = [] hBox = causalBox(winSize = 10) vBox = causalBox(winSize = 10) ret = True clf = getClassifier() plyerName = ["John","Doe","Tommy","Emmanuel"] game = GameCtrler(plyerName) causalTuis = None while ret: now = time.time() # get the time ret, rawImg = cap.read() if ret is False: print "Error aquring image" break board = Board(rawImg) board.getA4(size = 4) board.getCircle() # setup causal Box lenNeighbor = np.round(board.size*210/14) vBox.setThresh(lenNeighbor) hBox.setThresh(lenNeighbor) # check if circles in A4 if board.horizontalCircles is not None or board.verticalCircles is not None: board.drawCircles() tuis = board.getTuis() hTuiTmp = [] vTuiTmp = [] for tui in tuis: # latter + name tui.getLetter() if tui.letter.size == 0: print 'letter size 0' else: letterPercentage = float(np.count_nonzero(tui.letter))/tui.letter.size*100.0 if letterPercentage > 3.9: # if letter area is more than 3.9% of img tui.getHuMoment() tui.getTuiName(clf) # split h and v tuis for feed in causalbox if tui.position[0] == 'h': hTuiTmp.append((tui.position[1],tui.name)) elif tui.position[0] == 'v': vTuiTmp.append((tui.position[1],tui.name)) else: print "invalid h/v tui position" hBox.feedIn(hTuiTmp) # feed causalbox vBox.feedIn(vTuiTmp) # choose v or h causalBoard = board.vertical.copy() if len(vBox.tuis) > len(hBox.tuis) else board.horizontal.copy() causalTuis = vBox.tuis[:] if len(vBox.tuis) > len(hBox.tuis) else hBox.tuis[:] game.setArea(causalBoard) #set player area for i in causalTuis: cv2.circle(causalBoard ,i.position,np.round(board.size*210/14),(0,255,0),2) # draw the outer circle cv2.circle(causalBoard ,i.position,2,(0,0,255),3) # draw the center of the circle if i.isLegit: cv2.putText(causalBoard, i.getVotedName(), i.position, cv2.FONT_HERSHEY_SIMPLEX, 1,(255,0,0),4) # write name on img cv2.namedWindow('a', cv2.WINDOW_NORMAL) cv2.imshow('a',causalBoard) cv2.namedWindow('score', cv2.WINDOW_NORMAL) cv2.imshow('score',game.getScoreBoard()) else: print 'No circle in both hor and ver a4' # display cv2.namedWindow('thresh', cv2.WINDOW_NORMAL) cv2.imshow('thresh',board.thresh) cv2.namedWindow('boardDetect', cv2.WINDOW_NORMAL) cv2.imshow('boardDetect',board.boardDetect) # condition for exit # if len(tuis) > 0: # ret = stop(cv2.waitKey(1), tuis) # else: # ret = stop(cv2.waitKey(1)) if causalTuis is None: ret = playerDebug(cv2.waitKey(1), game) else: ret = playerDebug(cv2.waitKey(1), game, causalTuis) # time controller elapsed = time.time() - now # how long was it running? if elapsed < 0.2: time.sleep(0.2-elapsed) # sleep accordingly so the full iteration takes 1 second elapsed = time.time() - now # how long was it running? # print str(elapsed)+":", cv2.destroyAllWindows() return 0