darknet.py OpenCV
darknet.pyにコードが古くなってるっぽいexamples/detector-scipy-opencv.py
のOpenCV部を取り込み、Python 3用に
import cv2import sys, ossys.path.append(os.path.join(os.getcwd(),'python/'))import darknet as dnnet = dn.load_net("cfg/yolov3.cfg", "backup/hoge.weights", 0)meta = dn.load_meta('cfg/yolov3.cfg')np_image = cv2.imread('data/dog.jpg')r = dn.detect_cv(net, meta, np_image)print(r)
from ctypes import *import mathimport randomimport osDARKNET_SO_PATH = os.environ.get('DARKNET_SO_PATH', os.path.realpath('./libdarknet.so'))# assert os.path.exists(DARKNET_SO_PATH), DARKNET_SO_PATHdef 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 ireturn len(probs)-1def c_array(ctype, values):arr = (ctype*len(values))()arr[:] = valuesreturn arrclass 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)lib = CDLL(DARKNET_SO_PATH, RTLD_GLOBAL)lib.network_width.argtypes = [c_void_p]lib.network_width.restype = c_intlib.network_height.argtypes = [c_void_p]lib.network_height.restype = c_intpredict = lib.network_predictpredict.argtypes = [c_void_p, POINTER(c_float)]predict.restype = POINTER(c_float)set_gpu = lib.cuda_set_deviceset_gpu.argtypes = [c_int]make_image = lib.make_imagemake_image.argtypes = [c_int, c_int, c_int]make_image.restype = IMAGEget_network_boxes = lib.get_network_boxesget_network_boxes.argtypes = [c_void_p, c_int, c_int, c_float, c_float, POINTER(c_int), c_int, POINTER(c_int)]get_network_boxes.restype = POINTER(DETECTION)make_network_boxes = lib.make_network_boxesmake_network_boxes.argtypes = [c_void_p]make_network_boxes.restype = POINTER(DETECTION)free_detections = lib.free_detectionsfree_detections.argtypes = [POINTER(DETECTION), c_int]free_ptrs = lib.free_ptrsfree_ptrs.argtypes = [POINTER(c_void_p), c_int]network_predict = lib.network_predictnetwork_predict.argtypes = [c_void_p, POINTER(c_float)]reset_rnn = lib.reset_rnnreset_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_pdo_nms_obj = lib.do_nms_objdo_nms_obj.argtypes = [POINTER(DETECTION), c_int, c_int, c_float]do_nms_sort = lib.do_nms_sortdo_nms_sort.argtypes = [POINTER(DETECTION), c_int, c_int, c_float]free_image = lib.free_imagefree_image.argtypes = [IMAGE]letterbox_image = lib.letterbox_imageletterbox_image.argtypes = [IMAGE, c_int, c_int]letterbox_image.restype = IMAGE_load_meta = lib.get_metadatalib.get_metadata.argtypes = [c_char_p]lib.get_metadata.restype = METADATAload_image = lib.load_image_colorload_image.argtypes = [c_char_p, c_int, c_int]load_image.restype = IMAGErgbgr_image = lib.rgbgr_imagergbgr_image.argtypes = [IMAGE]predict_image = lib.network_predict_imagepredict_image.argtypes = [c_void_p, IMAGE]predict_image.restype = POINTER(c_float)def classify(net, meta, im):out = predict_image(net, im)res = []for i in range(meta.classes):res.append((meta.names[i], out[i]))res = sorted(res, key=lambda x: -x[1])return resdef load_net(cfg_path, weights_path, clear=0):return _load_net(cfg_path.encode('ascii'), weights_path.encode('ascii'), clear)def load_meta(cfg_path):return _load_meta(cfg_path.encode('ascii'))def _detect(net, meta, im, thresh=.5, hier_thresh=.5, nms=.45):num = c_int(0)pnum = pointer(num)predict_image(net, im)dets = get_network_boxes(net, im.w, im.h, thresh, hier_thresh, None, 0, pnum)num = pnum[0]if (nms): do_nms_obj(dets, num, meta.classes, nms);res = []for j in range(num):for i in range(meta.classes):if dets[j].prob[i] > 0:b = dets[j].bboxres.append((meta.names[i], dets[j].prob[i], (b.x, b.y, b.w, b.h)))res = sorted(res, key=lambda x: -x[1])free_detections(dets, num)return resdef detect(net, meta, image_path, thresh=.5, hier_thresh=.5, nms=.45):im = load_image(image_path, 0, 0)res = _detect(net, meta, im, thresh, hier_thresh, nms)free_image(im)return resdef array_to_image(arr):arr = arr.transpose(2,0,1)c = arr.shape[0]h = arr.shape[1]w = arr.shape[2]arr = (arr/255.0).flatten()data = c_array(c_float, arr)im = IMAGE(w,h,c,data)return imdef detect_cv(net, meta, np_image, thresh=.5, hier_thresh=.5, nms=.45):im = array_to_image(np_image)rgbgr_image(im)return _detect(net, meta, im, thresh, hier_thresh, nms)