The following method implements , Get pictures from the camera , And flip or rotate according to the conditions
import cv2
# flip_x, flip_y Decide whether to proceed x The axis turns y The axis turns
# clip = (x,y,w,h); Coordinates and width height of upper left corner Determines the square area to be cut ; If you don't cut it, set it all 0
# rotate: 0 No rotation ; 1 Turn clockwise 90 degree ; 2 rotate 180 degree ; -1 Counter clockwise rotation 90 degree
def take_picture(flip_x=False, flip_y=False, clip=(0, 0, 100, 100), rotate=0):
clip_x, clip_y, clip_w, clip_h = clip
cap = cv2.VideoCapture(0)
# print("set pic width: ", cap.set(3, 3664))
# print("set pic height: ", cap.set(4, 2748), rotate, flip_x)
res, frame = cap.read()
cap.release()
if res:
cv2.imwrite("images/origin.png", frame)
if flip_x:
frame = cv2.flip(frame, 1)
if flip_y:
frame = cv2.flip(frame, 0)
if rotate:
frame = cv2.rotate(frame, cv2.ROTATE_90_CLOCKWISE if rotate == 1 else cv2.ROTATE_90_COUNTERCLOCKWISE if rotate == -1 else cv2.ROTATE_180)
if flip_x or flip_y or rotate:
cv2.imwrite("images/res_rotate", frame)
if clip_x or clip_y or clip_w or clip_h:
img = frame[clip_y: clip_y + clip_h, clip_x: clip_x + clip_w]
cv2.imwrite("images/res_clip.png", img)
else:
cv2.imwrite("images/res_clip.png.png", frame)
return True
else:
return False