Last active
February 2, 2021 17:22
-
-
Save rosterloh/9775cae5b19c9052db73c9652de42100 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import cv2 | |
import numpy as np | |
from datetime import datetime | |
import array | |
import fcntl | |
import os | |
import argparse | |
from utils import ArducamUtils | |
import Jetson.GPIO as GPIO | |
from time import sleep | |
from threading import Timer | |
GPIO.setmode(GPIO.BOARD) # 40 header pin number | |
# GPIO.setwarnings(False) | |
laser_pin = 16 | |
trigger_pin = 18 | |
channels = [laser_pin, trigger_pin] | |
window_name = 'Gardin' | |
trigger_mode = 0 | |
def windowClicked(event, x, y, flags, param): | |
if event == cv2.EVENT_LBUTTONDOWN: | |
if trigger_mode == 1: | |
pulse(0.0001) | |
#def modeButtonClicked(state): | |
# print("Mode Button {}".format(state)) | |
def pulse(delay): | |
GPIO.output(channels, GPIO.HIGH) | |
Timer(delay, lambda: GPIO.output(channels, GPIO.LOW)).start() | |
def resize(frame, dst_width): | |
width = frame.shape[1] | |
height = frame.shape[0] | |
scale = dst_width / width | |
return cv2.resize(frame, (int(scale * width), int(scale * height))) | |
def display(cap, arducam_utils, device_num): | |
cv2.namedWindow(window_name) | |
cv2.setMouseCallback(window_name, windowClicked) | |
# cv2.createButton("Tigger", modeButtonClicked, None, cv2.QT_CHECKBOX) | |
counter = 0 | |
start_time = datetime.now() | |
while True: | |
ret, frame = cap.read() | |
if ret: | |
counter += 1 | |
frame = arducam_utils.convert(frame) | |
frame = resize(frame, 1280.0) | |
# display | |
cv2.imshow(window_name, frame) | |
ret = cv2.waitKey(1) | |
# press 'q' to exit. | |
if ret == ord('q'): | |
break | |
elif ret == ord('t'): | |
trigger_mode = 1 | |
os.system("v4l2-ctl -d {} -c trigger_mode={}".format(device_num, trigger_mode)) | |
# Trigger once to flush first frame | |
sleep(0.5) | |
pulse(0.0001) | |
elif ret == ord('c'): | |
trigger_mode = 0 | |
os.system("v4l2-ctl -d {} -c trigger_mode={}".format(device_num, trigger_mode)) | |
elif ret == ord('z'): | |
GPIO.output(channels, GPIO.HIGH) | |
sleep(0.0001) | |
GPIO.output(channels, GPIO.LOW) | |
end_time = datetime.now() | |
elapsed_time = end_time - start_time | |
avgtime = elapsed_time.total_seconds() / counter | |
print ("Average time between frames: " + str(avgtime)) | |
print ("Average FPS: " + str(1/avgtime)) | |
def fourcc(a, b, c, d): | |
return ord(a) | (ord(b) << 8) | (ord(c) << 16) | (ord(d) << 24) | |
def pixelformat(string): | |
if len(string) != 3 and len(string) != 4: | |
msg = "{} is not a pixel format".format(string) | |
raise argparse.ArgumentTypeError(msg) | |
if len(string) == 3: | |
return fourcc(string[0], string[1], string[2], ' ') | |
else: | |
return fourcc(string[0], string[1], string[2], string[3]) | |
if __name__ == "__main__": | |
parser = argparse.ArgumentParser(description='Arducam Jetson Nano MIPI Camera Sensor.') | |
parser.add_argument('-d', '--device', default=0, type=int, nargs='?', | |
help='/dev/videoX default is 0') | |
parser.add_argument('-f', '--pixelformat', type=pixelformat, | |
help="set pixelformat") | |
parser.add_argument('--width', type=lambda x: int(x,0), | |
help="set width of image") | |
parser.add_argument('--height', type=lambda x: int(x,0), | |
help="set height of image") | |
args = parser.parse_args() | |
# open camera | |
cap = cv2.VideoCapture(args.device, cv2.CAP_V4L2) | |
# set pixel format | |
# cap.set(cv2.CAP_PROP_FOURCC, cv2.VideoWriter_fourcc('Y', '1', '6', ' ')) | |
if args.pixelformat != None: | |
if not cap.set(cv2.CAP_PROP_FOURCC, args.pixelformat): | |
print("Failed to set pixel format.") | |
arducam_utils = ArducamUtils(args.device) | |
# turn off RGB conversion | |
if arducam_utils.convert2rgb == 0: | |
cap.set(cv2.CAP_PROP_CONVERT_RGB, arducam_utils.convert2rgb) | |
# set width | |
if args.width != None: | |
cap.set(cv2.CAP_PROP_FRAME_WIDTH, args.width) | |
# set height | |
if args.height != None: | |
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, args.height) | |
GPIO.setup(channels, GPIO.OUT, initial=GPIO.LOW) | |
try: | |
# begin display | |
display(cap, arducam_utils, args.device) | |
finally: | |
# release camera | |
cap.release() | |
# cleanup gpio | |
GPIO.cleanup() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import cv2 | |
import Jetson.GPIO as GPIO | |
import numpy as np | |
import signal | |
import subprocess | |
import sys | |
import time | |
# from matplotlib import pyplot as plt | |
def receiveSignal(signalNumber, frame): | |
print('Cleaning up') | |
subprocess.run(["v4l2-ctl", "-d", "0", "-c", "trigger_mode=0"]) | |
cap.release() | |
cv2.destroyAllWindows() | |
GPIO.output(channels, GPIO.LOW) | |
GPIO.cleanup() | |
sys.exit() | |
signal.signal(signal.SIGINT, receiveSignal) | |
GPIO.setmode(GPIO.BOARD) # 40 header pin number | |
GPIO.setwarnings(False) | |
laser_pin = 16 | |
trigger_pin = 18 | |
channels = [laser_pin, trigger_pin] | |
window_name = 'Gardin' | |
GPIO.setup(channels, GPIO.OUT, initial=GPIO.LOW) | |
cap = cv2.VideoCapture(0, cv2.CAP_V4L2) | |
cap.grab() | |
time.sleep(1) | |
output = subprocess.run(["v4l2-ctl", "-d", "0", "-c", "trigger_mode=1"]) | |
if output.returncode != 0: | |
print('{} FAILED {}'.format(' '.join(output.args), output.returncode)) | |
output = subprocess.run(["v4l2-ctl", "-d", "0", "-c", "disable_frame_timeout=1"]) | |
if output.returncode != 0: | |
print('{} FAILED {}'.format(' '.join(output.args), output.returncode)) | |
output = subprocess.run(["v4l2-ctl", "-d", "0", "-c", "frame_timeout=12000"]) | |
if output.returncode != 0: | |
print('{} FAILED {}'.format(' '.join(output.args), output.returncode)) | |
cap.grab() | |
print('Setup complete') | |
cv2.namedWindow(window_name) | |
start_time = time.time() | |
while True: | |
ret, frame = cap.read() | |
if ret: | |
elapsed_time = time.time() - start_time | |
h, w = frame.shape[:2] | |
print('{}x{} Frame acquired in {} ms'.format(w, h, int(elapsed_time*1000))) | |
cv2.imshow(window_name, frame) | |
#plt.imshow(frame, cmap='gray', interpolation='bicubic') | |
#plt.xticks([]), plt.yticks([]) | |
#plt.show() | |
if time.time() - start_time >= 5: | |
#start_time = time.time() | |
GPIO.output(channels, GPIO.HIGH) | |
time.sleep(0.0001) | |
GPIO.output(channels, GPIO.LOW) | |
#elapsed_time = time.time() - start_time | |
#print('GPIO toggled in {} ms'.format(int(elapsed_time*1000))) | |
start_time = time.time() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment