【问题标题】:cv2.VideoCapture parameter is not taking as inputcv2.VideoCapture 参数未作为输入
【发布时间】:2021-10-24 11:03:00
【问题描述】:

我想从 cv2 和 Tkinter 创建一个图像检测应用程序。我的计划是将绝对路径作为 cv2.videocapture() 的输入参数。但它不作为输入并给出 (-215:Assertion failed) !_src .empty() in function cv::cvtColor 错误。没有输入,我尝试更改斜线标记(/)。任何人都可以帮助解决这个问题

import cv2
import numpy as np
from tkinter import *
import os
import csv

'''Windows Interface'''
window = Tk()
window.title("Face Detection")
window.geometry('500x100+500+100')

#testbox
value=StringVar()
e=Entry(window,textvariable=value,width=50)
e.pack()

#command Function
def myclick():
   return value.get()

#Label for instruction
lbl=Label(window,text="Enter the Address")
lbl.place(x=100, y=100)

#button
mybutton=Button(window,text="Confirm",command=myclick)
mybutton.place(x=1, y=100)
mybutton.pack()

window.mainloop()

# Loading image
s=myclick()

s='\"'+s+'\"'

print(s)
print(type(s))

face_cascade=cv2.CascadeClassifier("haarcascade_frontalface_default.xml")
cap = cv2.VideoCapture(s)
cap.set(3, 640) # set video widht
cap.set(4, 480) # set video height
minW = 0.1*cap.get(3)
minH = 0.1*cap.get(4)

while True:
        # Read the frame
        _, img = cap.read()
        # Convert to grayscale
        gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
        # Detect the faces
        faces = face_cascade.detectMultiScale(gray, 1.01,1)
        # Draw the rectangle around each face
        for (x, y, w, h) in faces:
            cv2.rectangle(img, (x, y), (x + w, y + h), (255, 0, 0), 2)
        # Display
        cv2.imshow('img', img)
        # Stop if escape key is pressed
        k = cv2.waitKey(30) & 0xff
        if k == 27:
            break
    # Release the VideoCapture object
cap.release()
cv2.destroyAllWindows()

【问题讨论】:

    标签: python tkinter video-capture cv2 haar-classifier


    【解决方案1】:

    这是因为你在源路径的开头和结尾添加了不必要双引号"

    s='\"'+s+'\"'
    

    删除以上行将解决问题。

    【讨论】:

    • 是的,删除它后它起作用了。谢谢
    猜你喜欢
    • 1970-01-01
    • 2021-12-20
    • 2017-07-10
    • 1970-01-01
    • 2015-06-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多