demo.py 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. import gc
  2. import cv2
  3. import imutils
  4. import threading
  5. import argparse # 导入argparse
  6. from AIDetector_pytorch import Detector
  7. def start_camera_detector(camera_id, width, height, detector):
  8. name = 'Demo Camera {}'.format(camera_id)
  9. cap = cv2.VideoCapture(camera_id, cv2.CAP_V4L2)
  10. if not cap.isOpened():
  11. print('Error: Unable to open camera {}.'.format(camera_id))
  12. return
  13. cap.set(cv2.CAP_PROP_FRAME_WIDTH, width)
  14. cap.set(cv2.CAP_PROP_FRAME_HEIGHT, height)
  15. fps = cap.get(cv2.CAP_PROP_FPS)
  16. if fps <= 0:
  17. fps = 30
  18. t = int(1000 / fps)
  19. print('{} fps:'.format(name), fps)
  20. frame_count = 0
  21. while True:
  22. ret, im = cap.read()
  23. if not ret or im is None:
  24. break
  25. if frame_count % 3 == 0:
  26. result = detector.feedCap(im)
  27. result = result['frame']
  28. result = imutils.resize(result, height=500)
  29. cv2.imshow(name, result)
  30. if cv2.waitKey(t) & 0xFF == ord('q'):
  31. break
  32. frame_count += 1
  33. if frame_count % 30 == 0:
  34. gc.collect()
  35. cap.release()
  36. cv2.destroyWindow(name)
  37. def main():
  38. parser = argparse.ArgumentParser(description='Camera Detection with ONNX.')
  39. parser.add_argument('--camera_count', type=int, default=6, help='Number of cameras to use.')
  40. parser.add_argument('--width', type=int, default=1280, help='Input the wight of the video image(default=1280)')
  41. parser.add_argument('--height', type=int, default=720, help='Input the height of the video image(default=720)')
  42. args = parser.parse_args()
  43. detector = Detector()
  44. threads = []
  45. for i in range(args.camera_count):
  46. thread = threading.Thread(target=start_camera_detector, args=(i, args.width, args.height, detector))
  47. thread.start()
  48. threads.append(thread)
  49. for thread in threads:
  50. thread.join()
  51. if __name__ == "__main__":
  52. main()