processing 打开摄像头 并且捕捉的人脸以随机线条组成 颜色随机
时间: 2024-03-09 12:47:02 浏览: 30
好的,您可以通过使用 Python 中的 OpenCV 库来打开摄像头并捕获人脸,然后使用 Python 的随机库来生成随机线条和颜色。以下是示例代码:
```python
import cv2
import random
# 加载人脸检测器
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
# 打开摄像头
cap = cv2.VideoCapture(0)
while True:
# 捕获帧
ret, frame = cap.read()
# 转换为灰度图像
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# 检测人脸
faces = face_cascade.detectMultiScale(gray, 1.3, 5)
# 随机生成线条和颜色
for (x, y, w, h) in faces:
color = (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255))
thickness = random.randint(1, 5)
cv2.line(frame, (x, y), (x + w, y + h), color, thickness)
cv2.line(frame, (x, y + h), (x + w, y), color, thickness)
# 显示帧
cv2.imshow('frame', frame)
# 按 'q' 键退出
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# 释放资源
cap.release()
cv2.destroyAllWindows()
```
请注意,此代码仅在 Windows 平台上使用 Python 3.7 和 OpenCV 4.2.0 测试过。如果您使用的是其他平台或版本,则需要进行适当的修改才能正常工作。
相关推荐
![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)