print("Detected animal faces: ", len(animal_faces)) for (x, y, w, h) in animal_faces: print("Face size: ", w, "x", h)
时间: 2024-04-15 22:31:21 浏览: 23
这段代码将打印检测到的动物脸部数量以及每个脸部的大小(宽度和高度)。你可以将它添加到上面的示例代码中的相应位置来实现这个功能。完整的代码如下:
```python
import cv2
# 加载动物脸部识别器(例如猫脸识别器)
animal_cascade = cv2.CascadeClassifier('path_to_animal_cascade.xml')
# 读取原图
img = cv2.imread('path_to_image.jpg')
# 将图像转换为灰度图像
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# 检测图像中的动物脸部
animal_faces = animal_cascade.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=5, minSize=(30, 30))
# 打印检测到的动物脸部数量及其大小
print("Detected animal faces: ", len(animal_faces))
for (x, y, w, h) in animal_faces:
print("Face size: ", w, "x", h)
# 在图像中标记动物脸部位置
for (x, y, w, h) in animal_faces:
cv2.rectangle(img, (x, y), (x+w, y+h), (0, 255, 0), 2)
# 显示标记后的图像
cv2.imshow('Animal Face Detection', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
```
在运行代码时,请确保将`path_to_animal_cascade.xml`替换为你选择的动物脸部识别器的文件路径,将`path_to_image.jpg`替换为你的图像文件路径。代码将首先打印检测到的动物脸部数量,然后打印每个脸部的宽度和高度。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)