https://blog.csdn.net/qq_42403190/article/details/90453305
Create a file
camera.py
Simple reading
# / usr / bin / the env to python3! # - * - Coding: UTF-. 8 - * - Import numpy AS NP Import CV2 CAP = cv2.VideoCapture (0) # acquired from the camera video # acquires video player interface length and width width = int (cap.get (cv2.CAP_PROP_FRAME_WIDTH) + 0.5) height = int (cap.get (cv2.CAP_PROP_FRAME_HEIGHT) + 0.5) # define VideoWriter encoder creates an object fourcc = cv2.VideoWriter_fourcc (* 'mp4v' ) # Be sure to Lower Case The use OUT = cv2.VideoWriter ( 'output.mp4', the fourcc, 20.0, (width, height)) the while (cap.isOpened ()): # camera reads the frame RET, frame = cap.read () IF == True RET: # output current frame out.write (frame) cv2.imshow ( 'My Camera', frame) # Q keyboard press exit IF (cv2.waitKey (. 1) & 0xFF) == the ord ( 'Q'): BREAK the else: BREAK # release resources out.release () cap.release () cv2.destroyAllWindows ()
Process integration
! # / usr / bin / the env to python3 # - * - Coding: UTF-. 8 - * - Import CV2 Import numpy AS NP name = 0 CAP = cv2.VideoCapture (0) cap.set (3,640) cap.set (4,480) RET, Frame = cap.read () rows, cols, channels = frame.shape Print (cols, rows, channels) # image preprocessing DEF IMG P (IMG): # grayscale gray_img = cv2.cvtColor (frame, cv2. COLOR_BGR2GRAY) # smoothing Blur = cv2.blur (gray_img, (3,3)) # binarization RET1, ThI = cv2.threshold (Blur, 190, 255, cv2.THRESH_BINARY) # perspective transformation B = 50 PTS1 = NP .float32 ([[b, 0] , [cols-b, 0], [0, rows], [cols, rows]]) PTS2 = NP .float32 ([[0, 0] , [cols, 0], [0, rows], [cols, rows]]) M = cv2.getPerspectiveTransform(pts1, pts2) dst = cv2.warpPerspective(blur, M, (cols, rows)) return dst while(1): ret,frame = cap.read() dst = img_p(frame) cv2.imshow('usb camera', dst) k = cv2.waitKey(50) if (k == ord('q')): break elif(k == ord('s')): #name = input('name:') name += 1 filename = r'./camera/' + str(name) + '.jpg' cv2.imwrite(filename, dst) print(filename) #break cap.release() cv2.destroyAllWindows()
Run the file
python3 camera.py
result: