face_detect_dnn.jl 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. using OpenCV
  2. const cv = OpenCV
  3. size0 = Int32(300)
  4. # take the model from https://github.com/opencv/opencv_extra/tree/master/testdata/dnn
  5. net = cv.dnn_DetectionModel("opencv_face_detector.pbtxt", "opencv_face_detector_uint8.pb")
  6. cv.dnn.setPreferableTarget(net, cv.dnn.DNN_TARGET_CPU)
  7. cv.dnn.setInputMean(net, (104, 177, 123))
  8. cv.dnn.setInputScale(net, 1.)
  9. cv.dnn.setInputSize(net, size0, size0)
  10. cap = cv.VideoCapture(Int32(0))
  11. while true
  12. ok, frame = cv.read(cap)
  13. if ok == false
  14. break
  15. end
  16. classIds, confidences, boxes = cv.dnn.detect(net, frame, confThreshold=Float32(0.5))
  17. for i in 1:size(boxes,1)
  18. confidence = confidences[i]
  19. x0 = Int32(boxes[i].x)
  20. y0 = Int32(boxes[i].y)
  21. x1 = Int32(boxes[i].x+boxes[i].width)
  22. y1 = Int32(boxes[i].y+boxes[i].height)
  23. cv.rectangle(frame, cv.Point{Int32}(x0, y0), cv.Point{Int32}(x1, y1), (100, 255, 100); thickness = Int32(5))
  24. label = "face: " * string(confidence)
  25. lsize, bl = cv.getTextSize(label, cv.FONT_HERSHEY_SIMPLEX, 0.5, Int32(1))
  26. cv.rectangle(frame, cv.Point{Int32}(x0,y0), cv.Point{Int32}(x0+lsize.width, y0+lsize.height+bl), (100,255,100); thickness = Int32(-1))
  27. cv.putText(frame, label, cv.Point{Int32}(x0, y0 + lsize.height),
  28. cv.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 0); thickness = Int32(1), lineType = cv.LINE_AA)
  29. end
  30. cv.imshow("detections", frame)
  31. if cv.waitKey(Int32(30)) >= 0
  32. break
  33. end
  34. end