cv2.videoCapture(filename)动态分配文件名(已关闭)

时间:2017-05-04 07:18:49

标签: python opencv computer-vision face-recognition raw-input

我尝试从用户动态访问file_name,然后将其传递给videoCapture(file_name),然后进行处理。

代码:

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):循环。我错过了什么吗?

1 个答案:

答案 0 :(得分:1)

输入不带引号的文件名。它工作正常。因为输入有字母,所以它已经是字符串对象。添加引号就像输入错误的文件名一样。正如我在评论中所说,如果输入的文件名不存在,则视频捕获有时不会抛出错误。希望这有帮助