def conv_single(self, filein): try: filebase = os.path.basename(filein) fileout = filein if self.outdir == "" else self.outdir + "/" + filebase lineprint("Start converting " + filebase, label="pirecorder") if self.withframe: vid = cv2.VideoCapture(filein) fps, width, height, _ = get_vid_params(vid) if self.fps is None: self.fps = fps vidout = videowriter(fileout, width, height, self.fps, self.resizeval) while True: flag, frame = vid.read() if flag: if self.resizeval != 1: frame = imgresize(frame, self.resizeval) frame_nr = int(vid.get(cv2.CAP_PROP_POS_FRAMES)) draw_text(frame, str(frame_nr), (10, 10), 0.9, col="white", shadow=True) vidout.write(frame) if not flag: break else: if self.resizeval != 1: comm = "' -vf 'scale=iw*" + str(self.resizeval) + ":-2' '" else: comm = "' -vcodec copy '" bashcomm = "ffmpeg" if self.fps is not None: bashcomm = bashcomm + " -r " + str(self.fps) bashcomm = bashcomm + " -i '" + filein + comm + fileout[:-len( self.type)] + ".mp4'" bashcomm = bashcomm + " -y -nostats -loglevel 0" output = subprocess.check_output(['bash', '-c', bashcomm]) lineprint("Finished converting " + filebase, label="pirecorder") except KeyboardInterrupt: raise KeyboardInterruptError()
def overlay_stream(imagefile = "", alpha = 0.5, rotation = 0): # Check if image file loads assert os.path.isfile(imagefile), "Image file could not be loaded.." # Start videostream fullscreen = False vid = VideoIn(vidsize=1, rotation=rotation).start() frame = vid.read() overlay = frame.copy() h, w, _ = frame.shape cv2.namedWindow("Stream", cv2.WND_PROP_FULLSCREEN) # Load and resize the image to fit photo = cv2.imread(imagefile) photo = imgresize(photo, resize = 1, dims = (w,h)) # Draw photo on overlay overlay[0:h,0:w] = photo # Start the loop c = 0 vid.stop() vid = VideoIn(vidsize=1, rotation=rotation).start() while True: c += 1 print(c) # Extract the frame frame = vid.read() output = frame.copy() # Draw overlay semi-transparent on frame # cv2.addWeighted(overlay, alpha, output, 1 - alpha, 0, output) # Draw in black and white output = cv2.cvtColor(output, cv2.COLOR_BGR2GRAY) # Show the stream cv2.imshow("Stream", output) k = cv2.waitKey(1) & 0xFF if k == ord("["): alpha = max(alpha-0.05, 0) if k == ord("]"): alpha = min(alpha+0.05, 1) if k == ord("f"): fullscreen = not fullscreen if fullscreen: cv2.setWindowProperty("Stream", cv2.WND_PROP_FULLSCREEN, cv2.WINDOW_FULLSCREEN) else: cv2.setWindowProperty("Stream",cv2.WND_PROP_AUTOSIZE, cv2.WINDOW_NORMAL) cv2.resizeWindow("Stream", vid.res[0], vid.res[1]) if k == 27: break vid.stop() cv2.destroyAllWindows() cv2.waitKey(1)
def draw_stream(self): lineprint("Streaming video..") self.vid = VideoIn(system=self.system, framerate=self.framerate, vidsize=self.vidsize, rotation=self.rotation, maxres=self.maxres) self.vid.start() if self.overlay: h, w, _ = self.vid.read().shape self.overlayimg = imgresize(self.overlayimg, resize=1, dims=(w,h)) while True: self.img = self.vid.read() if self.overlay: overlay = self.img.copy() overlay[0:h,0:w] = self.overlayimg cv2.addWeighted(overlay, self.alpha, self.img, 1-self.alpha, 0, self.img) if self.cross: draw.draw_cross(self.img, pt2 = self.vid.res) if self.m.twoPoint is not None: draw.draw_crosshair(self.img, self.m.pos) if self.m.posDown is not None: cv2.rectangle(self.img, self.m.posDown, self.m.pos, self.tempcol, 2) if self.m.posUp is not None: cv2.rectangle(self.img, self.m.pts[-1], self.m.posUp, self.col, 2) cv2.imshow("Image", self.img) k = cv2.waitKey(self.waitms) & 0xFF if k == ord("o"): self.overlay = not self.overlay if k == ord("c"): self.cross = not self.cross if k == ord("f"): self.fullscreen = not self.fullscreen if self.fullscreen: cv2.setWindowProperty("Image", cv2.WND_PROP_FULLSCREEN, cv2.WINDOW_FULLSCREEN) else: cv2.setWindowProperty("Image", cv2.WND_PROP_AUTOSIZE, cv2.WINDOW_NORMAL) cv2.resizeWindow("Image", self.vid.res[0], self.vid.res[1]) if k == ord("s"): if self.m.twoPoint is not None: self.m.twoPoint = checkroi(self.m.twoPoint, self.vid.res) self.roi = roi_to_zoom(self.m.twoPoint, self.vid.res) if self.internal is True: lineprint("roi "+str(self.roi)+" stored..") else: lineprint("roi: "+str(self.roi)) else: lineprint("Nothing selected..") if k == ord("e"): self.m.posUp = None self.roi = False lineprint("roi data erased..") if k == ord("z"): if self.m.twoPoint is not None: lineprint("Creating zoomed image..") self.stream = False break if self.overlay and k == ord("["): self.alpha = max(self.alpha-0.05, 0) if self.overlay and k == ord("]"): self.alpha = min(self.alpha+0.05, 1) if k == 27: lineprint("User exited..") self.exit = True break self.vid.stop() time.sleep(1)