Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Python Wrapper #955

Open
sushant1727 opened this issue May 31, 2018 · 14 comments
Open

Python Wrapper #955

sushant1727 opened this issue May 31, 2018 · 14 comments
Labels
enhancement want enhancement Want to improve accuracy, speed or functionality

Comments

@sushant1727
Copy link

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

@AlexeyAB
Copy link
Owner

Yes. You can use darknet.py on Windows and Linux by using Python 2.x or 3.x:

  • on Linux: run darknet/darknet.py (use LIBSO=1 in the Makefile and compile)

  • on Windows: run darknet/build/darknet/x64/darknet.py (first compile darknet/build/darknet/yolo_cpp_dll.sln)

image


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:

darknet/darknet.py

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

@sushant1727
Copy link
Author

You're awesome, I will try this.
Since I was looking for Python Wrapper I also found PyhtonWrapper Yolo3-4-Py.

@MyVanitar
Copy link

The python wrapper does not have speed decrease?

@sushant1727
Copy link
Author

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.

@sushant1727
Copy link
Author

sushant1727 commented Jun 8, 2018

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

@AlexeyAB
Copy link
Owner

AlexeyAB commented Jun 8, 2018

@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?

@sushant1727
Copy link
Author

@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

@AlexeyAB
Copy link
Owner

AlexeyAB commented Jun 8, 2018

@sushant1727

On windows I am in the darknet.py directory

  1. What is the darknet.py directory?
  2. Is there a file yolo_cpp_dll.dll?

@sushant1727
Copy link
Author

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

@AlexeyAB
Copy link
Owner

AlexeyAB commented Jun 8, 2018

@sushant1727 Run:

C:\Users..\Desktop\darknet-master\build\darknet\x64>python darknet.py

@hj3yoo
Copy link

hj3yoo commented Sep 13, 2018

Yes. You can use darknet.py on Windows and Linux by using Python 2.x or 3.x:

  • on Linux: run darknet/darknet.py (use LIBSO=1 in the Makefile and compile)
  • on Windows: run darknet/build/darknet/x64/darknet.py (first compile darknet/build/darknet/yolo_cpp_dll.sln)

image

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:

darknet/darknet.py

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

screenshot from 2018-09-13 15-25-07

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

screenshot from 2018-09-13 15-26-30

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.

@AlexeyAB AlexeyAB added the want enhancement Want to improve accuracy, speed or functionality label Sep 13, 2018
@willSapgreen
Copy link

( 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

@bit-scientist
Copy link

bit-scientist commented Jan 29, 2019

@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.

@WefPok
Copy link

WefPok commented Feb 16, 2019

@hj3yoo Hi! Have you solved this problem? I also got empty results...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement want enhancement Want to improve accuracy, speed or functionality
Projects
None yet
Development

No branches or pull requests

7 participants