简体   繁体   English

IndexError 使用 OpenCV imwrite

[英]IndexError using OpenCV imwrite

I am working on a face detection script with OpenCV.我正在使用 OpenCV 编写人脸检测脚本。

This is my code:这是我的代码:

 # 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)

But I'm getting the following error:但我收到以下错误:

This is screenshot for the error这是错误的屏幕截图

> 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 . . .

I searched the Stack Overflow but I can't find any relevant questions/answers.我搜索了 Stack Overflow,但找不到任何相关的问题/答案。

ok here is full code that save captured images from camera It saves all camera window (face and all body) I want it to save only face that covered with rectangle好的,这里是保存从相机捕获的图像的完整代码它保存所有相机 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()

this line save all image from camera:此行保存来自相机的所有图像:

cv2.imwrite(img_name, frame)

i try to save only rectangle image by this code:我尝试通过此代码仅保存矩形图像:

cv2.imwrite(img_name, frame[[[(x, y), (x+w, y+h), (0, 255, 0), 2]]])

but am still getting that error但我仍然收到那个错误

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM