Python Wrapper
sushant1727 opened this issue · 14 comments
After successfully running darnknet.exe in windows now I am looking forward to use Python with your implementation.
Is there a way I can use Python wrapper in windows/ubuntu with/without GPU?
Thanks
Shan
Yes. You can use darknet.py
on Windows and Linux by using Python 2.x or 3.x:
-
on Linux: run
darknet/darknet.py
(useLIBSO=1
in theMakefile
and compile) -
on Windows: run
darknet/build/darknet/x64/darknet.py
(first compiledarknet/build/darknet/yolo_cpp_dll.sln
)
For detection on the video try to use Python-code like this:
def capture(thresh=.5, hier_thresh=.5, nms=.45, configPath = "./cfg/yolov3.cfg", weightPath = "yolov3.weights", metaPath= "./data/coco.data", showImage= True, makeImageOnly = False, initOnly= False):
global metaMain, netMain, altNames #pylint: disable=W0603
netMain = load_net_custom(configPath.encode("ascii"), weightPath.encode("ascii"), 0, 1) # batch size = 1
metaMain = load_meta(metaPath.encode("ascii"))
num = c_int(0)
pnum = pointer(num)
num = pnum[0]
capture = cv2.VideoCapture(0)
print(capture.get(cv2.CAP_PROP_FPS))
capture.set(cv2.CAP_PROP_FRAME_WIDTH, 1024)
capture.set(cv2.CAP_PROP_FRAME_HEIGHT, 768)
while True:
ret, frame = capture.read()
im, arr = array_to_image(frame)
predict_image(netMain, im)
dets = get_network_boxes(netMain, im.w, im.h, thresh, hier_thresh, None, 0, pnum, 1)
if nms:
do_nms_sort(dets, num, metaMain.classes, nms)
res = []
for j in range(num):
for i in range(metaMain.classes):
if dets[j].prob[i] > 0:
b = dets[j].bbox
nameTag = metaMain.names[i]
res.append((nameTag, dets[j].prob[i], (b.x, b.y, b.w, b.h)))
print(res)
cv2.imshow('frame', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
capture.release()
cv2.destroyAllWindows()
if __name__ == "__main__":
print(capture())
Perhaps you should optimize this function to avoid too much CPU-usage:
Lines 195 to 205 in c1bb8c1
You're awesome, I will try this.
Since I was looking for Python Wrapper I also found PyhtonWrapper Yolo3-4-Py.
The python wrapper does not have speed decrease?
I haven't tried the AlexeyAB wrapper but I have the Yolo3-4-Py. With CPU it runs at max 1 FPS and with GPU it should near to 10 FPS(As suggested by developer).
I will get back here and share how much FPS I get with AlexeyAB wrapper.
Hey Alex, I am back to bug you again. I compiled the yolo_cpp_dll without any errors but getting these errors when running darknet.py.
python darknet.py
Traceback (most recent call last):
File "darknet.py", line 109, in
raise ValueError("NoDLL")
ValueError: NoDLL
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "darknet.py", line 119, in
lib = CDLL(winGPUdll, RTLD_GLOBAL)
File "C:\Program Files\Python 3.5\lib\ctypes_init_.py", line 347, in init
self._handle = _dlopen(self._name, mode)
OSError: [WinError 126] The specified module could not be found
@sushant1727 What of these 2 files did you run?
-
on Linux: run
darknet/darknet.py
-
on Windows: run
darknet/build/darknet/x64/darknet.py
Is yolo_cpp_dll.dll
file near with your darknet.py
?
@AlexeyAB:
On windows I am in the darknet.py directory and ran python darknet.py
I copied the file "yolo_cpp_dll.dll"
and got this new error.
Traceback (most recent call last):
File "darknet.py", line 110, in
lib = CDLL(winGPUdll, RTLD_GLOBAL)
File "C:\Program Files\Python 3.5\lib\ctypes_init_.py", line 347, in init
self._handle = _dlopen(self._name, mode)
OSError: [WinError 126] The specified module could not be found
On windows I am in the darknet.py directory
- What is the darknet.py directory?
- Is there a file
yolo_cpp_dll.dll
?
Directory is C:\Users..\Desktop\darknet-master>python darknet.py
06/08/2018 03:52 PM 964,608 yolo_cpp_dll.dll
27 File(s) 169,749,927 bytes
11 Dir(s) 69,967,814,656 bytes free
@sushant1727 Run:
C:\Users..\Desktop\darknet-master\build\darknet\x64>python darknet.py
Yes. You can use
darknet.py
on Windows and Linux by using Python 2.x or 3.x:
- on Linux: run
darknet/darknet.py
(useLIBSO=1
in theMakefile
and compile)- on Windows: run
darknet/build/darknet/x64/darknet.py
(first compiledarknet/build/darknet/yolo_cpp_dll.sln
)For detection on the video try to use Python-code like this:
def capture(thresh=.5, hier_thresh=.5, nms=.45, configPath = "./cfg/yolov3.cfg", weightPath = "yolov3.weights", metaPath= "./data/coco.data", showImage= True, makeImageOnly = False, initOnly= False): global metaMain, netMain, altNames #pylint: disable=W0603 netMain = load_net_custom(configPath.encode("ascii"), weightPath.encode("ascii"), 0, 1) # batch size = 1 metaMain = load_meta(metaPath.encode("ascii")) num = c_int(0) pnum = pointer(num) num = pnum[0] capture = cv2.VideoCapture(0) print(capture.get(cv2.CAP_PROP_FPS)) capture.set(cv2.CAP_PROP_FRAME_WIDTH, 1024) capture.set(cv2.CAP_PROP_FRAME_HEIGHT, 768) while True: ret, frame = capture.read() im, arr = array_to_image(frame) predict_image(netMain, im) dets = get_network_boxes(netMain, im.w, im.h, thresh, hier_thresh, None, 0, pnum, 1) if nms: do_nms_sort(dets, num, metaMain.classes, nms) res = [] for j in range(num): for i in range(metaMain.classes): if dets[j].prob[i] > 0: b = dets[j].bbox nameTag = metaMain.names[i] res.append((nameTag, dets[j].prob[i], (b.x, b.y, b.w, b.h))) print(res) cv2.imshow('frame', frame) if cv2.waitKey(1) & 0xFF == ord('q'): break capture.release() cv2.destroyAllWindows() if __name__ == "__main__": print(capture())Perhaps you should optimize this function to avoid too much CPU-usage:
Lines 195 to 205 in c1bb8c1
def array_to_image(arr):
import numpy as np
# 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
I've copypasted capture()
into darknet.py
, and simply changed the bottom two lines:
if __name__ == "__main__":
print(capture())
When I ran this code, nothing was detected:
python darknet.py
Here's the result from the same video using the executable:
./darknet detector demo data/coco.data cfg/yolov3.cfg yolov3.weights ../data/test3.mp4
The same thing happens when I use my custom model cfg and weights to test.
ADD: Just to clarify, I understand that the image is supposed to be displayed without bounding box. I can just add couple of cv2.rectangle()
for that.
However, the program just doesn't detect anything at all - res
is empty, and num
is 0
.
( If the following resolution is not related, please let me know and I will remove this comment )
To resolve OSError: [WinError 126] The specified module could not be found issue,
I notice that I need to put all opencv related dlls, pthreadGC2.dll, and pthreadVC2.dll within the folder where yolo_cpp_dll.dll is located.
Windows 7 + Visual Studio 2015 + OpenCV 2.4.13
@willSapgreen Hi, I have the same error on my PC, Win 10, x64, opencv 3.4.0, CUDA 10, python 3.5.4.
The only difference is I am running darknet_video.py copied from here into build/darknet/x64
Traceback (most recent call last):
File "darknet_video.py", line 57, in <module>
lib = CDLL("./libdarknet.so", RTLD_GLOBAL)
File "C:\python354\lib\ctypes\__init__.py", line 351, in __init__
self._handle = _dlopen(self._name, mode)
OSError: [WinError 126] The specified module could not be found
My yolo_cpp_dll.dll
is located in build/darknet/x64 and there are pthreadGC2.dll, pthreadVC2.dll and opencv related dlls (opencv_ffmpeg340.dll, opencv_ffmpeg340_64.dll, opencv_world340.dll, opencv_world340d.dll ) from C:\opencv340\opencv\build\x64\vc14\bin
Could you help me as well? thanks.