Code Monkey home page Code Monkey logo

pyyolo's Introduction

Python Wrapper for the YOLO

logo

Installation

Dependencies

Darknet Shared Library

You should first install darknet library with BUILD_SHARED_LIBS set to ON. After the installation the LIB_DARKNET environment variable should be set to shared library path. The path is required in runtime so my recommendation is adding this to your rc file. export LIB_DARKNET=<path_to_libdarknet.so>

PyYOLO

From PyPi
pip3 install pyyolo --user
From source
git clone https://github.com/goktug97/PyYOLO
cd PyYOLO
python3 setup.py install --user

Documentation

Example

python sample.py

sample.py

import cv2
import pyyolo

def main():
    detector = pyyolo.YOLO("./models/yolov3-spp.cfg",
                           "./models/yolov3-spp.weights",
                           "./models/coco.data",
                           detection_threshold = 0.5,
                           hier_threshold = 0.5,
                           nms_threshold = 0.45)

    cap = cv2.VideoCapture(0)
    while True:
        ret, frame = cap.read()
        dets = detector.detect(frame, rgb=False)
        for i, det in enumerate(dets):
            print(f'Detection: {i}, {det}')
            xmin, ymin, xmax, ymax = det.to_xyxy()
            cv2.rectangle(frame, (xmin, ymin), (xmax, ymax), (0, 0, 255))
        cv2.imshow('cvwindow', frame)
        if cv2.waitKey(1) == 27:
            break

if __name__ == '__main__':
    main()

BBox Class

This class is just a numpy array with extra attributes and functions.

Python 3.8.0 (default, Oct 23 2019, 18:51:26)
[GCC 9.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import pyyolo
>>> bbox = pyyolo.BBox(x=10, y=20, w=100, h=200, prob=0.9, name='person')
>>> bbox
BBox([ 10,  20, 100, 200])
>>> print(bbox)
x: 10, y: 20, w: 100, h: 200, probability: 0.9, name: person
>>> x, y, w, h = bbox
>>> print(x, y, w, h)
10 20 100 200
>>> bbox + bbox
BBox([ 20,  40, 200, 400])
>>> bbox.prob
0.9
>>> bbox.name
'person'
>>> xmin, ymin, xmax, ymax = bbox.to_xyxy()
>>> xmin, ymin, xmax, ymax
(10, 20, 110, 220)

YOLO Class

  • detect function returns list of BBox Instances. See sample.py for example usage.
Python 3.8.0 (default, Oct 23 2019, 18:51:26)
[GCC 9.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import pyyolo
>>> detector = pyyolo.YOLO("./models/yolov3-spp.cfg",
                           "./models/yolov3-spp.weights",
                           "./models/coco.data",
                           detection_threshold = 0.5,
                           hier_threshold = 0.5,
                           nms_threshold = 0.45)
>>> import cv2
>>> img = cv2.imread('test.png')
>>> detector.detect(img)
[BBox([ 29, 134, 461, 339])]
>>> dets = detector.detect(img)
>>> print(dets[0])
x: 29, y: 134, w: 461, h: 339, probability: 0.6172798275947571, name: person

License

PyYOLO is licensed under the MIT License.

pyyolo's People

Contributors

goktug97 avatar joao-avelino avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

pyyolo's Issues

updated yolov4 repo

Hi!

I compiled darknet some days ago to get the dll and everything worked fine.

But something was changed by Alexey... when I compile the dll with the latest version (on Linux AND Windows), the PyYOLO code will just hang and stops working without an error-message.

Can this be a compatibility issue?

Thank you.

AttributeError: /usr/lib/libdarknet.so: undefined symbol: load_network_custom

I compiled darknet with the version at github as of 31 Jan 2020, which produced a libdarknet.so (without actually setting any special flags in the makefile, but I tried adding BUILD_SHARED_LIBS=on
in the Makefile in any case ; either way, I got a AttributeError: /usr/lib/libdarknet.so: undefined symbol: load_network_custom upon trying to import pyyolo

load_network_custom): symbol not found

Hi
Does this depend on an old version of darknet? I see this issue in darknet: libdarknet.so: undefined symbol: load_network_custom. Apparently, the function was renamed a while ago.

What I get is the following:

import cv2
import pyyolo

def main():
    detector = pyyolo.YOLO("darknet/cfg/yolov3-spp.cfg",
                           "yolo.weights",
                           "darknet/cfg/coco.data",
                           detection_threshold = 0.5,
                           hier_threshold = 0.5,
                           nms_threshold = 0.45)

    cap = cv2.VideoCapture(0)
    while True:
        ret, frame = cap.read()
        dets = detector.detect(frame, rgb=False)
        for i, det in enumerate(dets):
            print(f'Detection: {i}, {det}')
            xmin, ymin, xmax, ymax = det.to_xyxy()
            cv2.rectangle(frame, (xmin, ymin), (xmax, ymax), (0, 0, 255))
        cv2.imshow('cvwindow', frame)
        if cv2.waitKey(1) == 27:
            break

if __name__ == '__main__':
    main()

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-39-23db651c2c7e> in <module>
      1 import cv2
----> 2 import pyyolo
      3 
      4 def main():
      5     detector = pyyolo.YOLO("darknet/cfg/yolov3-spp.cfg",

~/anaconda3/lib/python3.6/site-packages/pyyolo/__init__.py in <module>
----> 1 from .yolo import *

~/anaconda3/lib/python3.6/site-packages/pyyolo/yolo.py in <module>
      2 
      3 import cv2
----> 4 from .cyolo import *
      5 import numpy as np
      6 

~/anaconda3/lib/python3.6/site-packages/pyyolo/cyolo.py in <module>
     95 load_net.restype = c_void_p
     96 
---> 97 load_net_custom = lib.load_network_custom
     98 load_net_custom.argtypes = [c_char_p, c_char_p, c_int, c_int]
     99 load_net_custom.restype = c_void_p

~/anaconda3/lib/python3.6/ctypes/__init__.py in __getattr__(self, name)
    359         if name.startswith('__') and name.endswith('__'):
    360             raise AttributeError(name)
--> 361         func = self.__getitem__(name)
    362         setattr(self, name, func)
    363         return func

~/anaconda3/lib/python3.6/ctypes/__init__.py in __getitem__(self, name_or_ordinal)
    364 
    365     def __getitem__(self, name_or_ordinal):
--> 366         func = self._FuncPtr((name_or_ordinal, self))
    367         if not isinstance(name_or_ordinal, int):
    368             func.__name__ = name_or_ordinal

AttributeError: dlsym(0x7fdfa86889f0, load_network_custom): symbol not found

Google colab keeps on crashing while using pyyolo

The modal keeps on crashing. I am using pyyolo to detect objects in a video. But every time after two detections, the kernel crashes and restarts.

Timestamp Level Message
Jun 11, 2020, 6:39:29 PM WARNING WARNING:root:kernel 1c954a2c-3ad4-449e-b204-44f85e85de9a restarted
Jun 11, 2020, 6:39:29 PM INFO KernelRestarter: restarting kernel (1/5), keep random ports
Jun 11, 2020, 6:39:16 PM WARNING Done! Loaded 162 layers from weights-file
Jun 11, 2020, 6:39:15 PM WARNING Try to load weights: /mydrive/backup/yolov4_custom2_final.weights
Jun 11, 2020, 6:39:15 PM WARNING nms_kind: greedynms (1), beta = 0.600000
Jun 11, 2020, 6:39:15 PM WARNING nms_kind: greedynms (1), beta = 0.600000
Jun 11, 2020, 6:39:15 PM WARNING nms_kind: greedynms (1), beta = 0.600000
Jun 11, 2020, 6:39:15 PM WARNING mini_batch = 1, batch = 1, time_steps = 1, train = 0
Jun 11, 2020, 6:39:15 PM WARNING net.optimized_memory = 0
Jun 11, 2020, 6:39:15 PM WARNING Try to load cfg: /content/darknet/cfg/yolov4_custom2.cfg, weights: /mydrive/backup/yolov4_custom2_final.weights, clear = 0
Jun 11, 2020, 6:39:15 PM WARNING Loading weights from /mydrive/backup/yolov4_custom2_final.weights...
Jun 11, 2020, 6:39:15 PM WARNING Allocate additional workspace_size = 52.43 MB
Jun 11, 2020, 6:39:15 PM WARNING avg_outputs = 489778
Jun 11, 2020, 6:39:15 PM WARNING Total BFLOPS 59.563
Jun 11, 2020, 6:39:15 PM WARNING [yolo] params: iou loss: ciou (4), iou_norm: 0.07, cls_norm: 1.00, scale_x_y: 1.05

Screenshot (38)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.