鸿蒙IPC摄像机,【HiSpark IPC DIY Camera试用连载 】第二篇 视频的人脸检测
【HiSpark IPC DIY Camera試用連載 】第二篇 視頻的人臉檢測,
??
本帖最后由 瑟寒凌風 于 2021-1-14 00:13 編輯
實在的,ipc本身就帶有這個功能,只要攝像頭出現畫面,本身就會檢測人臉,而且從app上看,還有移動檢測功能,已經非常強大了,但是自己做出來,可以了解更多的知識. 本文使用python和opencv來檢測的人臉. 使用的主要api如下:
cv2.cvtColor對色彩進行轉換
cv2.CascadeClassifier這個是輸入人臉數據,該數據基于文件haarcascade_frontalface_alt_tree.xml,已經保存了人臉特征,不需要我們去實現
cv2.detectMultiScale該函數用來檢測人臉,是本文處理人臉的主要函數
復制代碼 Opencv打開攝像頭使用函數:
camera = cv2.VideoCapture(0)??# 參數0表示第一個攝像頭
復制代碼 程序代碼如下
# -*- coding: utf-8 -*-
import cv2
import numpy as np
camera = cv2.VideoCapture(0)
# 判斷視頻是否打開
if (camera.isOpened())
print(\“Open\“)
else:
print(\“攝像頭未打開\“)
# 測試用,查看視頻size
size = (int(camera.get(cv2.CAP_PROP_FRAME_WIDTH)),
int(camera.get(cv2.CAP_PROP_FRAME_HEIGHT)))
print(\“size:\“ + repr(size))
# es = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (9, 4))
# kernel = np.ones((5, 5), np.uint8)
# background = None
while True:
# 讀取視頻流
grabbed, frame_lwpCV = camera.read()
gray_lwpCV = cv2.cvtColor(frame_lwpCV, cv2.COLOR_BGR2GRAY)
face_detector = cv2.CascadeClassifier(“D:/Python/Python39/Lib/site-packages/cv2/data/haarcascade_frontalface_alt_tree.xml“)
\“\“\“
cv2.cv2.CascadeClassifier.CascadeClassifier def __init__(self,
*args: Any,
**kwargs: Any) -> None
\“\“\“
faces = face_detector.detectMultiScale(gray_lwpCV, 1.02, 5)
\“\“\“
def detectMultiScale(self,
image: Any,? ?? ?? ?? ?? ? # 輸入待檢測的圖像,灰度
scaleFactor: Any = None,? ?# 尺度系數
minNeighbors: Any = None,??# 需要的鄰域數
flags: Any = None,
minSize: Any = None,? ?? ? # 最小檢測窗口
maxSize: Any = None) -> None? ?# 最大檢測窗口
\“\“\“
for x, y, w, h in faces:
cv2.rectangle(frame_lwpCV, (x, y), (x + w, y + h), (0, 0, 255), 2)??# 繪制矩形
# cv2.rectangle(frame_lwpCV, (x, y), (x + w, y + h), (0, 255, 0), 2)
# cv2.rectangle(src, (x, y), (x + w, y + h), (0, 0, 255), 2)??# 繪制矩形
\“\“\“
def rectangle(img: Any,? ?? ?? ?? ? # 圖像
pt1: Any,? ?? ?? ?? ?? ?? ?? ?# pt1 矩形的一個頂點
pt2: Any,? ?? ?? ?? ?? ?? ?? ?# pt2 矩形對角線上的另一個頂點
color: Any,? ?? ?? ?? ?? ?? ? # 線條顏色 (RGB) 或亮度(灰度圖像 )
thickness: Any = None,? ?? ???# 組成矩形的線條的粗細程度,取負值時(如 CV_FILLED)函數繪制填充了色彩的矩形
lineType: Any = None,? ?? ?? ?# 線條的類型
shift: Any = None) -> None
\“\“\“
cv2.imshow(\“contours\“, frame_lwpCV)
# cv2.imshow(\“dis\“, diff)
key = cv2.waitKey(1) & 0xFF
# 按\“q\“健退出循環
if key == ord(\“q\“):
break
# When everything done, release the capture
camera.release()
cv2.destroyAllWindows()
PS:我在發現代碼格式亂了之后,嘗試修改,出現如下錯誤
QQ截圖20210114001021.jpg (38.17 KB, 下載次數: 0)
下載附件??保存到相冊
1 小時前 上傳
沒法改,尷尬...
總結
以上是生活随笔為你收集整理的鸿蒙IPC摄像机,【HiSpark IPC DIY Camera试用连载 】第二篇 视频的人脸检测的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 关键帧和动画
- 下一篇: 深度学习(二)——从零自己制作数据集到利