---
# System prepended metadata

title: '[碩一下][week 17]face angle and landmark'

---

[碩一下][week 17]face angle and landmark
===
# 一、face detection with MTCNN
### 1. 環境
* 原本dlib版本
    * version : `19.9.0`
    * source : `conda-forge`
* 遇到問題
    * 無法同時安裝tensorflow
* 解決
    * 更改source並更新dlib
    * 使用`conda install -c gagphil1 dlib`將dlib更新至`19.16.0`
### 2. 輸出型態改變
* dlib輸出型態
    * [(x1, y1), (x2, y2)]
    * ![](https://i.imgur.com/z8zAx7O.png)
* 修改程式碼
    * source :: [dlib_box_up_the_head.html](http://johnny88850tw.asuscomm.com/hackmd/meeting/s1d_week15/dlib_box_up_the_head.html)
    * 修改`dlibLandmark`副程式如下(`detector`使用`MTCNN`)
```python=
def MTCNNLandmark(frame):
    start = timeit.default_timer()
    bboxes, faces = detector.run(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))
    print('face detection time cost : ', timeit.default_timer() - start); start = timeit.default_timer();
    faces = dlib.rectangles()
    for box in bboxes:
        faces.append(dlib.rectangle(int(box[0]), int(box[1]), int(box[2]), int(box[3])))
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    result = []
    for face in faces:
        pred = predictor(gray, face)
        result.append(getDlibLandmark(pred))
    print('pred and put landmark time cost : ', timeit.default_timer() - start)
    return result
```
### 輸出結果 :: [圖片來源](https://i.ytimg.com/vi/wJFG-O0JpVI/maxresdefault.jpg)
* ![](https://i.imgur.com/eqr4vhH.png)
:::success
可以發現，dlib在就算有偵測出人臉的情況下，失真程度還是非常高，只有在dlib的face detector有偵測到的情況下較不會失真
:::
___
# 二、face angle問題
* 選取兩個例子比較
    * 旋轉角度 :: [155.41722558  44.15150689 159.04108788] [旋轉結果](http://johnny88850tw.asuscomm.com/hackmd/meeting/s1d_week17/face1.gif)
        * ![](https://i.imgur.com/2qVIDGY.png)
    * 旋轉角度 :: [167.46748286  16.41903085  -1.73173953] [旋轉結果](http://johnny88850tw.asuscomm.com/hackmd/meeting/s1d_week17/face2.gif)
        * ![](https://i.imgur.com/3YI2qjA.png)
* 角度是直接由`euler angle`輸出轉換過去的，再用
___
# 三、pytorch landmark測試準度
> 同樣以上面那個臉當作例子
> ![](https://i.imgur.com/w3aFKIv.png)
### 隨機挑選當作base
* 我挑選隨機一個臉來當作base，用[siavashk/pycpd](https://github.com/siavashk/pycpd)程式碼(下載資料夾`pycpd`所有檔案)
* 直接看的話會發現其實還滿準的
    * ![](https://i.imgur.com/n80BLUl.png)
    * ![](https://i.imgur.com/cgomuiz.png)
    * ![](https://i.imgur.com/OvIbtdd.png)
* 但是有些匹配結果怪怪的
    * ![](https://i.imgur.com/81hHipg.png)
    * 好像是pycpd在迭代過程出現了問題
___
# 參考資料
* Issues
    * [How can i convert python tuple to dlib.rectangle type in python??](https://github.com/davisking/dlib/issues/1359)
* dlib
    * [dlib.rectangles](http://dlib.net/python/index.html#dlib.rectangles.__init__)
    * [dlib.rectangle](http://dlib.net/python/index.html#dlib.rectangle)
* matplotlib animation
    * [Animation 动画](https://morvanzhou.github.io/tutorials/data-manipulation/plt/5-1-animation/)
    * [python matplotlib繪製gif動圖以及儲存](https://www.itread01.com/content/1546708090.html)
* pycpd
    * [siavashk/pycpd](https://github.com/siavashk/pycpd)
    * [PyCPD: Tutorial on the Coherent Point Drift Algorithm](http://siavashk.github.io/2017/05/14/coherent-point-drift/)