def main_loop(): prev_locations = [(30,230), (620,230)] signal.signal(signal.SIGALRM, freeze.handler) while True: try: track_loop(prev_locations) except freeze.CameraFreezeException: print 'reseting camera' proxy.set_state('prey',0) proxy.set_state('predator',0)
def signal_handler(signal, frame): print('Exit Detected... Setting proxy state...') proxy.set_state('prey',0) proxy.set_state('predator',0) sys.exit(0)
def track_loop(prev_locations= None): #stream=urllib.urlopen('http://192.168.1.1/mjpeg.cgi') #stream=urllib.urlopen('http://71913554.cam.trendnetcloud.com/mjpeg.cgi') print 'Discovering Camera...' stream= discover() codec = cv.CV_FOURCC('M','J','P','G') #video = VideoWriter() filename = "recording_%d"%int(time.time()) #video.open(filename, codec, 24, (640,480),False) bytes='' previous = None current = None future = None if prev_locations == None: prev_locations = [(30,230), (620,230)] # test proxy try: print 'Searching for Rails server...' proxy.test() except proxy.requests.ConnectionError: print 'rails server not found! open a new tab in the terminal and type "rails -s"' return proxy.set_state('prey',1) proxy.set_state('predator',1) print 'rails server found... starting tracking' count = 0 try: while True: signal.alarm(3) # check for camera freeze. bytes+=stream.read(1024) if bytes == "": proxy.set_state('prey',0) proxy.set_state('predator',0) break a = bytes.find('\xff\xd8') b = bytes.find('\xff\xd9') if a!=-1 and b!=-1: jpg = bytes[a:b+2] bytes= bytes[b+2:] i = imdecode(np.fromstring(jpg, dtype=np.uint8),0) future = i if previous == None or current == None or future == None: pass else: s1 = subtract(current,previous) s2 = subtract(future,current) thmap = fast_threshmap(s1,s2) result = imfilter(thmap) imshow('o',result) contours = imcontours(result) #get contour current_copy = copy.copy(current) if len(contours) != 0: mu, clusters = means(contours, prev_locations) if len(mu): new_locations = mu prev_locations = new_locations robot_locations = copy.copy(prev_locations) for i,m in enumerate(mu): m = tuple(map(int, m)) points = clusters[i] for p in points: p = tuple(map(int, p)) circle(current_copy,p,2,(255)) if prev_locations != None: l1 = tuple(map(int,prev_locations[0])) l2 = tuple(map(int,prev_locations[1])) circle(current_copy,l1,20,(255,0,0)) circle(current_copy,l2,20,(0,255,0)) if count % 24 == 0: send_remote = count % (24 * 3) == 0 upload_remote = count % (24 * 300) == 0 proxy.prey_add_location(l1[0],l1[1], remote=send_remote) proxy.predator_add_location(l2[0],l2[1], remote=send_remote) if upload_remote: print 'writing and uploading' filename= time.strftime("%Y%m%d-%H%M%S") + "-snap.jpg" imwrite("snaps/"+filename,current_copy) proxy.upload_image("snapshot.jpg") imshow('i',current_copy) #video.write(current_copy) if waitKey(1) ==27: proxy.set_state('prey',0) proxy.set_state('predator',0) exit(0) previous = current current = future count+=1 else: print 'waiting on better data' proxy.set_state('prey',0) proxy.set_state('predator',0) except socket.timeout: print 'Camera Disappeared!' proxy.set_state('prey',0) proxy.set_state('predator',0) except freeze.CameraFreezeException: proxy.set_state('prey',0) proxy.set_state('predator',0) raise