def main(args): cam = cv2.VideoCapture(args.device) if args.codec == 'YUY2': cam.set(cv2.CAP_PROP_FOURCC, 844715353.0) elif args.codec == 'MJPG': cam.set(cv2.CAP_PROP_FOURCC, 0x47504A4D) else: print('use default video codec.') if args.resolution: cam.set(cv2.CAP_PROP_FRAME_WIDTH, args.resolution[0]) cam.set(cv2.CAP_PROP_FRAME_HEIGHT, args.resolution[1]) detector = CornerNet_Squeeze( model_name=args.model) if args.model else CornerNet_Squeeze() frame_count = 0 init_time = time() tic = time() try: while True: # Capture frame-by-frame if cam.grab(): _, frame = cam.retrieve() bboxes = detector(frame) frame = draw_bboxes(frame, bboxes) toc = time() frame_count += 1 else: continue # Calculate fps if toc - init_time > 3: fps = frame_count / (toc - tic) print('{:.2f}: {} x {} @ {:5.1f}'.format( time(), frame.shape[1], frame.shape[0], fps)) if toc - tic > 3: tic = time() frame_count = 0 # Show the resulting frame if args.visual: frame = cv2.resize(frame, (0, 0), fx=args.scale, fy=args.scale) cv2.imshow('/dev/video{}'.format(args.device), frame) if cv2.waitKey(1) & 0xFF == ord('q'): break except KeyboardInterrupt: print('\nKeyboardInterrupt') pass # When everything done, release the capture cam.release() cv2.destroyAllWindows()
def main(args): cam = cv2.VideoCapture(args.filename) detector = CornerNet_Squeeze(model_name=args.model) if args.model else CornerNet_Squeeze() frame_count = 0 init_time = time() tic = time() try: while True: # Capture frame-by-frame if cam.grab(): _, frame = cam.retrieve() frame = cv2.resize(frame, (640,360)) bboxes = detector(frame) frame = draw_bboxes(frame, bboxes) toc = time() frame_count += 1 else: continue # Calculate fps if toc - init_time > 3: fps = frame_count / (toc - tic) print('{:.2f}: {} x {} @ {:5.1f}'.format(time(), frame.shape[1], frame.shape[0], fps)) if toc -tic > 3: tic = time() frame_count = 0 # Show the resulting frame if args.visual: frame = cv2.resize(frame, (0, 0), fx=args.scale, fy=args.scale) cv2.imshow(args.filename, frame) if cv2.waitKey(1) & 0xFF == ord('q'): break except KeyboardInterrupt: print('\nKeyboardInterrupt') pass # When everything done, release the capture cam.release() cv2.destroyAllWindows()
#!/usr/bin/env python import cv2 from core.detectors import CornerNet_Squeeze from core.vis_utils import draw_bboxes import time # 打开编号为0的相机 cap = cv2.VideoCapture(0) if not cap.isOpened(): print("Camera is not ready ") exit() # 加载模型 detector = CornerNet_Squeeze() while True: ret, frame = cap.read() if not ret: pass #image = cv2.imread("demo.jpg") t0 = time.time() #bboxes = detector(image) bboxes = detector(frame) # 画框 image = draw_bboxes(frame, bboxes) #cv2.imwrite("demo_out.jpg", image)
def main(): args = sys.argv detector = CornerNet_Squeeze() cam(args[1], detector)