123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247 |
- import argparse
- import tag
- import sys
- import cv2
- import time
- import numpy as np
- import os
- from cmarkers import getCMarkers, get_transform_matrix_points
-
- def open_camera(cam_id = 1):
- cap = cv2.VideoCapture(cam_id)
- return cap
-
- def get_frame(device):
- ret, img = device.read()
- if (ret == False):
- print >> sys.stderr, "Error capturing from video device."
- return None
- return img
-
- def cleanup(cam_id = 0):
- cv2.destroyAllWindows()
- cv2.VideoCapture(cam_id).release()
-
- def new_rgb_image(width, height):
- image = numpy.zeros( (height, width, 3), numpy.uint8)
- return image
- corner_point_list = []
-
- def mouse_click_callback(event, x, y, flags, param):
- if event == cv2.EVENT_LBUTTONDOWN:
-
- corner_point_list.append( (x,y) )
- def find_centers(contours):
- centers = []
- for contour in contours:
- moments = cv2.moments(contour, True)
-
- center = (moments['m10']/moments['m00'] , moments['m01']/moments['m00'])
-
-
- center = (int(round(center[0])),int(round(center[1])))
- centers.append(center)
- return centers
- def find_contours(image):
- ret,thresh = cv2.threshold(image,127,255,0)
- image, contours, hierarchy = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
- return contours
- def get_transform_matrix(dev, board_size, dpi, calib_file = None):
-
- img = get_frame(dev)
-
-
- cv2.imshow("Calibrate", img)
-
-
-
-
- cv2.setMouseCallback("Calibrate", mouse_click_callback)
-
-
- while True:
-
- if (len(corner_point_list) >= 4):
- print ("Got 4 points: "+str(corner_point_list))
- break
-
-
- if (cv2.waitKey(10) >= 0):
- break;
-
-
- cv2.destroyWindow("Calibrate")
-
-
- if (len(corner_point_list) >= 4):
-
-
-
-
- src = np.array(corner_point_list, np.float32)
-
-
-
- dest = np.array( [ (0, 0), (0, board_size[1]*dpi), (board_size[0]*dpi, 0), (board_size[0]*dpi, board_size[1]*dpi) ], np.float32)
-
-
- trans = cv2.getPerspectiveTransform(src, dest)
-
-
- if calib_file:
- np.savetxt(calib_file, trans)
- return trans
- else:
- return None
- def get_green_dots(img_orig):
-
-
- b,g,r = cv2.split(img_orig)
- gg = g - 6
- green = ((gg > r) & (gg > b) & (g > 120)).astype(np.uint8)*255
-
- kernel = np.ones((4,4),np.uint8)
- opening = cv2.morphologyEx(green, cv2.MORPH_OPEN, kernel)
- img = cv2.bitwise_and(img_orig,img_orig,mask = opening)
- contours = find_contours(opening)
- return find_centers(contours)
- if __name__ == "__main__":
- parser = argparse.ArgumentParser()
- cam_id = 0
- dev = None
-
- parser.add_argument('-a',
- '--automatic',
- action='store_false',
- help='Enable auto detect green dots (default=False)')
- parser.add_argument('-n',
- '--nocamera',
- action='store_false',
- help='Disable camera, players need to send the position (default=False)')
- args = parser.parse_args()
- manual = args.automatic
- camera = args.nocamera
- dev = None
- if camera:
- dev = open_camera(cam_id)
-
-
-
- board_size = [84, 84]
- point = []
-
-
-
- dpi = 10
-
- transform_size = (int(board_size[0]*dpi), int(board_size[1]*dpi))
-
-
- transform = None
- if manual and camera:
- transform = get_transform_matrix(dev, board_size, dpi)
-
- server = tag.TagServer(10318, board_size)
- while True:
- img_orig = np.zeros([transform_size[0], transform_size[1], 3])
- if camera:
- img_orig = get_frame(dev)
- else:
- time.sleep(0.1)
- if img_orig is not None:
- img = img_orig
- if camera:
-
- if not manual:
- centers = get_green_dots(img_orig)
- transform = get_transform_matrix_points(centers, board_size, dpi)
- img = cv2.warpPerspective(img_orig, transform, dsize=transform_size)
- markers = getCMarkers(img)
- for i in markers:
- idd, p, head = i
- server.updatePosition(idd, p[0]/dpi, p[1]/dpi, head)
- p = (int(p[0]), int(p[1]))
- cv2.circle(img, p, 30, (0,255,0))
- cv2.putText(img, str(idd), p, cv2.FONT_HERSHEY_SIMPLEX, 1.0, (255, 0, 255))
- for robot in server.robots():
- for line in robot.lines():
- p1 = (line[0]*dpi).astype(int)
- p2 = (line[1]*dpi).astype(int)
- color = (255, 255, 0)
- if robot == server.tag:
- color = (0, 0, 255)
- cv2.line(img, (p1[0], p1[1]), (p2[0], p2[1]), color)
- if server.paused:
- cv2.putText(img, "PAUSE", (0,30), cv2.FONT_HERSHEY_SIMPLEX, 1.0, (0, 0, 255))
- cv2.imshow("warped", img)
-
- else:
- break
-
- k = cv2.waitKey(33)
- if k == 115:
- server.startGame()
- elif k == 112:
- server.stopGame()
- elif k == 113:
- server.stop()
- break
- if camera:
- cleanup(cam_id)
|