Guest User

Untitled

a guest
Sep 20th, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.46 KB | None | 0 0
  1. from ctypes import *
  2. import math
  3. import random
  4.  
  5. def sample(probs):
  6. s = sum(probs)
  7. probs = [a/s for a in probs]
  8. r = random.uniform(0, 1)
  9. for i in range(len(probs)):
  10. r = r - probs[i]
  11. if r <= 0:
  12. return i
  13. return len(probs)-1
  14.  
  15. def c_array(ctype, values):
  16. arr = (ctype*len(values))()
  17. arr[:] = values
  18. return arr
  19.  
  20. class BOX(Structure):
  21. _fields_ = [("x", c_float),
  22. ("y", c_float),
  23. ("w", c_float),
  24. ("h", c_float)]
  25.  
  26. class DETECTION(Structure):
  27. _fields_ = [("bbox", BOX),
  28. ("classes", c_int),
  29. ("prob", POINTER(c_float)),
  30. ("mask", POINTER(c_float)),
  31. ("objectness", c_float),
  32. ("sort_class", c_int)]
  33.  
  34.  
  35. class IMAGE(Structure):
  36. _fields_ = [("w", c_int),
  37. ("h", c_int),
  38. ("c", c_int),
  39. ("data", POINTER(c_float))]
  40.  
  41. class METADATA(Structure):
  42. _fields_ = [("classes", c_int),
  43. ("names", POINTER(c_char_p))]
  44.  
  45.  
  46.  
  47. #lib = CDLL("/home/pjreddie/documents/darknet/libdarknet.so", RTLD_GLOBAL)
  48. # lib = CDLL("libdarknet.so", RTLD_GLOBAL)
  49. lib = CDLL("/home/paradox/Nish/Programming/Python/conda-shit/mpercept/demo/yolo/darknet/libdarknet.so", RTLD_GLOBAL)
  50. lib.network_width.argtypes = [c_void_p]
  51. lib.network_width.restype = c_int
  52. lib.network_height.argtypes = [c_void_p]
  53. lib.network_height.restype = c_int
  54.  
  55. predict = lib.network_predict
  56. predict.argtypes = [c_void_p, POINTER(c_float)]
  57. predict.restype = POINTER(c_float)
  58.  
  59. set_gpu = lib.cuda_set_device
  60. set_gpu.argtypes = [c_int]
  61.  
  62. make_image = lib.make_image
  63. make_image.argtypes = [c_int, c_int, c_int]
  64. make_image.restype = IMAGE
  65.  
  66. get_network_boxes = lib.get_network_boxes
  67. get_network_boxes.argtypes = [c_void_p, c_int, c_int, c_float, c_float, POINTER(c_int), c_int, POINTER(c_int)]
  68. get_network_boxes.restype = POINTER(DETECTION)
  69.  
  70. make_network_boxes = lib.make_network_boxes
  71. make_network_boxes.argtypes = [c_void_p]
  72. make_network_boxes.restype = POINTER(DETECTION)
  73.  
  74. free_detections = lib.free_detections
  75. free_detections.argtypes = [POINTER(DETECTION), c_int]
  76.  
  77. free_ptrs = lib.free_ptrs
  78. free_ptrs.argtypes = [POINTER(c_void_p), c_int]
  79.  
  80. network_predict = lib.network_predict
  81. network_predict.argtypes = [c_void_p, POINTER(c_float)]
  82.  
  83. reset_rnn = lib.reset_rnn
  84. reset_rnn.argtypes = [c_void_p]
  85.  
  86. load_net = lib.load_network
  87. load_net.argtypes = [c_char_p, c_char_p, c_int]
  88. load_net.restype = c_void_p
  89.  
  90. do_nms_obj = lib.do_nms_obj
  91. do_nms_obj.argtypes = [POINTER(DETECTION), c_int, c_int, c_float]
  92.  
  93. do_nms_sort = lib.do_nms_sort
  94. do_nms_sort.argtypes = [POINTER(DETECTION), c_int, c_int, c_float]
  95.  
  96. free_image = lib.free_image
  97. free_image.argtypes = [IMAGE]
  98.  
  99. letterbox_image = lib.letterbox_image
  100. letterbox_image.argtypes = [IMAGE, c_int, c_int]
  101. letterbox_image.restype = IMAGE
  102.  
  103. load_meta = lib.get_metadata
  104. lib.get_metadata.argtypes = [c_char_p]
  105. lib.get_metadata.restype = METADATA
  106.  
  107. load_image = lib.load_image_color
  108. load_image.argtypes = [c_char_p, c_int, c_int]
  109. load_image.restype = IMAGE
  110.  
  111. rgbgr_image = lib.rgbgr_image
  112. rgbgr_image.argtypes = [IMAGE]
  113.  
  114. predict_image = lib.network_predict_image
  115. predict_image.argtypes = [c_void_p, IMAGE]
  116. predict_image.restype = POINTER(c_float)
  117.  
  118. def classify(net, meta, im):
  119. out = predict_image(net, im)
  120. res = []
  121. for i in range(meta.classes):
  122. res.append((meta.names[i], out[i]))
  123. res = sorted(res, key=lambda x: -x[1])
  124. return res
  125.  
  126. def detect(net, meta, image, thresh=.5, hier_thresh=.5, nms=.45):
  127. im = load_image(image, 0, 0)
  128. print(im)
  129. num = c_int(0)
  130. pnum = pointer(num)
  131. predict_image(net, im)
  132. dets = get_network_boxes(net, im.w, im.h, thresh, hier_thresh, None, 0, pnum)
  133. num = pnum[0]
  134. if (nms): do_nms_obj(dets, num, meta.classes, nms);
  135.  
  136. res = []
  137. for j in range(num):
  138. for i in range(meta.classes):
  139. if dets[j].prob[i] > 0:
  140. b = dets[j].bbox
  141. res.append((meta.names[i], dets[j].prob[i], (b.x, b.y, b.w, b.h)))
  142. res = sorted(res, key=lambda x: -x[1])
  143. free_image(im)
  144. free_detections(dets, num)
  145. return res
  146.  
  147. def array_to_image(arr):
  148. arr = arr.transpose(2,0,1)
  149. c = arr.shape[0]
  150. h = arr.shape[1]
  151. w = arr.shape[2]
  152. arr = (arr/255.0).flatten()
  153. data = c_array(c_float, arr)
  154. return IMAGE(w, h, c, data)
  155.  
  156. import cv2
  157.  
  158. def detect2(net, meta, image, thresh=.5, hier_thresh=.5, nms=.45):
  159. img = cv2.imread(image)
  160. # im = load_image(image, 0, 0)
  161. im = array_to_image(img)
  162. print(im)
  163. num = c_int(0)
  164. pnum = pointer(num)
  165. predict_image(net, im)
  166. dets = get_network_boxes(net, im.w, im.h, thresh, hier_thresh, None, 0, pnum)
  167. num = pnum[0]
  168. if (nms): do_nms_obj(dets, num, meta.classes, nms);
  169.  
  170. res = []
  171. for j in range(num):
  172. for i in range(meta.classes):
  173. if dets[j].prob[i] > 0:
  174. b = dets[j].bbox
  175. res.append((meta.names[i], dets[j].prob[i], (b.x, b.y, b.w, b.h)))
  176. res = sorted(res, key=lambda x: -x[1])
  177. free_image(im)
  178. free_detections(dets, num)
  179. return res
  180.  
  181. if __name__ == "__main__":
  182. #net = load_net("cfg/densenet201.cfg", "/home/pjreddie/trained/densenet201.weights", 0)
  183. #im = load_image("data/wolf.jpg", 0, 0)
  184. #meta = load_meta("cfg/imagenet1k.data")
  185. #r = classify(net, meta, im)
  186. #print r[:10]
  187. net = load_net("cfg/yolov2-tiny.cfg".encode('utf-8'), "weights/yolov2-tiny.weights".encode('utf-8'), 0)
  188. meta = load_meta("cfg/coco.data".encode('utf-8'))
  189. r = detect(net, meta, "data/dog.jpg".encode('utf-8'))
  190. print(r)
Add Comment
Please, Sign In to add comment