def readMjpegFrames(logger, frameBuf, frameBufMax): """Read frames and append to buffer""" global frameOk, LOCAL if LOCAL: res = (cv2.imread(x) for x in sorted(glob('photos/*.JPG'))) while (frameOk): now = datetime.datetime.now() # Make sure thread doesn't hang in case of socket time out, etc. image = None if LOCAL: try: image = next(res) except StopIteration: break else: image = mjpegclient.getFrame() #frameOk = len(jpeg) > 0 if image is not None: # Make sure we do not run out of memory if len(frameBuf) > frameBufMax: logger.error("Frame buffer exceeded: %d" % frameBufMax) frameOk = False else: # Add new image to end of list frameBuf.append((image, now)) logger.info("Exiting video stream thread")
def initMjpegVideo(): """Initialize MJPEG stream""" # Open MJPEG stream # Determine image dimensions image = mjpegclient.getFrame() frameHeight, frameWidth, channels = image.shape return frameWidth, frameHeight
handler.setFormatter(formatter) logger.addHandler(handler) # If no args passed then use default camera if len(sys.argv) < 3: url = "http://localhost:8080/?action=stream?dummy=param.mjpg" frames = 50 # True for mjpg_streamer extraln = True else: url = sys.argv[1] frames = int(sys.argv[2]) extraln = bool(sys.argv[3]) logger.info("OpenCV %s" % cv2.__version__) logger.info("URL: %s, frames to capture: %d" % (url, frames)) socketFile, streamSock, boundary = mjpegclient.openUrl(url, 10) jpeg, image = mjpegclient.getFrame(socketFile, boundary, extraln) height, width, unknown = image.shape logger.info("Resolution: %dx%d" % (width, height)) if width > 0 and height > 0: logger.info("Calculate FPS using %d frames" % frames) framesLeft = frames start = time.time() # Calculate FPS while(framesLeft > 0): jpeg, image = mjpegclient.getFrame(socketFile, boundary, extraln) framesLeft -= 1 elapsed = time.time() - start fps = frames / elapsed logger.info("Calculated %4.1f FPS, elapsed time: %4.2f seconds" % (fps, elapsed)) socketFile.close() streamSock.close()
if len(sys.argv) < 5: url = "http://localhost:8080/?action=stream" frames = 200 fourcc = "XVID" fps = 5 outputFile = "../../output/camera.avi" else: url = sys.argv[1] frames = int(sys.argv[2]) fourcc = sys.argv[3] fps = int(sys.argv[4]) outputFile = sys.argv[5] logger.info("OpenCV %s" % cv2.__version__) logger.info("URL: %s, frames to capture: %d" % (url, frames)) socketFile, streamSock, boundary, skipLines = mjpegclient.openUrl(url, 10) jpeg, image = mjpegclient.getFrame(socketFile, boundary, skipLines) height, width, unknown = image.shape logger.info("Resolution: %dx%d" % (width, height)) if width > 0 and height > 0: videoWriter = cv2.VideoWriter(outputFile, cv2.CAP_FFMPEG, cv2.VideoWriter_fourcc(fourcc[0], fourcc[1], fourcc[2], fourcc[3]), fps, (width, height), True) logger.info("Calculate FPS using %d frames" % frames) framesLeft = frames start = time.time() # Calculate FPS while(framesLeft > 0): jpeg, image = mjpegclient.getFrame(socketFile, boundary, skipLines) videoWriter.write(image) framesLeft -= 1 elapsed = time.time() - start fps = frames / elapsed logger.info("Calculated %4.1f FPS, elapsed time: %4.2f seconds" % (fps, elapsed))
url = "http://localhost:8080/?action=stream" frames = 200 fourcc = "XVID" fps = 5 outputFile = "camera.avi" else: url = sys.argv[1] frames = int(sys.argv[2]) fourcc = sys.argv[3] fps = int(sys.argv[4]) outputFile = sys.argv[5] logger.info("OpenCV %s" % cv2.__version__) logger.info("URL: %s, frames to capture: %d" % (url, frames)) socketFile, streamSock, boundary = mjpegclient.open(url, 10) jpeg, image = mjpegclient.getFrame(socketFile, boundary) height, width, unknown = image.shape logger.info("Resolution: %dx%d" % (width, height)) if width > 0 and height > 0: videoWriter = cv2.VideoWriter(outputFile, cv2.VideoWriter_fourcc(fourcc[0], fourcc[1], fourcc[2], fourcc[3]), fps, (width, height), True) logger.info("Calculate FPS using %d frames" % frames) framesLeft = frames start = time.time() # Calculate FPS while(framesLeft > 0): jpeg, image = mjpegclient.getFrame(socketFile, boundary) videoWriter.write(image) framesLeft -= 1 elapsed = time.time() - start fps = frames / elapsed logger.info("Calculated %4.1f FPS, elapsed time: %4.2f seconds" % (fps, elapsed))
url = "http://localhost:8080/?action=stream" frames = 200 fourcc = "XVID" fps = 5 outputFile = "../../output/camera.avi" else: url = sys.argv[1] frames = int(sys.argv[2]) fourcc = sys.argv[3] fps = int(sys.argv[4]) outputFile = sys.argv[5] logger.info("OpenCV %s" % cv2.__version__) logger.info("URL: %s, frames to capture: %d" % (url, frames)) socketFile, streamSock, boundary = mjpegclient.open(url, 10) jpeg, image = mjpegclient.getFrame(socketFile, boundary) height, width, unknown = image.shape logger.info("Resolution: %dx%d" % (width, height)) if width > 0 and height > 0: videoWriter = cv2.VideoWriter(outputFile, cv2.VideoWriter_fourcc(fourcc[0], fourcc[1], fourcc[2], fourcc[3]), fps, (width, height), True) logger.info("Calculate FPS using %d frames" % frames) framesLeft = frames start = time.time() # Calculate FPS while(framesLeft > 0): jpeg, image = mjpegclient.getFrame(socketFile, boundary) videoWriter.write(image) framesLeft -= 1 elapsed = time.time() - start fps = frames / elapsed logger.info("Calculated %4.1f FPS, elapsed time: %4.2f seconds" % (fps, elapsed))