|
| 1 | +#!/usr/bin/env python |
| 2 | +# MIT License |
| 3 | + |
| 4 | +# Copyright (c) 2017-18 Jetsonhacks |
| 5 | + |
| 6 | +# Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | +# of this software and associated documentation files (the "Software"), to deal |
| 8 | +# in the Software without restriction, including without limitation the rights |
| 9 | +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | +# copies of the Software, and to permit persons to whom the Software is |
| 11 | +# furnished to do so, subject to the following conditions: |
| 12 | + |
| 13 | +# The above copyright notice and this permission notice shall be included in all |
| 14 | +# copies or substantial portions of the Software. |
| 15 | + |
| 16 | +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | +# SOFTWARE. |
| 23 | + |
| 24 | +import sys |
| 25 | +import argparse |
| 26 | +import cv2 |
| 27 | +import numpy as np |
| 28 | + |
| 29 | +def parse_cli_args(): |
| 30 | + parser = argparse.ArgumentParser() |
| 31 | + parser.add_argument("--video_device", dest="video_device", |
| 32 | + help="Video device # of USB webcam (/dev/video?) [0]", |
| 33 | + default=0, type=int) |
| 34 | + arguments = parser.parse_args() |
| 35 | + return arguments |
| 36 | + |
| 37 | +# Use a Jetson TX1 or TX2 camera in the Jetson AGX Xavier Camera Slot |
| 38 | +def open_onboard_camera(): |
| 39 | + return cv2.VideoCapture("nvarguscamerasrc ! video/x-raw(memory:NVMM), width=(int)1280, height=(int)720, format=(string)NV12, framerate=(fraction)30/1 ! nvvidconv ! video/x-raw,format=(string)BGRx ! videoconvert ! video/x-raw, format=(string)BGR ! appsink") |
| 40 | + |
| 41 | +# Open an external usb camera /dev/videoX |
| 42 | +def open_camera_device(device_number): |
| 43 | + return cv2.VideoCapture(device_number) |
| 44 | + |
| 45 | +def read_cam(video_capture): |
| 46 | + if video_capture.isOpened(): |
| 47 | + windowName = "CannyDemo" |
| 48 | + cv2.namedWindow(windowName, cv2.WINDOW_NORMAL) |
| 49 | + cv2.resizeWindow(windowName,1280,720) |
| 50 | + cv2.moveWindow(windowName,0,0) |
| 51 | + cv2.setWindowTitle(windowName,"Canny Edge Detection") |
| 52 | + showWindow=3 # Show all stages |
| 53 | + showHelp = True |
| 54 | + font = cv2.FONT_HERSHEY_PLAIN |
| 55 | + helpText="'Esc' to Quit, '1' for Camera Feed, '2' for Canny Detection, '3' for All Stages. '4' to hide help" |
| 56 | + edgeThreshold=40 |
| 57 | + showFullScreen = False |
| 58 | + while True: |
| 59 | + if cv2.getWindowProperty(windowName, 0) < 0: # Check to see if the user closed the window |
| 60 | + # This will fail if the user closed the window; Nasties get printed to the console |
| 61 | + break; |
| 62 | + ret_val, frame = video_capture.read(); |
| 63 | + if frame is None: |
| 64 | + continue ; |
| 65 | + |
| 66 | + hsv=cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) |
| 67 | + blur=cv2.GaussianBlur(hsv,(7,7),1.5) |
| 68 | + edges=cv2.Canny(blur,0,edgeThreshold) |
| 69 | + if showWindow == 3: # Need to show the 4 stages |
| 70 | + # Composite the 2x2 window |
| 71 | + # Feed from the camera is RGB, the others gray |
| 72 | + # To composite, convert gray images to color. |
| 73 | + # All images must be of the same type to display in a window |
| 74 | + frameRs=cv2.resize(frame, (640,360)) |
| 75 | + hsvRs=cv2.resize(hsv,(640,360)) |
| 76 | + vidBuf = np.concatenate((frameRs, cv2.cvtColor(hsvRs,cv2.COLOR_GRAY2BGR)), axis=1) |
| 77 | + blurRs=cv2.resize(blur,(640,360)) |
| 78 | + edgesRs=cv2.resize(edges,(640,360)) |
| 79 | + vidBuf1 = np.concatenate( (cv2.cvtColor(blurRs,cv2.COLOR_GRAY2BGR),cv2.cvtColor(edgesRs,cv2.COLOR_GRAY2BGR)), axis=1) |
| 80 | + vidBuf = np.concatenate( (vidBuf, vidBuf1), axis=0) |
| 81 | + |
| 82 | + if showWindow==1: # Show Camera Frame |
| 83 | + displayBuf = frame |
| 84 | + elif showWindow == 2: # Show Canny Edge Detection |
| 85 | + displayBuf = edges |
| 86 | + elif showWindow == 3: # Show All Stages |
| 87 | + displayBuf = vidBuf |
| 88 | + |
| 89 | + if showHelp == True: |
| 90 | + cv2.putText(displayBuf, helpText, (11,20), font, 1.0, (32,32,32), 4, cv2.LINE_AA) |
| 91 | + cv2.putText(displayBuf, helpText, (10,20), font, 1.0, (240,240,240), 1, cv2.LINE_AA) |
| 92 | + cv2.imshow(windowName,displayBuf) |
| 93 | + key=cv2.waitKey(10) |
| 94 | + if key == 27: # Check for ESC key |
| 95 | + cv2.destroyAllWindows() |
| 96 | + break ; |
| 97 | + elif key==49: # 1 key, show frame |
| 98 | + cv2.setWindowTitle(windowName,"Camera Feed") |
| 99 | + showWindow=1 |
| 100 | + elif key==50: # 2 key, show Canny |
| 101 | + cv2.setWindowTitle(windowName,"Canny Edge Detection") |
| 102 | + showWindow=2 |
| 103 | + elif key==51: # 3 key, show Stages |
| 104 | + cv2.setWindowTitle(windowName,"Camera, Gray scale, Gaussian Blur, Canny Edge Detection") |
| 105 | + showWindow=3 |
| 106 | + elif key==52: # 4 key, toggle help |
| 107 | + showHelp = not showHelp |
| 108 | + elif key==44: # , lower canny edge threshold |
| 109 | + edgeThreshold=max(0,edgeThreshold-1) |
| 110 | + print ('Canny Edge Threshold Maximum: ',edgeThreshold) |
| 111 | + elif key==46: # , raise canny edge threshold |
| 112 | + edgeThreshold=edgeThreshold+1 |
| 113 | + print ('Canny Edge Threshold Maximum: ', edgeThreshold) |
| 114 | + elif key==74: # Toggle fullscreen; This is the F3 key on this particular keyboard |
| 115 | + # Toggle full screen mode |
| 116 | + if showFullScreen == False : |
| 117 | + cv2.setWindowProperty(windowName, cv2.WND_PROP_FULLSCREEN, cv2.WINDOW_FULLSCREEN) |
| 118 | + else: |
| 119 | + cv2.setWindowProperty(windowName, cv2.WND_PROP_FULLSCREEN, cv2.WINDOW_NORMAL) |
| 120 | + showFullScreen = not showFullScreen |
| 121 | + |
| 122 | + else: |
| 123 | + print ("camera open failed") |
| 124 | + |
| 125 | + |
| 126 | + |
| 127 | +if __name__ == '__main__': |
| 128 | + arguments = parse_cli_args() |
| 129 | + print("Called with args:") |
| 130 | + print(arguments) |
| 131 | + print("OpenCV version: {}".format(cv2.__version__)) |
| 132 | + print("Device Number:",arguments.video_device) |
| 133 | + print cv2.getBuildInformation() |
| 134 | + if arguments.video_device==0: |
| 135 | + print("Using on-board camera") |
| 136 | + video_capture=open_onboard_camera() |
| 137 | + else: |
| 138 | + video_capture=open_camera_device(arguments.video_device) |
| 139 | + # Only do this on external cameras; onboard will cause camera not to read |
| 140 | + video_capture.set(cv2.CAP_PROP_FPS,30) |
| 141 | + read_cam(video_capture) |
| 142 | + video_capture.release() |
| 143 | + cv2.destroyAllWindows() |
0 commit comments