Beispiel #1
0
def main():
    ap = argparse.ArgumentParser()
    ap.add_argument("-v", "--videopath", help="Path to video")
    ap.add_argument("-d", "--detector", help="Detection Algo")

    args = vars(ap.parse_args())
    if (args["videopath"] == "0"):
        source = 0
    elif (args["videopath"] == "1"):
        source = 1
    else:
        source = args["videopath"]

    if (args["detector"] == "HG" or args["detector"] == "HC"):
        detector_algo = args["detector"]
    else:
        print " Detector algo not correct"
        quit()

############ Detection Part starts here ##############
    dtector = Detector(src=source, detector=detector_algo).start()
    while True:
        frame = dtector.read()
        frame = imutils.resize(frame, width=400)
        cv2.imshow("Detection", frame)
        key = cv2.waitKey(20) & 0xFF
        if key == 27:
            break
    dtector.stop()
    rect, img = dtector.get_roi()

    cv2.destroyAllWindows()
    # print rect

    ############ Detection Part ends here ##############

    ############ Tracking Part starts here ##############

    global stop_arduino_thread
    q = Queue()
    tracker = Tracker(rect, img, src=source).start()
    print tracker
    data = tracker.get_points()
    q.put(data)
    thread_arduino = Thread(target=send_arduino, args=(q, ))
    thread_arduino.start()
    while True:
        frame = tracker.read()
        frame = imutils.resize(frame, width=400)
        cv2.imshow("Frame", frame)
        data = tracker.get_points()
        q.put(data)
        key = cv2.waitKey(50) & 0xFF
        if key == 27:
            break
    stop_arduino_thread = True
    tracker.stop()
    cv2.destroyAllWindows()
def DetectorThunk(conn,dir='C:/PAPA Control/',datacount=1000):
    """An infinite loop for controlling the detector

    Keyword arguments:
    conn -- a connection stream for communicating with the main process
    dir -- the directory in which to save data
    datacount -- the number of data points to attempts to read at a time

    The function loops through, either sleeping or recording data.  It polls
    its communication stream and expects a two element tuple.  The first
    element is an event code and the second is a tuple with the arguments for
    the event.

    """
    det = Detector() #detector object
    running = False
    paused = False
    runnumber = -1
    stream = None
    count = 0
    while True:
        if (not running) and (not conn.poll()):
            sleep(0.01)
            continue
        if conn.poll():
            cmd,args=conn.recv()
            if cmd==QUIT:
                break
            elif cmd==START:
                if runnumber<=0:
                    logging.error("Error: Invalid Run #")
                    continue
                if not running:
                    stream = open(dir+("%04d"%runnumber)+'.pel','wb')
                    stream.write(det.makeHeader())
                    det.start()
                    running = True
                    count=0
            elif cmd==STOP:
                if running:
                    det.stop()
                    det.clear()
                    running = False
                    stream.close()
                    stream=None
                    continue
            elif cmd==PAUSE:
                logging.info("Pausing Detector")
                det.stop()#Do NOT clear
                running = False
                paused = True
            elif cmd==RESUME:
                if paused:
                    logging.info("Resuming")
                    det.start()
                    running = True
                else:
                    logging.warning("Cannot resume: detector not paused")
            elif cmd==RUNNUMBER:
                runnumber = args[0]
                conn.send("Run # set to %d"%runnumber)
            elif cmd==DIRECTORY:
                dir = args[0]
            elif cmd == N_COUNT:
                conn.send(count/2)#Correction Needed for 64 Bit Mode
            elif cmd==PARAM:
                opt,val=args
                det.setParam(opt,val)
            elif cmd == QUERY:
                conn.send(det.status[args[0]])
                #print det.status[args[0]]

            else:
                logging.warning("Did not recognize command %d"%cmd)
        if running:
            if stream is None: continue
            try:
                #Get the number of data points and the data
                #note that num is the number of 32 bit data
                #points given by the detector, which may or
                #may not be the actual  number of neutrons,
                #depending on  if the  detector is in 32 or
                #64 bit mode
                (num,data)=det.read(datacount)
            except RuntimeError,(err):
                logging.error(err)
                break
            count += num
            #Pull only the actual data from the buffer
            if num < datacount:
                data=data[:num]
            stream.write(data.tostring())
            if num < datacount:
                stream.flush()
                sleep(0.01)