[英]IndexError using OpenCV imwrite
我正在使用 OpenCV 編寫人臉檢測腳本。
這是我的代碼:
# 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)
if k%256 == 32:
# SPACE pressed
print(my_random_string(9)) # For example, D9E50C
img_name = "face_{}.png".format(img_counter)
cv2.imwrite(img_name, frame[[[(x, y), (x+w, y+h), (0, 255, 0), 2]]])
print("{} written!".format(img_name))
img_counter += 1
# Display the resulting frame
cv2.imshow('FaceDetection', frame)
但我收到以下錯誤:
> c:\Users\Bakri\Desktop\FaceDetection-master\FaceDetection.py:39: FutureWarning: Using a non-tuple sequence for multidimensional indexing is deprecated; use `arr[tuple(seq)]` instead of `arr[seq]`. In the future this will be interpreted as an array index, `arr[np.array(seq)]`, which will result either in an error or a different result.
cv2.imwrite(img_name, frame[[[(x, y), (x+w, y+h), (0, 255, 0), 2]]])
Traceback (most recent call last):
File "C:\Users\Bakri\AppData\Local\Programs\Python\Python36\lib\runpy.py", line 193, in _run_module_as_main
"__main__", mod_spec)
File "c:\Users\Bakri\Desktop\FaceDetection-master\FaceDetection.py", line 39, in <module>
cv2.imwrite(img_name, frame[[[(x, y), (x+w, y+h), (0, 255, 0), 2]]])
IndexError: only integers, slices (`:`), ellipsis (`...`), numpy.newaxis (`None`) and integer or boolean arrays are valid indices
Press any key to continue . . .
我搜索了 Stack Overflow,但找不到任何相關的問題/答案。
好的,這里是保存從相機捕獲的圖像的完整代碼它保存所有相機 window(臉部和整個身體)我希望它只保存被矩形覆蓋的臉部
import uuid
import cv2
import sys
faceCascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
video_capture = cv2.VideoCapture('http://192.168.43.1:8080/video')
img_counter = 0
def my_random_string(string_length=10):
"""Returns a random string of length string_length."""
random = str(uuid.uuid4()) # Convert UUID format to a Python string.
random = random.upper() # Make all characters uppercase.
random = random.replace("-","") # Remove the UUID '-'.
return random[0:string_length] # Return the random string.
while True:
# Capture frame-by-frame
ret, frame = video_capture.read()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
k = cv2.waitKey(1)
faces = faceCascade.detectMultiScale(
gray,
scaleFactor=1.5,
minNeighbors=5,
minSize=(30, 30),
flags=cv2.CASCADE_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)
if k%256 == 32:
# SPACE pressed
print(my_random_string(9)) # For example, D9E50C
img_name = "face_{}.png".format(img_counter)
#cv2.imwrite(img_name, frame)
cv2.imwrite(img_name, frame[[[(x, y), (x+w, y+h), (0, 255, 0), 2]]])
print("{} written!".format(img_name))
img_counter += 1
# Display the resulting frame
cv2.imshow('FaceDetection', frame)
if k%256 == 27: #ESC Pressed
break
elif k%256 == 32:
# SPACE pressed
print('')
#print(my_random_string(9)) # For example, D9E50C
#img_name = "facedetect_webcam_{}.png".format(img_counter)
#cv2.imwrite(img_name, frame)
#print("{} written!".format(img_name))
#img_counter += 1
# When everything is done, release the capture
video_capture.release()
cv2.destroyAllWindows()
此行保存來自相機的所有圖像:
cv2.imwrite(img_name, frame)
我嘗試通過此代碼僅保存矩形圖像:
cv2.imwrite(img_name, frame[[[(x, y), (x+w, y+h), (0, 255, 0), 2]]])
但我仍然收到那個錯誤
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.