예제 #1
0
    server_response, server_time = talk2server(cmd='connect',devN=1)
    """
    try:
        server_response, server_time = client.check_tcp_server(
            cmd=cmd, dev=devN).split("_")
        server_response, server_time = clientConnectThread.get_command()
    except:  # noserver response
        server_time = "na"
        server_response = "none"
    # print "server reponse: {} and timestamp: {}".format(server_response, server_time)
    return server_response, server_time


# TCP communication
# Start the client thread:
clientConnectThread = client.ClientConnect("connect", "{}".format(devN))
clientConnectThread.setDaemon(True)
clientConnectThread.start()  # launching thread
# time.sleep(1)
server_time = 0.0
server_response = "none"
response = clientConnectThread.get_command()
if "_" in response:
    server_response, server_time = response.split("_")
else:
    server_reponse = response
# ==============================================================================
# Video .avi output setup
# ==============================================================================
#############################################################################
# setup thermal camera
def save_videos(folder4frames, c, vid_num, tic, nf=360):
    """
    save videos
    """
    fourcc = cv2.cv.CV_FOURCC('X', 'V', 'I', 'D')
    fps = 30

    # TCP communication
    # Start the client thread:
    clientConnectThread = client.ClientConnect("connect", "{}".format(devN))
    clientConnectThread.setDaemon(True)
    clientConnectThread.start()  # launching thread
    # time.sleep(1)
    server_time = 0.0
    server_response = "none"
    response = clientConnectThread.get_command()
    if "_" in response:
        server_response, server_time = response.split("_")
    else:
        server_reponse = response
    # print(server_response, server_time)

    # Create a pandas dataframe to hold the information (index starts at 1)
    cols = ["frameN", "localtime", "servertime"]
    df = pd.DataFrame(columns=cols)
    df.loc[c] = [0, server_time, time.time()]

    print "Creating Video Headers"
    # Initialize the videowriter
    #vid_num     = 0
    video_rgb = cv2.VideoWriter(folder4frames + "/rgb/dev" + str(devN) +
                                "rgb" + str(vid_num) + ".avi",
                                fourcc,
                                fps=fps,
                                frameSize=(w, h))
    video_depth = cv2.VideoWriter(folder4frames + "/depth/dev" + str(devN) +
                                  "depth" + str(vid_num) + ".avi",
                                  fourcc,
                                  fps=fps,
                                  frameSize=(w, h))
    video_dmap = cv2.VideoWriter(folder4frames + "/dmap/dev" + str(devN) +
                                 "dmap" + str(vid_num) + ".avi",
                                 fourcc,
                                 fps=fps,
                                 frameSize=(w, h))
    rgb_stream.start()
    depth_stream.start()
    for f in range(nf):
        openni2.wait_for_any_stream()
        # RGB-D Streams
        rgb = get_rgb()
        dmap, d4d = get_depth()
        # if vis_frames: # Display the streams
        rgbdm = np.hstack((rgb, d4d, dmap))
        rgbdm_small = cv2.resize(rgbdm, (960, 240))  # smallest
        cv2.imshow("1:4 scale", rgbdm_small)
        # === Keyboard Commands ===
        key = cv2.waitKey(1) & 255
        if key == 27:
            print "Terminating code!"
            df.loc[c] = [f, run_time, server_time]
            video_rgb.write(rgb)  # write to vid file
            video_depth.write(d4d)  # write to vid file
            video_dmap.write(dmap)
            # Write data to csv
            df.to_csv(folder4csv + "dev" + str(devN) + '_data' + str(vid_num) +
                      '.csv')
            # release video writers
            print "==== Releasing the video writers"
            video_rgb.release()
            video_depth.release()
            video_dmap.release()
            #done = True
            sys.exit(0)

        # Poll the server:
        clientConnectThread.update_command("check")
        sresponse = clientConnectThread.get_command()
        if "_" in response:
            server_response, server_time = response.split("_")
        else:
            server_reponse = response

        run_time = time.time() - tic
        print "\t Processing {} session and frame number {} -- {}".format(
            vid_num, f, (vid_num * nf) + f)

        video_rgb.write(rgb)  # --> rgb vid file
        video_depth.write(d4d)  # --> depth vid file
        video_dmap.write(dmap)  # --> dmap vid file

        # Write Datarows
        df.loc[c] = [f, run_time, server_time]

        c += 1
    # endfor f in range nf
    df.to_csv(folder4csv + "dev" + str(devN) + '_data' + str(vid_num) + '.csv')
    # release video writers
    video_rgb.release()
    video_depth.release()
    video_dmap.release()
    done = True
    del df
    del video_rgb
    del video_depth
    del video_dmap
    print "\n === Cleared the video and pandas variables".format(vid_num)
    # endfor vid_num in range(0, t_num_vids):
    rgb_stream.stop()
    depth_stream.stop()
    return c, run_time
예제 #3
0
import client
import time, sys

done = False
if __name__ == '__main__':
    dev = 1
    clientConnectThread = client.ClientConnect('connect', dev=dev)
    clientConnectThread.setDaemon(True)
    clientConnectThread.start()  #launching thread
    while not done:  # view <= nviews
        clientConnectThread.update_command('check')
        server_response = clientConnectThread.get_command()
        if "save" in server_response:
            print "RECEIVED FLAG TO SAVE DATA"
            time.sleep(5)
        elif "close" in server_response:
            print('Closing the connection')
            clientConnectThread.update_command('close')
            time.sleep(5)
            done = True
        else:
            print "\t doing other stuff"
        print server_response
        time.sleep(2)
sys.exit(1)
def run_old_loop():
    """
    Creates session videos. However, opencv video release is not working. HUGE memory
    leak that.
#if __name__ == "__main__":
    #time.sleep(20) # secs pause! for startup
    #devN = 1

    """
    synctype = "relaxed"
    actorname = "patient_0"

    # Flags
    vis_frames = True  # True   # display frames
    save_frames_flag = False  # save all frames
    test_flag = True

    test_frames = 5000000
    fps = 30
    c = 0
    ## Runtime and Controls
    nf = 500  # 172800# 60*60*24*2 # Number of video frames in each clip and video
    f = 1  # frame counter
    tic = 0
    run_time = 0
    total_t = 0
    fourcc = cv2.cv.CV_FOURCC('X', 'V', 'I', 'D')

    done = False

    # TCP communication
    # Start the client thread:
    clientConnectThread = client.ClientConnect("connect", "{}".format(devN))
    clientConnectThread.setDaemon(True)
    clientConnectThread.start()  # launching thread
    # time.sleep(1)
    server_time = 0.0
    server_response = "none"
    response = clientConnectThread.get_command()
    if "_" in response:
        server_response, server_time = response.split("_")
    else:
        server_reponse = response
    # print(server_response, server_time)

    # Create a pandas dataframe to hold the information (index starts at 1)
    cols = ["frameN", "localtime", "servertime"]
    df = pd.DataFrame(columns=cols)
    df.loc[c] = [0, server_time, time.time()]

    # The folders for all data
    folder4frames, folder4csv = createFolders(actorname)
    print "Creating Video Headers"
    # Initialize the videowriter
    vid_num = 0
    video_rgb = cv2.VideoWriter(folder4frames + "/rgb/dev" + str(devN) +
                                "rgb" + str(vid_num) + ".avi",
                                fourcc,
                                fps=fps,
                                frameSize=(w, h))
    video_depth = cv2.VideoWriter(folder4frames + "/depth/dev" + str(devN) +
                                  "depth" + str(vid_num) + ".avi",
                                  fourcc,
                                  fps=fps,
                                  frameSize=(w, h))
    video_dmap = cv2.VideoWriter(folder4frames + "/dmap/dev" + str(devN) +
                                 "dmap" + str(vid_num) + ".avi",
                                 fourcc,
                                 fps=fps,
                                 frameSize=(w, h))

    # Get the first timestamp
    tic = time.time()
    start_t = tic

    # --- main loop ---
    done = False
    while not done:  # view <= nviews
        # RGB-D Streams
        rgb = get_rgb()
        dmap, d4d = get_depth()

        if vis_frames:  # Display the streams
            rgbdm = np.hstack((rgb, d4d, dmap))
            # rgbdm_small = rgbdm # orginal size
            # rgbdm_small = cv2.resize(rgbdm,(1280,240)) # medium
            # rgbdm_small = cv2.resize(rgbdm,(640,240)) # smallest
            rgbdm_small = cv2.resize(rgbdm, (960, 240))  # smallest
            cv2.imshow("1:4 scale", rgbdm_small)
            # === Keyboard Commands ===
            key = cv2.waitKey(1) & 255
            if key == 27:
                print "Terminating code!"
                done = True
        # Poll the server:
        clientConnectThread.update_command("check")
        response = clientConnectThread.get_command()
        if "_" in response:
            server_response, server_time = response.split("_")
        else:
            server_reponse = response

        run_time = time.time() - tic
        print "Processing {} session and frame number {}".format(vid_num, f)

        # === check synchronization type
        if synctype == 'strict':
            if server_response == 'save':
                video_rgb.write(rgb)  # --> rgb vid file
                video_depth.write(d4d)  # --> depth vid file
                video_dmap.write(dmap)  # --> dmap vid file
                # Write Datarows
                df.loc[c] = [
                    f,
                    strftime("%a, %d %b %Y %H:%M:%S +0000", localtime()),
                    server_time
                ]
                f += 1
                c += 1
        elif synctype == 'relaxed':
            video_rgb.write(rgb)  # --> rgb vid file
            video_depth.write(d4d)  # --> depth vid file
            video_dmap.write(dmap)  # --> dmap vid file

            # Write Datarows
            df.loc[c] = [f, run_time, server_time]
            f += 1
            c += 1
        else:
            print "synchronization type unknown"

        if test_flag and (f == test_frames):
            print "Terminating code!"
            done = True

        # if np.mod(f,nf) == 0: # close and create new csv and video
        if f == nf:
            df.to_csv(folder4csv + "dev" + str(devN) + '_data' + str(vid_num) +
                      '.csv')
            print "session {} saved".format(vid_num)
            # release video writers
            video_rgb.release()
            video_depth.release()
            video_dmap.release()
            del df
            del video_rgb
            del video_depth
            del video_dmap
            print "\t Cleared the vars and memory and prepping next session"
            vid_num += 1
            # Create new video writers
            video_rgb = cv2.VideoWriter(folder4frames + "/rgb/dev" +
                                        str(devN) + "rgb" + str(vid_num) +
                                        ".avi",
                                        fourcc,
                                        fps=fps,
                                        frameSize=(w, h))
            video_depth = cv2.VideoWriter(folder4frames + "/depth/dev" +
                                          str(devN) + "depth" + str(vid_num) +
                                          ".avi",
                                          fourcc,
                                          fps=fps,
                                          frameSize=(w, h))
            video_dmap = cv2.VideoWriter(folder4frames + "/dmap/dev" +
                                         str(devN) + "dmap" + str(vid_num) +
                                         ".avi",
                                         fourcc,
                                         fps=fps,
                                         frameSize=(w, h))
            # reset pandas dataframe
            df = pd.DataFrame(columns=cols)
            c = 0
            f = 0
            # done = True #stop after the first recording.

        # elif chr(key) =='s':  #s-key to save current screen
        #    save_frames(f,rgb,dmap,p=folder4screens)

        # if
        # --- keyboard commands ---
    # while

    # TERMINATE
    print "=== Terminating code! ==="
    # Close carmine context and stop device
    print "==== Closing carmine context"
    rgb_stream.stop()
    depth_stream.stop()
    openni2.unload()
    # write last datapoints
    print "==== Writing last portions of data."
    vid_num = +1
    df.loc[c] = [f, run_time, server_time]
    video_rgb.write(rgb)  # write to vid file
    video_depth.write(d4d)  # write to vid file
    video_dmap.write(dmap)
    # Write data to csv
    df.to_csv(folder4csv + "dev" + str(devN) + '_data' + str(vid_num) + '.csv')
    # release video writers
    print "==== Releasing the video writers"
    video_rgb.release()
    video_depth.release()
    video_dmap.release()
    # Disconnect the client from the server
    print "==== Disconecting client and closing the server"
    clientConnectThread.update_command("close")
    # Release video/image resources
    cv2.destroyAllWindows()
    # print some timing information:
    fps = f / run_time
    print "\nTime metrics for {} frames:".format(f)
    print("\ttotal run time is %.2f secs over" % run_time)
    print("\tfps: %.2f" % fps)
    sys.exit(1)