import cv2 import numpy as np import os import bottle def fetch_face_pic(img,face_cascade): # 将图像灰度化 gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # 人脸检测 faces,rl,wl = face_cascade.detectMultiScale3(gray,scaleFactor=1.1, minNeighbors=3, minSize=(30, 30), flags = cv2.CASCADE_SCALE_IMAGE, outputRejectLevels = True ) #输出分数最高的 ol = 0 for i,(x,y,w,h) in enumerate(faces): if wl[i][0] > ol: crop = img[y:y+h, x:x+w] # 使用切片操作直接提取感兴趣的区域 print(wl[i][0],ol,x,y,w,h) ol = wl[i][0] return crop face_cascade = cv2.CascadeClassifier('/root/girl/opencv-master/data/haarcascades/haarcascade_frontalface_default.xml') @bottle.route('/find/<w>', method='GET') def do_find(w): jaffe_pic = '/web/maps.cc/public/girl/img/' + w img = cv2.imread(jaffe_pic) crop = fetch_face_pic(img,face_cascade) if crop is not None: cv2.imwrite("/web/maps.cc/public/girl/thumb/"+w,crop) return w return "" bottle.run(host='0.0.0.0', port=8080)