使用OpenCV imwrite的索引器

2024-09-30 12:21:49 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用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)

但我得到了以下错误:

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

我搜索了堆栈溢出,但找不到任何相关的问题/答案


Tags: thenameinpyimgforcv2frame
1条回答
网友
1楼 · 发布于 2024-09-30 12:21:49

好的,这是保存相机拍摄图像的完整代码 它保存所有摄影机窗口(面和全身) 我希望它只保存覆盖着矩形的面

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

但我还是犯了那个错误

相关问题 更多 >

    热门问题