Skip to content

Instantly share code, notes, and snippets.

View guilhermemauro's full-sized avatar
🎯
Focusing

Guilherme Mauro guilhermemauro

🎯
Focusing
View GitHub Profile
import numpy as np
import cv2
import tictactoe as tic
import time
#choose X and O color for color detection
CARD_COLOR_UP = np.array([255,255,255],dtype="uint8")
CARD_COLOR_LOW = np.array([150,150,150],dtype="uint8")
#font to cv2.putText
#like the function's name, choose the biggest contour in mask
def findBiggestContour(mask):
temp_bigger = []
img1, cont, hier = cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
if len(cont) == 0:
return False
for cnt in cont:
temp_bigger.append(cv2.contourArea(cnt))
greatest = max(temp_bigger)
index_big = temp_bigger.index(greatest)
#cut image staying just the field choosed
def getField(coord, frame):
crop_img = frame[coord[0]:coord[1], coord[2]:coord[3]]
return crop_img
#find X and/or O with color detection in cut image
def getContoursHSV(img):
mask = cv2.inRange(cv2.cvtColor(img, cv2.COLOR_BGR2HSV), CARD_COLOR_LOW, CARD_COLOR_UP)
if len(mask) > 0:
dilation = cv2.dilate(mask,KERNEL,iterations = 2)
#compare contour founded with the pattern in X.png and O.png files
def compareContourHSV(cnt):
global player, computer
if cnt is False:
return False
patternX = cv2.cvtColor(cv2.imread('X.png'), cv2.COLOR_BGR2GRAY)
patternX = findBiggestContour(patternX)
resX = cv2.matchShapes(patternX, cnt, 1, 0.0)
patternO = cv2.cvtColor(cv2.imread('O.png'), cv2.COLOR_BGR2GRAY)
patternO = findBiggestContour(patternO)
#get the player play and write in game board (tic.game).
def getPlayer(points, frame):
global choose_user, init, last_time, player
hW,wW = frame.shape[0:2]
cv2.line(frame,(0,hW-25),(wW,hW-25),(35,35,155),25)
cv2.putText(frame, "Jogador, escolha uma jogada.",(150, hW-20), FONTE, 0.7,(0,255,0),2,cv2.LINE_AA)
field_key = 0
gameboard = np.array(tic.game[0]+tic.game[1]+tic.game[2])
indexs = np.where(gameboard == 0)[0]
for field in points:
write_comp = False
cap = cv2.VideoCapture(1)
while(game):
fields = []
ret, frame = cap.read()
#mirros the image to stay in the same position of screen computer
#maybe necessary to change
frame = cv2.flip(frame,1)
frame = cv2.flip(frame,0)
from bs4 import BeautifulSoup as bs
import requests, sys, re
from termcolor import colored
if len(sys.argv) < 2:
print colored("Falta o argumento palavra: Tente 'python main.py palavra'", 'red')
exit()
elif len(sys.argv) > 2:
print colored("O programa so toma apenas um argumento, o primeiro argumento sera utilizado.", 'red')
from bs4 import BeautifulSoup as bs
import requests, sys, re
from termcolor import colored
if len(sys.argv) < 2:
print colored("Falta o argumento palavra: Tente 'python main.py palavra'", 'red')
exit()
elif len(sys.argv) > 2:
print colored("O programa so toma apenas um argumento, o primeiro argumento sera utilizado.", 'red')
from bs4 import BeautifulSoup as bs
import requests, sys, re
from termcolor import colored
if len(sys.argv) < 2:
print colored("Falta o argumento palavra: Tente 'python main.py palavra'", 'red')
exit()
elif len(sys.argv) > 2:
print colored("O programa so toma apenas um argumento, o primeiro argumento sera utilizado.", 'red')
from bs4 import BeautifulSoup as bs
import requests, sys, re
from termcolor import colored
if len(sys.argv) < 2:
print colored("Falta o argumento palavra: Tente 'python main.py palavra'", 'red')
exit()
elif len(sys.argv) > 2:
print colored("O programa so toma apenas um argumento, o primeiro argumento sera utilizado.", 'red')