2017-05-04 102 views
0

我試圖從用戶動態訪問file_name,然後將它傳遞給videoCapture(file_name)然後處理它。cv2.videoCapture(文件名)動態分配文件名(關閉)

代碼:

import cv2 
import numpy as np 
import os 
import sqlite3 
import pickle 
from PIL import Image 
import sys 


faceDetect = cv2.CascadeClassifier('haarcascade_frontalface_default.xml'); 
rec = cv2.createLBPHFaceRecognizer(); 


''' 
Dynamically accessing the fileName 

Error seems to be here in the following couple of codes 
Note: i am assigning file_name as <"test.mp4"> 
''' 
file_name = raw_input("Enter file name: ") 
print file_name 


cam = cv2.VideoCapture(file_name) 

while cam.isOpened(): 
    ret,img = cam.read() 

    if ret == True: 
     gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)   
     faces = faceDetect.detectMultiScale(gray,1.3,5); 
     for(x,y,w,h) in faces : 
      cv2.rectangle(img,(x,y),(x+w,y+h),(0,255,0),2) 
      id,conf=rec.predict(gray[y:y+h,x:x+w]) 
      ''' 
      Few lines of code 
      ''' 

     cv2.imshow("Face",img); 
     if (cv2.waitKey(1) == ord('q')): 
      break; 
    else : 
     print ('ret is false') 
     break 
cam.release() 
cv2.destroyAllWindows() 

它沒有顯示出錯誤,但它不執行while(cam.isOpened):循環。我錯過了什麼?

+0

如果你硬編碼的文件名工程? –

+0

是的。如果cam = cv2.VideoCapture(file_name)被替換爲cam = cv2.VideoCapture(「test.mp4」),它工作正常。 –

+1

試過我的機器上的代碼,工作正常。 ret說什麼?它是真的還是不是?我之前觀察到,VideoCapture在許多情況下不會拋出錯誤。 – harshkn

回答

1

輸入不帶引號的文件名。它工作正常。因爲由於輸入有字母表,它已經是字符串對象了。添加引號就像輸入錯誤的文件名稱一樣。正如我在評論中所說,如果輸入的文件名不存在,videocapture有時不會拋出錯誤。希望這有助於