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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 人工智能 > 目标检测 >内容正文

目标检测

CV:基于Keras利用训练好的hdf5模型进行目标检测实现输出模型中的脸部表情或性别的gradcam(可视化)

發布時間:2025/3/21 目标检测 70 豆豆
生活随笔 收集整理的這篇文章主要介紹了 CV:基于Keras利用训练好的hdf5模型进行目标检测实现输出模型中的脸部表情或性别的gradcam(可视化) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

CV:基于Keras利用訓練好的hdf5模型進行目標檢測實現輸出模型中的臉部表情或性別的gradcam(可視化)

?

?

?

目錄

設計思路

核心代碼


?

?

?

設計思路

?

核心代碼

#CV:基于keras利用訓練好的hdf5模型進行目標檢測實現輸出模型中的表情或性別的gradcam——Jason Niuimport sysimport cv2 import numpy as np from keras.models import load_model# getting the correct model given the input #1、首先指定想實現人臉灰凸特征圖像(salient region detection)a檢測的是emotion還是gender # task = sys.argv[1] # class_name = sys.argv[2] task = 'emotion' # task = 'gender'#2、if條件判斷給定的是性別模型還是表情模型 if task == 'gender':model_filename = '../trained_models/gender_models/gender_mini_XCEPTION.21-0.95.hdf5'class_to_arg = get_class_to_arg('imdb') # predicted_class = class_to_arg[class_name]predicted_class = 0offsets = (0, 0) elif task == 'emotion':model_filename = '../trained_models/emotion_models/fer2013_mini_XCEPTION.102-0.66.hdf5' #默認開啟 # model_filename = '../trained_models/fer2013_big_XCEPTION.54-0.66.hdf5'class_to_arg = get_class_to_arg('fer2013') # predicted_class = class_to_arg[class_name]predicted_class = 1 # predicted_class = 'fear'offsets = (0, 0)#3、加載模型、梯度函數,指導模型、凸函數(灰凸化特征) model = load_model(model_filename, compile=False) gradient_function = compile_gradient_function(model, predicted_class, 'conv2d_7') #調用compile_gradient_function編譯梯度函數,返回名稱為conv2d_7的卷積層 register_gradient() guided_model = modify_backprop(model, 'GuidedBackProp', task) #調用modify_backprop函數,修改CNN更新為一個新的模型 saliency_function = compile_saliency_function(guided_model, 'conv2d_7') #調用compile_saliency_function函數,激活層采用conv2d_7層;saliency是指灰色圖像下凸出特征# parameters for loading data and images 加載人臉檢測識別默認庫haarcascade_frontalface_default.xml detection_model_path = '../trained_models/detection_models/haarcascade_frontalface_default.xml' face_detection = load_detection_model(detection_model_path) color = (0, 255, 0) #綠色# getting input model shapes for inference獲取輸入模型形狀進行推理(輸入hadf5庫內張量集合中的下標1~3) target_size = model.input_shape[1:3] #輸入hadf5庫內張量集合中的下標1~3# starting lists for calculating modes表情窗口列表初始化:通過計算模型,開始列表 emotion_window = []#4、打開本地攝像頭,進行實時捕捉實現salient region detection灰凸特征圖像(繪制面部方框) # starting video streaming 第一步、先定義攝像頭窗口名稱,再打開攝像頭,并開始實時讀取畫面 cv2.namedWindow('window_frame') video_capture = cv2.VideoCapture(0) #第二步、while循環間隔刷新圖像實時捕獲人臉,實現人臉變為凸優化特征圖像 while True:bgr_image = video_capture.read()[1] #從攝像設備中實時讀入圖像數據,(第一個參數[0]表示讀取是否成功,第二個參數[1]是讀取的圖像)gray_image = cv2.cvtColor(bgr_image, cv2.COLOR_BGR2GRAY) #分別將讀取的圖像進行灰化、RGB化處理rgb_image = cv2.cvtColor(bgr_image, cv2.COLOR_BGR2RGB)faces = detect_faces(face_detection, gray_image) #detect_faces函數:調用detectMultiScale函數進行識別人臉(檢測出圖片中所有的人臉),并將人臉用vector保存各個人臉的坐標、大小(用矩形表示),函數由分類器對象調用for face_coordinates in faces: #for循環對人臉表情進行實時將圖像進行灰凸化特征x1, x2, y1, y2 = apply_offsets(face_coordinates, offsets)#apply_offsets函數:大概是根據圖像實時偏移( HoG檢測窗口移動時的步長,原圖外圍添加像素)gray_face = gray_image[y1:y2, x1:x2] #[坐標參數,尺寸參數]try:gray_face = cv2.resize(gray_face, (target_size))#cv2.resize(image, image2,dsize) 圖像縮放方法;即(輸入原始圖像,輸出新圖像,圖像的大小)except:continuegray_face = preprocess_input(gray_face, True) #preprocess_input函數先將gray_face轉換為'float32'然后 /255.0gray_face = np.expand_dims(gray_face, 0) #在標簽數據上增加一個維度,0是增加在第一個軸上gray_face = np.expand_dims(gray_face, -1) guided_gradCAM = calculate_guided_gradient_CAM(gray_face,gradient_function, saliency_function) #calculate_guided_gradient_CAM函數?guided_gradCAM = cv2.resize(guided_gradCAM, (x2-x1, y2-y1))try:rgb_guided_gradCAM = np.repeat(guided_gradCAM[:, :, np.newaxis],3, axis=2)rgb_image[y1:y2, x1:x2, :] = rgb_guided_gradCAMexcept:continuedraw_bounding_box((x1, y1, x2 - x1, y2 - y1), rgb_image, color) #draw_bounding_box函數:在人臉區域畫一個正方形出來#輸出圖像先顏色空間轉換,然后命名窗口、顯示窗口、結束程序條件:cv2.waitKey函數用來檢測特定鍵q是否被按下,則break直接跳出當前循環,也就是結束了bgr_image = cv2.cvtColor(rgb_image, cv2.COLOR_RGB2BGR) #顏色空間轉換#命名窗口、顯示窗口、結束程序條件:cv2.waitKey函數用來檢測特定鍵q是否被按下,則break退出程序try:cv2.imshow('window_frame', bgr_image)except:continueif cv2.waitKey(1) & 0xFF == ord('q'):break

?

總結

以上是生活随笔為你收集整理的CV:基于Keras利用训练好的hdf5模型进行目标检测实现输出模型中的脸部表情或性别的gradcam(可视化)的全部內容,希望文章能夠幫你解決所遇到的問題。

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