diff --git a/ProjectWriteupReflection.pdf b/ProjectWriteupReflection.pdf new file mode 100644 index 0000000..9a30b85 Binary files /dev/null and b/ProjectWriteupReflection.pdf differ diff --git a/README.md b/README.md index 61ec120..a806895 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/TicTacToeTest.py b/TicTacToeTest.py new file mode 100644 index 0000000..23f79c5 --- /dev/null +++ b/TicTacToeTest.py @@ -0,0 +1,216 @@ +import sys, pygame +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() + +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() + + diff --git a/sandbox/.TicTacToeTest.py.swp b/sandbox/.TicTacToeTest.py.swp new file mode 100644 index 0000000..6795c91 Binary files /dev/null and b/sandbox/.TicTacToeTest.py.swp differ diff --git a/sandbox/avgMask.py b/sandbox/avgMask.py new file mode 100644 index 0000000..87c5fa0 --- /dev/null +++ b/sandbox/avgMask.py @@ -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() diff --git a/sandbox/maskOpenCV.py b/sandbox/maskOpenCV.py new file mode 100644 index 0000000..e417742 --- /dev/null +++ b/sandbox/maskOpenCV.py @@ -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() diff --git a/sandbox/pygameTest1 b/sandbox/pygameTest1 new file mode 100644 index 0000000..2699237 --- /dev/null +++ b/sandbox/pygameTest1 @@ -0,0 +1,2 @@ +import sys, pygame +pygame.init() \ No newline at end of file diff --git a/sandbox/pygameTest1.py b/sandbox/pygameTest1.py new file mode 100644 index 0000000..9cd8700 --- /dev/null +++ b/sandbox/pygameTest1.py @@ -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() \ No newline at end of file diff --git a/sandbox/trackOpenCV.py b/sandbox/trackOpenCV.py new file mode 100644 index 0000000..6f67394 --- /dev/null +++ b/sandbox/trackOpenCV.py @@ -0,0 +1,47 @@ +import numpy as np +import cv2 + +cap = cv2.VideoCapture(0) + +# take first frame of the video +ret,frame = cap.read() + +# setup initial location of window +r,h,c,w = 0,200,0,200 # 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) +mask = cv2.inRange(hsv_roi, np.array((100., 50.,50.)), np.array((130.,100.,100.))) +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 ) + +while(1): + ret ,frame = cap.read() + frame = cv2.flip(frame,1) + if ret == True: + hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV) + dst = cv2.calcBackProject([hsv],[0],roi_hist,[0,180],1) + + # apply meanshift to get the new location + ret, track_window = cv2.meanShift(dst, track_window, term_crit) + + # Draw it on image + x,y,w,h = track_window + img2 = cv2.rectangle(frame, (x,y), (x+w,y+h), 255,2) + cv2.imshow('img2',frame) + + k = cv2.waitKey(60) & 0xff + if k == 27: + break + else: + cv2.imwrite(chr(k)+".jpg",img2) + else: + break + +cv2.destroyAllWindows() +cap.release() diff --git a/sandbox/trackingOpencv.py b/sandbox/trackingOpencv.py new file mode 100644 index 0000000..c24ba39 --- /dev/null +++ b/sandbox/trackingOpencv.py @@ -0,0 +1,83 @@ +import cv2 +import numpy as np + +def grid(x,y): + x = x/640. + y = y/480. + + if x < .33: + x_return = 0 + elif x < .66: + x_return = 1 + else: + x_return = 2 + + if y < .33: + y_return = 0 + elif y < .66: + y_return = 1 + else: + y_return = 2 + + return x_return, y_return + +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 ) + +while(1): + + # 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 + + print (x+(w/2), y+(h/2)) + + 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 + +cv2.destroyAllWindows() diff --git a/sandbox/webcamCaptureTest.py b/sandbox/webcamCaptureTest.py new file mode 100644 index 0000000..2dfcb04 --- /dev/null +++ b/sandbox/webcamCaptureTest.py @@ -0,0 +1,36 @@ +import numpy as np +import cv2 + +#make color filter function +def rbgcvt(image): + boundaries = [([0, 0, 50], [100, 100, 255])] + for (lower, upper) in boundaries: + # create NumPy arrays from the boundaries + 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 + +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) + if cv2.waitKey(1) & 0xFF == ord('q'): + break + +# When everything done, release the capture +cap.release() +cv2.destroyAllWindows() diff --git a/sandbox/webcamFaceTrackingTest.py b/sandbox/webcamFaceTrackingTest.py new file mode 100644 index 0000000..0df8985 --- /dev/null +++ b/sandbox/webcamFaceTrackingTest.py @@ -0,0 +1,31 @@ +import numpy as np +import cv2 + +face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml') + +def face_track(img): + + gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) + + faces = face_cascade.detectMultiScale(gray, 1.3, 5) + for (x,y,w,h) in faces: + cv2.rectangle(img, (x, y), (x+w, y+h), (0, 255, 0), 2) + + return img + + +cap = cv2.VideoCapture(0) + +while(True): + # Capture frame-by-frame + ret, frame = cap.read() + detection = face_track(frame) + + # Display the resulting frame + cv2.imshow('frame',detection) + if cv2.waitKey(1) & 0xFF == ord('q'): + break + +# When everything done, release the capture +cap.release() +cv2.destroyAllWindows() \ No newline at end of file