import sys import cv2 as vision from pathlib import Path from datetime import datetime from frame import PP_Frame from frame import PP_Frame_Context # make the folder of this file the root folder. ROOT_DIR = Path(__file__).parent # make ROOT_DIR/images the default save folder. SAVE_DIR = ROOT_DIR / "images" # create folders and parent folders in case they dont exist. SAVE_DIR.mkdir(parents=True, exist_ok=True) def take_photo(width, height, color=True, filename=None): # select camera device camera = vision.VideoCapture(0) #set width and height if not isinstance(width, int) or not isinstance(height, int): print("width and height needs to be integer") camera.set(3,width) camera.set(4,height) #take picture _, picture = camera.read() picture = Frame(picture, color) camera.release() #save picture if filename is sent as a parameter. if isinstance(filename, str): vision.imwrite(str(ROOT_DIR / "images" / f"{filename}.png"), picture.data) return picture def show_picture(picture, title="Kuken"): while True: key = vision.waitKey(1) & 0xFF vision.imshow(title, picture) if key == ord('q'): vision.destroyAllWindows() break elif key == ord('s'): if not title.endswith(".png"): title = f"{title}.png" vision.imwrite(title, picture) print(f"saved: {title}.png") vision.destroyAllWindows() break return """function start_video() @Param width : int [required] ----------------------------- width for video stream to use @Param height : int [required] ------------------------------ height for video stream to use @Param color : bool [default: True] ----------------------------------- True will leave colors on False will display grayscale. @Param filename : str [default: "saved"] ---------------------------------------- when saving a picture it will be saved as {filename}_{year}-{month}-{day}_{timme}_{minut}.png @Param camera : int | str [default: 0] -------------------------------------- camera device by int or filename to a video. @Param callback : function [default: None] ------------------------------------------ a callable function that will manipulate the picture accordingly. @Returns status : bool ---------------------- True if successfully opened a videostream False if it failed to open a videostream. """ def start_video(ctx: PP_Frame_Context = None, callback=None): if ctx is None: ctx = PP_Frame_Context() #select camera device camera = vision.VideoCapture(ctx.moviefile) #set width and height of picture if isinstance(ctx.width, int) and isinstance(ctx.height, int): camera.set(3,ctx.width) camera.set(4,ctx.height) if not camera.isOpened(): print("could not open camera stream :(") return False while ctx.run: # get picture from camera. success, frame = camera.read() frame = PP_Frame(frame, ctx) if ctx.get_or_false("current_frame"): ctx.set("current_frame", ctx["current_frame"] + 1) key = vision.waitKey(1) & 0xFF # gracefull quit if camera feed dies or 'q' is pressed if not success or key == ord('q'): ctx.run = False continue # save image if 's' is pressed elif key == ord('s'): now = datetime.now().strftime("%Y-%m-%d_%H_%M") vision.imwrite(f"{ctx.savename}_{now}.png", frame.data) print(f"saved: {ctx.savename}_{now}.png") # show frame elif key == ord('v'): now = datetime.now().strftime("%Y-%m-%d_%H_%M") show_picture(frame.data, title=f"{ctx.savename}_{now}") elif key == ord('c'): ctx.set("color", not ctx.color) elif key == ord('g'): if ctx._has("gblur"): ctx.set("gblur", not ctx.gblur) else: ctx.set("gblur", True) elif key == ord('h'): hide = ctx.get_or_set("hide", False) ctx.set("hide", not ctx.hide) if ctx.hide: print("hide new frames..") # run callback function if it is callable if not callback is None and callable(callback): try: frame = callback(frame, context) except Exception: pass if not ctx.get_or_false("hide"): vision.imshow(f"{ctx.savename} | puckoprutt", frame.data) vision.destroyAllWindows() camera.release() return True if __name__ == '__main__': #pic = take_photo(640, 480, color=False, filename=None) #show_picture(pic) vid = start_video(640, 480, color=True)