日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python中cv2库_Python cv2库(人脸检测)

發布時間:2024/10/14 python 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python中cv2库_Python cv2库(人脸检测) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

根據訪問圖片識別

# coding:utf-8

import sys

import math

import cv2

# 待檢測的圖片路徑

imagepath = r'l.png'

face_cascade = cv2.CascadeClassifier(r'./haarcascade_frontalface_default.xml')

# 讀取圖片

image = cv2.imread(imagepath)

gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

# 探測圖片中的人臉

faces = face_cascade.detectMultiScale(

gray,

scaleFactor=1.15,

minNeighbors=5,

minSize=(5, 5),

flags=cv2.CASCADE_SCALE_IMAGE

)

print ("發現{0}個人臉!".format(len(faces)))

for (x, y, w, h) in faces:

#cv2.rectangle(image,(x,y),(x+w,y+w),(0,255,0),2)

green = (0, 255, 0)

cv2.rectangle(image, (x, y), (x + w, y + h), green, 2)

#cv2.rectangle(image, (x, y), (x + w, y + h), (0, 255, 0), 2)

#cv2.circle(image, ((x + x + w) // 2, (y + y + h) // 2), w // 2, (0, 255, 0), 2)

cv2.imshow("Find Faces!", image)

cv2.waitKey(0)

haarcascade_frontalface_default.xml文件下載:

鏈接:https://pan.baidu.com/s/1puL055J6CWa6dXks79UYkg? 密碼:r4sy

開啟攝像頭檢測

# coding:utf-8

import cv2

import numpy as np

# Load the face cascade file 按照自己的文件位置加入,如果是下載的原書的程序包那就不用改了

face_cascade = cv2.CascadeClassifier('./haarcascade_frontalface_default.xml')

# Check if the face cascade file has been loaded

if face_cascade.empty():

raise IOError('Unable to load the face cascade classifier xml file')

# Initialize the video capture object

cap = cv2.VideoCapture(0)

# Define the scaling factor

scaling_factor = 0.5

# Loop until you hit the Esc key

while True:

# Capture the current frame and resize it

ret, frame = cap.read()

frame = cv2.resize(frame, None, fx=scaling_factor, fy=scaling_factor,

interpolation=cv2.INTER_AREA)

# Convert to grayscale

gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

# Run the face detector on the grayscale image

face_rects = face_cascade.detectMultiScale(gray, 1.3, 5)

# Draw rectangles on the image

for (x, y, w, h) in face_rects:

cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 3)

# Display the image

cv2.imshow('Face Detector', frame)

# Check if Esc key has been pressed

c = cv2.waitKey(1)

if c == 27:

break

# Release the video capture object and close all windows

cap.release()

cv2.destroyAllWindows()

總結

以上是生活随笔為你收集整理的python中cv2库_Python cv2库(人脸检测)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。