我正在尝试从用户动态访问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):循环。我想念什么吗?

最佳答案

输入不带引号的文件名。它工作正常。因为输入具有字母,所以它已经是字符串对象。添加引号就像输入错误的文件名。正如我在评论中所说,如果输入的文件名不存在,那么videocapture有时不会引发错误。希望这可以帮助

关于python - cv2.videoCapture(filename)动态分配文件名(已关闭),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/43776341/

10-15 15:41