You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

yolov4.py 1.1 kB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. # Copyright 2020 Tencent
  2. # SPDX-License-Identifier: BSD-3-Clause
  3. import sys
  4. import cv2
  5. from ncnn.model_zoo import get_model
  6. from ncnn.utils import draw_detection_objects
  7. if __name__ == "__main__":
  8. if len(sys.argv) != 2:
  9. print("Usage: %s [v4l input device or image]\n" % (sys.argv[0]))
  10. sys.exit(0)
  11. devicepath = sys.argv[1]
  12. net = get_model("yolov4_tiny", num_threads=4, use_gpu=True)
  13. # net = get_model("yolov4", num_threads=4, use_gpu=True)
  14. if devicepath.find("/dev/video") == -1:
  15. m = cv2.imread(devicepath)
  16. if m is None:
  17. print("cv2.imread %s failed\n" % (devicepath))
  18. sys.exit(0)
  19. objects = net(m)
  20. draw_detection_objects(m, net.class_names, objects)
  21. else:
  22. cap = cv2.VideoCapture(devicepath)
  23. if cap.isOpened() == False:
  24. print("Failed to open %s" % (devicepath))
  25. sys.exit(0)
  26. while True:
  27. ret, frame = cap.read()
  28. objects = net(frame)
  29. draw_detection_objects(frame, net.class_names, objects)