-
Notifications
You must be signed in to change notification settings - Fork 46
Completed Interactivity Project #10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
c82045c
08d3128
69128f8
b0a8959
7cbf1ff
801d11b
aaef984
a25b216
b528cc4
de48484
040b0c1
3ab3adc
9d368cb
1ee6b25
f5d88ff
774c278
ef8e9b9
3bfa9d4
62ea039
7011fe8
a074cf5
6afe48d
b315e3d
ba82751
8af7b10
1119d1f
1f21b8a
d7c2e10
e785a04
e4b47bb
042fd8f
81492d2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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. | ||
|
||
Nathan | ||
John |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,216 @@ | ||
import sys, pygame | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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() | ||
|
||
|
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() |
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() |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
import sys, pygame | ||
pygame.init() |
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() |
There was a problem hiding this comment.
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.