cv2.VideoCapture 参数未作为输入
Posted
技术标签:
【中文标题】cv2.VideoCapture 参数未作为输入【英文标题】:cv2.VideoCapture parameter is not taking as input 【发布时间】: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()
【问题讨论】:
【参考方案1】:这是因为你在源路径的开头和结尾添加了不必要双引号"
:
s='\"'+s+'\"'
删除以上行将解决问题。
【讨论】:
是的,删除它后它起作用了。谢谢以上是关于cv2.VideoCapture 参数未作为输入的主要内容,如果未能解决你的问题,请参考以下文章
对opencv读取的图片进行像素调整(1080, 1920) 1.cv2.VideoCapture(构造图片读取) 2.cv2.nameWindow(构建视频显示的窗口) 3.cv2.setWindo
cv2.VideoCapture(0).read() 返回(假,无)