使用opencv检测图片中是否有人物
Created At :
Count:326
Views 👀 :
使用opencv检测图片中是否有人物
""" @Project :all_daily_tasks_code @File :使用opencv检测图片中是否有人物.py @Author :木子 @Date :2024/5/29 上午11:25 """ import os import shutil
import sys import cv2
cur_path = os.path.abspath(__file__)
father_path = os.path.abspath(os.path.dirname(cur_path) + os.path.sep + ".")
sys.path.append(father_path)
def detect_person(image_path): img = cv2.imread(image_path) if img is None: print(f"Error: Unable to load image at {image_path}") return gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml') faces = face_cascade.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=5) if len(faces) > 0: return True else: return False
if __name__ == '__main__': _dir = r"F:\upload\others\ios\img\chat" pho = r"F:\upload\others\ios\img\photo" for item in os.listdir(_dir): t = os.path.join(_dir, item) if detect_person(t): _dst = os.path.join(pho, item) shutil.move(t, _dst) print(f"{item} 已移动到 {_dst}") else: print(f"{item} 无人脸")
|