detector-scipy-opencv.py 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. # Stupid python path shit.
  2. # Instead just add darknet.py to somewhere in your python path
  3. # OK actually that might not be a great idea, idk, work in progress
  4. # Use at your own risk. or don't, i don't care
  5. from scipy.misc import imread
  6. import cv2
  7. def array_to_image(arr):
  8. arr = arr.transpose(2,0,1)
  9. c = arr.shape[0]
  10. h = arr.shape[1]
  11. w = arr.shape[2]
  12. arr = (arr/255.0).flatten()
  13. data = dn.c_array(dn.c_float, arr)
  14. im = dn.IMAGE(w,h,c,data)
  15. return im
  16. def detect2(net, meta, image, thresh=.5, hier_thresh=.5, nms=.45):
  17. boxes = dn.make_boxes(net)
  18. probs = dn.make_probs(net)
  19. num = dn.num_boxes(net)
  20. dn.network_detect(net, image, thresh, hier_thresh, nms, boxes, probs)
  21. res = []
  22. for j in range(num):
  23. for i in range(meta.classes):
  24. if probs[j][i] > 0:
  25. res.append((meta.names[i], probs[j][i], (boxes[j].x, boxes[j].y, boxes[j].w, boxes[j].h)))
  26. res = sorted(res, key=lambda x: -x[1])
  27. dn.free_ptrs(dn.cast(probs, dn.POINTER(dn.c_void_p)), num)
  28. return res
  29. import sys, os
  30. sys.path.append(os.path.join(os.getcwd(),'python/'))
  31. import darknet as dn
  32. # Darknet
  33. net = dn.load_net("cfg/tiny-yolo.cfg", "tiny-yolo.weights", 0)
  34. meta = dn.load_meta("cfg/coco.data")
  35. r = dn.detect(net, meta, "data/dog.jpg")
  36. print r
  37. # scipy
  38. arr= imread('data/dog.jpg')
  39. im = array_to_image(arr)
  40. r = detect2(net, meta, im)
  41. print r
  42. # OpenCV
  43. arr = cv2.imread('data/dog.jpg')
  44. im = array_to_image(arr)
  45. dn.rgbgr_image(im)
  46. r = detect2(net, meta, im)
  47. print r