Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added my shit

  • Loading branch information...
commit 08f736c9a8bb99a7b9ee7aeeddcb92ebf2265965 1 parent e6aaf1c
Dominick authored
Showing with 189 additions and 0 deletions.
  1. +44 −0 async.py
  2. +26 −0 beat.py
  3. +66 −0 grid.py
  4. +53 −0 utils.py
View
44 async.py
@@ -0,0 +1,44 @@
+import sys
+import cv2
+import numpy as np
+import SocketServer
+import json
+import threading
+from utils import *
+
+class ThreadedTCPRequestHandler(SocketServer.BaseRequestHandler):
+
+ def handle(self):
+ data = self.request.recv(1024)
+ if data:
+ dic = json.loads(data)
+ level = dic['level']
+ threshold = dic['threshold']
+ inv = dic['invert']
+ to_send = read_frame(src, level, threshold, inv)
+ self.request.send(json.dumps(to_send))
+
+class ThreadedTCPServer(SocketServer.ThreadingMixIn, SocketServer.TCPServer):
+ pass
+
+
+cap = cv2.VideoCapture(0)
+(un, src) = cap.read()
+to_send = {}
+if __name__ == '__main__':
+ host = ''
+ port = 50000
+ server = ThreadedTCPServer((host,port), ThreadedTCPRequestHandler)
+ server_thread = threading.Thread(target=server.serve_forever)
+ server_thread.setDaemon(True)
+ server_thread.start()
+ while(1):
+ (un, src) = cap.read()
+
+
+
+
+
+
+
+
View
26 beat.py
@@ -0,0 +1,26 @@
+import socket, json
+import time, os
+def print_dic(dic):
+ os.system("clear")
+ for i in range(8):
+ l = dic[str(i)]
+ p = [" " for k in range(8)]
+ for j in l:
+ p[j] = "O"
+ print '|'.join(p)
+
+if __name__ == '__main__':
+ while(1):
+ s = socket.socket()
+ s.connect(('',50000))
+ a = {"level":100, "threshold":0.5, "invert": True}
+ b = json.dumps(a)
+ s.send(b)
+ c = s.recv(1024)
+ s.close()
+ d = json.loads(c)
+ #print '\n'.join([str(i) + " : " + str(d[i]) for i in d])
+ print_dic(d)
+ time.sleep(0.5)
+
+
View
66 grid.py
@@ -0,0 +1,66 @@
+import sys
+import cv2
+import numpy as np
+from utils import threshold, get_points, get_grid_cell
+import os
+import curses
+
+def gen_grid(img):
+ h = img.shape[0]
+ w = img.shape[1]
+ dx = int(w / 8.0)
+ dy = int(h / 8.0)
+ rectangles = []
+ for i in xrange(8):
+ for j in xrange(8):
+ rectangles.append(((dx*i, dy*j), ((i+1)*dx, (j+1)*dy), (i, j)))
+ return rectangles
+
+def draw_grid(img, rectangles):
+ for r in rectangles:
+ pt1 = r[0]
+ pt2 = r[1]
+ cv2.rectangle(img, pt1, pt2, (255,0,255,0))
+
+
+def nope( yep):
+ pass
+
+def print_dic(dic):
+ for i in range(8):
+ l = dic[i]
+ p = [" " for k in range(8)]
+ for j in l:
+ p[j] = "O"
+ stdscr.addstr(i,0, '|'.join(p))
+
+if __name__ == '__main__':
+ # start the capture
+ stdscr = curses.initscr()
+ curses.noecho()
+ curses.cbreak()
+ cap = cv2.VideoCapture(0)
+ cv2.namedWindow("test")
+ cv2.createTrackbar("level", "test", 150, 255, nope)
+ cv2.createTrackbar("inv", "test", 0, 1, nope)
+ while(1):
+ (un,src) = cap.read()
+ rects = gen_grid(src)
+ r = cv2.getTrackbarPos("level","test")
+
+ if cv2.getTrackbarPos("inv", "test") == 1:
+ mask = threshold(src, r, False)
+ else:
+ mask = threshold(src, r)
+ points = get_points(mask)
+ rows = {i:[] for i in range(8)}
+ for p in points:
+ (i,j) = get_grid_cell(src, p)
+ cv2.circle(mask, (p[1],p[0]), 10, (100,100,100,200))
+ cv2.circle(mask, (p[1],p[0]), 5, (100,100,100,200))
+ rows[i].append(j)
+ print_dic(rows)
+ stdscr.refresh()
+ draw_grid(mask,rects)
+ cv2.imshow("test", mask)
+ cv2.waitKey(1)
View
53 utils.py
@@ -0,0 +1,53 @@
+import cv2
+import numpy as np
+from scipy import ndimage
+
+def threshold(img, level, inv = True):
+ # creates a thresholded image from a color image
+ c = cv2.THRESH_BINARY_INV
+ if inv:
+ c = cv2.THRESH_BINARY
+ #convert to binary
+ bw = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
+ bl = cv2.blur(bw, (9,9))
+ (un, mask) = cv2.threshold(bl, level, 255, c)
+ element = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (9,9))
+ # Remove small white regions
+ close_img = cv2.morphologyEx(mask,cv2.MORPH_CLOSE, element)
+ # Remove small black holes
+ open_img = cv2.morphologyEx(close_img,cv2.MORPH_OPEN, element)
+ return open_img
+
+def get_points(img):
+ # given a binary image, will return a list of mass centers
+ lab = ndimage.measurements.label(img)
+ l = ndimage.measurements.center_of_mass(img, lab[0], range(1,lab[1] + 1))
+ if type(l) != type([]):
+ l = [l]
+ # convert to ints
+ r = [tuple([int(b) for b in a]) for a in l]
+
+ return r
+
+def get_grid_cell(img, pt):
+ # given a point and an image, assigns a grid cell
+ h = img.shape[0]
+ w = img.shape[1]
+ dx = int(w / 8.0)
+ dy = int(h / 8.0)
+ y = pt[0]
+ x = pt[1]
+ return(y / dy, x / dx)
+
+def read_frame(src, level, threshold, inv = True):
+ # returns a dict of rows
+ if inv:
+ mask = threshold(src, level, False)
+ else:
+ mask = threshold(src, level)
+ rows = {i:[] for i in range(8)}
+ pts = get_points(mask)
+ for p in pts:
+ (i,j) = get_grid_cell(src, p)
+ rows[i].append(j)
+ return rows
Please sign in to comment.
Something went wrong with that request. Please try again.