我在使用Python命令时遇到问题“;cascPath=sys.argv[1]&x201D;我获取错误索引器错误:列表索引超出范围
我使用的是Raspberry Pi 3模型B,安装了Raspbian、opencv 2.x和Python 3 我想访问我的USB网络摄像头并用它拍照。我已经找到了很多代码,但没有一个是有用的。我找到了一个更好的,但是当我运行命令时我在使用Python命令时遇到问题“;cascPath=sys.argv[1]&x201D;我获取错误索引器错误:列表索引超出范围,python,opencv,debian,webcam,raspberry-pi3,Python,Opencv,Debian,Webcam,Raspberry Pi3,我使用的是Raspberry Pi 3模型B,安装了Raspbian、opencv 2.x和Python 3 我想访问我的USB网络摄像头并用它拍照。我已经找到了很多代码,但没有一个是有用的。我找到了一个更好的,但是当我运行命令时 cascPath = sys.argv[1] 我得到了错误 回溯(最近一次呼叫最后一次): 文件“/home/pi/test.py”,第4行,在 cascPath=sys.argv[1] 索引器:列表索引超出范围 我只需要访问我的网络摄像头来拍照 我正在使用以下代码
cascPath = sys.argv[1]
我得到了错误
回溯(最近一次呼叫最后一次):
文件“/home/pi/test.py”,第4行,在
cascPath=sys.argv[1]
索引器:列表索引超出范围
我只需要访问我的网络摄像头来拍照
我正在使用以下代码:
import cv2
import sys
cascPath = sys.argv[1]
faceCascade = cv2.CascadeClassifier(cascPath)
video_capture = cv2.VideoCapture(0)
while True:
# Capture frame-by-frame
ret, frame = video_capture.read()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
faces = faceCascade.detectMultiScale(
gray,
scaleFactor=1.1,
minNeighbors=5,
minSize=(30, 30),
flags=cv2.cv.CV_HAAR_SCALE_IMAGE
)
# Draw a rectangle around the faces
for (x, y, w, h) in faces:
cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 2)
# Display the resulting frame
cv2.imshow('Video', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
#When everything is done, release the capture
video_capture.release()
这段代码试图识别图像上的人脸,并且
sys.argv[1]
希望您运行带有帮助识别人脸的XML文件路径的脚本
若你们不想识别人脸,那个么你们只需要这个代码就可以在监视器上显示来自摄像机的视频
import cv2
import sys
video_capture = cv2.VideoCapture(0)
while True:
# Capture frame-by-frame
ret, frame = video_capture.read()
# Display the resulting frame
cv2.imshow('Video', frame)
# exit if you press key `q`
if cv2.waitKey(1) & 0xFF == ord('q'):
break
#When everything is done, release the capture
video_capture.release()
或使用此选项保存图像
import cv2
video_capture = cv2.VideoCapture(0)
# Capture frame
ret, frame = video_capture.read()
# Write frame in file
cv2.imwrite('image.jpg', frame)
# When everything is done, release the capture
video_capture.release()
sys.argv[1]
希望您使用参数运行脚本-python test.py一些参数
。但是您可以在code-cascPath=some_参数中直接使用此参数。我认为它必须是XML文件的路径才能识别face.WOW Furas,这真的很有帮助。您能在代码中添加一个小选项吗?如果我想将图片保存在目录中的某个位置?result=cv2.imwrite(…)
如果无法写入文件,则返回值False
。如果你有正确的路径,那么你可能没有在这个地方写作的特权。是的,它起作用了,但拍摄的照片与实时提要相比更暗。如何改进?