#----------------------------------------------------# # 对视频中的predict.py进行了修改, # 将单张图片预测、摄像头检测和FPS测试功能 # 整合到了一个py文件中,通过指定mode进行模式的修改。 #----------------------------------------------------# import time import cv2 import numpy as np from retinaface import Retinaface if __name__ == "__main__": retinaface = Retinaface() #-------------------------------------------------------------------------# # mode用于指定测试的模式: # 'predict'表示单张图片预测 # 'video'表示视频检测 # 'fps'表示测试fps #-------------------------------------------------------------------------# mode = "predict" #-------------------------------------------------------------------------# # video_path用于指定视频的路径,当video_path=0时表示检测摄像头 # video_save_path表示视频保存的路径,当video_save_path=""时表示不保存 # video_fps用于保存的视频的fps # video_path、video_save_path和video_fps仅在mode='video'时有效 # 保存视频时需要ctrl+c退出才会完成完整的保存步骤,不可直接结束程序。 #-------------------------------------------------------------------------# video_path = 0 video_save_path = "" video_fps = 25.0
import tensorflow as tf import tqdm from retinaface import Retinaface from utils.utils_map import evaluation gpus = tf.config.experimental.list_physical_devices(device_type='GPU') for gpu in gpus: tf.config.experimental.set_memory_growth(gpu, True) #-------------------------------------------# # 进行retinaface的map计算 # 需要现在retinaface.py里面修改model_path #-------------------------------------------# if __name__ == '__main__': mAP_retinaface = Retinaface(confidence=0.01, nms_iou=0.5) save_folder = './widerface_evaluate/widerface_txt/' gt_dir = "./widerface_evaluate/ground_truth/" imgs_folder = './data/widerface/val/images/' sub_folders = os.listdir(imgs_folder) test_dataset = [] for sub_folder in sub_folders: image_names = os.listdir(os.path.join(imgs_folder, sub_folder)) for image_name in image_names: test_dataset.append(os.path.join(sub_folder, image_name)) num_images = len(test_dataset) for img_name in tqdm.tqdm(test_dataset): image = cv2.imread(os.path.join(imgs_folder, img_name))
#-------------------------------------# # 调用摄像头检测 #-------------------------------------# from retinaface import Retinaface from PIL import Image import numpy as np import tensorflow as tf import time import cv2 gpus = tf.config.experimental.list_physical_devices(device_type='GPU') for gpu in gpus: tf.config.experimental.set_memory_growth(gpu, True) retinaface = Retinaface() # 调用摄像头 capture = cv2.VideoCapture(0) # capture=cv2.VideoCapture("1.mp4") fps = 0.0 while (True): t1 = time.time() # 读取某一帧 ref, frame = capture.read() # 格式转变,BGRtoRGB frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) # 进行检测 frame = np.array(retinaface.detect_image(frame)) # RGBtoBGR满足opencv显示格式 frame = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR)
import os from retinaface import Retinaface ''' 在更换facenet网络后一定要重新进行人脸编码,运行encoding.py。 ''' retinaface = Retinaface(1) list_dir = os.listdir("face_dataset") image_paths = [] names = [] for name in list_dir: image_paths.append("face_dataset/" + name) names.append(name.split("_")[0]) retinaface.encode_face_dataset(image_paths, names)
''' predict.py有几个注意点 1、无法进行批量预测,如果想要批量预测,可以利用os.listdir()遍历文件夹,利用cv2.imread打开图片文件进行预测。 2、如果想要保存,利用cv2.imwrite("img.jpg", r_image)即可保存。 3、如果想要获得框的坐标,可以进入detect_image函数,读取(b[0], b[1]), (b[2], b[3])这四个值。 4、如果想要截取下目标,可以利用获取到的(b[0], b[1]), (b[2], b[3])这四个值在原图上利用矩阵的方式进行截取。 ''' import cv2 from retinaface import Retinaface retinaface = Retinaface() while True: img = input('Input image filename:') image = cv2.imread(img) if image is None: print('Open Error! Try again!') continue else: image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) r_image = retinaface.detect_image(image) r_image = cv2.cvtColor(r_image, cv2.COLOR_RGB2BGR) cv2.imshow("after", r_image) cv2.waitKey(0)
from predict import norm_size import numpy as np import tensorflow as tf from keras.preprocessing.image import img_to_array import imutils import time import cv2 from lenet import LeNet from train import CLASS_NUM mp = {'[0]': 'sun', '[1]': 'wang', '[2]': 'gong', '[3]': 'xie', '[4]': 'xu'} gpus = tf.config.experimental.list_physical_devices(device_type='GPU') for gpu in gpus: tf.config.experimental.set_memory_growth(gpu, True) retinaface = Retinaface() MODEL = 'ResNet' # model = load_model("test_sign.model") ## TODO: TEMP if MODEL == 'LeNet': model = LeNet.build(width=norm_size, height=norm_size, depth=3, classes=CLASS_NUM) else: ### from keras.applications.resnet50 import ResNet50 from keras.preprocessing import image from keras.models import Model from keras.layers import Dense, GlobalAveragePooling2D from keras import backend as K
import tensorflow as tf import keras from urllib import request import configs from retinaface import Retinaface app = flask.Flask(__name__) global graph, sess graph = tf.get_default_graph() sess = keras.backend.get_session() with sess.as_default(): with graph.as_default(): retinaface = Retinaface() @app.route('/face_detection') def face_detection(): ans = {'status': 200, 'err_msg': ''} try: image_path = flask.request.args.get('image_path') face_image = configs.get_url + '/' + image_path face_image = request.urlopen(face_image) face_image = cv2.imdecode( np.asarray(bytearray(face_image.read()), dtype="uint8"), cv2.IMREAD_COLOR) face_image = cv2.cvtColor(face_image, cv2.COLOR_BGR2RGB) print(face_image.shape) with graph.as_default():