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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 人工智能 > pytorch >内容正文

pytorch

dlib实现人脸检测方法

發布時間:2024/3/24 pytorch 64 豆豆
生活随笔 收集整理的這篇文章主要介紹了 dlib实现人脸检测方法 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

文章目錄

  • `dlib`概述
  • 人臉檢測
    • CPU版本人臉檢測算法
      • 檢測步驟
      • 示例代碼:
    • CUDA版本人臉檢測算法
      • 檢測步驟
      • 示例代碼
    • 類定義與接口源碼
      • 人臉檢測中用到的重要的類(概述)
      • `fhog_object_detector`類接口定義
      • `rectangle`類接口定義
      • `cnn_face_detection_model_v1`類定義
      • `mmod_rectangle` `mmod_rectangles` `mmod_rectangless`

dlib概述

Dlib是一個包含機器學習算法的C++開源工具包。Dlib可以幫助您創建很多復雜的機器學習方面的軟件來幫助解決實際問題。目前Dlib已經被廣泛的用在行業和學術領域,包括機器人,嵌入式設備,移動電話和大型高性能計算環境.

人臉檢測

CPU版本人臉檢測算法

檢測步驟

  • 獲取hog detector
  • 傳入image,數據格式為numpy.ndarray
  • (可選項) 獲取分數值和檢測列表
  • 獲取人臉坐標
  • 示例代碼:

    import dlibfrom cv2 import cv2# step 1. create an object detector based on hog detector = dlib.get_frontal_face_detector() # _dlib_pybind11.fhog_object_detector# step 2. read an image using dlib or cv2 # note that the difference between the image data formated as numpy.ndarray read by dlib and cv2 is that dlib read it channels as *R G B* order while cv2 read as *B G R*,so you should do one more step to convert the image if using cv2 image_path = "sample.jpg" img = dlib.load_rgb_image(image_path) # img = cv2.imread(image_path) # img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)# step 3. using the numpy.ndarray image data as input to detect the front face on the image # The 1 in the second argument indicates that we should upsample the image 1 time. # This will make everything bigger and allow us to detect more faces. detections = detector(img, 1) # List[_dlib_pybind11.rectangle]# step 3.1 (Optional) if you want to get more detail information,using function run() instead # detections, scores, idx = detector.run(img, 1, 0.5) # List[_dlib_pybind11.rectangle] List[int] List[int]# step 4. get point coordinates from the detection results # let's just fetch one instead all of the in a loop detection = detections[0] left,top,right,bottom = detection .left(),detection .top(),detection .right(),detection .bottom()# step x : now you can do whatever you want since you've already got what you want.

    CUDA版本人臉檢測算法

    檢測步驟

  • 下載模型文件
  • 加載模型文件,生成卷積神經網絡人臉檢測對象
  • 傳入image,數據格式為numpy.ndarray
  • (可選項) 獲取分數值和檢測列表
  • 獲取人臉坐標
  • 示例代碼

    import dlib # step 1. make sure you have downloaded the correct model file face_detector_model_path = '../models/mmod_human_face_detector.dat'# step 2. load this model and create a cnn face detector face_detector = dlib.cnn_face_detection_model_v1(face_detector_model_path) # dlib.cnn_face_detection_model_v1# step 3. read an image using dlib or cv2 # note that the difference between the image data formated as numpy.ndarray read by dlib and cv2 is that dlib read it channels as *R G B* order while cv2 read as *B G R*,so you should do one more step to convert the image if using cv2 image_path = "sample.jpg" img = dlib.load_rgb_image(image_path) # img = cv2.imread(image_path) # img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)# step 4. predict and detect detections = face_detector(img, 1) # dlib.mmod_rectangles# step 5. get just one of the rectangle instead all of them ,the type is mmod_rectangle detection = detections[0] # dlib.mmod_rectangle # the mmod_rectangle contains two parts : confidence and rect print(detection.confidence, detection.rect)# step 6.get face coordinates for just one as sample left,top,right,bottom = detection.rect.left(),detection.rect.top(),detection.rect.right(),detection.rect.bottom()# step x. do anything you would like to

    類定義與接口源碼

    人臉檢測中用到的重要的類(概述)

  • dlib.fhog_object_detector : hog模型的人臉檢測對象,常用方法: __call()__和run()
  • dlib.rectangle:人臉檢測結果,用于表示人臉的矩形區域,常用方法:left() right()``top()``bottom()
  • dlib.cnn_face_detection_model_v1:卷積神經網絡模型的人臉檢測對象,常用方法: __call()__
  • dlib.mmod_rectangle:人臉檢測結果,包含了表示人臉的巨型區域以及檢測置信度,成員包含: rect和confidence,其中,rect為 dlib.rectangle類型,confidence為float類型
  • dlib.mmod_rectangles:包含多個 dlib.mmod_rectangle對象
  • 類定義參考鏈接:Python API 鏈接

    fhog_object_detector類接口定義

    fhog_object_detector類在源碼中為C++類,這里使用偽代碼編譯觀察其接口與調用方法

    class dlib.fhog_object_detector():"""This object represents a sliding window histogram-of-oriented-gradients based object detector."""def __call__(self: dlib.fhog_object_detector, image: array, upsample_num_times: int = 0L) -> dlib.rectangles:"""requiresimage is a numpy ndarray containing either an 8bit grayscale or RGB image.upsample_num_times >= 0ensuresThis function runs the object detector on the input image and returns a list of detections.Upsamples the image upsample_num_times before running the basic detector."""def __init__(self: dlib.fhog_object_detector, arg0: unicode) -> None:'''Loads an object detector from a file that contains the output of the train_simple_object_detector() routine or a serialized C++ object of type object_detector<scan_fhog_pyramid<pyramid_down<6>>>.detection_window_heightdetection_window_widthnum_detectors'''passdef run(self: dlib.fhog_object_detector, image: array, upsample_num_times: int = 0L,adjust_threshold: float = 0.0) -> tuple:"""requiresimage is a numpy ndarray containing either an 8bit grayscale or RGB image.upsample_num_times >= 0ensuresThis function runs the object detector on the input image and returns a tuple of (list of detections, list of scores, list of weight_indices).Upsamples the image upsample_num_times before running the basic detector."""passdef run_multiple(detectors: list, image: array, upsample_num_times: int = 0L, adjust_threshold: float = 0.0)->tuple:"""requiresdetectors is a list of detectors.image is a numpy ndarray containing either an 8bit grayscale or RGB image.upsample_num_times >= 0ensuresThis function runs the list of object detectors at once on the input image and returns a tuple of (list of detections, list of scores, list of weight_indices).Upsamples the image upsample_num_times before running the basic detector."""passdef save(self: dlib.fhog_object_detector, detector_output_filename: unicode)->None:'''Save a simple_object_detector to the provided path.'''pass

    rectangle類接口定義

    class dlib.rectangleThis object represents a rectangular area of an image.__init__(*args, **kwargs)Overloaded function.__init__(self: dlib.rectangle, left: int, top: int, right: int, bottom: int) -> None__init__(self: dlib.rectangle, rect: dlib::drectangle) -> None__init__(self: dlib.rectangle, rect: dlib.rectangle) -> None__init__(self: dlib.rectangle) -> Nonearea(self: dlib.rectangle) → intbl_corner(self: dlib.rectangle) → dlib.pointReturns the bottom left corner of the rectangle.bottom(self: dlib.rectangle) → intbr_corner(self: dlib.rectangle) → dlib.pointReturns the bottom right corner of the rectangle.center(self: dlib.rectangle) → dlib.pointcontains(*args, **kwargs)Overloaded function.contains(self: dlib.rectangle, point: dlib.point) -> boolcontains(self: dlib.rectangle, point: dlib.dpoint) -> boolcontains(self: dlib.rectangle, x: int, y: int) -> boolcontains(self: dlib.rectangle, rectangle: dlib.rectangle) -> booldcenter(self: dlib.rectangle) → dlib.pointheight(self: dlib.rectangle) → intintersect(self: dlib.rectangle, rectangle: dlib.rectangle) → dlib.rectangleis_empty(self: dlib.rectangle) → boolleft(self: dlib.rectangle) → intright(self: dlib.rectangle) → inttl_corner(self: dlib.rectangle) → dlib.pointReturns the top left corner of the rectangle.top(self: dlib.rectangle) → inttr_corner(self: dlib.rectangle) → dlib.pointReturns the top right corner of the rectangle.width(self: dlib.rectangle) → int

    cnn_face_detection_model_v1類定義

    class dlib.cnn_face_detection_model_v1This object detects human faces in an image. The constructor loads the face detection model from a file. You can download a pre-trained model from http://dlib.net/files/mmod_human_face_detector.dat.bz2.__call__(*args, **kwargs)Overloaded function.__call__(self: dlib.cnn_face_detection_model_v1, imgs: list, upsample_num_times: int=0L, batch_size: int=128L) -> std::vector<std::vector<dlib::mmod_rect, std::allocator<dlib::mmod_rect> >, std::allocator<std::vector<dlib::mmod_rect, std::allocator<dlib::mmod_rect> > > >takes a list of images as input returning a 2d list of mmod rectangles__call__(self: dlib.cnn_face_detection_model_v1, img: array, upsample_num_times: int=0L) -> std::vector<dlib::mmod_rect, std::allocator<dlib::mmod_rect> >Find faces in an image using a deep learning model.Upsamples the image upsample_num_times before running the face detector.__init__(self: dlib.cnn_face_detection_model_v1, filename: unicode) → Non

    mmod_rectangle mmod_rectangles mmod_rectangless

    class dlib.mmod_rectangleWrapper around a rectangle object and a detection confidence score.__init__x.__init__(...) initializes x; see help(type(x)) for signatureconfidencerect class dlib.mmod_rectanglesAn array of mmod rectangle objects.__init__(*args, **kwargs)Overloaded function.__init__(self: dlib.mmod_rectangles) -> None__init__(self: dlib.mmod_rectangles, arg0: dlib.mmod_rectangles) -> NoneCopy constructor__init__(self: dlib.mmod_rectangles, arg0: iterable) -> Noneappend(self: dlib.mmod_rectangles, x: dlib.mmod_rectangle) → NoneAdd an item to the end of the listcount(self: dlib.mmod_rectangles, x: dlib.mmod_rectangle) → intReturn the number of times x appears in the listextend(*args, **kwargs)Overloaded function.extend(self: dlib.mmod_rectangles, L: dlib.mmod_rectangles) -> NoneExtend the list by appending all the items in the given listextend(self: dlib.mmod_rectangles, arg0: list) -> Noneinsert(self: dlib.mmod_rectangles, i: int, x: dlib.mmod_rectangle) → NoneInsert an item at a given position.pop(*args, **kwargs)Overloaded function.pop(self: dlib.mmod_rectangles) -> dlib.mmod_rectangleRemove and return the last itempop(self: dlib.mmod_rectangles, i: int) -> dlib.mmod_rectangleRemove and return the item at index iremove(self: dlib.mmod_rectangles, x: dlib.mmod_rectangle) → NoneRemove the first item from the list whose value is x. It is an error if there is no such item. class dlib.mmod_rectanglessA 2D array of mmod rectangle objects.__init__(*args, **kwargs)Overloaded function.__init__(self: dlib.mmod_rectangless) -> None__init__(self: dlib.mmod_rectangless, arg0: dlib.mmod_rectangless) -> NoneCopy constructor__init__(self: dlib.mmod_rectangless, arg0: iterable) -> Noneappend(self: dlib.mmod_rectangless, x: dlib.mmod_rectangles) → NoneAdd an item to the end of the listcount(self: dlib.mmod_rectangless, x: dlib.mmod_rectangles) → intReturn the number of times x appears in the listextend(*args, **kwargs)Overloaded function.extend(self: dlib.mmod_rectangless, L: dlib.mmod_rectangless) -> NoneExtend the list by appending all the items in the given listextend(self: dlib.mmod_rectangless, arg0: list) -> Noneinsert(self: dlib.mmod_rectangless, i: int, x: dlib.mmod_rectangles) → NoneInsert an item at a given position.pop(*args, **kwargs)Overloaded function.pop(self: dlib.mmod_rectangless) -> dlib.mmod_rectanglesRemove and return the last itempop(self: dlib.mmod_rectangless, i: int) -> dlib.mmod_rectanglesRemove and return the item at index iremove(self: dlib.mmod_rectangless, x: dlib.mmod_rectangles) → NoneRemove the first item from the list whose value is x. It is an error if there is no such item.

    總結

    以上是生活随笔為你收集整理的dlib实现人脸检测方法的全部內容,希望文章能夠幫你解決所遇到的問題。

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