Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import cv2
- video_length = 30*61 #in frames
- #play the video twice, once to collect data for the right hand
- # and once for the left hand
- for i in range(2):
- import time
- time.sleep(3)
- cap = cv2.VideoCapture('/home/sm/Desktop/7b.mp4')
- frame_num = 0
- in_hand = False
- dwell_times = []
- frame_nums = []
- if i == 0: hand = 'LEFT'
- if i == 1: hand = 'RIGHT'
- print 'Press ESC for each catch and throw from the',hand,'hand'
- while frame_num < video_length:
- ret, frame = cap.read()
- #if the enter key is pressed either a catch or a throw is recorded
- k = cv2.waitKey(75) & 0xff
- if k == 13:
- if in_hand:
- print 'Throw'
- dwell_time = frame_num - catch_frame
- dwell_times.append(dwell_time)
- frame_nums.append(frame_num)
- in_hand = False
- else:
- print 'Catch'
- catch_frame = frame_num
- in_hand = True
- cv2.imshow('frame', frame)
- frame_num+=1
- print hand,' Hand Max Dwell Time',max(dwell_times)
- print hand,' Hand Min Dwell Time',min(dwell_times)
- import numpy as np
- print hand,' Hand Average Dwell Time', np.average(dwell_times)
- if hand == 'LEFT': lh_dwell_times, lh_frame_nums = dwell_times, frame_nums
- if hand == 'RIGHT': rh_dwell_times, rh_frame_nums = dwell_times, frame_nums
- #make graph
- import matplotlib.pyplot as plt
- plt.plot(rh_frame_nums, rh_dwell_times, color = 'red', label = 'Right hand dwell times')
- plt.plot(lh_frame_nums, lh_dwell_times, color = 'green', label = 'Left hand dwell times')
- for i in lh_frame_nums:
- plt.scatter(i,lh_dwell_times[lh_frame_nums.index(i)], c='green', s=10)
- for i in rh_frame_nums:
- plt.scatter(i,rh_dwell_times[rh_frame_nums.index(i)], c='red', s=10)
- plt.xlabel('Frame Number (120 frames per second)')
- plt.ylabel('Dwell Time (in 1/120ths of a second)')
- plt.legend( loc='upper left', numpoints = 1 )
- plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement