代码:
import cv2 color = (0, 0, 255) # reverse of RGB (B,G,R) - weird strokeWeight = 1 # thickness of outline # set window name windowName = "Object Detection" # load an image to search for faces img = cv2.imread("mao4.jpg") # load detection file (various files for different views and uses) cascade = cv2.CascadeClassifier("haarcascade_frontalface_alt.xml") cascade.load('E:/python/projects/imageProcess/venv/Lib/site-packages/cv2/data/haarcascade_frontalface_alt.xml') # detect objects, return as list rects = cascade.detectMultiScale(img) while True: # get a list of rectangles for x, y, width, height in rects: cv2.rectangle(img, (x, y), (x + width, y + height), color, strokeWeight) # display! cv2.imshow(windowName, img) # escape key (ASCII 27) closes window if cv2.waitKey(20) == 27: break # if esc key is hit, quit! exit()