Created
          October 11, 2018 11:58 
        
      - 
      
- 
        Save skipperkongen/8dfc4095d22be334e8ee81d8d8b4ba36 to your computer and use it in GitHub Desktop. 
    Inference on video
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | # Infer on live video | |
| from math import ceil | |
| import subprocess | |
| import cv2 | |
| TEST_FRAMES = 500 | |
| # Initialize camera | |
| cap = cv2.VideoCapture(0) | |
| # Check if camera opened successfully | |
| if (cap.isOpened() == False): | |
| print("Unable to read camera feed") | |
| TEST_FRAMES = 0 | |
| # Start processing video | |
| for i in range(TEST_FRAMES): | |
| ret, frame = cap.read() | |
| if not ret: continue | |
| x_pred = frame_preprocessor.process(frame) | |
| y_pred = model.predict(x_pred)[0] | |
| conf_negative = y_pred[NEG_IDX] | |
| conf_positive = y_pred[POS_IDX] | |
| cla = CLASSES[np.argmax(y_pred)] | |
| if cla == CLASSES[POS_IDX]: | |
| subprocess.call(['say', CLASSES[POS_IDX]]) | |
| progress = int(100 * (i / TEST_FRAMES)) | |
| message = 'testing {}% conf_neg = {:.02f} conf_pos = {:.02f} class = {} \r'.format(progress, conf_negative, conf_positive, cla) | |
| sys.stdout.write(message) | |
| sys.stdout.flush() | |
| cap.release() | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment