CV2 Import Import Time Import datetime Import os DEF mkdir (path): Folder = os.path.exists (path) IF not Folder: # determine whether there is a folder if not exist, create the folder os.makedirs (path) # makedirs when you create a file path will be created if this path does not exist Print ( "new new Folder ... --- ---") Print ( "the OK --- ---") the else: Print ( "--- There IS the this --- Folder ")! file =" D: \\ CCTVlook "save location # mkdir (file) Print (" files are stored in the D: \\ CCTVlook ") # selected camera, notebook 0 built-in camera, 1,2 ??? for the external camera camera = cv2.VideoCapture (0) Time.sleep (5) # delay 5s perform background = None # initialization BACKGROUND def nothing (x): Pass cv2.namedWindow ( "FPS") # Create a window cv2.createTrackbar ( "level", "fps ", 21, 255, nothing) # Create a threshold slider shot_idx = 0 the while True: text = "No the Target" Flat = 0 # slider assignment kerne = cv2.getTrackbarPos ( "Level", "FPS") IF kerne% 2 == 0: kerne. 1 = # + kerne slider solution Gaussian filter is an even number assigned to an exception is thrown (grabbed, frame ) = camera.read () # frame preprocessing, the first grayscale, then the Gaussian filter. # Gaussian filtering on the image processing parameters to avoid small changes in brightness, vibration and the like affect the results Gray = cv2.cvtColor (Frame, cv2.COLOR_BGR2GRAY) Gray = cv2.GaussianBlur (Gray, (kerne, kerne), 0) # of the first background of the entire frame is set as the input if background is None: = cv2.absdiff frameDelta (background, Gray) # binarized Thresh = cv2.threshold (frameDelta, 25, 255, cv2.THRESH_BINARY) [. 1] # erosion and dilation thresh = cv2.dilate (thresh, None, iterations = 2) # fetch contour CNTs = cv2.findContours (thresh.copy (), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) [ -2 ] # traverse profile for CNTs in C: IF cv2.contourArea (C) <1800: for smaller rectangle # region, choose to ignore Continue Flat set # 1 = a tag, when there is a movement # calculated contour bounding box, the box is drawn in the current frame (x, y, w, h ) = cv2.boundingRect (c) cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 0, 255), 2) text = "Find Target! save as D:\CCTVlook" print("Find Target!") cv2.putText(frame, text, (10, 20), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 2) cv2.putText( frame, datetime.datetime.now().strftime("%A %d %B %Y %I:%M:%S%p"), (10, frame.shape[0] - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.35, (0, 0, 255), 1, ) # cv2.imshow("Frame Delta", frameDelta) cv2.imshow("fps", frame) # cv2.imshow("Thresh", thresh) key = cv2.waitKey(1) &0xFF # If the q key has been pressed out of the loop ch = cv2.waitKey(1) if key == ord("q"): break if flat == 1: # 设置一个标签,当有运动的时候为1 fn = "D:\CCTVlook\shot%d.jpg" % (shot_idx) cv2.imwrite(fn, frame) shot_idx += 1 continue
Original: https://blog.csdn.net/dgut_guangdian/article/details/79762409