mirror of https://github.com/AlexeyAB/darknet.git
fix python main module to be compiled with recent build scripts (#7876)
* fix python main module to be compiled with recent build scripts * fixes for posix systems
This commit is contained in:
parent
08088dccbb
commit
9c26b291fa
|
@ -9,6 +9,7 @@
|
||||||
*.dll
|
*.dll
|
||||||
*.lib
|
*.lib
|
||||||
*.dylib
|
*.dylib
|
||||||
|
*.pyc
|
||||||
mnist/
|
mnist/
|
||||||
data/
|
data/
|
||||||
caffe/
|
caffe/
|
||||||
|
@ -39,6 +40,7 @@ build/.ninja_log
|
||||||
build/Makefile
|
build/Makefile
|
||||||
*/vcpkg-manifest-install.log
|
*/vcpkg-manifest-install.log
|
||||||
build.log
|
build.log
|
||||||
|
__pycache__/
|
||||||
|
|
||||||
# OS Generated #
|
# OS Generated #
|
||||||
.DS_Store*
|
.DS_Store*
|
||||||
|
|
78
darknet.py
78
darknet.py
|
@ -1,26 +1,13 @@
|
||||||
#!python3
|
#!/usr/bin/env python3
|
||||||
|
|
||||||
"""
|
"""
|
||||||
Python 3 wrapper for identifying objects in images
|
Python 3 wrapper for identifying objects in images
|
||||||
|
|
||||||
Requires DLL compilation
|
Running the script requires opencv-python to be installed (`pip install opencv-python`)
|
||||||
|
|
||||||
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"
|
|
||||||
- Set environment variable "DARKNET_PATH" to path darknet lib .so (for Linux)
|
|
||||||
|
|
||||||
Directly viewing or returning bounding-boxed images requires scikit-image to be installed (`pip install scikit-image`)
|
Directly viewing or returning bounding-boxed images requires scikit-image to be installed (`pip install scikit-image`)
|
||||||
|
Use pip3 instead of pip on some systems to be sure to install modules for python3
|
||||||
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
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from ctypes import *
|
from ctypes import *
|
||||||
import math
|
import math
|
||||||
import random
|
import random
|
||||||
|
@ -178,51 +165,17 @@ def detect_image(network, class_names, image, thresh=.5, hier_thresh=.5, nms=.45
|
||||||
return sorted(predictions, key=lambda x: x[1])
|
return sorted(predictions, key=lambda x: x[1])
|
||||||
|
|
||||||
|
|
||||||
# lib = CDLL("/home/pjreddie/documents/darknet/libdarknet.so", RTLD_GLOBAL)
|
if os.name == "posix":
|
||||||
# lib = CDLL("libdarknet.so", RTLD_GLOBAL)
|
cwd = os.path.dirname(__file__)
|
||||||
hasGPU = True
|
lib = CDLL(cwd + "/libdarknet.so", RTLD_GLOBAL)
|
||||||
if os.name == "nt":
|
elif os.name == "nt":
|
||||||
cwd = os.path.dirname(__file__)
|
cwd = os.path.dirname(__file__)
|
||||||
os.environ['PATH'] = cwd + ';' + os.environ['PATH']
|
os.environ['PATH'] = cwd + ';' + os.environ['PATH']
|
||||||
winGPUdll = os.path.join(cwd, "yolo_cpp_dll.dll")
|
lib = CDLL("darknet.dll", RTLD_GLOBAL)
|
||||||
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 {} not forcing CPU mode".format(tmp))
|
|
||||||
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 as cpu_error:
|
|
||||||
print(cpu_error)
|
|
||||||
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 {}. Trying a GPU run anyway.".format(winNoGPUdll))
|
|
||||||
else:
|
else:
|
||||||
lib = CDLL(os.path.join(
|
print("Unsupported OS")
|
||||||
os.environ.get('DARKNET_PATH', './'),
|
exit
|
||||||
"libdarknet.so"), RTLD_GLOBAL)
|
|
||||||
lib.network_width.argtypes = [c_void_p]
|
lib.network_width.argtypes = [c_void_p]
|
||||||
lib.network_width.restype = c_int
|
lib.network_width.restype = c_int
|
||||||
lib.network_height.argtypes = [c_void_p]
|
lib.network_height.argtypes = [c_void_p]
|
||||||
|
@ -235,10 +188,7 @@ predict = lib.network_predict_ptr
|
||||||
predict.argtypes = [c_void_p, POINTER(c_float)]
|
predict.argtypes = [c_void_p, POINTER(c_float)]
|
||||||
predict.restype = POINTER(c_float)
|
predict.restype = POINTER(c_float)
|
||||||
|
|
||||||
if hasGPU:
|
set_gpu = lib.cuda_set_device
|
||||||
set_gpu = lib.cuda_set_device
|
|
||||||
set_gpu.argtypes = [c_int]
|
|
||||||
|
|
||||||
init_cpu = lib.init_cpu
|
init_cpu = lib.init_cpu
|
||||||
|
|
||||||
make_image = lib.make_image
|
make_image = lib.make_image
|
||||||
|
|
Loading…
Reference in New Issue