darknet.py 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  1. #!python3
  2. """
  3. Python 3 wrapper for identifying objects in images
  4. Requires DLL compilation
  5. Both the GPU and no-GPU version should be compiled; the no-GPU version should be renamed "yolo_cpp_dll_nogpu.dll".
  6. On a GPU system, you can force CPU evaluation by any of:
  7. - Set global variable DARKNET_FORCE_CPU to True
  8. - Set environment variable CUDA_VISIBLE_DEVICES to -1
  9. - Set environment variable "FORCE_CPU" to "true"
  10. To use, either run performDetect() after import, or modify the end of this file.
  11. See the docstring of performDetect() for parameters.
  12. Directly viewing or returning bounding-boxed images requires scikit-image to be installed (`pip install scikit-image`)
  13. Original *nix 2.7: https://github.com/pjreddie/darknet/blob/0f110834f4e18b30d5f101bf8f1724c34b7b83db/python/darknet.py
  14. Windows Python 2.7 version: https://github.com/AlexeyAB/darknet/blob/fc496d52bf22a0bb257300d3c79be9cd80e722cb/build/darknet/x64/darknet.py
  15. @author: Philip Kahn
  16. @date: 20180503
  17. """
  18. #pylint: disable=R, W0401, W0614, W0703
  19. from ctypes import *
  20. import math
  21. import random
  22. import os
  23. def sample(probs):
  24. s = sum(probs)
  25. probs = [a/s for a in probs]
  26. r = random.uniform(0, 1)
  27. for i in range(len(probs)):
  28. r = r - probs[i]
  29. if r <= 0:
  30. return i
  31. return len(probs)-1
  32. def c_array(ctype, values):
  33. arr = (ctype*len(values))()
  34. arr[:] = values
  35. return arr
  36. class BOX(Structure):
  37. _fields_ = [("x", c_float),
  38. ("y", c_float),
  39. ("w", c_float),
  40. ("h", c_float)]
  41. class DETECTION(Structure):
  42. _fields_ = [("bbox", BOX),
  43. ("classes", c_int),
  44. ("prob", POINTER(c_float)),
  45. ("mask", POINTER(c_float)),
  46. ("objectness", c_float),
  47. ("sort_class", c_int),
  48. ("uc", POINTER(c_float)),
  49. ("points", c_int)]
  50. class IMAGE(Structure):
  51. _fields_ = [("w", c_int),
  52. ("h", c_int),
  53. ("c", c_int),
  54. ("data", POINTER(c_float))]
  55. class METADATA(Structure):
  56. _fields_ = [("classes", c_int),
  57. ("names", POINTER(c_char_p))]
  58. #lib = CDLL("/home/pjreddie/documents/darknet/libdarknet.so", RTLD_GLOBAL)
  59. #lib = CDLL("libdarknet.so", RTLD_GLOBAL)
  60. hasGPU = True
  61. if os.name == "nt":
  62. cwd = os.path.dirname(__file__)
  63. os.environ['PATH'] = cwd + ';' + os.environ['PATH']
  64. winGPUdll = os.path.join(cwd, "yolo_cpp_dll.dll")
  65. winNoGPUdll = os.path.join(cwd, "yolo_cpp_dll_nogpu.dll")
  66. envKeys = list()
  67. for k, v in os.environ.items():
  68. envKeys.append(k)
  69. try:
  70. try:
  71. tmp = os.environ["FORCE_CPU"].lower()
  72. if tmp in ["1", "true", "yes", "on"]:
  73. raise ValueError("ForceCPU")
  74. else:
  75. print("Flag value '"+tmp+"' not forcing CPU mode")
  76. except KeyError:
  77. # We never set the flag
  78. if 'CUDA_VISIBLE_DEVICES' in envKeys:
  79. if int(os.environ['CUDA_VISIBLE_DEVICES']) < 0:
  80. raise ValueError("ForceCPU")
  81. try:
  82. global DARKNET_FORCE_CPU
  83. if DARKNET_FORCE_CPU:
  84. raise ValueError("ForceCPU")
  85. except NameError:
  86. pass
  87. # print(os.environ.keys())
  88. # print("FORCE_CPU flag undefined, proceeding with GPU")
  89. if not os.path.exists(winGPUdll):
  90. raise ValueError("NoDLL")
  91. lib = CDLL(winGPUdll, RTLD_GLOBAL)
  92. except (KeyError, ValueError):
  93. hasGPU = False
  94. if os.path.exists(winNoGPUdll):
  95. lib = CDLL(winNoGPUdll, RTLD_GLOBAL)
  96. print("Notice: CPU-only mode")
  97. else:
  98. # Try the other way, in case no_gpu was
  99. # compile but not renamed
  100. lib = CDLL(winGPUdll, RTLD_GLOBAL)
  101. print("Environment variables indicated a CPU run, but we didn't find `"+winNoGPUdll+"`. Trying a GPU run anyway.")
  102. else:
  103. lib = CDLL("./libdarknet.so", RTLD_GLOBAL)
  104. lib.network_width.argtypes = [c_void_p]
  105. lib.network_width.restype = c_int
  106. lib.network_height.argtypes = [c_void_p]
  107. lib.network_height.restype = c_int
  108. copy_image_from_bytes = lib.copy_image_from_bytes
  109. copy_image_from_bytes.argtypes = [IMAGE,c_char_p]
  110. def network_width(net):
  111. return lib.network_width(net)
  112. def network_height(net):
  113. return lib.network_height(net)
  114. predict = lib.network_predict_ptr
  115. predict.argtypes = [c_void_p, POINTER(c_float)]
  116. predict.restype = POINTER(c_float)
  117. if hasGPU:
  118. set_gpu = lib.cuda_set_device
  119. set_gpu.argtypes = [c_int]
  120. init_cpu = lib.init_cpu
  121. make_image = lib.make_image
  122. make_image.argtypes = [c_int, c_int, c_int]
  123. make_image.restype = IMAGE
  124. get_network_boxes = lib.get_network_boxes
  125. get_network_boxes.argtypes = [c_void_p, c_int, c_int, c_float, c_float, POINTER(c_int), c_int, POINTER(c_int), c_int]
  126. get_network_boxes.restype = POINTER(DETECTION)
  127. make_network_boxes = lib.make_network_boxes
  128. make_network_boxes.argtypes = [c_void_p]
  129. make_network_boxes.restype = POINTER(DETECTION)
  130. free_detections = lib.free_detections
  131. free_detections.argtypes = [POINTER(DETECTION), c_int]
  132. free_ptrs = lib.free_ptrs
  133. free_ptrs.argtypes = [POINTER(c_void_p), c_int]
  134. network_predict = lib.network_predict_ptr
  135. network_predict.argtypes = [c_void_p, POINTER(c_float)]
  136. reset_rnn = lib.reset_rnn
  137. reset_rnn.argtypes = [c_void_p]
  138. load_net = lib.load_network
  139. load_net.argtypes = [c_char_p, c_char_p, c_int]
  140. load_net.restype = c_void_p
  141. load_net_custom = lib.load_network_custom
  142. load_net_custom.argtypes = [c_char_p, c_char_p, c_int, c_int]
  143. load_net_custom.restype = c_void_p
  144. do_nms_obj = lib.do_nms_obj
  145. do_nms_obj.argtypes = [POINTER(DETECTION), c_int, c_int, c_float]
  146. do_nms_sort = lib.do_nms_sort
  147. do_nms_sort.argtypes = [POINTER(DETECTION), c_int, c_int, c_float]
  148. free_image = lib.free_image
  149. free_image.argtypes = [IMAGE]
  150. letterbox_image = lib.letterbox_image
  151. letterbox_image.argtypes = [IMAGE, c_int, c_int]
  152. letterbox_image.restype = IMAGE
  153. load_meta = lib.get_metadata
  154. lib.get_metadata.argtypes = [c_char_p]
  155. lib.get_metadata.restype = METADATA
  156. load_image = lib.load_image_color
  157. load_image.argtypes = [c_char_p, c_int, c_int]
  158. load_image.restype = IMAGE
  159. rgbgr_image = lib.rgbgr_image
  160. rgbgr_image.argtypes = [IMAGE]
  161. predict_image = lib.network_predict_image
  162. predict_image.argtypes = [c_void_p, IMAGE]
  163. predict_image.restype = POINTER(c_float)
  164. predict_image_letterbox = lib.network_predict_image_letterbox
  165. predict_image_letterbox.argtypes = [c_void_p, IMAGE]
  166. predict_image_letterbox.restype = POINTER(c_float)
  167. def array_to_image(arr):
  168. import numpy as np
  169. # need to return old values to avoid python freeing memory
  170. arr = arr.transpose(2,0,1)
  171. c = arr.shape[0]
  172. h = arr.shape[1]
  173. w = arr.shape[2]
  174. arr = np.ascontiguousarray(arr.flat, dtype=np.float32) / 255.0
  175. data = arr.ctypes.data_as(POINTER(c_float))
  176. im = IMAGE(w,h,c,data)
  177. return im, arr
  178. def classify(net, meta, im):
  179. out = predict_image(net, im)
  180. res = []
  181. for i in range(meta.classes):
  182. if altNames is None:
  183. nameTag = meta.names[i]
  184. else:
  185. nameTag = altNames[i]
  186. res.append((nameTag, out[i]))
  187. res = sorted(res, key=lambda x: -x[1])
  188. return res
  189. def detect(net, meta, image, thresh=.5, hier_thresh=.5, nms=.45, debug= False):
  190. """
  191. Performs the meat of the detection
  192. """
  193. #pylint: disable= C0321
  194. im = load_image(image, 0, 0)
  195. if debug: print("Loaded image")
  196. ret = detect_image(net, meta, im, thresh, hier_thresh, nms, debug)
  197. free_image(im)
  198. if debug: print("freed image")
  199. return ret
  200. def detect_image(net, meta, im, thresh=.5, hier_thresh=.5, nms=.45, debug= False):
  201. #import cv2
  202. #custom_image_bgr = cv2.imread(image) # use: detect(,,imagePath,)
  203. #custom_image = cv2.cvtColor(custom_image_bgr, cv2.COLOR_BGR2RGB)
  204. #custom_image = cv2.resize(custom_image,(lib.network_width(net), lib.network_height(net)), interpolation = cv2.INTER_LINEAR)
  205. #import scipy.misc
  206. #custom_image = scipy.misc.imread(image)
  207. #im, arr = array_to_image(custom_image) # you should comment line below: free_image(im)
  208. num = c_int(0)
  209. if debug: print("Assigned num")
  210. pnum = pointer(num)
  211. if debug: print("Assigned pnum")
  212. predict_image(net, im)
  213. letter_box = 0
  214. #predict_image_letterbox(net, im)
  215. #letter_box = 1
  216. if debug: print("did prediction")
  217. #dets = get_network_boxes(net, custom_image_bgr.shape[1], custom_image_bgr.shape[0], thresh, hier_thresh, None, 0, pnum, letter_box) # OpenCV
  218. dets = get_network_boxes(net, im.w, im.h, thresh, hier_thresh, None, 0, pnum, letter_box)
  219. if debug: print("Got dets")
  220. num = pnum[0]
  221. if debug: print("got zeroth index of pnum")
  222. if nms:
  223. do_nms_sort(dets, num, meta.classes, nms)
  224. if debug: print("did sort")
  225. res = []
  226. if debug: print("about to range")
  227. for j in range(num):
  228. if debug: print("Ranging on "+str(j)+" of "+str(num))
  229. if debug: print("Classes: "+str(meta), meta.classes, meta.names)
  230. for i in range(meta.classes):
  231. if debug: print("Class-ranging on "+str(i)+" of "+str(meta.classes)+"= "+str(dets[j].prob[i]))
  232. if dets[j].prob[i] > 0:
  233. b = dets[j].bbox
  234. if altNames is None:
  235. nameTag = meta.names[i]
  236. else:
  237. nameTag = altNames[i]
  238. if debug:
  239. print("Got bbox", b)
  240. print(nameTag)
  241. print(dets[j].prob[i])
  242. print((b.x, b.y, b.w, b.h))
  243. res.append((nameTag, dets[j].prob[i], (b.x, b.y, b.w, b.h)))
  244. if debug: print("did range")
  245. res = sorted(res, key=lambda x: -x[1])
  246. if debug: print("did sort")
  247. free_detections(dets, num)
  248. if debug: print("freed detections")
  249. return res
  250. netMain = None
  251. metaMain = None
  252. altNames = None
  253. def performDetect(imagePath="data/dog.jpg", thresh= 0.25, configPath = "./cfg/yolov3.cfg", weightPath = "yolov3.weights", metaPath= "./cfg/coco.data", showImage= True, makeImageOnly = False, initOnly= False):
  254. """
  255. Convenience function to handle the detection and returns of objects.
  256. Displaying bounding boxes requires libraries scikit-image and numpy
  257. Parameters
  258. ----------------
  259. imagePath: str
  260. Path to the image to evaluate. Raises ValueError if not found
  261. thresh: float (default= 0.25)
  262. The detection threshold
  263. configPath: str
  264. Path to the configuration file. Raises ValueError if not found
  265. weightPath: str
  266. Path to the weights file. Raises ValueError if not found
  267. metaPath: str
  268. Path to the data file. Raises ValueError if not found
  269. showImage: bool (default= True)
  270. Compute (and show) bounding boxes. Changes return.
  271. makeImageOnly: bool (default= False)
  272. If showImage is True, this won't actually *show* the image, but will create the array and return it.
  273. initOnly: bool (default= False)
  274. Only initialize globals. Don't actually run a prediction.
  275. Returns
  276. ----------------------
  277. When showImage is False, list of tuples like
  278. ('obj_label', confidence, (bounding_box_x_px, bounding_box_y_px, bounding_box_width_px, bounding_box_height_px))
  279. The X and Y coordinates are from the center of the bounding box. Subtract half the width or height to get the lower corner.
  280. Otherwise, a dict with
  281. {
  282. "detections": as above
  283. "image": a numpy array representing an image, compatible with scikit-image
  284. "caption": an image caption
  285. }
  286. """
  287. # Import the global variables. This lets us instance Darknet once, then just call performDetect() again without instancing again
  288. global metaMain, netMain, altNames #pylint: disable=W0603
  289. assert 0 < thresh < 1, "Threshold should be a float between zero and one (non-inclusive)"
  290. if not os.path.exists(configPath):
  291. raise ValueError("Invalid config path `"+os.path.abspath(configPath)+"`")
  292. if not os.path.exists(weightPath):
  293. raise ValueError("Invalid weight path `"+os.path.abspath(weightPath)+"`")
  294. if not os.path.exists(metaPath):
  295. raise ValueError("Invalid data file path `"+os.path.abspath(metaPath)+"`")
  296. if netMain is None:
  297. netMain = load_net_custom(configPath.encode("ascii"), weightPath.encode("ascii"), 0, 1) # batch size = 1
  298. if metaMain is None:
  299. metaMain = load_meta(metaPath.encode("ascii"))
  300. if altNames is None:
  301. # In Python 3, the metafile default access craps out on Windows (but not Linux)
  302. # Read the names file and create a list to feed to detect
  303. try:
  304. with open(metaPath) as metaFH:
  305. metaContents = metaFH.read()
  306. import re
  307. match = re.search("names *= *(.*)$", metaContents, re.IGNORECASE | re.MULTILINE)
  308. if match:
  309. result = match.group(1)
  310. else:
  311. result = None
  312. try:
  313. if os.path.exists(result):
  314. with open(result) as namesFH:
  315. namesList = namesFH.read().strip().split("\n")
  316. altNames = [x.strip() for x in namesList]
  317. except TypeError:
  318. pass
  319. except Exception:
  320. pass
  321. if initOnly:
  322. print("Initialized detector")
  323. return None
  324. if not os.path.exists(imagePath):
  325. raise ValueError("Invalid image path `"+os.path.abspath(imagePath)+"`")
  326. # Do the detection
  327. #detections = detect(netMain, metaMain, imagePath, thresh) # if is used cv2.imread(image)
  328. detections = detect(netMain, metaMain, imagePath.encode("ascii"), thresh)
  329. if showImage:
  330. try:
  331. from skimage import io, draw
  332. import numpy as np
  333. image = io.imread(imagePath)
  334. print("*** "+str(len(detections))+" Results, color coded by confidence ***")
  335. imcaption = []
  336. for detection in detections:
  337. label = detection[0]
  338. confidence = detection[1]
  339. pstring = label+": "+str(np.rint(100 * confidence))+"%"
  340. imcaption.append(pstring)
  341. print(pstring)
  342. bounds = detection[2]
  343. shape = image.shape
  344. # x = shape[1]
  345. # xExtent = int(x * bounds[2] / 100)
  346. # y = shape[0]
  347. # yExtent = int(y * bounds[3] / 100)
  348. yExtent = int(bounds[3])
  349. xEntent = int(bounds[2])
  350. # Coordinates are around the center
  351. xCoord = int(bounds[0] - bounds[2]/2)
  352. yCoord = int(bounds[1] - bounds[3]/2)
  353. boundingBox = [
  354. [xCoord, yCoord],
  355. [xCoord, yCoord + yExtent],
  356. [xCoord + xEntent, yCoord + yExtent],
  357. [xCoord + xEntent, yCoord]
  358. ]
  359. # Wiggle it around to make a 3px border
  360. rr, cc = draw.polygon_perimeter([x[1] for x in boundingBox], [x[0] for x in boundingBox], shape= shape)
  361. rr2, cc2 = draw.polygon_perimeter([x[1] + 1 for x in boundingBox], [x[0] for x in boundingBox], shape= shape)
  362. rr3, cc3 = draw.polygon_perimeter([x[1] - 1 for x in boundingBox], [x[0] for x in boundingBox], shape= shape)
  363. rr4, cc4 = draw.polygon_perimeter([x[1] for x in boundingBox], [x[0] + 1 for x in boundingBox], shape= shape)
  364. rr5, cc5 = draw.polygon_perimeter([x[1] for x in boundingBox], [x[0] - 1 for x in boundingBox], shape= shape)
  365. boxColor = (int(255 * (1 - (confidence ** 2))), int(255 * (confidence ** 2)), 0)
  366. draw.set_color(image, (rr, cc), boxColor, alpha= 0.8)
  367. draw.set_color(image, (rr2, cc2), boxColor, alpha= 0.8)
  368. draw.set_color(image, (rr3, cc3), boxColor, alpha= 0.8)
  369. draw.set_color(image, (rr4, cc4), boxColor, alpha= 0.8)
  370. draw.set_color(image, (rr5, cc5), boxColor, alpha= 0.8)
  371. if not makeImageOnly:
  372. io.imshow(image)
  373. io.show()
  374. detections = {
  375. "detections": detections,
  376. "image": image,
  377. "caption": "\n<br/>".join(imcaption)
  378. }
  379. except Exception as e:
  380. print("Unable to show image: "+str(e))
  381. return detections
  382. if __name__ == "__main__":
  383. print(performDetect())