Last active
March 5, 2019 12:32
-
-
Save aayushsahu/9331e7943e50c7f1bad0dd5a3231c22f 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
| #running the file | |
| #python rtspcamera.py --rtsp --uri rtsp://192.168.1.8:8080/h264_pcm.sdp --width 1280 --height 720 | |
| def open_cam_rtsp(url, uri, width=1000, height=786, latency=0): | |
| gst_str = ('rtspsrc location={} latency={} ! ' | |
| 'rtph264depay ! h264parse ! omxh264dec ! ' | |
| 'nvvidconv ! ' | |
| 'video/x-raw, width=(int){}, height=(int){}, ' | |
| 'format=(string)BGRx ! ' | |
| 'videoconvert ! appsink').format(uri, latency, width, height) | |
| return cv2.VideoCapture(gst_str, cv2.CAP_GSTREAMER) | |
| def open_window(width, height): | |
| cv2.namedWindow(WINDOW_NAME, cv2.WINDOW_NORMAL) | |
| cv2.resizeWindow(WINDOW_NAME, width, height) | |
| cv2.moveWindow(WINDOW_NAME, 0, 0) | |
| cv2.setWindowTitle(WINDOW_NAME, 'Camera Demo for Jetson TX2/TX1') | |
| def read_cam(cap): | |
| show_help = True | |
| full_scrn = False | |
| help_text = '"Esc" to Quit, "H" for Help, "F" to Toggle Fullscreen' | |
| font = cv2.FONT_HERSHEY_PLAIN | |
| x=cap.isOpened() | |
| print(x) | |
| while(x): | |
| if cv2.getWindowProperty(WINDOW_NAME, 0) < 0: | |
| # Check to see if the user has closed the window | |
| # If yes, terminate the program | |
| break | |
| _, img = cap.read() # grab the next image frame from camera | |
| if show_help: | |
| cv2.putText(img, help_text, (11, 20), font, | |
| 1.0, (32, 32, 32), 4, cv2.LINE_AA) | |
| cv2.putText(img, help_text, (10, 20), font, | |
| 1.0, (240, 240, 240), 1, cv2.LINE_AA) | |
| if img != None: | |
| cv2.imshow(WINDOW_NAME, img) | |
| key = cv2.waitKey(10) | |
| if key == ord('Q') or key == ord('q'): | |
| break | |
| elif key == ord('H') or key == ord('h'): # toggle help message | |
| show_help = not show_help | |
| elif key == ord('F') or key == ord('f'): # toggle fullscreen | |
| full_scrn = not full_scrn | |
| if full_scrn: | |
| cv2.setWindowProperty(WINDOW_NAME, cv2.WND_PROP_FULLSCREEN, | |
| cv2.WINDOW_FULLSCREEN) | |
| else: | |
| cv2.setWindowProperty(WINDOW_NAME, cv2.WND_PROP_FULLSCREEN, | |
| cv2.WINDOW_NORMAL) | |
| def parse_args(): | |
| # Parse input arguments | |
| desc = 'Capture and display live camera video on Jetson TX2/TX1' | |
| parser = argparse.ArgumentParser(description=desc) | |
| parser.add_argument('--rtsp', dest='use_rtsp', | |
| help='use IP CAM (remember to also set --uri)', | |
| action='store_true') | |
| parser.add_argument('--uri', dest='rtsp_uri', | |
| help='RTSP URI, e.g. rtsp://192.168.1.64:554', | |
| default=None, type=str) | |
| parser.add_argument('--latency', dest='rtsp_latency', | |
| help='latency in ms for RTSP [200]', | |
| default=200, type=int) | |
| parser.add_argument('--width', dest='image_width', | |
| help='image width [1920]', | |
| default=1080, type=int) | |
| parser.add_argument('--height', dest='image_height', | |
| help='image height [1080]', | |
| default=760, type=int) | |
| args = parser.parse_args() | |
| return args | |
| def main() : | |
| try: | |
| path = "C:/Users/SOL INVICTUS/OneDrive/WORKSPACE_FOR_PYTHON/Workspace for jupyter/car_trained_peleenet_10epochs.pt" | |
| model= PeleeNet() # this is a class which is existing in this same file but not mentioned in the gist | |
| model.load_state_dict(torch.load(path, map_location='cpu')) | |
| print("model loaded...") | |
| args = parse_args() | |
| print('Called with args:') | |
| print(args) | |
| print('OpenCV version: {}'.format(cv2.__version__)) | |
| if args.use_rtsp: | |
| cap = open_cam_rtsp(args.use_rtsp, | |
| args.rtsp_uri, | |
| args.image_width, | |
| args.image_height, | |
| args.rtsp_latency) | |
| if not cap.isOpened(): | |
| print('Failed to open camera!') | |
| open_window(args.image_width, args.image_height) | |
| read_cam(cap) | |
| except Exception as e: | |
| print(e) | |
| if __name__ == '__main__': | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment