test_dnn.jl 1.1 KB

1234567891011121314151617181920212223242526272829303132
  1. function IOU(boxA, boxB)
  2. xA = max(boxA[1], boxB[1])
  3. yA = max(boxA[2], boxB[2])
  4. xB = min(boxA[3], boxB[3])
  5. yB = min(boxA[4], boxB[4])
  6. interArea = max(0, xB - xA + 1) * max(0, yB - yA + 1)
  7. boxAArea = (boxA[3] - boxA[1] + 1) * (boxA[4] - boxA[2] + 1)
  8. boxBArea = (boxB[3] - boxB[1] + 1) * (boxB[4] - boxB[2] + 1)
  9. iou = interArea / float(boxAArea + boxBArea - interArea)
  10. return iou
  11. end
  12. const cv = OpenCV
  13. net = cv.dnn.DetectionModel(joinpath(ENV["OPENCV_TEST_DATA_PATH"], "dnn", "opencv_face_detector.pbtxt"),joinpath(ENV["OPENCV_TEST_DATA_PATH"], "dnn", "opencv_face_detector_uint8.pb"))
  14. size0 = 300
  15. cv.dnn.setPreferableTarget(net, cv.dnn.DNN_TARGET_CPU)
  16. cv.dnn.setInputMean(net, (104, 177, 123))
  17. cv.dnn.setInputScale(net, 1.)
  18. cv.dnn.setInputSize(net, size0, size0)
  19. img = OpenCV.imread(joinpath(test_dir, "cascadeandhog", "images", "mona-lisa.png"))
  20. classIds, confidences, boxes = cv.dnn.detect(net, img, confThreshold=0.5)
  21. box = (boxes[1].x, boxes[1].y, boxes[1].x+boxes[1].width, boxes[1].y+boxes[1].height)
  22. expected_rect = (185,101,129+185,169+101)
  23. @test IOU(box, expected_rect) > 0.8
  24. print("dnn test passed\n")