def __init__(self, src=0, camera=JETSONCAMERA, resolution=(1280, 720), framerate=21): # check to see if the picamera module should be used if camera == self.PICAMERA: # only import the picamera packages unless we are # explicity told to do so -- this helps remove the # requirement of `picamera[array]` from desktops or # laptops that still want to use the `imutils` package from imutils.video.pivideostream import PiVideoStream # initialize the picamera stream and allow the camera # sensor to warmup self.stream = PiVideoStream(resolution=resolution, framerate=framerate) elif camera == self.JETSONCAMERA: from .jetsonvideostream import JetsonVideoStream self.stream = JetsonVideoStream(resolution=resolution, framerate=framerate) # otherwise, we are using OpenCV so initialize the webcam # stream else: self.stream = WebcamVideoStream(src=src)
def __init__(self, src=0, usePiCamera=False, useFlirCamera=False, resolution=(320, 240), framerate=32, **kwargs): # check to see if the picamera module should be used if usePiCamera: # only import the picamera packages unless we are # explicity told to do so -- this helps remove the # requirement of `picamera[array]` from desktops or # laptops that still want to use the `imutils` package from imutils.video.pivideostream import PiVideoStream # initialize the picamera stream and allow the camera # sensor to warmup self.stream = PiVideoStream(resolution=resolution, framerate=framerate, **kwargs) elif useFlirCamera: from .thermalcamvideostream import ThermalcamVideoStream self.stream = ThermalcamVideoStream(src=src) # otherwise, we are using OpenCV so initialize the webcam # stream else: from imutils.video.webcamvideostream import WebcamVideoStream self.stream = WebcamVideoStream(src=src)
class VisionManager(): def __init__(self, vertex_callback=None, direction_callback=None): self.camera = None if USE_PI: self.camera = PiVideoStream(resolution=(640, 480)) else: #self.stream = cv2.VideoCapture(0) self.camera = WebcamVideoStream(src=0) # Set width/height self.camera.stream.set(cv2.CAP_PROP_FRAME_WIDTH, 640) self.camera.stream.set(cv2.CAP_PROP_FRAME_HEIGHT, 480) self.frames = [] self.on_vertex = False self.vertex_callback = vertex_callback self.direction_callback = direction_callback for i in range(N_SLICES): self.frames.append(image.Image()) def start(self): self.camera.start() return self # Used for tk def read_rgb(self, detect_lanes=True): frame = self.camera.read() if frame is None: return None (b, g, r) = cv2.split(frame) frame = cv2.merge((r, g, b)) if detect_lanes: tmp_frame = utils.RemoveBackground(frame) directions, contours = utils.SlicePart(tmp_frame, self.frames, N_SLICES) # Check vertex every 2 seconds if int(time.time() % 2) == 0: vertex = True if statistics.stdev( contours[0:5]) >= 30 else False if vertex != self.on_vertex and self.vertex_callback: self.vertex_callback(vertex) self.on_vertex = vertex if not self.on_vertex: m = statistics.mean(directions[0:6]) self.direction_callback(m / 250) return tmp_frame def stop(self): self.camera.stop()
def __init__(self, src=0, use_pi_camera=False, resolution=(800, 600), framerate=30): if use_pi_camera: from imutils.video.pivideostream import PiVideoStream self.stream = PiVideoStream(resolution=resolution, framerate=framerate) else: from imutils.video.webcamvideostream import WebcamVideoStream self.stream = WebcamVideoStream(src=src)
class PersonDetector(object): def __init__(self, flip = True): self.last_upload = time.time() self.vs = WebcamVideoStream().start() self.flip = flip time.sleep(2.0) def __del__(self): self.vs.stop() def flip_if_needed(self, frame): if self.flip: return np.flip(frame, 0) return frame def get_frame(self): frame = self.flip_if_needed(self.vs.read()) frame = self.process_image(frame) ret, jpeg = cv2.imencode('.jpg', frame) return jpeg.tobytes() def process_image(self, frame): frame = imutils.resize(frame, width=300) (h, w) = frame.shape[:2] blob = cv2.dnn.blobFromImage(frame, 0.007843, (300, 300), 127.5) net.setInput(blob) detections = net.forward() count = 0 for i in np.arange(0, detections.shape[2]): confidence = detections[0, 0, i, 2] if confidence < 0.2: continue idx = int(detections[0, 0, i, 1]) if idx != 15: continue box = detections[0, 0, i, 3:7] * np.array([w, h, w, h]) (startX, startY, endX, endY) = box.astype('int') label = '{}: {:.2f}%'.format('person', confidence * 100)#('Person', confidence * 100) cv2.rectangle(frame, (startX, startY), (endX, endY), (0, 255, 0), 2) y = startY - 15 if startY - 15 > 15 else startY + 15 cv2.putText(frame, label, (startX, y), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 1) count += 1 if count > 0: print('Count_person: {}'.format(count)) elapsed = time.time() - self.last_upload if elapsed > 5: request(count) self.last_upload = time.time() return frame
def render(source): # initialize the video stream and allow the cammera sensor to warmup if source: vs = FileVideoStream(source) if not vs.stream.isOpened(): print("File couldn't be opened") sys.exit() video_fps = vs.stream.get(cv2.CAP_PROP_FPS) fourcc = cv2.VideoWriter_fourcc(*'MJPG') ofn = '../data/output/{}'.format(sys.argv[1].split('/')[-1].replace( 'clip_', 'output_')) out = cv2.VideoWriter(ofn, fourcc, video_fps, (640, 512)) else: vs = WebcamVideoStream().start() out = None # If Camera Device is not opened, exit the program # loop over the frames from the video stream cv2.startWindowThread() cv2.namedWindow('Movie', cv2.WINDOW_NORMAL) cv2.resizeWindow('Movie', 640 + 60, 480 + 60) cv2.moveWindow('Movie', 1920 + 1400, 600) p = Processor() video_fps = vs.stream.get(cv2.CAP_PROP_FPS) print("Video FPS: {0}".format(video_fps)) while True: frame = vs.read() if frame is None: break #print("Read:", frame.shape) frame = imutils.resize(frame, width=640) p.update(frame) # show the frame #print(frame.shape) cv2.imshow("Movie", frame) if out: out.write(frame) key = cv2.waitKey(1) & 0xFF # if the Esc or `q` key was pressed, break from the loop if key == ord("q") or key == 27: break print("Finished") vs.stop() vs.stream.release() if out: out.release() p.finish() # do a bit of cleanup cv2.destroyWindow("Movie")
async def start(self): try: import picamera from picamera.array import PiRGBArray with picamera.PiCamera() as camera: camera.resolution = self.resolution camera.framerate = self.framerate camera.rotation = self.rotation camera.vflip = self.vflip camera.hflip = self.vflip camera.exposure_mode = self.exposure_mode rawCapture = PiRGBArray(camera, size=self.resolution) logging.info("PiVideoStream loaded.. .warming camera") camera.start_preview() await asyncio.sleep(2) logging.info("PiVideoStream starting continuous stream") for f in camera.capture_continuous(rawCapture, format="bgr", use_video_port=True): frame = f.array if frame is None: await asyncio.sleep(0.1) continue self.frame = frame rawCapture.truncate(0) self.frame_time = time.time() await asyncio.sleep(0.001) if self.stopped: return except: logging.error("Could not load picamera, running in test mode") from imutils.video.webcamvideostream import WebcamVideoStream stream = WebcamVideoStream(src=0).start() logging.info("Starting WebcamVideoStream stream") while not self.stopped: frame = stream.read() if frame is None: await asyncio.sleep(0.1) continue self.frame = frame self.frame_time = time.time() await asyncio.sleep(0.001)
def __init__(self, vertex_callback=None, direction_callback=None): self.camera = None if USE_PI: self.camera = PiVideoStream(resolution=(640, 480)) else: #self.stream = cv2.VideoCapture(0) self.camera = WebcamVideoStream(src=0) # Set width/height self.camera.stream.set(cv2.CAP_PROP_FRAME_WIDTH, 640) self.camera.stream.set(cv2.CAP_PROP_FRAME_HEIGHT, 480) self.frames = [] self.on_vertex = False self.vertex_callback = vertex_callback self.direction_callback = direction_callback for i in range(N_SLICES): self.frames.append(image.Image())
class VideoCamera(object): def __init__(self, flip=False): self.vs = WebcamVideoStream(settings.facevideosource).start() self.flip = flip time.sleep(2.0) def __del__(self): self.vs.stop() def flip_if_needed(self, frame): if self.flip: return np.flip(frame, 0) return frame def get_frame(self): frame = self.flip_if_needed(self.vs.read()) ret, jpeg = cv2.imencode('.jpg', frame) return jpeg.tobytes() def get_object(self, classifier): found_objects = False frame = self.flip_if_needed(self.vs.read()).copy() gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) objects = classifier.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=5, minSize=(30, 30), flags=cv2.CASCADE_SCALE_IMAGE) if len(objects) > 0: found_objects = True # Draw a rectangle around the objects for (x, y, w, h) in objects: cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2) ret, jpeg = cv2.imencode('.jpg', frame) return (jpeg.tobytes(), found_objects)
def start(self) -> None: logger.info('Starting Camera') if self.rpi_camera: self.cap = VideoStream(src=self.src, usePiCamera=True, resolution=(self.width, self.height), framerate=self.fps) self.cap.start() self.set_hardware_rpi() elif self.threaded: self.cap = WebcamVideoStream(self.src, name='cam') self.cap.start() self.set_hardware_threaded() else: self.cap = cv.VideoCapture(self.src) if self.multiple_monitors: self.move_window(self.MONITOR_LIMIT + 1, 0) cv.namedWindow(self.window_name, cv.WINDOW_NORMAL) cv.resizeWindow(self.window_name, self.monitor_display) if self.full_screen: cv.setWindowProperty(self.window_name, cv.WND_PROP_FULLSCREEN, cv.WINDOW_FULLSCREEN)
class VideoStream: def __init__(self, src=0, usePiCamera=False, useFlirCamera=False, resolution=(320, 240), framerate=32, **kwargs): # check to see if the picamera module should be used if usePiCamera: # only import the picamera packages unless we are # explicity told to do so -- this helps remove the # requirement of `picamera[array]` from desktops or # laptops that still want to use the `imutils` package from imutils.video.pivideostream import PiVideoStream # initialize the picamera stream and allow the camera # sensor to warmup self.stream = PiVideoStream(resolution=resolution, framerate=framerate, **kwargs) elif useFlirCamera: from .thermalcamvideostream import ThermalcamVideoStream self.stream = ThermalcamVideoStream(src=src) # otherwise, we are using OpenCV so initialize the webcam # stream else: from imutils.video.webcamvideostream import WebcamVideoStream self.stream = WebcamVideoStream(src=src) def start(self): # start the threaded video stream return self.stream.start() def update(self): # grab the next frame from the stream self.stream.update() def read(self): # return the current frame return self.stream.read() def stop(self): # stop the thread and release any resources self.stream.stop()
class VideoStream: WEBCAM = 0 PICAMERA = 1 JETSONCAMERA = 2 def __init__(self, src=0, camera=JETSONCAMERA, resolution=(1280, 720), framerate=21): # check to see if the picamera module should be used if camera == self.PICAMERA: # only import the picamera packages unless we are # explicity told to do so -- this helps remove the # requirement of `picamera[array]` from desktops or # laptops that still want to use the `imutils` package from imutils.video.pivideostream import PiVideoStream # initialize the picamera stream and allow the camera # sensor to warmup self.stream = PiVideoStream(resolution=resolution, framerate=framerate) elif camera == self.JETSONCAMERA: from .jetsonvideostream import JetsonVideoStream self.stream = JetsonVideoStream(resolution=resolution, framerate=framerate) # otherwise, we are using OpenCV so initialize the webcam # stream else: self.stream = WebcamVideoStream(src=src) def start(self): # start the threaded video stream return self.stream.start() def update(self): # grab the next frame from the stream self.stream.update() def read(self): # return the current frame return self.stream.read() def stop(self): # stop the thread and release any resources self.stream.stop()
class CameraStream: def __init__(self, src=0, use_pi_camera=False, resolution=(800, 600), framerate=30): if use_pi_camera: from imutils.video.pivideostream import PiVideoStream self.stream = PiVideoStream(resolution=resolution, framerate=framerate) else: from imutils.video.webcamvideostream import WebcamVideoStream self.stream = WebcamVideoStream(src=src) def start(self): return self.stream.start() def update(self): self.stream.update() def read(self): return self.stream.read() def stop(self): self.stream.stop()
import numpy as np import argparse from imutils.video.webcamvideostream import WebcamVideoStream import imutils import datetime import time import cv2 ap = argparse.ArgumentParser() ap.add_argument("-a", "--min-area", type=int, default=500, help="minimum area") args = vars(ap.parse_args()) vs = WebcamVideoStream(src=0).start() time.sleep(2) # first_frame = None avg = None while True: frame = vs.read() text = "Unoccupied" if frame is None: break scale_percent = 60 width = int(frame.shape[1] * scale_percent / 100) height = int(frame.shape[0] * scale_percent / 100) dim = (width, height) frame = cv2.resize(frame, dim) gray_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
def __init__(self, flip = True): self.last_upload = time.time() self.vs = WebcamVideoStream().start() self.flip = flip time.sleep(2.0)
class Camera: cap = None output = None enabled = True pause = False recording = False frame_counter = 0 current_frame = 0 window_name = 'Main' multiple_monitors = False monitor_counter = 0 full_screen = True def __init__(self, config_file='config.yml') -> None: self.config_file = config_file self.load_config() def load_config(self) -> None: with open(self.config_file) as file: config = yaml.safe_load(file)['camera'] self.debug = config['debug'] self.number = config['number'] self.width = config['resolution'][0] self.height = config['resolution'][1] self.display = tuple(config['display']) self.center_x_offset = config['center_x_offset'] self.center_y_offset = config['center_y_offset'] self.roi = config['roi'] self.rpi_camera = config['rpi_camera'] self.fps = config['fps'] self.src = config['src'] self.full_screen = config['full_screen'] self.brightness = config['brightness'] self.contrast = config['contrast'] self.saturation = config['saturation'] self.sharpness = config['sharpness'] self.exposure = config['exposure'] self.exposure_comp = config['exposure'] self.white_balance = config['white_balance'] self.awb_mode = config['awb_mode'] self.threaded = config['threaded'] self.monitor_display = tuple(config['display']) self.hue = 0 def start(self) -> None: logger.info('Starting Camera') if self.rpi_camera: self.cap = VideoStream(src=self.src, usePiCamera=True, resolution=(self.width, self.height), framerate=self.fps) self.cap.start() self.set_hardware_rpi() elif self.threaded: self.cap = WebcamVideoStream(self.src, name='cam') self.cap.start() self.set_hardware_threaded() else: self.cap = cv.VideoCapture(self.src) if self.multiple_monitors: self.move_window(self.MONITOR_LIMIT + 1, 0) cv.namedWindow(self.window_name, cv.WINDOW_NORMAL) cv.resizeWindow(self.window_name, self.monitor_display) if self.full_screen: cv.setWindowProperty(self.window_name, cv.WND_PROP_FULLSCREEN, cv.WINDOW_FULLSCREEN) def stop(self) -> None: if self.threaded or self.rpi_camera: self.cap.stop() self.cap.stream.release() else: self.cap.release() def set_hardware_threaded(self, **kwargs) -> None: self.cap.stream.set(cv.CAP_PROP_BRIGHTNESS, self.brightness) # # min: 0 max: 255 increment:1 self.cap.stream.set(cv.CAP_PROP_CONTRAST, self.contrast) # min: 0 max: 255 increment:1 self.cap.stream.set(cv.CAP_PROP_SATURATION, self.saturation) # min: 0 max: 255 increment:1 def set_hardware_rpi(self) -> None: self.cap.stream.camera.iso = 640 # 100, 200, 320, 400, 500, 640, 800. self.cap.stream.camera.awb_mode = self.awb_mode self.cap.stream.camera.exposure_mode = 'off' # snow, beach, spotlight self.cap.stream.camera.exposure_compensation = self.__scale( self.exposure_comp, -25, 25) self.cap.stream.camera.brightness = self.__scale( self.brightness, 0, 100) self.cap.stream.camera.contrast = self.__scale(self.contrast, -100, 100) self.cap.stream.camera.saturation = self.__scale( self.saturation, -100, 100) self.cap.stream.camera.sharpness = self.__scale( self.sharpness, -100, 100) def __scale(self, x, y0, y1) -> int: x0, x1 = 0, 255 return int(y0 + ((y1 - y0) / (x1 - x0) * (x - x0))) def show(self, frame: np.ndarray = np.ones((400, 400, 1))): frame = cv.resize(frame, self.display) cv.imshow(self.window_name, frame) self.__update_frame_counter() def move_window(self, x, y) -> None: if self.MAX_MONITORS == 2: logging.info(f'Moving window to {x}, {y}') cv.moveWindow(self.window_name, x, y) def __update_frame_counter(self) -> None: if self.frame_counter % 200 == 0: logging.info("Keep Alive Camera Message") self.frame_counter = self.frame_counter + \ 1 if not self.pause else self.frame_counter self.frame_counter = 0 if self.frame_counter >= 1000 else self.frame_counter def read(self): if self.threaded: return True, self.cap.read() return self.cap.read() def get_tune_point(self, frame, x, y): gray = cv.cvtColor(frame, cv.COLOR_BGR2GRAY) return gray[y, x] def auto_tune(self, pixel): # set contrast, bright and threshold if pixel <= 120: pass elif pixel >= 180: pass elif pixel >= 220: pass def set_alpha(self, image): # loop over the alpha transparency values alpha = 0.5 beta = (1 - alpha) gamma = 0.0 background = np.zeros((self.height, self.width, 3), np.uint8) background[:, :, 1] = 200 background[:, :, 2] = 200 added_image = cv.addWeighted(background, alpha, image, beta, gamma) return added_image
def __init__(self, flip=True): self.vs = WebcamVideoStream().start() #self.vs = PiVideoStream(resolution=(800, 608)).start() self.flip = flip time.sleep(1.0)
def main(args): # created a *threaded *video stream, allow the camera sensor to warmup, # and start the FPS counter vs = PiVideoStream().start() if args["picamera"] else WebcamVideoStream().start() print("[INFO] warming up camera...") time.sleep(2.0) if not os.path.isdir(args["buffer"]): os.makedirs(args["buffer"]) if not os.path.isdir(args["output"]): os.makedirs(args["output"]) # initialize the FourCC, video writer, dimensions of the frame, and # zeros array fourcc = cv2.VideoWriter_fourcc(*args["codec"]) (h, w) = (None, None) length = args["length"] temp = list() counter = 0 timeframe = 0 cv2.namedWindow("Frame", cv2.WINDOW_NORMAL) params = { "output": args["output"], "buffer": args["buffer"], "type": args["type"], "length": length, "fps": args["fps"], "counter": 0, "temp": None, "frame": None, "writer": None, "idx": 0, "res_code": 0 } buffer_prefix = os.path.join(args["buffer"], buffer_name) # loop over some frames...this time using the threaded stream while True: # grab the frame from the threaded video stream and resize it # to have a maximum width of 400 pixels frame = vs.read() frame = imutils.resize(frame, width=400) if params["writer"] is None: # store the image dimensions, initialzie the video writer, # and construct the zeros array (h, w) = frame.shape[:2] params["writer"] = cv2.VideoWriter(buffer_prefix + "_" + str(timeframe) + "." + args["type"], fourcc, args["fps"], (w, h), True) temp.append(frame) counter += 1 params["counter"] = counter params["temp"] = temp params["frame"] = frame if counter >= args["fps"]: for img in temp: params["writer"].write(img) temp = list() counter = 0 timeframe = (timeframe + 1) % length params["timeframe"] = timeframe params["writer"].release() params["writer"] = cv2.VideoWriter(buffer_prefix + "_" + str(timeframe) + "." + args["type"], fourcc, args["fps"], (w, h), True) if not q.empty(): flag = q.get() if flag == 0: params["writer"].release() break click(params) # check to see if the frame should be displayed to our screen if args["display"]: cv2.imshow("Frame", frame) print("[INFO] cleaning up...") cv2.destroyAllWindows() vs.stop() print("[INFO] done")
class PersonDetector(object): def __init__(self, flip=True): self.vs = WebcamVideoStream().start() #self.vs = PiVideoStream(resolution=(800, 608)).start() self.flip = flip time.sleep(1.0) def __del__(self): self.vs.stop() def flip_if_needed(self, frame): if self.flip: return np.flip(frame, 0) return frame def get_frame(self): frame = self.flip_if_needed(self.vs.read()) frame = self.process_image(frame) ret, jpeg = cv2.imencode('.jpg', frame) return jpeg.tobytes() def process_image(self, frame): frame = imutils.resize(frame, width=300) (h, w) = frame.shape[:2] blob = cv2.dnn.blobFromImage(frame, 0.007843, (300, 300), 127.5) net.setInput(blob) detections = net.forward() data_list = [] data = {'device': os.environ['DEVICE'], 'data': {}} data['data']['timestamp'] = str(datetime.datetime.now()) data['data']['person_id'] = 0 for i in np.arange(0, detections.shape[2]): confidence = detections[0, 0, i, 2] if confidence < 0.2: continue idx = int(detections[0, 0, i, 1]) if idx != 15: continue box = detections[0, 0, i, 3:7] * np.array([w, h, w, h]) (startX, startY, endX, endY) = box.astype('int') label = '{}: {:.2f}%'.format('Person', confidence * 100) cv2.rectangle(frame, (startX, startY), (endX, endY), (0, 255, 0), 2) y = startY - 15 if startY - 15 > 15 else startY + 15 cv2.putText(frame, label, (startX, y), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 1) data['timestamp'] = str(datetime.datetime.now()) data['data']['x'] = str((endX + startX) / 2) data['data']['y'] = str((endY + startY) / 2) print(data) data_list.append(copy.deepcopy(data)) data['data']['person_id'] += 1 q = threading.Thread(target=insert, args=(data_list, )) q.start() data_list = [] return frame
def __init__(self, flip=False): self.vs = WebcamVideoStream(settings.facevideosource).start() self.flip = flip time.sleep(2.0)
import time import cv2 from google.cloud import pubsub_v1 from imutils.video.fps import FPS from imutils.video.webcamvideostream import WebcamVideoStream import os from datetime import datetime, timezone # TODO: Add headless mode # TODO: reset FPS beginning once in a while # TODO: Add some caching for long internet outages # VideoStream stream = WebcamVideoStream(src=0).start() time.sleep(2.0) fps = FPS().start() # Setup PubSub client = pubsub_v1.PublisherClient.from_service_account_file( '/home/pi/service_account.json') PROJECT_ID = 'dhodun1' TOPIC_NAME = 'test_traffic_topic' topic_name = client.topic_path(PROJECT_ID, TOPIC_NAME) project = client.project_path(PROJECT_ID) # See if topic created topics = []
print('Succeeded :', item) return #obj = ["background", "aeroplane", "bicycle", "bird", "boat", # "bottle", "bus", "car", "cat", "chair", "cow", "diningtable", # "dog", "horse", "motorbike", "person", "pottedplant", "sheep", # "sofa", "train", "tvmonitor"] print('starting... model reading...') net = cv2.dnn.readNetFromCaffe( '/var/isaax/project/camera/processor/MobileNetSSD_deploy.prototxt', '/var/isaax/project/camera/processor/MobileNetSSD_deploy.caffemodel') data = {'device': os.environ['DEVICE'], 'data': {}} print('start detecting...') vs = WebcamVideoStream().start() time.sleep(2.0) data_list = [] while True: frame = vs.read() frame = imutils.resize(frame, width=300) (h, w) = frame.shape[:2] blob = cv2.dnn.blobFromImage(frame, 0.007843, (300, 300), 127.5) net.setInput(blob) detections = net.forward() data['data']['timestamp'] = str(datetime.datetime.now()) data['data']['person_id'] = 0 for i in np.arange(0, detections.shape[2]): confidence = detections[0, 0, i, 2]
def __init__(self, flip=False): self.vs = WebcamVideoStream() self.flip = flip