|
| 1 | + |
| 2 | +# coding: utf-8 |
| 3 | + |
| 4 | +# In[77]: |
| 5 | + |
| 6 | + |
| 7 | +import os |
| 8 | +import cv2 |
| 9 | +import time |
| 10 | + |
| 11 | + |
| 12 | +# In[78]: |
| 13 | + |
| 14 | + |
| 15 | +gesture_name = raw_input("Enter Gesture Name ") |
| 16 | + |
| 17 | + |
| 18 | +# In[79]: |
| 19 | + |
| 20 | + |
| 21 | +images = [] |
| 22 | +num_frames = 60 |
| 23 | + |
| 24 | + |
| 25 | +# In[80]: |
| 26 | + |
| 27 | + |
| 28 | +capture_start = False |
| 29 | +cv2.namedWindow("Video", cv2.WINDOW_NORMAL) |
| 30 | + |
| 31 | +vc = cv2.VideoCapture(0) |
| 32 | +vc.set(cv2.CAP_PROP_FPS, 5) |
| 33 | + |
| 34 | +rval, frame = vc.read() |
| 35 | + |
| 36 | +cnt = 0 |
| 37 | + |
| 38 | +while True: |
| 39 | + |
| 40 | + if frame is not None: |
| 41 | + |
| 42 | + frame = cv2.flip(frame, 1) |
| 43 | + cv2.imshow("Video", frame) |
| 44 | + |
| 45 | + rval, frame = vc.read() |
| 46 | + keypress = cv2.waitKey(1) |
| 47 | + |
| 48 | + if keypress == ord('q'): |
| 49 | + break |
| 50 | + elif keypress == ord('c'): |
| 51 | + capture_start = True |
| 52 | + |
| 53 | + if ( capture_start ): |
| 54 | + img = cv2.flip(frame, 1) |
| 55 | + #img = cv2.cvtColor( img, cv2.COLOR_RGB2BGR ) |
| 56 | + images.append(img) |
| 57 | + cnt += 1 |
| 58 | + if ( cnt > num_frames ): |
| 59 | + break |
| 60 | + |
| 61 | +vc.release() |
| 62 | +cv2.destroyAllWindows() |
| 63 | +cv2.waitKey(1) |
| 64 | + |
| 65 | + |
| 66 | +# In[81]: |
| 67 | + |
| 68 | + |
| 69 | +if len(images) == 0: |
| 70 | + print(" ERROR !! No Frames Recorded" ) |
| 71 | + exit() |
| 72 | + |
| 73 | + |
| 74 | +# In[83]: |
| 75 | + |
| 76 | + |
| 77 | +last_num = 0 |
| 78 | +folder_name = gesture_name |
| 79 | +gestures_rec = os.listdir('./') |
| 80 | + |
| 81 | +for i in gestures_rec: |
| 82 | + |
| 83 | + if i[0] == '.': |
| 84 | + continue |
| 85 | + if '_'.join( i.split('_')[:-1] ) == gesture_name: |
| 86 | + last_num = max(last_num, int( i.split('_')[-1] ) ) |
| 87 | + |
| 88 | +last_num += 1 |
| 89 | +folder_name = gesture_name + '_' + str(last_num) |
| 90 | +try: |
| 91 | + print('[i] Creating directory {}...'.format(folder_name)) |
| 92 | + os.makedirs(folder_name) |
| 93 | +except (IOError) as e: |
| 94 | + print('[!]', str(e)) |
| 95 | + exit() |
| 96 | + |
| 97 | + |
| 98 | +# In[84]: |
| 99 | + |
| 100 | + |
| 101 | +print ("[i] Creating Video") |
| 102 | +output = './' + folder_name + '/output.mp4' |
| 103 | +fourcc = cv2.VideoWriter_fourcc(*'mp4v') # Be sure to use lower case |
| 104 | +out = cv2.VideoWriter(output, fourcc, 5.0, (images[0].shape[1], images[0].shape[0]) ) |
| 105 | + |
| 106 | +for image in images: |
| 107 | + |
| 108 | + f = image |
| 109 | + #print (f.shape) |
| 110 | + out.write(f) # Write out frame to video |
| 111 | + |
| 112 | + #cv2.imshow('video',frame) |
| 113 | + if (cv2.waitKey(1) & 0xFF) == ord('q'): # Hit `q` to exit |
| 114 | + break |
| 115 | + |
| 116 | +# Release everything if job is finished |
| 117 | +out.release() |
| 118 | +cv2.destroyAllWindows() |
| 119 | + |
| 120 | + |
| 121 | +# In[ ]: |
| 122 | + |
| 123 | + |
| 124 | +print ("Output Video is in", output) |
| 125 | + |
0 commit comments