Advertisement
Guest User

Untitled

a guest
Feb 6th, 2016
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.00 KB | None | 0 0
  1. import cv2
  2. video_length = 30*61 #in frames
  3.  
  4. #play the video twice, once to collect data for the right hand
  5. # and once for the left hand
  6. for i in range(2):
  7.     import time
  8.     time.sleep(3)
  9.     cap = cv2.VideoCapture('/home/sm/Desktop/7b.mp4')
  10.     frame_num = 0
  11.     in_hand = False
  12.     dwell_times = []
  13.     frame_nums = []
  14.     if i == 0: hand = 'LEFT'
  15.     if i == 1: hand = 'RIGHT'
  16.     print 'Press ESC for each catch and throw from the',hand,'hand'
  17.     while frame_num < video_length:
  18.         ret, frame = cap.read()
  19.         #if the enter key is pressed either a catch or a throw is recorded
  20.         k = cv2.waitKey(75) & 0xff
  21.         if k == 13:
  22.             if in_hand:
  23.                 print 'Throw'
  24.                 dwell_time = frame_num - catch_frame
  25.                 dwell_times.append(dwell_time)
  26.                 frame_nums.append(frame_num)
  27.                 in_hand = False
  28.             else:
  29.                 print 'Catch'
  30.                 catch_frame = frame_num
  31.                 in_hand = True
  32.         cv2.imshow('frame', frame)
  33.         frame_num+=1
  34.     print hand,' Hand Max Dwell Time',max(dwell_times)
  35.     print hand,' Hand Min Dwell Time',min(dwell_times)
  36.     import numpy as np
  37.     print hand,' Hand Average Dwell Time', np.average(dwell_times)
  38.     if hand == 'LEFT': lh_dwell_times, lh_frame_nums = dwell_times, frame_nums
  39.     if hand == 'RIGHT': rh_dwell_times, rh_frame_nums = dwell_times, frame_nums
  40.  
  41. #make graph
  42. import matplotlib.pyplot as plt
  43. plt.plot(rh_frame_nums, rh_dwell_times, color = 'red', label = 'Right hand dwell times')
  44. plt.plot(lh_frame_nums, lh_dwell_times, color = 'green', label = 'Left hand dwell times')
  45. for i in lh_frame_nums:
  46.     plt.scatter(i,lh_dwell_times[lh_frame_nums.index(i)], c='green', s=10)
  47. for i in rh_frame_nums:
  48.     plt.scatter(i,rh_dwell_times[rh_frame_nums.index(i)], c='red', s=10)
  49. plt.xlabel('Frame Number (120 frames per second)')
  50. plt.ylabel('Dwell Time (in 1/120ths of a second)')
  51. plt.legend( loc='upper left', numpoints = 1 )
  52. plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement