|
| 1 | +import argparse |
| 2 | +import cv2 |
| 3 | +import redis |
| 4 | +import time |
| 5 | +import sys |
| 6 | +import os |
| 7 | +import atexit |
| 8 | + |
| 9 | +try: |
| 10 | + import urllib.parse |
| 11 | +except ImportError: |
| 12 | + import urllib.parse as urlparse |
| 13 | + |
| 14 | +IMAGE_WIDTH = 640 |
| 15 | +IMAGE_HEIGHT = 480 |
| 16 | + |
| 17 | +MAX_IMAGES = 1000 # 5 |
| 18 | + |
| 19 | +def gstreamer_pipeline( |
| 20 | + capture_width=IMAGE_WIDTH, |
| 21 | + capture_height=IMAGE_HEIGHT, |
| 22 | + display_width=IMAGE_WIDTH, |
| 23 | + display_height=IMAGE_HEIGHT, |
| 24 | + framerate=15, |
| 25 | + flip_method=0, |
| 26 | +): |
| 27 | + return ( |
| 28 | + "nvarguscamerasrc ! " |
| 29 | + "video/x-raw(memory:NVMM), " |
| 30 | + "width=(int)%d, height=(int)%d, " |
| 31 | + "format=(string)NV12, framerate=(fraction)%d/1 ! " |
| 32 | + "nvvidconv flip-method=%d ! " |
| 33 | + "video/x-raw, width=(int)%d, height=(int)%d, format=(string)BGRx ! " |
| 34 | + "videoconvert ! " |
| 35 | + "video/x-raw, format=(string)BGR ! appsink" |
| 36 | + % ( |
| 37 | + capture_width, |
| 38 | + capture_height, |
| 39 | + framerate, |
| 40 | + flip_method, |
| 41 | + display_width, |
| 42 | + display_height, |
| 43 | + ) |
| 44 | + ) |
| 45 | + |
| 46 | + |
| 47 | +class Webcam: |
| 48 | + def __init__(self, infile=0, fps=15.0): |
| 49 | + if infile: |
| 50 | + self.cam = cv2.VideoCapture(infile) |
| 51 | + self.cam.set(cv2.CAP_PROP_FPS, fps) |
| 52 | + self.cam.set(cv2.CAP_PROP_FRAME_WIDTH, IMAGE_WIDTH) |
| 53 | + self.cam.set(cv2.CAP_PROP_FRAME_HEIGHT, IMAGE_HEIGHT) |
| 54 | + else: |
| 55 | + self.cam = cv2.VideoCapture(gstreamer_pipeline(flip_method=0), cv2.CAP_GSTREAMER) |
| 56 | + atexit.register(self.cam.release) |
| 57 | + |
| 58 | + |
| 59 | + def __iter__(self): |
| 60 | + self.count = -1 |
| 61 | + return self |
| 62 | + |
| 63 | + def __next__(self): # Python 2.7 |
| 64 | + self.count += 1 |
| 65 | + |
| 66 | + # Read image |
| 67 | + ret_val, img0 = self.cam.read() |
| 68 | + assert ret_val, 'Webcam Error' |
| 69 | + |
| 70 | + # Preprocess |
| 71 | + # img = cv2.flip(img0, 1) |
| 72 | + img = img0 |
| 73 | + |
| 74 | + return self.count, img |
| 75 | + |
| 76 | + def __len__(self): |
| 77 | + return 0 |
| 78 | + |
| 79 | + |
| 80 | +if __name__ == '__main__': |
| 81 | + parser = argparse.ArgumentParser() |
| 82 | + parser.add_argument('infile', help='Input file (leave empty to use webcam)', nargs='?', type=str, default=None) |
| 83 | + parser.add_argument('-o', '--output', help='Output stream key name', type=str, default='camera:0') |
| 84 | + parser.add_argument('-u', '--url', help='Redis URL', type=str, default='redis://localhost:6379') |
| 85 | + parser.add_argument('--fmt', help='Frame storage format', type=str, default='.jpg') |
| 86 | + parser.add_argument('--fps', help='Frames per second (webcam)', type=float, default=15.0) |
| 87 | + parser.add_argument('--maxlen', help='Maximum length of output stream', type=int, default=1000) |
| 88 | + parser.add_argument('--test', help='transmit image instead of reading webcam', action="store_true") |
| 89 | + args = parser.parse_args() |
| 90 | + |
| 91 | + # Set up Redis connection |
| 92 | + url = urllib.parse.urlparse(args.url) |
| 93 | + conn = redis.Redis(host=url.hostname, port=url.port) |
| 94 | + if not conn.ping(): |
| 95 | + raise Exception('Redis unavailable') |
| 96 | + print('Connected to Redis') |
| 97 | + sys.stdout.flush() |
| 98 | + |
| 99 | + if args.test is False: |
| 100 | + print('Operating in camera mode') |
| 101 | + sys.stdout.flush() |
| 102 | + if args.infile is None: |
| 103 | + loader = Webcam(infile=0, fps=args.fps) |
| 104 | + else: |
| 105 | + loader = Webcam(infile=int(args.infile), fps=args.fps) |
| 106 | + |
| 107 | + for (count, img) in loader: |
| 108 | + _, data = cv2.imencode(args.fmt, img) |
| 109 | + msg = { |
| 110 | + 'count': count, |
| 111 | + 'image': data.tobytes() |
| 112 | + } |
| 113 | + _id = conn.execute_command('xadd', args.output, 'MAXLEN', '~', str(MAX_IMAGES), '*', 'count', msg['count'], 'img', msg['image']) |
| 114 | + print('count: {} id: {}'.format(count, _id)) |
| 115 | + sys.stdout.flush() |
| 116 | + else: |
| 117 | + image_file = os.environ['ANIMAL'] + '.jpg' |
| 118 | + print('Operating in test mode with image ' + image_file) |
| 119 | + sys.stdout.flush() |
| 120 | + img0 = cv2.imread(image_file) |
| 121 | + img = cv2.resize(img0, (IMAGE_WIDTH, IMAGE_HEIGHT)) |
| 122 | + _, data = cv2.imencode(args.fmt, img) |
| 123 | + count = 1 |
| 124 | + while True: |
| 125 | + msg = { |
| 126 | + 'count': count, |
| 127 | + 'image': data.tobytes() |
| 128 | + } |
| 129 | + _id = conn.execute_command('xadd', args.output, 'MAXLEN', '~', str(MAX_IMAGES), '*', 'count', msg['count'], 'img', msg['image']) |
| 130 | + # print('count: {} rc: {} id: {}'.format(count, rc, _id)) |
| 131 | + # sys.stdout.flush() |
| 132 | + count += 1 |
| 133 | + time.sleep(0.1) |
0 commit comments