|
| 1 | +import cv2 |
| 2 | +import numpy as np |
| 3 | +from datetime import datetime |
| 4 | +import array |
| 5 | +import fcntl |
| 6 | +import os |
| 7 | +import argparse |
| 8 | +import time |
| 9 | +from utils import ArducamUtils |
| 10 | + |
| 11 | +def resize(frame, dst_width): |
| 12 | + width = frame.shape[1] |
| 13 | + height = frame.shape[0] |
| 14 | + scale = dst_width / width |
| 15 | + return cv2.resize(frame, (int(scale * width), int(scale * height))) |
| 16 | + |
| 17 | +def display(cap, arducam_utils, device_num): |
| 18 | + counter = 0 |
| 19 | + frame_count = 0 |
| 20 | + start_time = datetime.now() |
| 21 | + cv2.namedWindow("Arducam") |
| 22 | + start = time.time() |
| 23 | + while True: |
| 24 | + ret, frame = cap.read() |
| 25 | + if ret: |
| 26 | + counter += 1 |
| 27 | + |
| 28 | + frame = arducam_utils.convert(frame) |
| 29 | + |
| 30 | + frame = resize(frame, 640.0) |
| 31 | + # frame = cv2.rotate(frame, cv2.ROTATE_180) |
| 32 | + # display |
| 33 | + cv2.imshow("Arducam", frame) |
| 34 | + frame_count += 1 |
| 35 | + ret = cv2.waitKey(10) |
| 36 | + # press 'q' to exit. |
| 37 | + if ret == ord('q'): |
| 38 | + break |
| 39 | + |
| 40 | + if time.time() - start >= 1: |
| 41 | + print("fps: {}".format(frame_count)) |
| 42 | + start = time.time() |
| 43 | + frame_count = 0 |
| 44 | + |
| 45 | + end_time = datetime.now() |
| 46 | + elapsed_time = end_time - start_time |
| 47 | + avgtime = elapsed_time.total_seconds() / counter |
| 48 | + print ("Average time between frames: " + str(avgtime)) |
| 49 | + print ("Average FPS: " + str(1/avgtime)) |
| 50 | + |
| 51 | + |
| 52 | +def fourcc(a, b, c, d): |
| 53 | + return ord(a) | (ord(b) << 8) | (ord(c) << 16) | (ord(d) << 24) |
| 54 | + |
| 55 | +def pixelformat(string): |
| 56 | + if len(string) != 3 and len(string) != 4: |
| 57 | + msg = "{} is not a pixel format".format(string) |
| 58 | + raise argparse.ArgumentTypeError(msg) |
| 59 | + if len(string) == 3: |
| 60 | + return fourcc(string[0], string[1], string[2], ' ') |
| 61 | + else: |
| 62 | + return fourcc(string[0], string[1], string[2], string[3]) |
| 63 | + |
| 64 | +if __name__ == "__main__": |
| 65 | + parser = argparse.ArgumentParser(description='Arducam Jetson Nano MIPI Camera Sensor.') |
| 66 | + |
| 67 | + parser.add_argument('-d', '--device', default=0, type=int, nargs='?', |
| 68 | + help='/dev/videoX default is 0') |
| 69 | + parser.add_argument('-f', '--pixelformat', type=pixelformat, |
| 70 | + help="set pixelformat") |
| 71 | + parser.add_argument('--width', type=lambda x: int(x,0), |
| 72 | + help="set width of image") |
| 73 | + parser.add_argument('--height', type=lambda x: int(x,0), |
| 74 | + help="set height of image") |
| 75 | + |
| 76 | + args = parser.parse_args() |
| 77 | + |
| 78 | + # open camera |
| 79 | + cap = cv2.VideoCapture(args.device, cv2.CAP_V4L2) |
| 80 | + # set pixel format |
| 81 | + # cap.set(cv2.CAP_PROP_FOURCC, cv2.VideoWriter_fourcc('Y', '1', '6', ' ')) |
| 82 | + |
| 83 | + if args.pixelformat != None: |
| 84 | + if not cap.set(cv2.CAP_PROP_FOURCC, args.pixelformat): |
| 85 | + print("Failed to set pixel format.") |
| 86 | + |
| 87 | + arducam_utils = ArducamUtils(args.device) |
| 88 | + # turn off RGB conversion |
| 89 | + if arducam_utils.convert2rgb == 0: |
| 90 | + cap.set(cv2.CAP_PROP_CONVERT_RGB, arducam_utils.convert2rgb) |
| 91 | + cap.set(cv2.CAP_PROP_BUFFERSIZE, 4) |
| 92 | + # set width |
| 93 | + if args.width != None: |
| 94 | + cap.set(cv2.CAP_PROP_FRAME_WIDTH, args.width) |
| 95 | + # set height |
| 96 | + if args.height != None: |
| 97 | + cap.set(cv2.CAP_PROP_FRAME_HEIGHT, args.height) |
| 98 | + cap.grab() |
| 99 | + time.sleep(1) |
| 100 | + os.system("v4l2-ctl -d {} -c trigger_mode=1".format(args.device)) |
| 101 | + os.system("v4l2-ctl -d {} -c frame_timeout=10000".format(args.device)) |
| 102 | + cap.grab() |
| 103 | + |
| 104 | + # After the above operation, there are still two frames of data in the buffer, |
| 105 | + # and one frame can be read out every time one frame is triggered.Therefore, |
| 106 | + # there is a two-frame delay from triggering to reading the image. |
| 107 | + # The first two frames are not the triggered image. |
| 108 | + |
| 109 | + # begin display |
| 110 | + display(cap, arducam_utils, args.device) |
| 111 | + |
| 112 | + # release camera |
| 113 | + cap.release() |
0 commit comments