# 使用 jetson agx 執行 deepface 辨識年齡性別
```
git clone https://github.com/serengil/deepface.git
cd deepface
```
# 更改 Dockerfile
```
#base image
FROM nvcr.io/nvidia/l4t-tensorflow:r34.1.1-tf2.8-py3
# -----------------------------------
# create required folder
RUN mkdir /app
RUN mkdir /app/api
RUN mkdir /app/deepface
# -----------------------------------
# Copy required files from repo into image
COPY ./deepface /app/deepface
COPY ./api/api.py /app/
COPY ./setup.py /app/
COPY ./README.md /app/
# -----------------------------------
# switch to application directory
WORKDIR /app
# -----------------------------------
# update image os
RUN apt-get update
RUN apt-get install ffmpeg libsm6 libxext6 -y
# -----------------------------------
# install deepface from pypi release (might be out-of-the-date)
# RUN pip install --trusted-host pypi.org --trusted-host pypi.python.org --trusted-host=files.pythonhosted.org deepface
# -----------------------------------
# install deepface from source code (always up-to-date)
# RUN pip install --trusted-host pypi.org --trusted-host pypi.python.org --trusted-host=files.pythonhosted.org -e .
# -----------------------------------
# some packages are optional in deepface. activate if your task depends on one.
# RUN pip install --trusted-host pypi.org --trusted-host pypi.python.org --trusted-host=files.pythonhosted.org cmake==3.24.1.1
# RUN pip install --trusted-host pypi.org --trusted-host pypi.python.org --trusted-host=files.pythonhosted.org dlib==19.20.0
# RUN pip install --trusted-host pypi.org --trusted-host pypi.python.org --trusted-host=files.pythonhosted.org lightgbm==2.3.1
# -----------------------------------
# environment variables
ENV PYTHONUNBUFFERED=1
# -----------------------------------
# run the app (re-configure port if necessary)
EXPOSE 5000
# flask run is not recommended in production, move this to gunicorn
# CMD ["python", "/app/api.py", "--port", "5000"]
RUN cp -r /usr/share/opencv4/haarcascades/ /usr/local/lib/python3.8/dist-packages/cv2/python-3.8/data/
```
# 編譯
```
docker build -t raidavid/deepface_agx .
```
# 執行
```
docker run \
-d -it \
-p 5125:5000 \
--name deepface \
--restart=always \
--log-opt max-size=10m \
--shm-size="15g" \
--log-opt max-file=10 \
--runtime nvidia \
raidavid/deepface_agx \
/bin/bash -c "cd /app/api && flask run --no-debugger --no-reload --host 0.0.0.0"
```
# 測試 code
```
from deepface import DeepFace
from deepface.detectors import FaceDetector
import cv2
import time
backends = [
'opencv',
'ssd',
'dlib',
'mtcnn',
'retinaface',
'mediapipe'
]
backendsno = 0
# # 下載模型用
# demographies = DeepFace.analyze(img_path = "1.jpeg",
# detector_backend = backends[backendsno]
# )
detector = FaceDetector.build_model(backends[backendsno])
img = cv2.imread("test.jpg")
while True:
t0 = time.time()
faces_2 = FaceDetector.detect_faces(detector, backends[backendsno], img,align=False)
print(str(time.time() - t0 ))
faces_3 = FaceDetector.detect_faces(detector, 'opencv', cv2.imread("1.jpg"))
print("")
```
發生錯誤
```
ImportError: /lib/aarch64-linux-gnu/libgomp.so.1: cannot allocate memory in static TLS block
```
解決
```
export LD_PRELOAD=/usr/lib/aarch64-linux-gnu/libgomp.so.1
```