Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
c82045c
testing git by adding names to readme
Mar 3, 2016
08d3128
removed duplicate readme
Mar 3, 2016
69128f8
Merge pull request #1 from NathanYee/nathan
Mar 3, 2016
b0a8959
added a sandbox folder for experimentation, also added a openCV test …
Mar 3, 2016
7cbf1ff
Merge pull request #2 from NathanYee/nathan
Mar 3, 2016
801d11b
adding facetracking test code
johnmoreland Mar 3, 2016
aaef984
Merge pull request #3 from NathanYee/john
johnmoreland Mar 3, 2016
a25b216
added a file for playing around with average positions of a mask. Ca…
Mar 3, 2016
b528cc4
added average horizontal. Added picture of frame for visual confirma…
Mar 3, 2016
de48484
added video capture with mask + avg locations of mask
Mar 3, 2016
040b0c1
Merge pull request #4 from NathanYee/nathan
Mar 4, 2016
3ab3adc
adding some elementary test files
johnmoreland Mar 5, 2016
9d368cb
added basic logic
johnmoreland Mar 5, 2016
1ee6b25
added a GUI
johnmoreland Mar 5, 2016
f5d88ff
Merge pull request #5 from NathanYee/john
johnmoreland Mar 5, 2016
774c278
got a working tracker for green roly poly ball
Mar 6, 2016
ef8e9b9
made a grid function to determine position
Mar 6, 2016
3bfa9d4
moved starting rectangle to center
Mar 6, 2016
62ea039
Merge pull request #6 from NathanYee/nathan
Mar 6, 2016
7011fe8
integrated openCV and TicTacToe in TicTacToeTest.py
Mar 9, 2016
a074cf5
Merge pull request #7 from NathanYee/nathan
Mar 9, 2016
6afe48d
updates
johnmoreland Mar 10, 2016
b315e3d
Merge branch 'master' of github.com:NathanYee/InteractiveProgramming …
johnmoreland Mar 10, 2016
ba82751
added an AI
johnmoreland Mar 10, 2016
8af7b10
Merge pull request #8 from NathanYee/john
johnmoreland Mar 10, 2016
1119d1f
play the game on a timer
Mar 10, 2016
1f21b8a
Merge pull request #9 from NathanYee/nathan
Mar 10, 2016
d7c2e10
Finalized project
Mar 10, 2016
e785a04
added comments to clarify program
Mar 10, 2016
e4b47bb
Merge pull request #10 from NathanYee/nathan
Mar 10, 2016
042fd8f
cleaned up readme file
Mar 10, 2016
81492d2
Merge pull request #11 from NathanYee/nathan
Mar 10, 2016
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added ProjectWriteupReflection.pdf
Binary file not shown.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,7 @@
# InteractiveProgramming
This is the base repo for the interactive programming project for Software Design, Spring 2016 at Olin College.

To play tic tac toe, just run TicTacToeTest.py. Moves update every three seconds. Use a green object to track properly.
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would also be helpful to include the modules/installs needed to get this up and running.


Nathan
John
216 changes: 216 additions & 0 deletions TicTacToeTest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,216 @@
import sys, pygame
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't forget to add a header/docstring that gives a brief summary of what the file contains/does.

import cv2
import numpy as np
import random
import time
from time import time

cap = cv2.VideoCapture(0)

# take first frame of the video
ret, frame = cap.read()

# define range of green color in HSV
lower_blue = np.array([50,75,50])
upper_blue = np.array([75,150,200])

# setup initial location of window
r,h,c,w = 200,120,260,120 # simply hardcoded the values
track_window = (c,r,w,h)

# set up the ROI for tracking
roi = frame[r:r+h, c:c+w]
hsv_roi = cv2.cvtColor(roi, cv2.COLOR_BGR2HSV)

# Threshold the HSV image to get only blue colors
mask = cv2.inRange(hsv_roi, lower_blue, upper_blue)

roi_hist = cv2.calcHist([hsv_roi],[0],mask,[180],[0,180])
cv2.normalize(roi_hist,roi_hist,0,255,cv2.NORM_MINMAX)

# Setup the termination criteria, either 10 iteration or move by atleast 1 pt
term_crit = ( cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 10, 1 )

pygame.init()
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In terms of code formatting/layout, to make your code easier to read, wrapping all your setup code into a function could be beneficial.


def print_game_state(i,j):
""" Prints game state as a matrix"""
for i in range(3):
for j in range(3):
print cell_states.get((i,j)),
print ""

def find_third(pos):
""" Finds which box was clicked in """
#determine which third the click was in (veritcal rectangles)
if pos[0] < left_third:
j = 0
elif left_third <= pos[0] and pos[0] <= right_third:
j = 1
elif right_third <= pos[0]:
j = 2

#determine which third the click was in (horizontal rectangles)
if pos[1] < top_third:
i = 0
elif top_third <= pos[1] and pos[1] <= bottom_third:
i = 1
elif bottom_third <= pos[1]:
i = 2
return (i , j)

def generate_random_cell():
"""
generate_random_cell generates a random cell to be used by the computer player
"""
i = random.randint(0,2)
j = random.randint(0,2)
# print i,j
return (i , j)

def check_for_win(cell_states):
"""
Takes the game state dictionary and checks to see if there is a winner. Calls declare_winner if there is a winner.
"""
#check verticals
for j in range(3):
if ( cell_states.get((0,j)) == cell_states.get((1,j))
== cell_states.get((2,j)) != 0) :
declare_winner(cell_states.get((0,j)))
#check horizontals
for i in range(3):
if ( cell_states.get((i, 0)) == cell_states.get((i, 1))
== cell_states.get((i ,2)) != 0) :
declare_winner(cell_states.get((i, 0)))
#check diagonals
if (cell_states.get((0, 0)) == cell_states.get((1, 1))
== cell_states.get((2,2)) != 0) :
declare_winner(cell_states.get((1, 1)))
elif (cell_states.get((2, 0)) == cell_states.get((1, 1))
== cell_states.get((0, 2)) != 0) :
declare_winner(cell_states.get((1, 1)))

def declare_winner(int):
"""
declare_winner determines which player wins the game and prints to the console
"""
if int == 1:
print "Player 1 wins"
elif int == -1:
print "Player 2 wins"


size = width, height = 1280, 920
black = 0, 0, 0
red = 200, 0, 0
blue = 0, 0, 200

screen = pygame.display.set_mode(size)

# Initializing the game state
cell_states = dict()
for i in range(3):
for j in range(3):
cell_states[(i,j)] = 0

#Find the screen's "thirds"
left_third = width/3.0
right_third = 2.0 * (left_third)
top_third = height/3.0
bottom_third = 2.0 * (top_third)

#Creating borders
left = pygame.Rect((left_third-5.0), 0, 10, height)
pygame.draw.rect(screen, (200,200,200) , left)
right = pygame.Rect((right_third-5.0), 0, 10, height)
pygame.draw.rect(screen, (200,200,200) , right)
top = pygame.Rect(0, (top_third-5.0), width, 10)
pygame.draw.rect(screen, (200,200,200) , top)
bottom = pygame.Rect(0, (bottom_third-5.0), width, 10)
pygame.draw.rect(screen, (200,200,200) , bottom)

rect_widths = (width-20)/3.0
rect_heights = (height-20)/3.0
rect_x_pos = [0, left_third+5, right_third+5]
rect_y_pos = [0, top_third+5, bottom_third+5]

#creates a dictionary of rectangles
rect_dict = dict()
for i in range(len(rect_x_pos)):
for j in range(len(rect_y_pos)):
rect_dict[(j,i)] = pygame.Rect(rect_x_pos[i], rect_y_pos[j], rect_widths, rect_heights)

turn = 0 #initialize the turn

t = time() #capture the current time. Will update gamestate when the time is at least three seconds later


while True: #main control loop
# Take each frame
ret, frame = cap.read()
frame = cv2.flip(frame,1)

# Convert BGR to HSV
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
dst = cv2.calcBackProject([hsv],[0],roi_hist,[0,180],1)

# Threshold the HSV image to get only blue colors

mask = cv2.inRange(hsv, lower_blue, upper_blue)

# Bitwise-AND mask and original image
# res = cv2.bitwise_and(frame,frame, mask= mask)

ret, track_window = cv2.meanShift(dst, track_window, term_crit)

# #draw it on frame
x,y,w,h = track_window

img2 = cv2.rectangle(frame, (x,y), (x+w,y+h), 255,2)

cv2.imshow('frame',frame)
cv2.imshow('mask',mask)
# cv2.imshow('res',res)
k = cv2.waitKey(5) & 0xFF
if k == 27:
break


for event in pygame.event.get():

if event.type == pygame.QUIT: sys.exit()

#control loop

if time() > t + 3:
t = time()
pos = (2*(x+(w/2)), 2*(y+(h/2))) #green ball location
# print pos

#update clicked cell
if cell_states[find_third(pos)] == 0 and (turn % 2 == 0) :
#user input
cell_states[find_third(pos)] = 1
update_rectangle = rect_dict.get(find_third(pos))
pygame.draw.rect(screen, blue, update_rectangle)
turn +=1
check_for_win (cell_states)

#AI turn
random_cell = generate_random_cell()
cell_states[random_cell] = -1
update_rectangle = rect_dict.get(random_cell)
pygame.draw.rect(screen, red, update_rectangle)

turn +=1

# print_game_state(i,j)

check_for_win (cell_states)

pygame.display.flip()

#close opencv windows
cv2.destroyAllWindows()


Binary file added sandbox/.TicTacToeTest.py.swp
Binary file not shown.
85 changes: 85 additions & 0 deletions sandbox/avgMask.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import numpy as np
import cv2

# # set full length numpy arrays
# np.set_printoptions(threshold=np.nan)



#make color filter function
def rbgcvt(image):
lower = [0, 0, 100]
upper = [100, 100, 255]
lower = np.array(lower, dtype = "uint8")
upper = np.array(upper, dtype = "uint8")

# find the colors within the specified boundaries and apply
# the mask
mask = cv2.inRange(image, lower, upper)
#output = cv2.bitwise_and(image, image, mask = mask)

return mask

def vertical(image):
#find average vertical location
#first find number of successes in each line
numberLine = []
for line in image:
counter = 0
for value in line:
if value == 255:
counter += 1
numberLine.append(counter)
#next compute the average vertical distance using distance and weight
numerator = 0
for i in range(len(numberLine)):
numerator += (i+1) * numberLine[i]
denominator = np.sum(numberLine)
vertPos = int(numerator / denominator)
return vertPos

#next find horizontal positon
def horizontal(image):
#first find number of success for vertical line
numberVertLine = []
total = 0
for i in range(len(image[0])):
counter = 0
for line in image:
if line[i] == 255:
counter += 1
total += 1
numberVertLine.append(counter*(i+1))


return np.sum(numberVertLine)/total

#create capture object
cap = cv2.VideoCapture(0)

while(True):
# Capture frame-by-frame
ret, frame = cap.read()
detection = rbgcvt(frame)

# Our operations on the frame come here
# gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

# Display the resulting frame
cv2.imshow('frame',detection)
print (horizontal(detection)/640., vertical(detection)/480.)

if cv2.waitKey(1) & 0xFF == ord('q'):
break

print (horizontal(detection)/640., vertical(detection)/480.)

# cv2.imshow("image", detection)
# cv2.waitKey(0)
# # Our operations on the frame come here
# # gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

# # Display the resulting frame

# # When everything done, release the capture
cap.release()
31 changes: 31 additions & 0 deletions sandbox/maskOpenCV.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import cv2
import numpy as np

cap = cv2.VideoCapture(0)

while(1):

# Take each frame
_, frame = cap.read()

# Convert BGR to HSV
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)

# define range of blue color in HSV
lower_blue = np.array([110,50,50])
upper_blue = np.array([130,255,255])

# Threshold the HSV image to get only blue colors
mask = cv2.inRange(hsv, lower_blue, upper_blue)

# Bitwise-AND mask and original image
res = cv2.bitwise_and(frame,frame, mask= mask)

cv2.imshow('frame',frame)
cv2.imshow('mask',mask)
cv2.imshow('res',res)
k = cv2.waitKey(5) & 0xFF
if k == 27:
break

cv2.destroyAllWindows()
2 changes: 2 additions & 0 deletions sandbox/pygameTest1
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import sys, pygame
pygame.init()
48 changes: 48 additions & 0 deletions sandbox/pygameTest1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import sys, pygame
pygame.init()

size = width, height = 1280, 920
speed = [1, 2]
black = 0, 0, 0

screen = pygame.display.set_mode(size)

ball = pygame.image.load("ball.png")
ballrect = ball.get_rect()

while 1:
for event in pygame.event.get():
if event.type == pygame.QUIT: sys.exit()
if event.type == pygame.MOUSEBUTTONUP:
pos = pygame.mouse.get_pos()
print pos [0]
# print ballrect.left, pos[0], ballrect.right
# print ballrect.bottom, pos[1], ballrect.top
if ballrect.left < pos[0] and pos[0] < ballrect.right:
print "true"
if ballrect.bottom > pos[1] and pos[1] > ballrect.top:
print "true"
speed = [0,0]


ballrect = ballrect.move(speed)
if ballrect.left < 0:
speed[0] = -speed[0]*.99
if abs(speed[0]) < 2:
speed[0] += 2
if ballrect.right > width:
speed[0] = -speed[0]*.99
if abs(speed[0]) < 2:
speed[0] -= 2
if ballrect.top < 0:
speed[1] = -speed[1]*.99
if abs(speed[0]) < 2:
speed[0] += 2
if ballrect.bottom > height:
speed[1] = -speed[1]*.99
if abs(speed[0]) < 2:
speed[0] -= 2

screen.fill(black)
screen.blit(ball, ballrect)
pygame.display.flip()
Loading