зеркало из
1
0
Форкнуть 0

Port to ARM64 and add GPU acceleration

This commit is contained in:
Paul DeCarlo 2019-07-10 15:32:36 -05:00
Родитель 6d4eef8942
Коммит 01753791c7
18 изменённых файлов: 844 добавлений и 114 удалений

10
.env
Просмотреть файл

@ -1,5 +1,5 @@
CONTAINER_REGISTRY_URL=<ACR Login Server>
CONTAINER_REGISTRY_USERNAME=<ACR User Name>
CONTAINER_REGISTRY_PASSWORD=<ACR Password>
CONTAINER_MODULE_VERSION=step7-8
CONTAINER_VIDEO_SOURCE=<URL to Video>
CONTAINER_REGISTRY_URL=toolboc
CONTAINER_REGISTRY_USERNAME=
CONTAINER_REGISTRY_PASSWORD=
CONTAINER_MODULE_VERSION=latest
CONTAINER_VIDEO_SOURCE=https://www.youtube.com/watch?v=YZkp0qBBmpw

6
.vscode/settings.json поставляемый Normal file
Просмотреть файл

@ -0,0 +1,6 @@
{
"azure-iot-edge.defaultPlatform": {
"platform": "arm32v7",
"alias": null
}
}

Просмотреть файл

@ -0,0 +1,71 @@
{
"modulesContent": {
"$edgeAgent": {
"properties.desired": {
"schemaVersion": "1.0",
"runtime": {
"type": "docker",
"settings": {
"minDockerVersion": "v1.25",
"loggingOptions": "",
"registryCredentials": {
"bootcampfy19acr": {
"username": "$CONTAINER_REGISTRY_USERNAME",
"password": "$CONTAINER_REGISTRY_PASSWORD",
"address": "toolboc"
}
}
}
},
"systemModules": {
"edgeAgent": {
"type": "docker",
"settings": {
"image": "mcr.microsoft.com/azureiotedge-agent:1.0.8-rc1",
"createOptions": "{}"
}
},
"edgeHub": {
"type": "docker",
"status": "running",
"restartPolicy": "always",
"settings": {
"image": "mcr.microsoft.com/azureiotedge-hub:1.0.8-rc1",
"createOptions": "{\"HostConfig\":{\"PortBindings\":{\"5671/tcp\":[{\"HostPort\":\"5671\"}],\"8883/tcp\":[{\"HostPort\":\"8883\"}],\"443/tcp\":[{\"HostPort\":\"443\"}]}}}"
}
}
},
"modules": {
"YoloModule": {
"version": "1.0",
"type": "docker",
"status": "running",
"restartPolicy": "always",
"settings": {
"image": "toolboc/yolomodule:latest-arm32v7",
"createOptions": "{\"Env\":[\"VIDEO_PATH=https://www.youtube.com/watch?v=YZkp0qBBmpw\",\"VIDEO_WIDTH=0\",\"VIDEO_HEIGHT=0\",\"FONT_SCALE=0.8\"],\"HostConfig\":{\"Devices\":[{\"PathOnHost\":\"/dev/nvhost-ctrl\",\"PathInContainer\":\"/dev/nvhost-ctrl\",\"CgroupPermissions\":\"rwm\"},{\"PathOnHost\":\"/dev/nvhost-ctrl-gpu\",\"PathInContainer\":\"dev/nvhost-ctrl-gpu\",\"CgroupPermissions\":\"rwm\"},{\"PathOnHost\":\"/dev/nvhost-prof-gpu\",\"PathInContainer\":\"dev/nvhost-prof-gpu \",\"CgroupPermissions\":\"rwm\"},{\"PathOnHost\":\"/dev/nvmap\",\"PathInContainer\":\"/dev/nvmap\",\"Cgroup",
"createOptions01": "Permissions\":\"rwm\"},{\"PathOnHost\":\"dev/nvhost-gpu\",\"PathInContainer\":\"dev/nvhost-gpu\",\"CgroupPermissions\":\"rwm\"},{\"PathOnHost\":\"/dev/nvhost-as-gpu\",\"PathInContainer\":\"/dev/nvhost-as-gpu\",\"CgroupPermissions\":\"rwm\"},{\"PathOnHost\":\"/dev/nvhost-vic\",\"PathInContainer\":\"/dev/nvhost-vic\",\"CgroupPermissions\":\"rwm\"},{\"PathOnHost\":\"/dev/tegra_dc_ctrl\",\"PathInContainer\":\"/dev/tegra_dc_ctrl\",\"CgroupPermissions\":\"rwm\"}],\"PortBindings\":{\"80/tcp\":[{\"HostPort\":\"80\"}]}}}"
}
}
}
}
},
"$edgeHub": {
"properties.desired": {
"schemaVersion": "1.0",
"routes": {},
"storeAndForwardConfiguration": {
"timeToLiveSecs": 7200
}
}
},
"YoloModule": {
"properties.desired": {
"ConfidenceLevel": "0.3",
"VerboseMode": 0,
"Inference": 1,
"VideoSource": ""
}
}
}
}

Просмотреть файл

@ -22,7 +22,7 @@
"edgeAgent": {
"type": "docker",
"settings": {
"image": "mcr.microsoft.com/azureiotedge-agent:1.0",
"image": "mcr.microsoft.com/azureiotedge-agent:1.0.8-rc1",
"createOptions": {}
}
},
@ -31,7 +31,7 @@
"status": "running",
"restartPolicy": "always",
"settings": {
"image": "mcr.microsoft.com/azureiotedge-hub:1.0",
"image": "mcr.microsoft.com/azureiotedge-hub:1.0.8-rc1",
"createOptions": {
"HostConfig": {
"PortBindings": {
@ -64,7 +64,65 @@
"restartPolicy": "always",
"settings": {
"image": "${MODULES.YoloModule}",
"createOptions": "{\"Env\":[\"VIDEO_PATH=$CONTAINER_VIDEO_SOURCE\", \"VIDEO_WIDTH=0\", \"VIDEO_HEIGHT=0\", \"FONT_SCALE=0.8\"], \"HostConfig\":{\"PortBindings\":{\"80/tcp\":[{\"HostPort\":\"80\"}]}}}"
"createOptions": {
"Env": [
"VIDEO_PATH=$CONTAINER_VIDEO_SOURCE",
"VIDEO_WIDTH=0",
"VIDEO_HEIGHT=0",
"FONT_SCALE=0.8"
],
"HostConfig": {
"Devices": [
{
"PathOnHost": "/dev/nvhost-ctrl",
"PathInContainer":"/dev/nvhost-ctrl",
"CgroupPermissions":"rwm"
},
{
"PathOnHost": "/dev/nvhost-ctrl-gpu",
"PathInContainer":"dev/nvhost-ctrl-gpu",
"CgroupPermissions":"rwm"
},
{
"PathOnHost": "/dev/nvhost-prof-gpu",
"PathInContainer":"dev/nvhost-prof-gpu ",
"CgroupPermissions":"rwm"
},
{
"PathOnHost": "/dev/nvmap",
"PathInContainer":"/dev/nvmap",
"CgroupPermissions":"rwm"
},
{
"PathOnHost": "dev/nvhost-gpu",
"PathInContainer":"dev/nvhost-gpu",
"CgroupPermissions":"rwm"
},
{
"PathOnHost": "/dev/nvhost-as-gpu",
"PathInContainer":"/dev/nvhost-as-gpu",
"CgroupPermissions":"rwm"
},
{
"PathOnHost": "/dev/nvhost-vic",
"PathInContainer":"/dev/nvhost-vic",
"CgroupPermissions":"rwm"
},
{
"PathOnHost": "/dev/tegra_dc_ctrl",
"PathInContainer":"/dev/tegra_dc_ctrl",
"CgroupPermissions":"rwm"
}
],
"PortBindings": {
"80/tcp": [
{
"HostPort": "80"
}
]
}
}
}
}
}
}

Просмотреть файл

@ -0,0 +1,21 @@
FROM toolboc/jetson-nano-l4t-cuda-cudnn-opencv
RUN apt update && apt install -y libcanberra-gtk-module && \
rm -rf /var/lib/apt/lists/*
#GET Darknet sources
WORKDIR /usr/local/src
RUN git clone https://github.com/AlexeyAB/darknet.git && \
cd darknet && \
sed -i 's/GPU=0/GPU=1/g' Makefile && \
sed -i 's/CUDNN=0/CUDNN=1/g' Makefile && \
sed -i 's/CUDNN_HALF=0/CUDNN_HALF=1/g' Makefile && \
sed -i 's/OPENCV=0/OPENCV=1/g' Makefile && \
sed -i 's/LIBSO=0/LIBSO=1/g' Makefile && \
make
RUN cd darknet && LD_LIBRARY_PATH=./:$LD_LIBRARY_PATH
RUN cd darknet && \
curl https://pjreddie.com/media/files/yolov3-tiny.weights -o yolov3-tiny.weights

Просмотреть файл

@ -0,0 +1,31 @@
FROM toolboc/jetson-nano-l4t-cuda-cudnn
#Required for libjasper-dev
RUN echo "deb http://ports.ubuntu.com/ubuntu-ports/ xenial-security main restricted" | sudo tee -a /etc/apt/sources.list
#INSTALL OPENCV dependencies
RUN apt update && apt purge *libopencv* && apt install -y build-essential cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev \
libgstreamer1.0-dev libgstreamer-plugins-base1.0-dev \
python2.7-dev python3.6-dev python-dev python-numpy python3-numpy \
libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libjasper-dev libdc1394-22-dev \
libv4l-dev v4l-utils qv4l2 v4l2ucp \
curl unzip && \
rm -rf /var/lib/apt/lists/*
#GET OPENCV sources
WORKDIR /usr/local/src
RUN curl -L https://github.com/opencv/opencv/archive/4.1.0.zip -o opencv-4.1.0.zip && \
curl -L https://github.com/opencv/opencv_contrib/archive/4.1.0.zip -o opencv_contrib-4.1.0.zip && \
unzip opencv-4.1.0.zip && \
unzip opencv_contrib-4.1.0.zip && \
rm -rf opencv*.zip
#INSTALL OPENCV
RUN cd opencv-4.1.0/ && mkdir release && cd release/ && \
cmake -D OPENCV_GENERATE_PKGCONFIG=ON -D OPENCV_PC_FILE_NAME=opencv.pc -D WITH_CUDA=ON -D CUDA_ARCH_BIN="5.3" -D CUDA_ARCH_PTX="" -D OPENCV_EXTRA_MODULES_PATH=../../opencv_contrib-4.1.0/modules -D WITH_GSTREAMER=ON -D WITH_LIBV4L=ON -D BUILD_opencv_python2=ON -D BUILD_opencv_python3=ON -D BUILD_TESTS=OFF -D BUILD_PERF_TESTS=OFF -D BUILD_EXAMPLES=OFF -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local .. && \
make -j3 && \
make install && \
cp unix-install/opencv.pc /usr/local/lib/pkgconfig && \
rm -rf /usr/local/src/opencv-4.1.0
RUN ldconfig

Просмотреть файл

@ -0,0 +1,28 @@
FROM toolboc/jetson-nano-l4t-cuda
# NVIDIA CUDA Deep Neural Network library (cuDNN)
ENV CUDNN_VERSION 7.3.1.28
ENV CUDNN_PKG_VERSION=${CUDA_VERSION}-1
LABEL com.nvidia.cudnn.version="${CUDNN_VERSION}"
ARG libcudnn7_URL=https://onedrive.live.com/download?cid=54AD8562A32D8752&resid=54AD8562A32D8752%21376196&authkey=ADTDdL0bhMWq4vM
ARG libcudnn7_dev_URL=https://onedrive.live.com/download?cid=54AD8562A32D8752&resid=54AD8562A32D8752%21376197&authkey=APizXm-di7JPR0Y
ARG libcudnn7_doc_URL=https://onedrive.live.com/download?cid=54AD8562A32D8752&resid=54AD8562A32D8752%21376195&authkey=ADqH53K9oRnkO-8
RUN curl -sL $libcudnn7_URL -o libcudnn7_$CUDNN_VERSION-1+cuda10.0_arm64.deb && \
echo "92867c0a495f84ec11d108f84b776620 libcudnn7_$CUDNN_VERSION-1+cuda10.0_arm64.deb" | md5sum -c - && \
dpkg -i libcudnn7_$CUDNN_VERSION-1+cuda10.0_arm64.deb && \
rm libcudnn7_$CUDNN_VERSION-1+cuda10.0_arm64.deb
RUN curl -sL $libcudnn7_dev_URL -o libcudnn7-dev_$CUDNN_VERSION-1+cuda10.0_arm64.deb && \
echo "dd0fbfa225b2374b946febc98e2cdec4 libcudnn7-dev_$CUDNN_VERSION-1+cuda10.0_arm64.deb" | md5sum -c - && \
dpkg -i libcudnn7-dev_$CUDNN_VERSION-1+cuda10.0_arm64.deb && \
rm libcudnn7-dev_$CUDNN_VERSION-1+cuda10.0_arm64.deb
RUN curl -sL $libcudnn7_doc_URL -o libcudnn7-doc_$CUDNN_VERSION-1+cuda10.0_arm64.deb && \
echo "9478c16ceeaaca937d4d26b982e48bd1 libcudnn7-doc_$CUDNN_VERSION-1+cuda10.0_arm64.deb" | md5sum -c - && \
dpkg -i libcudnn7-doc_$CUDNN_VERSION-1+cuda10.0_arm64.deb && \
rm libcudnn7-doc_$CUDNN_VERSION-1+cuda10.0_arm64.deb

Просмотреть файл

@ -0,0 +1,22 @@
FROM toolboc/jetson-nano-l4t
#INSTALL CUDA Toolkit for L4T
ARG URL=https://onedrive.live.com/download?cid=54AD8562A32D8752&resid=54AD8562A32D8752%21376191&authkey=APwtvHgdqlgnJzo
ARG CUDA_TOOLKIT_PKG="cuda-repo-l4t-10-0-local-10.0.166_1.0-1_arm64.deb"
RUN apt-get update && \
apt-get install -y --no-install-recommends curl && \
curl -sL ${URL} -o ${CUDA_TOOLKIT_PKG} && \
echo "5e3eedc3707305f9022d41754d6becde ${CUDA_TOOLKIT_PKG}" | md5sum -c - && \
dpkg --force-all -i ${CUDA_TOOLKIT_PKG} && \
rm ${CUDA_TOOLKIT_PKG} && \
apt-key add var/cuda-repo-*-local*/*.pub && \
apt-get update && \
apt-get install -y --allow-downgrades cuda-toolkit-10-0 libgomp1 libfreeimage-dev libopenmpi-dev openmpi-bin && \
dpkg --purge cuda-repo-l4t-10-0-local-10.0.166 && \
apt-get clean && \
rm -rf /var/lib/apt/lists/*
ENV CUDA_HOME=/usr/local/cuda
ENV LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/cuda/lib64:/usr/local/cuda/extras/CUPTI/lib64
ENV PATH=$PATH:$CUDA_HOME/bin

Просмотреть файл

@ -0,0 +1,31 @@
FROM balenalib/jetson-tx2-ubuntu:bionic
ARG URL=https://onedrive.live.com/download?cid=54AD8562A32D8752&resid=54AD8562A32D8752%21376194&authkey=ADUfVNPnEHviFoU
ARG DRIVER_PACK=Jetson-210_Linux_R32.1.0_aarch64.tbz2
RUN apt-get update && apt-get install -y --no-install-recommends \
bzip2 \
ca-certificates \
curl \
lbzip2 \
sudo \
&& \
curl -sSL $URL -o ${DRIVER_PACK} && \
echo "9138c7dd844eb290a20b31446b757e1781080f63 *./${DRIVER_PACK}" | sha1sum -c --strict - && \
tar -xpj --overwrite -f ./${DRIVER_PACK} && \
sed -i '/.*tar -I lbzip2 -xpmf ${LDK_NV_TEGRA_DIR}\/config\.tbz2.*/c\tar -I lbzip2 -xpm --overwrite -f ${LDK_NV_TEGRA_DIR}\/config.tbz2' ./Linux_for_Tegra/apply_binaries.sh && \
./Linux_for_Tegra/apply_binaries.sh -r / && \
rm -rf ./Linux_for_Tegra && \
rm ./${DRIVER_PACK} \
&& \
apt-get purge --autoremove -y bzip2 curl lbzip2 && \
apt-get clean && \
rm -rf /var/lib/apt/lists/*
ENV LD_LIBRARY_PATH=/usr/lib/aarch64-linux-gnu/tegra:/usr/lib/aarch64-linux-gnu/tegra-egl:${LD_LIBRARY_PATH}
RUN ln -s /usr/lib/aarch64-linux-gnu/tegra/libnvidia-ptxjitcompiler.so.32.1.0 /usr/lib/aarch64-linux-gnu/tegra/libnvidia-ptxjitcompiler.so && \
ln -s /usr/lib/aarch64-linux-gnu/tegra/libnvidia-ptxjitcompiler.so.32.1.0 /usr/lib/aarch64-linux-gnu/tegra/libnvidia-ptxjitcompiler.so.1 && \
ln -sf /usr/lib/aarch64-linux-gnu/tegra/libGL.so /usr/lib/aarch64-linux-gnu/libGL.so && \
ln -s /usr/lib/aarch64-linux-gnu/libcuda.so /usr/lib/aarch64-linux-gnu/libcuda.so.1 && \
ln -sf /usr/lib/aarch64-linux-gnu/tegra-egl/libEGL.so /usr/lib/aarch64-linux-gnu/libEGL.so

Просмотреть файл

@ -1,20 +0,0 @@
FROM ubuntu:xenial
WORKDIR /app
RUN apt-get update && \
apt-get install -y --no-install-recommends libcurl4-openssl-dev python-pip libboost-python-dev libgtk2.0-dev && \
rm -rf /var/lib/apt/lists/*
COPY /build/requirements.txt ./
RUN pip install --upgrade pip
RUN pip install --no-cache-dir -r requirements.txt
RUN pip install tornado==4.5.3 trollius && \
pip install -U youtube-dl
ADD /app/ .
# Expose the port
EXPOSE 80
ENTRYPOINT [ "python", "-u", "./main.py" ]

Просмотреть файл

@ -0,0 +1,54 @@
FROM balenalib/jetson-tx2-ubuntu:bionic as iot-sdk-python-builder
# Update image
SHELL ["/bin/bash", "-c"]
RUN apt-get update && apt-get install -y cmake build-essential curl libcurl4-openssl-dev \
libssl-dev uuid-dev apt-utils python python-pip python-virtualenv python3 python3-pip python3-virtualenv \
libboost-python-dev pkg-config valgrind sudo git software-properties-common && \
rm -rf /var/lib/apt/lists/*
WORKDIR /usr/sdk
RUN python -m virtualenv --python=python3 env3
RUN source env3/bin/activate && pip install --upgrade pip && pip install -U setuptools wheel
RUN git clone --recursive --depth=1 https://github.com/Azure/azure-iot-sdk-python.git src
# Build for Python 3
RUN add-apt-repository ppa:deadsnakes/ppa
RUN source env3/bin/activate && ./src/build_all/linux/setup.sh --python-version 3.6
RUN source env3/bin/activate && ./src/build_all/linux/release.sh --build-python 3.6
# Build for Python 2
#RUN pip install --upgrade pip==10.0.1 && python -m pip install -U setuptools wheel
#RUN ./src/build_all/linux/setup.sh
#RUN ./src/build_all/linux/release.sh
FROM toolboc/jetson-nano-l4t-cuda-cudnn-opencv-darknet
WORKDIR /app
RUN apt-get update && \
DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends libcurl4-openssl-dev python3-pip libboost-python-dev libgtk2.0-dev python3-setuptools python3-numpy python3-opencv python-opencv && \
rm -rf /var/lib/apt/lists/*
COPY --from=iot-sdk-python-builder /usr/sdk/src/device/doc/package-readme.md /src/device/doc/package-readme.md
COPY --from=iot-sdk-python-builder /usr/sdk/src/build_all/linux/release_device_client /src/build_all/linux/release_device_client
RUN cd /src/build_all/linux/release_device_client && python3 setup.py install
COPY --from=iot-sdk-python-builder /usr/sdk/src/device/samples/iothub_client.so /app/iothub_client.so
RUN cp /usr/local/src/darknet/libdarknet.so /app/libdarknet.so
COPY /build/requirements.txt ./
RUN pip3 install --upgrade pip
RUN pip3 install --no-cache-dir -r requirements.txt
RUN pip3 install tornado==4.5.3 trollius && \
pip3 install -U youtube-dl
ADD /app/ .
# Expose the port
EXPOSE 80
ENTRYPOINT [ "python3", "-u", "./main.py" ]

Просмотреть файл

@ -13,14 +13,9 @@ import ImageServer
from ImageServer import ImageServer
import VideoStream
from VideoStream import VideoStream
'''***********************************************************
Step-10 : Uncomment Start
***********************************************************'''
# import YoloInference
# from YoloInference import YoloInference
'''***********************************************************
Step-10 : Uncomment End
***********************************************************'''
import YoloInference
from YoloInference import YoloInference
class VideoCapture(object):
@ -63,13 +58,8 @@ class VideoCapture(object):
self.imageServer = ImageServer(80, self)
self.imageServer.start()
'''***********************************************************
Step-10 : Uncomment Start
***********************************************************'''
# self.yoloInference = YoloInference(self.fontScale)
'''***********************************************************
Step-10 : Uncomment End
***********************************************************'''
self.yoloInference = YoloInference(self.fontScale)
def __IsCaptureDev(self, videoPath):
try:
@ -287,14 +277,8 @@ class VideoCapture(object):
frame = cv2.resize(frame, (self.videoW, self.videoH))
# Run Object Detection
'''***********************************************************
Step-10 : Uncomment Start
***********************************************************'''
# if self.inference:
# self.yoloInference.runInference(frame, frameW, frameH, self.confidenceLevel)
'''***********************************************************
Step-10 : Uncomment End
***********************************************************'''
if self.inference:
self.yoloInference.runInference(frame, frameW, frameH, self.confidenceLevel)
# Calculate FPS
timeElapsedInMs = (time.time() - tFrameStart) * 1000

Просмотреть файл

@ -7,7 +7,7 @@ if sys.version_info[0] < 3:#e.g python version <3
import cv2
else:
import cv2
from cv2 import cv2
# from cv2 import cv2
# pylint: disable=E1101
# pylint: disable=E0401
# Disabling linting that is not supported by Pylint for C extensions such as OpenCV. See issue https://github.com/PyCQA/pylint/issues/1955

Просмотреть файл

@ -2,6 +2,8 @@
from __future__ import division
from __future__ import absolute_import
from darknet import darknet
import cv2
#import cv2.cv as cv
import numpy as np
@ -11,6 +13,10 @@ import os
yolocfg = r'yolo/yolov3-tiny.cfg'
yoloweight = r'yolo/yolov3-tiny.weights'
classesFile = r'yolo/coco.names'
dataFile = r'yolo/coco.data'
encoding = 'utf-8'
class YoloInference(object):
@ -43,7 +49,7 @@ class YoloInference(object):
# Read pre-trained model and config file
print(" - Loading Model and Config")
self.net = cv2.dnn.readNetFromDarknet( yolocfg, yoloweight )
darknet.performDetect( configPath = yolocfg, weightPath = yoloweight, metaPath= dataFile, initOnly= True )
def __get_output_layers(self, net):
layerNames = net.getLayerNames()
@ -59,8 +65,8 @@ class YoloInference(object):
print("draw_rect h :" + str(h))
label = '%.2f' % confidence
label = '%s:%s' % (self.classLabels[class_id], label)
color = self.colors[class_id]
label = '%s:%s' % (class_id, label)
color = self.colors[self.classLabels.index(class_id)]
labelSize, baseLine = cv2.getTextSize(label, cv2.FONT_HERSHEY_SIMPLEX, self.fontScale, self.fontThickness)
@ -71,65 +77,30 @@ class YoloInference(object):
def runInference(self, frame, frameW, frameH, confidenceLevel):
try:
# Create input blob
blob = cv2.dnn.blobFromImage(frame, 1.0/255.0, (416, 416), (0,0,0), True, crop=False)
# Set input blob for the network
self.net.setInput(blob)
detections = darknet.detect(darknet.netMain, darknet.metaMain, frame, confidenceLevel)
# Run inference
outputs = self.net.forward(self.__get_output_layers(self.net))
for detection in detections:
classLabel = detection[0]
classID = str(detection[0], encoding)
confidence = detection[1]
# Initialize arrays
boxes = []
confidences = []
classIDs = []
if confidence > confidenceLevel:
for output in outputs:
for detection in output:
scores = detection[5:]
classID = np.argmax(scores)
confidence = scores[classID]
classLabel = self.classLabels[classID]
if self.verbose:
print( "Class Label : %s Confidence %f" % (classLabel, confidence))
if confidence > confidenceLevel:
bounds = detection[2]
xEntent = int(bounds[2])
yExtent = int(bounds[3])
# Coordinates are around the center
xCoord = int(bounds[0] - bounds[2]/2)
yCoord = int(bounds[1] - bounds[3]/2)
if self.verbose:
print( "Class Label : %s Confidence %f" % (classLabel, confidence))
self.__draw_rect(frame, classID, confidence, xCoord, yCoord, xCoord + xEntent, yCoord + yExtent)
centerX = int(detection[0] * frameW)
centerY = int(detection[1] * frameH)
rectWidth = int(detection[2] * frameW)
rectHeight = int(detection[3] * frameH)
left = int(max((centerX - (rectWidth / 2)), 5))
top = int(max((centerY - (rectHeight / 2)), 5))
if rectHeight + top > frameH:
rectHeight = frameH - top - 5
if rectWidth + left > frameW:
rectWidth = frameW - left - 5
boxes.append([left, top, rectWidth, rectHeight])
confidences.append(float(confidence))
classIDs.append(classID)
idxs = cv2.dnn.NMSBoxes(boxes, confidences, confidenceLevel, self.nmsThreshold)
for i in idxs:
i = i[0]
box = boxes[i]
# Get the bounding box coordinates
x = box[0]
y = box[1]
w = box[2]
h = box[3]
# draw a bounding box rectangle and label on the image
self.__draw_rect(frame, classIDs[i], confidences[i], x, y, x + w, y + h)
except:
except Exception as e:
print("Exception during AI Inference")
print(e)

Просмотреть файл

@ -0,0 +1,466 @@
#!python3
"""
Python 3 wrapper for identifying objects in images
Requires DLL compilation
Both the GPU and no-GPU version should be compiled; the no-GPU version should be renamed "yolo_cpp_dll_nogpu.dll".
On a GPU system, you can force CPU evaluation by any of:
- Set global variable DARKNET_FORCE_CPU to True
- Set environment variable CUDA_VISIBLE_DEVICES to -1
- Set environment variable "FORCE_CPU" to "true"
To use, either run performDetect() after import, or modify the end of this file.
See the docstring of performDetect() for parameters.
Directly viewing or returning bounding-boxed images requires scikit-image to be installed (`pip install scikit-image`)
Original *nix 2.7: https://github.com/pjreddie/darknet/blob/0f110834f4e18b30d5f101bf8f1724c34b7b83db/python/darknet.py
Windows Python 2.7 version: https://github.com/AlexeyAB/darknet/blob/fc496d52bf22a0bb257300d3c79be9cd80e722cb/build/darknet/x64/darknet.py
@author: Philip Kahn
@date: 20180503
"""
#pylint: disable=R, W0401, W0614, W0703
from ctypes import *
import math
import random
import os
import numpy as np
def sample(probs):
s = sum(probs)
probs = [a/s for a in probs]
r = random.uniform(0, 1)
for i in range(len(probs)):
r = r - probs[i]
if r <= 0:
return i
return len(probs)-1
def c_array(ctype, values):
arr = (ctype*len(values))()
arr[:] = values
return arr
class BOX(Structure):
_fields_ = [("x", c_float),
("y", c_float),
("w", c_float),
("h", c_float)]
class DETECTION(Structure):
_fields_ = [("bbox", BOX),
("classes", c_int),
("prob", POINTER(c_float)),
("mask", POINTER(c_float)),
("objectness", c_float),
("sort_class", c_int)]
class IMAGE(Structure):
_fields_ = [("w", c_int),
("h", c_int),
("c", c_int),
("data", POINTER(c_float))]
class METADATA(Structure):
_fields_ = [("classes", c_int),
("names", POINTER(c_char_p))]
#lib = CDLL("/home/pjreddie/documents/darknet/libdarknet.so", RTLD_GLOBAL)
#lib = CDLL("libdarknet.so", RTLD_GLOBAL)
hasGPU = True
if os.name == "nt":
cwd = os.path.dirname(__file__)
os.environ['PATH'] = cwd + ';' + os.environ['PATH']
winGPUdll = os.path.join(cwd, "yolo_cpp_dll.dll")
winNoGPUdll = os.path.join(cwd, "yolo_cpp_dll_nogpu.dll")
envKeys = list()
for k, v in os.environ.items():
envKeys.append(k)
try:
try:
tmp = os.environ["FORCE_CPU"].lower()
if tmp in ["1", "true", "yes", "on"]:
raise ValueError("ForceCPU")
else:
print("Flag value '"+tmp+"' not forcing CPU mode")
except KeyError:
# We never set the flag
if 'CUDA_VISIBLE_DEVICES' in envKeys:
if int(os.environ['CUDA_VISIBLE_DEVICES']) < 0:
raise ValueError("ForceCPU")
try:
global DARKNET_FORCE_CPU
if DARKNET_FORCE_CPU:
raise ValueError("ForceCPU")
except NameError:
pass
# print(os.environ.keys())
# print("FORCE_CPU flag undefined, proceeding with GPU")
if not os.path.exists(winGPUdll):
raise ValueError("NoDLL")
lib = CDLL(winGPUdll, RTLD_GLOBAL)
except (KeyError, ValueError):
hasGPU = False
if os.path.exists(winNoGPUdll):
lib = CDLL(winNoGPUdll, RTLD_GLOBAL)
print("Notice: CPU-only mode")
else:
# Try the other way, in case no_gpu was
# compile but not renamed
lib = CDLL(winGPUdll, RTLD_GLOBAL)
print("Environment variables indicated a CPU run, but we didn't find `"+winNoGPUdll+"`. Trying a GPU run anyway.")
else:
lib = CDLL("./libdarknet.so", RTLD_GLOBAL)
lib.network_width.argtypes = [c_void_p]
lib.network_width.restype = c_int
lib.network_height.argtypes = [c_void_p]
lib.network_height.restype = c_int
copy_image_from_bytes = lib.copy_image_from_bytes
copy_image_from_bytes.argtypes = [IMAGE,c_char_p]
def network_width(net):
return lib.network_width(net)
def network_height(net):
return lib.network_height(net)
predict = lib.network_predict_ptr
predict.argtypes = [c_void_p, POINTER(c_float)]
predict.restype = POINTER(c_float)
if hasGPU:
set_gpu = lib.cuda_set_device
set_gpu.argtypes = [c_int]
make_image = lib.make_image
make_image.argtypes = [c_int, c_int, c_int]
make_image.restype = IMAGE
get_network_boxes = lib.get_network_boxes
get_network_boxes.argtypes = [c_void_p, c_int, c_int, c_float, c_float, POINTER(c_int), c_int, POINTER(c_int), c_int]
get_network_boxes.restype = POINTER(DETECTION)
make_network_boxes = lib.make_network_boxes
make_network_boxes.argtypes = [c_void_p]
make_network_boxes.restype = POINTER(DETECTION)
free_detections = lib.free_detections
free_detections.argtypes = [POINTER(DETECTION), c_int]
free_ptrs = lib.free_ptrs
free_ptrs.argtypes = [POINTER(c_void_p), c_int]
network_predict = lib.network_predict_ptr
network_predict.argtypes = [c_void_p, POINTER(c_float)]
reset_rnn = lib.reset_rnn
reset_rnn.argtypes = [c_void_p]
load_net = lib.load_network
load_net.argtypes = [c_char_p, c_char_p, c_int]
load_net.restype = c_void_p
load_net_custom = lib.load_network_custom
load_net_custom.argtypes = [c_char_p, c_char_p, c_int, c_int]
load_net_custom.restype = c_void_p
do_nms_obj = lib.do_nms_obj
do_nms_obj.argtypes = [POINTER(DETECTION), c_int, c_int, c_float]
do_nms_sort = lib.do_nms_sort
do_nms_sort.argtypes = [POINTER(DETECTION), c_int, c_int, c_float]
free_image = lib.free_image
free_image.argtypes = [IMAGE]
letterbox_image = lib.letterbox_image
letterbox_image.argtypes = [IMAGE, c_int, c_int]
letterbox_image.restype = IMAGE
load_meta = lib.get_metadata
lib.get_metadata.argtypes = [c_char_p]
lib.get_metadata.restype = METADATA
load_image = lib.load_image_color
load_image.argtypes = [c_char_p, c_int, c_int]
load_image.restype = IMAGE
rgbgr_image = lib.rgbgr_image
rgbgr_image.argtypes = [IMAGE]
predict_image = lib.network_predict_image
predict_image.argtypes = [c_void_p, IMAGE]
predict_image.restype = POINTER(c_float)
predict_image_letterbox = lib.network_predict_image_letterbox
predict_image_letterbox.argtypes = [c_void_p, IMAGE]
predict_image_letterbox.restype = POINTER(c_float)
def array_to_image(arr):
# need to return old values to avoid python freeing memory
arr = arr.transpose(2,0,1)
c = arr.shape[0]
h = arr.shape[1]
w = arr.shape[2]
arr = np.ascontiguousarray(arr.flat, dtype=np.float32) / 255.0
data = arr.ctypes.data_as(POINTER(c_float))
im = IMAGE(w,h,c,data)
return im, arr
def classify(net, meta, im):
out = predict_image(net, im)
res = []
for i in range(meta.classes):
if altNames is None:
nameTag = meta.names[i]
else:
nameTag = altNames[i]
res.append((nameTag, out[i]))
res = sorted(res, key=lambda x: -x[1])
return res
def detect(net, meta, image, thresh=.5, hier_thresh=.5, nms=.45):
if isinstance(image, bytes):
# image is a filename
# i.e. image = b'/darknet/data/dog.jpg'
im = load_image(image, 0, 0)
else:
# image is an nparray
# i.e. image = cv2.imread('/darknet/data/dog.jpg')
im, image = array_to_image(image)
rgbgr_image(im)
num = c_int(0)
pnum = pointer(num)
letter_box = 0
predict_image(net, im)
dets = get_network_boxes(net, im.w, im.h, thresh,
hier_thresh, None, 0, pnum, letter_box)
num = pnum[0]
if nms: do_nms_obj(dets, num, meta.classes, nms)
res = []
for j in range(num):
a = dets[j].prob[0:meta.classes]
if any(a):
ai = np.array(a).nonzero()[0]
for i in ai:
b = dets[j].bbox
res.append((meta.names[i], dets[j].prob[i],
(b.x, b.y, b.w, b.h)))
res = sorted(res, key=lambda x: -x[1])
if isinstance(image, bytes): free_image(im)
free_detections(dets, num)
return res
def detect_image(net, meta, im, thresh=.5, hier_thresh=.5, nms=.45, debug= False):
#import cv2
#custom_image_bgr = cv2.imread(image) # use: detect(,,imagePath,)
#custom_image = cv2.cvtColor(custom_image_bgr, cv2.COLOR_BGR2RGB)
#custom_image = cv2.resize(custom_image,(lib.network_width(net), lib.network_height(net)), interpolation = cv2.INTER_LINEAR)
#import scipy.misc
#custom_image = scipy.misc.imread(image)
#im, arr = array_to_image(custom_image) # you should comment line below: free_image(im)
num = c_int(0)
if debug: print("Assigned num")
pnum = pointer(num)
if debug: print("Assigned pnum")
predict_image(net, im)
letter_box = 0
#predict_image_letterbox(net, im)
#letter_box = 1
if debug: print("did prediction")
#dets = get_network_boxes(net, custom_image_bgr.shape[1], custom_image_bgr.shape[0], thresh, hier_thresh, None, 0, pnum, letter_box) # OpenCV
dets = get_network_boxes(net, im.w, im.h, thresh, hier_thresh, None, 0, pnum, letter_box)
if debug: print("Got dets")
num = pnum[0]
if debug: print("got zeroth index of pnum")
if nms:
do_nms_sort(dets, num, meta.classes, nms)
if debug: print("did sort")
res = []
if debug: print("about to range")
for j in range(num):
if debug: print("Ranging on "+str(j)+" of "+str(num))
if debug: print("Classes: "+str(meta), meta.classes, meta.names)
for i in range(meta.classes):
if debug: print("Class-ranging on "+str(i)+" of "+str(meta.classes)+"= "+str(dets[j].prob[i]))
if dets[j].prob[i] > 0:
b = dets[j].bbox
if altNames is None:
nameTag = meta.names[i]
else:
nameTag = altNames[i]
if debug:
print("Got bbox", b)
print(nameTag)
print(dets[j].prob[i])
print((b.x, b.y, b.w, b.h))
res.append((nameTag, dets[j].prob[i], (b.x, b.y, b.w, b.h)))
if debug: print("did range")
res = sorted(res, key=lambda x: -x[1])
if debug: print("did sort")
free_detections(dets, num)
if debug: print("freed detections")
return res
netMain = None
metaMain = None
altNames = None
def performDetect(imagePath="data/dog.jpg", thresh= 0.25, configPath = "./cfg/yolov3.cfg", weightPath = "yolov3.weights", metaPath= "./cfg/coco.data", showImage= True, makeImageOnly = False, initOnly= False):
"""
Convenience function to handle the detection and returns of objects.
Displaying bounding boxes requires libraries scikit-image and numpy
Parameters
----------------
imagePath: str
Path to the image to evaluate. Raises ValueError if not found
thresh: float (default= 0.25)
The detection threshold
configPath: str
Path to the configuration file. Raises ValueError if not found
weightPath: str
Path to the weights file. Raises ValueError if not found
metaPath: str
Path to the data file. Raises ValueError if not found
showImage: bool (default= True)
Compute (and show) bounding boxes. Changes return.
makeImageOnly: bool (default= False)
If showImage is True, this won't actually *show* the image, but will create the array and return it.
initOnly: bool (default= False)
Only initialize globals. Don't actually run a prediction.
Returns
----------------------
When showImage is False, list of tuples like
('obj_label', confidence, (bounding_box_x_px, bounding_box_y_px, bounding_box_width_px, bounding_box_height_px))
The X and Y coordinates are from the center of the bounding box. Subtract half the width or height to get the lower corner.
Otherwise, a dict with
{
"detections": as above
"image": a numpy array representing an image, compatible with scikit-image
"caption": an image caption
}
"""
# Import the global variables. This lets us instance Darknet once, then just call performDetect() again without instancing again
global metaMain, netMain, altNames #pylint: disable=W0603
assert 0 < thresh < 1, "Threshold should be a float between zero and one (non-inclusive)"
if not os.path.exists(configPath):
raise ValueError("Invalid config path `"+os.path.abspath(configPath)+"`")
if not os.path.exists(weightPath):
raise ValueError("Invalid weight path `"+os.path.abspath(weightPath)+"`")
if not os.path.exists(metaPath):
raise ValueError("Invalid data file path `"+os.path.abspath(metaPath)+"`")
if netMain is None:
netMain = load_net_custom(configPath.encode("ascii"), weightPath.encode("ascii"), 0, 1) # batch size = 1
if metaMain is None:
metaMain = load_meta(metaPath.encode("ascii"))
if altNames is None:
# In Python 3, the metafile default access craps out on Windows (but not Linux)
# Read the names file and create a list to feed to detect
try:
with open(metaPath) as metaFH:
metaContents = metaFH.read()
import re
match = re.search("names *= *(.*)$", metaContents, re.IGNORECASE | re.MULTILINE)
if match:
result = match.group(1)
else:
result = None
try:
if os.path.exists(result):
with open(result) as namesFH:
namesList = namesFH.read().strip().split("\n")
altNames = [x.strip() for x in namesList]
except TypeError:
pass
except Exception:
pass
if initOnly:
print("Initialized detector")
return None
if not os.path.exists(imagePath):
raise ValueError("Invalid image path `"+os.path.abspath(imagePath)+"`")
# Do the detection
#detections = detect(netMain, metaMain, imagePath, thresh) # if is used cv2.imread(image)
detections = detect(netMain, metaMain, imagePath.encode("ascii"), thresh)
if showImage:
try:
from skimage import io, draw
image = io.imread(imagePath)
print("*** "+str(len(detections))+" Results, color coded by confidence ***")
imcaption = []
for detection in detections:
label = detection[0]
confidence = detection[1]
pstring = label+": "+str(np.rint(100 * confidence))+"%"
imcaption.append(pstring)
print(pstring)
bounds = detection[2]
shape = image.shape
# x = shape[1]
# xExtent = int(x * bounds[2] / 100)
# y = shape[0]
# yExtent = int(y * bounds[3] / 100)
yExtent = int(bounds[3])
xEntent = int(bounds[2])
# Coordinates are around the center
xCoord = int(bounds[0] - bounds[2]/2)
yCoord = int(bounds[1] - bounds[3]/2)
boundingBox = [
[xCoord, yCoord],
[xCoord, yCoord + yExtent],
[xCoord + xEntent, yCoord + yExtent],
[xCoord + xEntent, yCoord]
]
# Wiggle it around to make a 3px border
rr, cc = draw.polygon_perimeter([x[1] for x in boundingBox], [x[0] for x in boundingBox], shape= shape)
rr2, cc2 = draw.polygon_perimeter([x[1] + 1 for x in boundingBox], [x[0] for x in boundingBox], shape= shape)
rr3, cc3 = draw.polygon_perimeter([x[1] - 1 for x in boundingBox], [x[0] for x in boundingBox], shape= shape)
rr4, cc4 = draw.polygon_perimeter([x[1] for x in boundingBox], [x[0] + 1 for x in boundingBox], shape= shape)
rr5, cc5 = draw.polygon_perimeter([x[1] for x in boundingBox], [x[0] - 1 for x in boundingBox], shape= shape)
boxColor = (int(255 * (1 - (confidence ** 2))), int(255 * (confidence ** 2)), 0)
draw.set_color(image, (rr, cc), boxColor, alpha= 0.8)
draw.set_color(image, (rr2, cc2), boxColor, alpha= 0.8)
draw.set_color(image, (rr3, cc3), boxColor, alpha= 0.8)
draw.set_color(image, (rr4, cc4), boxColor, alpha= 0.8)
draw.set_color(image, (rr5, cc5), boxColor, alpha= 0.8)
if not makeImageOnly:
io.imshow(image)
io.show()
detections = {
"detections": detections,
"image": image,
"caption": "\n<br/>".join(imcaption)
}
except Exception as e:
print("Unable to show image: "+str(e))
return detections
if __name__ == "__main__":
print(performDetect())

Просмотреть файл

@ -0,0 +1,7 @@
classes= 80
train = /home/pjreddie/data/coco/trainvalno5k.txt
valid = coco_testdev
#valid = data/coco_val_5k.list
names = /app/yolo/coco.names
backup = /home/pjreddie/backup/
eval=coco

Просмотреть файл

@ -1,6 +1,6 @@
azure-iothub-device-client~=1.4.3
setuptools
numpy
#azure-iothub-device-client~=1.4.3
#setuptools
#numpy
requests
opencv-contrib-python
#opencv-contrib-python

Просмотреть файл

@ -6,7 +6,7 @@
"tag": {
"version": "$CONTAINER_MODULE_VERSION",
"platforms": {
"amd64": "./Dockerfile.amd64"
"arm32v7": "./Dockerfile.arm64v8"
}
},
"buildOptions": []